Fix MBASub pass order to prevent instruction explosion#2
Merged
PeterHackz merged 1 commit intoPeterHackz:masterfrom Feb 18, 2026
Merged
Fix MBASub pass order to prevent instruction explosion#2PeterHackz merged 1 commit intoPeterHackz:masterfrom
PeterHackz merged 1 commit intoPeterHackz:masterfrom
Conversation
Owner
|
Good catch! Thank you for pointing this out. I’m considering removing or updating the SUB and XOR multiplication cases since they may be unsafe for signed arithmetic. Will revisit when I have more time. |
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.
I reordered the MBA transformation passes in MBASub::RunOnBasicBlock so that runOnMul runs first, before runOnSub, runOnAdd, and runOnXor.
The problem
Some of the transformations (especially in xor_ops and sub_ops) generate new Mul instructions as part of their MBA formulas.
For example:
From xor_ops:
a ^ b = (a + b) - 2 * (a & b)From sub_ops:
X - Y == (X ^ -Y) + 2*(X & -Y)Before runOnMul would run after these passes, immediately picking up these newly generated Mul instructions and expanding them again using mul_ops (which itself expands 1 Mul into 2 Muls + overhead).
The issue is that mul_ops expands a multiplication into multiple new multiplications:
b * c = (((b | c) * (b & c)) + ((b & ~c) * (c & ~b)))So what ended up happening was:
Sub / Xor generate Mul, Mul expands into more Mul, Those can generate even more Mul and Repeat next iteration xD
On math-heavy or crypto-heavy code (like ChaCha20), this cause the IR size to explode and hanging at runtime