Skip to content

Avoid creating overlapping assignments in MatchBranchSimplification - #162785

Open
saethlin wants to merge 1 commit into
rust-lang:mainfrom
saethlin:match_branch_simplification
Open

saethlin wants to merge 1 commit into
rust-lang:mainfrom
saethlin:match_branch_simplification

Conversation

@saethlin

@saethlin saethlin commented Sep 14, 2026

Copy link
Copy Markdown
Member

MatchBranchSimplification can turn a match into an assignment, but doesn't make sure the LHS and RHS do not overlap. For example, the enum version of the mir-opt test in this PR is currently compiled with -Zmir-opt-level=0 -Zmir-enable-passes=+MatchBranchSimplification to

fn g(_1: Foo) -> Foo {
    let mut _0: Foo;
    let mut _2: Foo;
    let mut _3: *const Foo;
    let mut _4: u8;

    bb0: {
        _2 = copy _1;
        _3 = &raw const _2;
        _4 = discriminant((*_3));
        _2 = no_retag copy (*_3); // Since _3 = &raw const 2 above, this overlaps!
        _0 = copy _2;
        return;
    }
}

While this is technically a miscompile, I haven't found any way to turn this into something exciting, in part because the only thing that can be made to overlap here is a by-value enum discriminant which is always register-sized so backends lower this in a way that is okay with overlapping.

@rustbot rustbot added 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. labels Sep 14, 2026
@saethlin

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 14, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 14, 2026
@rust-bors

rust-bors Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 511c733 (511c733988d791a6dfd9fbc8c8b3d608dc4f84c8)
Base parent: 574ff7d (574ff7d98bd6d037e5236a8453029173b32631fd)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (511c733): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 1.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.7% [3.4%, 5.1%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.8% [-7.9%, -3.7%] 2
All ❌✅ (primary) - - 0

Cycles

Results (primary -0.7%, secondary 0.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.3% [1.3%, 1.3%] 1
Regressions ❌
(secondary)
13.9% [8.4%, 24.5%] 3
Improvements ✅
(primary)
-2.6% [-2.6%, -2.6%] 1
Improvements ✅
(secondary)
-5.2% [-7.4%, -3.3%] 7
All ❌✅ (primary) -0.7% [-2.6%, 1.3%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 496.664s -> 494.065s (-0.52%)
Artifact size: 406.89 MiB -> 406.92 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 15, 2026
@saethlin
saethlin force-pushed the match_branch_simplification branch from 35cf001 to 324f136 Compare September 15, 2026 05:07
@saethlin
saethlin marked this pull request as ready for review September 15, 2026 05:09
@rustbot

rustbot commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

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

rustbot commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 76 candidates
  • Random selection from 19 candidates

@saethlin

Copy link
Copy Markdown
Member Author

r? mir-opt

@rustbot rustbot assigned oli-obk and unassigned TaKO8Ki Sep 15, 2026
@saethlin saethlin changed the title Fix a bug in MatchBranchSimplification Avoid creating overlapping assignments in MatchBranchSimplification Sep 15, 2026
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.

5 participants