Skip to content

mir-opt: add BoolChainOpt pass scaffold to fold pure bool && chains#157945

Open
gustavo89587 wants to merge 1 commit into
rust-lang:mainfrom
gustavo89587:mir-opt-bool-chain-fold
Open

mir-opt: add BoolChainOpt pass scaffold to fold pure bool && chains#157945
gustavo89587 wants to merge 1 commit into
rust-lang:mainfrom
gustavo89587:mir-opt-bool-chain-fold

Conversation

@gustavo89587

@gustavo89587 gustavo89587 commented Jun 15, 2026

Copy link
Copy Markdown

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 away.

Motivation

When a && b && c && d is lowered to MIR, each operand gets its own
BasicBlock with a SwitchInt terminator. This produces phi nodes in
LLVM IR that mem2reg and instcombine cannot eliminate, because the
short-circuit CFG structure prevents LLVM from proving side-effect-freedom.

Clang compiles equivalent C++ to a flat sequence of and i1 instructions
that LLVM can vectorize with SSE2/AVX2. Rust produces a chain of branches.

What this PR does

  • Adds compiler/rustc_mir_transform/src/bool_chain_opt.rs with the pass
    skeleton: detects SwitchInt blocks over pure bool locals with no
    side-effecting statements
  • Registers the pass after InstSimplify::AfterSimplifyCfg at mir-opt level 2
  • Adds tests/mir-opt/bool_chain_opt.rs as the test fixture

The rewrite logic (emitting BinOp(BitAnd, ...)) will follow in the next
commit once the detection is reviewed and confirmed correct.

Related

Fixes #83623

r? @nikic

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
@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 15, 2026
@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING:end] tool::ToolBuild { build_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "tidy", path: "src/tools/tidy", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "", cargo_args: [], artifact_kind: Binary } -- 12.959
[TIMING:end] tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/compiler/rustc_mir_transform/src/bool_chain_opt.rs:1:
 //! Folds chains of `&&` over pure boolean expressions into `BitAnd` BinOps,
 //! eliminating the phi nodes that LLVM cannot optimize away.
 
-use crate::MirPass;
 use rustc_middle::mir::*;
 use rustc_middle::ty::{self, TyCtxt};
+
+use crate::MirPass;
 
 pub(crate) struct BoolChainOpt;
 
Diff in /checkout/compiler/rustc_mir_transform/src/lib.rs:751:
             // optimizations. This invalidates CFG caches, so avoid putting between
             // `ReferencePropagation` and `GVN` which both use the dominator tree.
             &instsimplify::InstSimplify::AfterSimplifyCfg,
-                &bool_chain_opt::BoolChainOpt,
+            &bool_chain_opt::BoolChainOpt,
             // After `InstSimplify-after-simplifycfg` with `-Zub_checks=false`, simplify
             // ```
             // _13 = const false;
fmt: checked 6913 files
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Build completed unsuccessfully in 0:00:50

@dianqk dianqk 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.

I can’t see what transformations this pass performs.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2026
@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@traviscross

traviscross commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

&& operator chains (and ||, possibly) generates unoptimizable LLVM IR

6 participants