google / xls

XLS: Accelerated HW Synthesis
http://google.github.io/xls/
Apache License 2.0
1.22k stars 181 forks source link

[enhancement] support for const if expr in proc config #1749

Open proppy opened 1 day ago

proppy commented 1 day ago

What's hard to do? (limit 100 words)

Currently it's different to perform compile time conditional in proc's config due to the lack of const if expressions.

Example:

        let f32_0 = float32::zero(false);
        let f32_2 = F32 { sign: false, bexp: u8:128, fraction: u23:0 };

        unroll_for! (row, _): (u32, ()) in u32:0..ROWS {
            unroll_for! (col, _): (u32, ()) in u32:0..COLS {
                let weight = if row == col { f32_2 } else { f32_0 };
                spawn node(
                    west_inputs[row][col], north_inputs[row][col], east_outputs[row][col + u32:1],
                    south_outputs[row + u32:1][col], weight);
            }(());
        }(());

Would trigger the following error:

E1128 00:34:17.014452    3268 command_line_utils.cc:47] Could not extract a textual position from error message: INTERNAL: Let RHS not evaluated as constexpr: weight : if u32:0 == u32:0 { f32_2 } else { f32_0 }

Even though weight could be resolve staticaly from indexes of the unrolled loops.

Current best alternative workaround (limit 100 words)

Nest the if expression inside the struct initialization:

                let weight = F32 {
                    sign: false,
                    bexp: if col == row { u8:128 } else { u8:0 },
                    fraction: u23:0,
                };

Your view of the "best case XLS enhancement" (limit 100 words)

if expression would be inferred as constant in the context of a proc config function or there would be a dedicated const_if! macros.