Add clamped_clog2 parallel to clog2 for parametric bitwidths
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 283
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 135
Description
### What's hard to do? (limit 100 words)
Currently, `std::clog2` is special cased to return a parametric width that is constexpr for doing things like:
https://github.com/google/xls/blob/4accf99e348821a23687be5de10e8f1c0bdfcafa/xls/dslx/tests/constexpr_clog2.x#L19-L21
See https://github.com/google/xls/blob/4accf99e348821a23687be5de10e8f1c0bdfcafa/xls/dslx/type_system/deduce.cc#L125-L136
However, commonly in hardware, you want the bit width to cover the case where the thing may have only one entry depending on parameter values, i.e. "clamp" the result to 1 if the input is `<= 1`.
It's easy to write such a function, but such a user function is limited in its use for parametrics. Per https://github.com/google/xls/issues/1941#issuecomment-2676662473, you can't import this function. Also, if you try to use it in a struct impl, e.g.
```
fn clamped_clog2(x: bits[N]) -> bits[N] {
if x > bits[N]:1 { std::clog2(x) } else { bits[N]:1 }
}
struct Foo { num_foo: bits[DEPTH], foo_idx: bits[clamped_clog2(DEPTH)] }
```
you get the error:
```
TypeInferenceError: Could not evaluate dimension expression `clamped_clog2(DEPTH)` to a constant value.
```
### Current best alternative workaround (limit 100 words)
Not aware of any workaround.
### Your view of the "best case XLS enhancement" (limit 100 words)
It would be good to at least cover this common scenario and special case some form of `std::clamped_clog2` as well.
More generally, it would be nice to be able to have full support for constexpr functions in DSLX as a user.
Contributor guide
Assessment
This issue has not been assessed yet.