Skip to content

[OptimizeInstructions] Generalize low-bit masks - #9293

Merged
tlively merged 1 commit into
mainfrom
opt-inst-1-1-and-low-mask
Oct 22, 2026
Merged

tlively merged 1 commit into
mainfrom
opt-inst-1-1-and-low-mask

Conversation

@tlively

@tlively tlively commented Oct 22, 2026

Copy link
Copy Markdown
Member

Extend visitBinary's low-bit mask elimination and narrow-load unsigned conversion from i32.and to both i32.and and i64.and using Match::binary(Abstract::And, ...). Also replace the contiguous 2^k - 1 mask check with Bits::lowBitMask64(getMaxBits(left)) so non-contiguous masks that preserve all active low bits of the LHS are eliminated as well.

Extend visitBinary's low-bit mask elimination and narrow-load unsigned conversion from i32.and to both i32.and and i64.and using Match::binary(Abstract::And, ...). Also replace the contiguous 2^k - 1 mask check with Bits::lowBitMask64(getMaxBits(left)) so non-contiguous masks that preserve all active low bits of the LHS are eliminated as well.
@tlively
tlively requested a review from a team as a code owner October 22, 2026 01:25
@tlively
tlively requested review from kripken and removed request for a team October 22, 2026 01:25

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors and generalizes the bitmask optimization logic in OptimizeInstructions.cpp to support both 32-bit and 64-bit masks. It introduces a new lowBitMask64 helper in bits.h and uses pattern matching to simplify redundant masks that preserve all possible non-zero low bits of an expression. Additionally, it cleans up overlapping optimization rules and adds comprehensive test coverage in optimize-instructions-mvp.wast. There are no review comments, so I have no additional feedback to provide.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm though I do think this kind of thing is low priority. We can probably get larger wins from more general approaches, like say integrating LLVM's instcombine.

@tlively

tlively commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

That might have been plausible back when it would have been infeasible to figure out what the gap between OptimizeInstructions and instcombine was. Now that we can just have Gemini cross-reference OptimizeInstructions with LLVM's instcombine to find missing opportunities, I think the maintenance burden of an updated standalone OptimizeInstructions will be much lower than a new integration with LLVM.

I agree this is lower priority than many of the other promising projects we have in the works, though. My plan for now is to upload just one of these a day to avoid taking time away from other projects. Does that sound reasonable to you?

@tlively
tlively merged commit b6720c0 into main Oct 22, 2026
16 of 17 checks passed
@tlively
tlively deleted the opt-inst-1-1-and-low-mask branch October 22, 2026 18:22
@kripken

kripken commented Oct 22, 2026

Copy link
Copy Markdown
Member

One a day sounds reasonable.

But rather than cross-reference InstCombine... how about using it? Literally take those source files and build a shim around them. The shim would be the only thing to verify, and we'd get everything at once.

@tlively

tlively commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

My intuition is that the shim would be far more code and complexity than its worth, but I'd be happy to be proven wrong about that. Probably something we can prototype "for free."

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