google / google/xls

DSLX `enumerate` function not working correctly

Open
#1,289 0 comments 0 reactions 0 assignees View on GitHub
dslx
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

I tried working through the DSLX tutorials, and ran into a problem with the `enumerate` built-in function.

I'm getting this error when running [dslx_intro/prefix_scan_equality.x](https://github.com/google/xls/blob/main/xls/examples/dslx_intro/prefix_scan_equality.x):

```
0018: let (_, _, result) =
0019: for ((i, elem), (prior, count, result)): ((u32, u32), (u32, u3, u3[8]))
0020: in enumerate(x) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^-^ ConversionError: iterable (enumerate(x)) must be bits-typed, constexpr, and its start must be less than or equal to its limit.
```

The example is fairly complex, so I tried to create a simple function to sum a list of 8 bit numbers to see if I could isolate things:

```
fn sum_list(x : u8[8]) -> u8 {
for((_, element), result): ((u32, u8), u8) in enumerate(x) {
let new_result = result + element;
new_result
}(u8:0)
}
#[test]
fn sum_list_test() {
let input = u8[8]: [u8:0, u8:1, u8:2, u8:3, u8:4, u8:5, u8:6, u8:7];
let result = sum_list(input);
assert_eq(result, u8:28)
}
```

And it has the same problem with the enumerate expression.

I looked at the documentation for enumerate, and for examples in the dslx/tests/... directory and there isn't much there, suggesting that this is not a well-used or tested language feature.

[dslx/tests/enumerate.x](https://github.com/google/xls/blob/main/xls/dslx/tests/enumerate.x) assigns the result of enumerate to a variable:

```
let my_array = u32[4]:[u32:1, u32:2, u32:4, u32:8];
let enumerated = enumerate(my_array);
```
and that works fine, but if I try to use the same syntax in a non-test function:

```
fn sum_list(x : u8[8]) -> u8 {
let e = enumerate(x);
for((_, element), result): ((u32, u8), u8) in e {
let new_result = result + element;
new_result
}(u8:0)
}
```
I get a different error:
```
0001: fn sum_list(x : u8[8]) -> u8 {
0002: let e = enumerate(x);
~~~~~~~~~~~~~~~~~~~~~~~~~^-^ ConversionError: Could not find name for invocation: enumerate; available: [sum_list]
```
which is even more confusing.... does the language use different syntax for test and non-test code?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.