Skip to content

[handshake] Fix sign-extension in ArithBound#822

Merged
zero9178 merged 4 commits into
mainfrom
users/zero9179/bound-opti
Mar 30, 2026
Merged

[handshake] Fix sign-extension in ArithBound#822
zero9178 merged 4 commits into
mainfrom
users/zero9179/bound-opti

Conversation

@zero9178
Copy link
Copy Markdown
Collaborator

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

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 requested a review from Jiahui17 March 29, 2026 19:40
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp Outdated
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp Outdated
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp Outdated
@zero9178
Copy link
Copy Markdown
Collaborator Author

In addressing your comments I also found out that the logic is simply broken when the upper-bound is sign-extended.
I adjusted the logic accordingly now and made it more conservative, but there are likely some regressions. Some will be fixed by the constants support (since it gets rid of some unecessary extsi while correctly representing bitwidths of constants), but not all of them.

PTAL

@zero9178 zero9178 requested a review from Jiahui17 March 30, 2026 10:15
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp Outdated
Comment thread lib/Transforms/HandshakeOptimizeBitwidths.cpp
@zero9178 zero9178 merged commit 01f8a36 into main Mar 30, 2026
8 checks passed
@zero9178 zero9178 deleted the users/zero9179/bound-opti branch March 30, 2026 15:16
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.

Wrong sign-extension of bounded cond_br

2 participants