google / google/xls

[DSLX][TIv2] Superfluous type validation for untaken branch

Open
#2,423 1 comment 0 reactions 1 assignee Claimed by @huangjd View on GitHub
dslx dslx:TIv2
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

**Describe the bug**
When compiling code with parametrics under TIv2, we may encounter a scenario where a branch of a conditional that's known at compile time **not** being taken contains statements that are invalid under type checking or type validation. This causes compilation to unnecessarily fail, as that branch is guaranteed never taken in that parametric instantiation. This kind of code can be quite common for real world application, for example it is affecting `fixed_point.x` in the floating point library, as we usually want to process input differently based on the configuration from parametrics.

**To Reproduce**
Compile the following code with ir_converter_main
`xls/dslx/ir_convert/ir_converter_main --type_inference_v2 input.x`

```
fn f(a : u32) -> u32 {
if A == 0 {
a
} else {
let b : u32[A] = [1,];
for (i, acc) in 0..A {
b[i] + acc
} (0)
}
}
const X = f(u32:1);
const Y = f(u32:1);
```

**Expected behavior**
Program compiles without error.

**Analysis**
I am proposing a way to handle this similar to C++ `if constexpr` (https://www.cppreference.com/w/cpp/language/if.html). In a parametric, if a conditional is known to be a compile time constant, only the taken branch will be compiled, and the untaken branch is discarded, with no type checking and validation performed on its statements. Of course, the untaken branch should still be parsed as valid DSLX 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.