mir-opt: add BoolChainOpt pass scaffold to fold pure bool && chains#157945
mir-opt: add BoolChainOpt pass scaffold to fold pure bool && chains#157945gustavo89587 wants to merge 1 commit into
Conversation
Add initial scaffolding for a MIR optimization pass that detects chains of && operators over pure boolean expressions and prepares to fold them into BitAnd BinOps, eliminating phi nodes that LLVM cannot optimize. The pass is registered after InstSimplify::AfterSimplifyCfg at mir-opt level 2. Currently performs detection only; rewrite logic to follow. Related: rust-lang#83623
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikic (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
Reminder, once the PR becomes ready for a review, use |
|
@gustavo89587: Please tell us about what steps you took to self-review and validate the correctness of this PR and your process for creating it. |
Add initial scaffolding for a MIR optimization pass that detects chains
of
&&operators over pure boolean expressions and prepares to fold theminto
BitAndBinOps, eliminating phi nodes that LLVM cannot optimize away.Motivation
When
a && b && c && dis lowered to MIR, each operand gets its ownBasicBlockwith aSwitchIntterminator. This produces phi nodes inLLVM IR that
mem2regandinstcombinecannot eliminate, because theshort-circuit CFG structure prevents LLVM from proving side-effect-freedom.
Clang compiles equivalent C++ to a flat sequence of
and i1instructionsthat LLVM can vectorize with SSE2/AVX2. Rust produces a chain of branches.
What this PR does
compiler/rustc_mir_transform/src/bool_chain_opt.rswith the passskeleton: detects
SwitchIntblocks over pureboollocals with noside-effecting statements
InstSimplify::AfterSimplifyCfgat mir-opt level 2tests/mir-opt/bool_chain_opt.rsas the test fixtureThe rewrite logic (emitting
BinOp(BitAnd, ...)) will follow in the nextcommit once the detection is reviewed and confirmed correct.
Related
Fixes #83623
r? @nikic