Skip to content

[HandshakeOptimizeBitwidths] Fix forward logic for shrsi#815

Merged
zero9178 merged 3 commits into
mainfrom
users/zero9179/shrsi-bitwidth
Mar 29, 2026
Merged

[HandshakeOptimizeBitwidths] Fix forward logic for shrsi#815
zero9178 merged 3 commits into
mainfrom
users/zero9179/shrsi-bitwidth

Conversation

@zero9178
Copy link
Copy Markdown
Collaborator

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

Depends on #812

@zero9178 zero9178 requested a review from Jiahui17 March 26, 2026 10:52
@zero9178 zero9178 force-pushed the users/zero9179/shrui-bitwidth branch from f9fdbee to e3a5d9b Compare March 26, 2026 12:39
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
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.

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
@zero9178 zero9178 force-pushed the users/zero9179/shrsi-bitwidth branch from dae00eb to e8a51c0 Compare March 27, 2026 14:01
@zero9178 zero9178 changed the base branch from users/zero9179/shrui-bitwidth to main March 27, 2026 14:01
@Jiahui17 Jiahui17 marked this pull request as draft March 27, 2026 15:01
@Jiahui17 Jiahui17 marked this pull request as ready for review March 27, 2026 15:01
@Jiahui17
Copy link
Copy Markdown
Member

Had to mark as draft + mark as ready to trigger CI..

@zero9178 zero9178 merged commit d29de58 into main Mar 29, 2026
8 checks passed
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 zero9178 deleted the users/zero9179/shrsi-bitwidth branch March 29, 2026 15:12
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
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.

integer bitwidth is limited to 16777215 bits crash

2 participants