Skip to content

[HandshakeOptimizeBitwidths] Reduce constant op bitwidths#816

Open
zero9178 wants to merge 3 commits intomainfrom
users/zero9179/optimize-constants
Open

[HandshakeOptimizeBitwidths] Reduce constant op bitwidths#816
zero9178 wants to merge 3 commits intomainfrom
users/zero9179/optimize-constants

Conversation

@zero9178
Copy link
Copy Markdown
Collaborator

Prior to this PR the bitwidths of constants was reduced by a dedicated pass called handshake-minimize-cst-width which would insert trunci and exts 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 requested a review from Jiahui17 March 26, 2026 13:11
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp Outdated
@zero9178 zero9178 force-pushed the users/zero9179/shrsi-bitwidth branch from dae00eb to e8a51c0 Compare March 27, 2026 14:01
@zero9178 zero9178 force-pushed the users/zero9179/optimize-constants branch from e5a2da9 to c5ede00 Compare March 29, 2026 13:21
@zero9178 zero9178 changed the base branch from users/zero9179/shrsi-bitwidth to main March 29, 2026 13:22
@zero9178 zero9178 marked this pull request as draft March 29, 2026 13:22
@zero9178 zero9178 marked this pull request as ready for review March 29, 2026 13:22
zero9178 added a commit that referenced this pull request Mar 29, 2026
Prior to this PR, the pass performed extension of the result of a bounded `cond_br` based on the extension used for the operand.
This is incorrect since they have no correlation in the algorithm's logic: The extension that should be used depends on the bound of the data operand, not the data operand itself.

More specifically, since the optimization currently only implements unsigned comparisons, the result should then only ever be zero-extended. The only except is for `eq` and `ne` where the result should equal the extend of the constant.

Fixes #817
Unblocks #816
zero9178 added a commit that referenced this pull request Mar 30, 2026
Prior to this PR, the pass performed extension of the result of a
bounded `cond_br` based on the extension used for the operand. This is
incorrect since they have no correlation in the algorithm's logic: The
extension that should be used depends on the bound of the data operand,
not the data operand itself.

More specifically, since the optimization currently only implements
unsigned comparisons, the result should then only ever be zero-extended.
The only except is for `eq` and `ne` where the result should equal the
extend of the constant.

Fixes #817 
Unblocks #816
@zero9178 zero9178 force-pushed the users/zero9179/optimize-constants branch 2 times, most recently from bed97a6 to ea9b038 Compare March 30, 2026 14:36
zero9178 added a commit that referenced this pull request Apr 10, 2026
Prior to this PR the cycle bitwidth reduction pattern correctly
calculated the optimal bitwidth using backtracking on forwarded branch
operands, but failed to do so when determining the extension type. The
extension type was instead calculated using the data operands which was
incorrect. This resulted in the wrong extension type being calculated at
times.

This PR fixes that by calculating the extension type and optimal
bitwidth of a cyclic mux at the same time.

Fixes #827
Required to land #816
@zero9178 zero9178 force-pushed the users/zero9179/optimize-constants branch from ea9b038 to 8fc8d4a Compare April 10, 2026 12:44
@zero9178 zero9178 force-pushed the users/zero9179/optimize-constants branch from 8fc8d4a to e624e4b Compare April 20, 2026 09:36
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 force-pushed the users/zero9179/optimize-constants branch from e624e4b to 0bf2bf4 Compare April 20, 2026 12:12
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.

2 participants