google / xls

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

[DSLX] Not able to assert on parametrics (as constexpr) in `const_assert!` #1674

Open cdleary opened 1 month ago

cdleary commented 1 month ago

Describe the bug

Parametric values are flagging an issue in const_assert! as not being registered as constexpr -- I think this happens when we provide an incomplete set of parametrics -- the phantom parametric probably resolves to a ParametricSymbol (which we have intent to rip out) but the body of the function is still being evaluated for constexprs somehow. cc @richmckeever

To Reproduce

$ ~/opt/xlsynth/latest/dslx_interpreter_main reverse_chunks.x 
reverse_chunks.x:19:3-19:36
0017: 
0018: fn reverse_chunks<N: u32, FLAT: u32>(x: bits[FLAT]) -> bits[FLAT] {
0019:   const_assert!(FLAT % N == u32:0);  // Ensure FLAT is a multiple of N.
~~~~~~~~^-------------------------------^ TypeInferenceError: const_assert! expression is not constexpr
0020:   const NUM_CHUNKS = FLAT / N;

Minimal reproducer:

fn f<N: u32, FLAT: u32>(x: bits[FLAT]) {
  const_assert!(FLAT % N == u32:0);
}

#[test]
fn test_f() {
    f(bits[42]:0)
}
$ ~/opt/xlsynth/latest/dslx_interpreter_main reverse_chunks.x 
reverse_chunks.x:2:3-2:36
0001: fn f<N: u32, FLAT: u32>(x: bits[FLAT]) {
0002:   const_assert!(FLAT % N == u32:0);
~~~~~~~~^-------------------------------^ TypeInferenceError: const_assert! expression is not constexpr
0003: }
0004: 

Expected behavior I'd expect this expression to be constexpr as the parametric values are compile-time constant, or to flag that not enough parametrics were provided to instantiate the function.