google / google/xls

Failure to optimize out divide of select of constants

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

Description

XLS doesn't generate good code for the following construct:

```
fn foo(x: u32, p: u1) -> u32 {
x / if p { u32:8 } else { u32:16 }
}
```

The divide never gets removed. It should convert the expression to (the IR equivalent of):

```
if p { x / 8 } else { x / 16 }
````

which then is simplified to

```
if p { x>>3 } else { x >>4 }
```

Generally, this is hoisting an operation above a select resulting in duplication of the operation. However, if the subsequent optimization make each of the duplicated ops much cheaper then it is worthwhile. In the example, a divide is duplicated but subsequent optimizations replace the divides with shift-by-constant which is free.

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.