[HandshakeOptimizeBitwidths] Fix forward logic for shrsi#815
Merged
Conversation
f9fdbee to
e3a5d9b
Compare
zero9178
added a commit
that referenced
this pull request
Mar 26, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
Jiahui17
approved these changes
Mar 26, 2026
Member
Jiahui17
left a comment
There was a problem hiding this comment.
makes sense, check out the comments in the previous one!
The previous logic for `shrsi` for the forward pass often crashed in edge cases such as the shift amount was larger than the bitwidth. This PR rewrites the forward logic for `shrsi` into a dedicated pattern. In the case of the input of `shrsi` being zero-extended we just optimize it to a `shrui` and reuse the existing optimization logic there. Fixes #792
dae00eb to
e8a51c0
Compare
Member
|
Had to mark as draft + mark as ready to trigger CI.. |
zero9178
added a commit
that referenced
this pull request
Mar 29, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
zero9178
added a commit
that referenced
this pull request
Mar 30, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
zero9178
added a commit
that referenced
this pull request
Mar 30, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
zero9178
added a commit
that referenced
this pull request
Apr 10, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
zero9178
added a commit
that referenced
this pull request
Apr 20, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
zero9178
added a commit
that referenced
this pull request
Apr 20, 2026
Prior to this PR the bitwidths of constants was reduced by a dedicated pass called `handshake-minimize-cst-width` which would insert `trunci` and `ext`s after reducing the bitwidths of constants. This created a bit of a phase ordering issue however: * Generally speaking we ideally want a single canonical IR form such that all passes can match against that form. Placing `trunci` and `ext` between constants negatively impacts patterns that want to match against constant operands (such as the shift patterns). * The optimize bitwidths pass was then dependent on `minimize-cst-width` running first. If the pass itself created any constants, it'd pessimize optimizations. Additionally, the `minimize-cst-width` pass was too conservative in reducing the bitwidths and would leave sign-bits, even when not necessary. This PR fixes that issue by handling constant ops specially when calculating the "minimal values". Minimal values are now a class that encapsulates the concept of "there exists a minimal value for a value that may be of a smaller bitwidth and can be extended back to the original value". Constant ops are treated as if they were of less bitwidth (equal to the least bitwidth required to represent them) with either sign-extension or zero-extension used depending on whether they were originally negative (to preserve the bit pattern). Fixes #798 Fixes #30 Depends on #815 to not crash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The previous logic for
shrsifor the forward pass often crashed in edge cases such as the shift amount was larger than the bitwidth.This PR rewrites the forward logic for
shrsiinto a dedicated pattern. In the case of the input ofshrsibeing zero-extended we just optimize it to ashruiand reuse the existing optimization logic there.Fixes #792
Depends on #812