Skip to content

Optimize MIR CFG generation for chained logical operators (#83623)#157315

Open
sunny026 wants to merge 1 commit into
rust-lang:mainfrom
sunny026:fix-mir-logical-ops
Open

Optimize MIR CFG generation for chained logical operators (#83623)#157315
sunny026 wants to merge 1 commit into
rust-lang:mainfrom
sunny026:fix-mir-logical-ops

Conversation

@sunny026
Copy link
Copy Markdown

@sunny026 sunny026 commented Jun 2, 2026

Fixes #83623

Description

This PR resolves the issue where chained logical operators (&& and ||) evaluated into a boolean destination generated suboptimal MIR. Previously, the AST lowering in compiler/rustc_mir_build/src/builder/expr/into.rs recursively invoked expr_into_dest, which allocated duplicate destination = false basic blocks for every operator in the chain. This fractured control flow prevented LLVM's mem2reg pass from recognizing the pattern and blocked auto-vectorization.

Changes

  • Modified ExprKind::LogicalOp lowering inside into.rs to route the entire expression through then_else_break.
  • Because then_else_break optimally shares if/then/else scopes without creating temporary booleans, this natively collapses all short-circuiting branches into a single unified success block and a single unified failure block.

Result

LLVM can now instantly convert the unified goto failures into SSA Phi nodes, enabling seamless SIMD vectorization for constructs like #[derive(PartialEq)] on large structs!

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

Some changes occurred in coverage instrumentation.

cc @Zalathar

@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 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (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
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 73 candidates
  • Random selection from 18 candidates

@rustbot

This comment has been minimized.

@sunny026 sunny026 force-pushed the fix-mir-logical-ops branch from 33084fa to d5c3189 Compare June 2, 2026 13:53
@rust-log-analyzer

This comment has been minimized.

@sunny026 sunny026 force-pushed the fix-mir-logical-ops branch from d5c3189 to 594756f Compare June 2, 2026 14:04
@cjgillot
Copy link
Copy Markdown
Contributor

cjgillot commented Jun 2, 2026

How does this fix the bug? From a glance it looks like it's making it worse. Do you have a test?

@rust-log-analyzer

This comment has been minimized.

@sunny026 sunny026 force-pushed the fix-mir-logical-ops branch from 594756f to 5763a1b Compare June 2, 2026 19:37
@sunny026
Copy link
Copy Markdown
Author

sunny026 commented Jun 2, 2026

How does this fix the bug? From a glance it looks like it's making it worse. Do you have a test?

Yes tested

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants