Skip to content

[handshake] Add constant folding to ext(u|s)i ops#802

Merged
zero9178 merged 8 commits intomainfrom
users/zero9179/ext-constant-canon
Mar 26, 2026
Merged

[handshake] Add constant folding to ext(u|s)i ops#802
zero9178 merged 8 commits intomainfrom
users/zero9179/ext-constant-canon

Conversation

@zero9178
Copy link
Copy Markdown
Collaborator

No constant folding of these ops existed up until now. Having these kinds of constant folding is useful besides being an optimization, since it allows code to assume that if an operand can be calculated as a constant, it'll be a constant.

This PR adds such a constant folding pattern for extui and extsi ops.

Part of #792

No constant folding of these ops existed up until now.
Having these kinds of constant folding is useful besides being an optimization, since it allows code to assume that if an operand can be calculated as a constant, it'll be a constant.

This PR adds such a constant folding pattern for `extui` and `extsi` ops.

Part of #792
@zero9178 zero9178 requested a review from Jiahui17 March 19, 2026 17:49
@Jiahui17
Copy link
Copy Markdown
Member

there are some tests contain hardcoded op names, emmet is fixing them (#793

@zero9178
Copy link
Copy Markdown
Collaborator Author

there are some tests contain hardcoded op names, emmet is fixing them (#793

Is there any way to "regenerate" the buffer.json file or is this handcrafted?

@Jiahui17
Copy link
Copy Markdown
Member

They are handcrafted

@murphe67
Copy link
Copy Markdown
Collaborator

Do I need to fix the speculator also to avoid blocking this or just the buffering? @Jiahui17

@Jiahui17
Copy link
Copy Markdown
Member

@murphe67 Just buffering should be good (placing the speculator at qualitatively the same location seems straightforward

zero9178 added a commit that referenced this pull request Mar 23, 2026
With the recently landed bitwidth type system we have an initial generator that is capable of generating C programs known to compute under a given bitwidth. The initial PR used this for functional testing, where the programs generated are simply more likely to trigger mis-compilations in the optimization pass.

The new non-functional oracle introduced in this PR tests the optimization capabilities of the pass: It checks the output IR for any computations that are done at a higher bitwidth than expected.
The logic for doing so is currently simple: All results of operations that aren't operands of an `end` should be done using a bitwidth that is at most equal to the bitwidth the generator used.
The logic for this is via a separate oracle executable called `hls-fuzzer-check-bitwidth`. This enables a nice workflow where reproduction can be done through a simple `bash execute.sh` as is done for the functional testing.

The mode is currently selected using a `--non-functional` flag.

The PR contains following limitations to be fixed later or simple known issues:
* It finds bugs very, very quickly due to: #798 which is blocked by #792 which is blocked by #802. I manually verified some bugs it found that the logic ought to work, but it is hard to say until we get more usage.

As a drive-by fix, also disable right-shift generation for now (the logic was incorrect and was accidentally included).
zero9178 added a commit that referenced this pull request Mar 23, 2026
With the recently landed bitwidth type system we have an initial generator that is capable of generating C programs known to compute under a given bitwidth. The initial PR used this for functional testing, where the programs generated are simply more likely to trigger mis-compilations in the optimization pass.

The new non-functional oracle introduced in this PR tests the optimization capabilities of the pass: It checks the output IR for any computations that are done at a higher bitwidth than expected.
The logic for doing so is currently simple: All results of operations that aren't operands of an `end` should be done using a bitwidth that is at most equal to the bitwidth the generator used.
The logic for this is via a separate oracle executable called `hls-fuzzer-check-bitwidth`. This enables a nice workflow where reproduction can be done through a simple `bash execute.sh` as is done for the functional testing.

The mode is currently selected using a `--non-functional` flag.

The PR contains following limitations to be fixed later or simple known issues:
* It finds bugs very, very quickly due to: #798 which is blocked by #792 which is blocked by #802. I manually verified some bugs it found that the logic ought to work, but it is hard to say until we get more usage.

As a drive-by fix, also disable right-shift generation for now (the logic was incorrect and was accidentally included).
Copy link
Copy Markdown
Member

@Jiahui17 Jiahui17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@zero9178 zero9178 merged commit 26b437b into main Mar 26, 2026
8 checks passed
zero9178 added a commit that referenced this pull request Mar 26, 2026
With the recently landed bitwidth type system we have an initial
generator that is capable of generating C programs known to compute
under a given bitwidth. The initial PR used this for functional testing,
where the programs generated are simply more likely to trigger
mis-compilations in the optimization pass.

The new non-functional oracle introduced in this PR tests the
optimization capabilities of the pass: It checks the output IR for any
computations that are done at a higher bitwidth than expected. The logic
for doing so is currently simple: All results of operations that aren't
operands of an `end` should be done using a bitwidth that is at most
equal to the bitwidth the generator used. The logic for this is via a
separate oracle executable called `hls-fuzzer-check-bitwidth`. This
enables a nice workflow where reproduction can be done through a simple
`bash execute.sh` as is done for the functional testing.

The mode is currently selected using a `--non-functional` flag.

The PR contains following limitations to be fixed later or simple known
issues:
* It finds bugs very, very quickly due to:
#798 which is blocked by
#792 which is blocked by
#802. I manually verified some
bugs it found that the logic ought to work, but it is hard to say until
we get more usage.

As a drive-by fix, also disable right-shift generation for now (the
logic was incorrect and was accidentally included).
@zero9178 zero9178 deleted the users/zero9179/ext-constant-canon branch March 26, 2026 12:41
zero9178 added a commit that referenced this pull request Mar 27, 2026
The previous logic for `shrui` for the forward pass did not properly
handle sign-extension for `shrui`, resulting in sign-extension of the
result often being performed in cases where the shifting should fill in
0 bits. Furthermore, if the constant was larger than the minimal input
bitwidth, integer overflow would occur in the implementation causing
crashes.

This PR fixes that issue by splitting the forward pass for `shrui` out
of the common shift pattern and taking both zero-extension and
sign-extension of the input into account. I believe the logic of `shrui`
and `shrsi` to likely be different enough to require different
forwarding patterns. The older generic `ArithShift` pattern remains in
use for the backward pass.

Fixes #792

Depends on #802 to function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants