Skip to content

Conversation

@dianqk
Copy link
Member

@dianqk dianqk commented Dec 23, 2025

As an alternative to #150192.

Introduces a new pass that propagates the known ranges of SSA locals.
We can know the ranges of SSA locals at some locations for the following code:

fn foo(a: u32) {
  let b = a < 9;
  if b {
    let c = b; // c is true since b is whitin the range [1, 2)
    let d = a < 8; // d is true since b whitin the range [0, 9)
  }
}

This PR only implements a trivial range: we know one value on switch, assert, and assume.

@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 Dec 23, 2025
@Kobzol
Copy link
Member

Kobzol commented Dec 23, 2025

@bors try @rust-timer queue

(I want to test if try builds work and this PR seems like a good fit for a perf. run :) )

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Dec 23, 2025
[EXPERIMENT] New MIR Pass: SsaRangePropagation
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2025
@rust-bors
Copy link
Contributor

rust-bors bot commented Dec 23, 2025

☀️ Try build successful (CI)
Build commit: d9c5f75 (d9c5f757034b39605aac2eeef1b90a892b2c647d, parent: 99ff3fbb86658b427f5dd7daaae8db5626a63c26)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d9c5f75): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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.3% [0.2%, 0.8%] 6
Regressions ❌
(secondary)
0.1% [0.0%, 0.3%] 8
Improvements ✅
(primary)
-0.4% [-0.9%, -0.2%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.9%, 0.8%] 13

Max RSS (memory usage)

Results (primary -0.9%, 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)
2.0% [1.9%, 2.1%] 2
Regressions ❌
(secondary)
4.2% [0.9%, 5.5%] 7
Improvements ✅
(primary)
-3.8% [-5.4%, -2.2%] 2
Improvements ✅
(secondary)
-1.9% [-2.9%, -1.5%] 5
All ❌✅ (primary) -0.9% [-5.4%, 2.1%] 4

Cycles

Results (primary 1.8%, secondary -1.6%)

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

mean range count
Regressions ❌
(primary)
1.8% [1.8%, 1.8%] 1
Regressions ❌
(secondary)
2.4% [2.3%, 2.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.2% [-6.2%, -1.2%] 5
All ❌✅ (primary) 1.8% [1.8%, 1.8%] 1

Binary size

Results (primary -0.0%, secondary -0.1%)

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

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.7%] 4
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 2
Improvements ✅
(primary)
-0.1% [-0.8%, -0.0%] 17
Improvements ✅
(secondary)
-0.1% [-0.8%, -0.0%] 16
All ❌✅ (primary) -0.0% [-0.8%, 0.7%] 21

Bootstrap: 482.582s -> 484.115s (0.32%)
Artifact size: 390.35 MiB -> 390.71 MiB (0.09%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Dec 23, 2025
@dianqk
Copy link
Member Author

dianqk commented Jan 2, 2026

@bors try parent=99ff3fbb86658b427f5dd7daaae8db5626a63c26 @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jan 2, 2026
[EXPERIMENT] New MIR Pass: SsaRangePropagation
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 2, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 2, 2026

☀️ Try build successful (CI)
Build commit: edacb2e (edacb2e2bfe3e402771d6cc818d38a1c1e0aa524, parent: 99ff3fbb86658b427f5dd7daaae8db5626a63c26)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (edacb2e): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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.3% [0.2%, 0.7%] 6
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 7
Improvements ✅
(primary)
-0.4% [-0.9%, -0.1%] 10
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.9%, 0.7%] 16

Max RSS (memory usage)

Results (primary -0.2%, secondary 1.6%)

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

mean range count
Regressions ❌
(primary)
2.3% [2.0%, 2.6%] 3
Regressions ❌
(secondary)
3.8% [0.5%, 5.8%] 8
Improvements ✅
(primary)
-3.9% [-5.4%, -2.5%] 2
Improvements ✅
(secondary)
-1.9% [-3.0%, -1.5%] 5
All ❌✅ (primary) -0.2% [-5.4%, 2.6%] 5

Cycles

Results (primary 1.8%, secondary -1.3%)

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

mean range count
Regressions ❌
(primary)
1.8% [1.8%, 1.8%] 1
Regressions ❌
(secondary)
2.2% [2.1%, 2.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.4% [-6.3%, -1.2%] 5
All ❌✅ (primary) 1.8% [1.8%, 1.8%] 1

Binary size

Results (primary -0.0%, secondary -0.1%)

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

mean range count
Regressions ❌
(primary)
0.4% [0.1%, 0.7%] 4
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 2
Improvements ✅
(primary)
-0.1% [-0.8%, -0.0%] 16
Improvements ✅
(secondary)
-0.1% [-0.8%, -0.0%] 16
All ❌✅ (primary) -0.0% [-0.8%, 0.7%] 20

Bootstrap: 482.582s -> 482.879s (0.06%)
Artifact size: 390.35 MiB -> 390.72 MiB (0.09%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 2, 2026
@rust-log-analyzer

This comment has been minimized.

@dianqk
Copy link
Member Author

dianqk commented Jan 7, 2026

The pass is fast, and it reduced MIR body definitely. All regressions are from opt, and there are probably more inlining.

@dianqk dianqk marked this pull request as ready for review January 7, 2026 13:23
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
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

@dianqk dianqk added the A-mir-opt Area: MIR optimizations label Jan 7, 2026
@dianqk dianqk changed the title [EXPERIMENT] New MIR Pass: SsaRangePropagation New MIR Pass: SsaRangePropagation Jan 7, 2026
@cjgillot cjgillot self-assigned this Jan 7, 2026
Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

Thanks a lot for coding this! I don't have many comments on the design, this pass is very clear. I mostly have nits/suggestions on the implementation.

View changes since this review

if let Err(Some(place)) = self.simplify_operand(cond, location) {
let successor = Location { block: *target, statement_index: 0 };
if location.block != successor.block
&& self.unique_predecessors.contains(successor.block)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why check location.block != successor.block? We know that location.block has at least one predecessor which is reachable from bb0, so which is not itself. So if successor.block has a unique predecessor, it must be different from location.block, mustn't it?
I'm tempted to turn the location.block != successor.block into an assertion, or do you have a test to exhibit this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, and I can change to location.dominates(successor, self.dominators) for assert.

}
let successor = Location { block: target, statement_index: 0 };
if location.block != successor.block
&& self.unique_predecessors.contains(successor.block)
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, the unique_predecessors check should be equivalent to location.dominates(successor, self.dominators), isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider the test case on_match_2 and on_if_2. They are not equivalent for the SwitchInt terminator.

}

let otherwise = Location { block: targets.otherwise(), statement_index: 0 };
if place.ty(self.local_decls, self.tcx).ty.is_bool()
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind leaving a FIXME to extend this to other types?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have left a FIXME. Those FIXMEs can be fixed when it shows beneficial.

//! let b = a < 9;
//! if b {
//! let c = b; // c is true since b is within the range [1, 2)
//! let d = a < 8; // d is true since b within the range [0, 9)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand these comments. b is boolean, how can it be within a numeric range? And where does the [1, 2) come from?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the integer representation in MIR, so the full range of boolean is [0, 2).

@nnethercote
Copy link
Contributor

I have done a review pass but @cjgillot knows much more about MIR passes than I do and has already made good comments, so I will hand the review over:

r? @cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Jan 8, 2026

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

// FIXME: This should use the intersection of all valid ranges.
let (_, range) =
ranges.iter().find(|(range_loc, _)| range_loc.dominates(location, &self.dominators))?;
Some(*range)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't have a better range, should we fallback to the type-based range? For discriminants in particular?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I will reland #148443 as a subsequent PR.

continue;
}
let successor = Location { block: target, statement_index: 0 };
if self.unique_predecessors.contains(successor.block) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use location.dominates(successor, &self.dominators) here too?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. SwitchInt has multiple targets. For instance, bb0 dominates bb2, but the match value can be 1 or 2 in bb2. on_if_2 show this case.

graph TD;
    bb0(["bb0: match(1:bb1, 2:bb2)"])-->|1|bb1;
    bb0-->|2|bb2;
    bb1-->bb2;
Loading

There is another case. The test case is on_match_2. bb0 dominates bb1, but the match value can be 1 or 2.

graph TD;
    bb0(["bb0: match(1:bb1, 2:bb1)"])-->|1|bb1;
    bb0-->|2|bb1;
Loading

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah too bad, I would have loved to get rid of unique_predecessors...

@nnethercote nnethercote removed their assignment Jan 11, 2026
@cjgillot
Copy link
Contributor

Thanks!
@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 18, 2026

📌 Commit e9a67c7 has been approved by cjgillot

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 18, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 19, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 19, 2026

☀️ Test successful - CI
Approved by: cjgillot
Pushing 3d087e6 to main...

@rust-bors rust-bors bot merged commit 3d087e6 into rust-lang:main Jan 19, 2026
12 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 19, 2026
@github-actions
Copy link
Contributor

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 9b37157 (parent) -> 3d087e6 (this PR)

Test differences

Show 8 test diffs

Stage 1

  • [mir-opt] tests/mir-opt/range/ssa_range.rs: [missing] -> pass (J0)

Stage 2

  • [mir-opt] tests/mir-opt/range/ssa_range.rs: [missing] -> pass (J1)

Additionally, 6 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 3d087e6044bddc65723bf42c76fe4cc33a0076b0 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-apple: 5766.1s -> 7603.5s (+31.9%)
  2. dist-apple-various: 5015.2s -> 3425.8s (-31.7%)
  3. aarch64-apple: 12508.1s -> 9379.5s (-25.0%)
  4. pr-check-1: 2075.9s -> 1805.5s (-13.0%)
  5. pr-check-2: 2660.8s -> 2334.6s (-12.3%)
  6. x86_64-gnu-debug: 7489.6s -> 6605.4s (-11.8%)
  7. dist-aarch64-msvc: 5220.6s -> 5825.0s (+11.6%)
  8. x86_64-gnu-llvm-21-3: 6549.2s -> 7291.7s (+11.3%)
  9. dist-sparcv9-solaris: 5112.8s -> 5671.7s (+10.9%)
  10. dist-x86_64-llvm-mingw: 6582.4s -> 7289.9s (+10.7%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@dianqk dianqk deleted the ssa-range branch January 19, 2026 06:27
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3d087e6): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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.3% [0.2%, 0.4%] 4
Regressions ❌
(secondary)
0.2% [0.2%, 0.3%] 2
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-0.4%, 0.4%] 5

Max RSS (memory usage)

Results (primary -1.7%, secondary 0.7%)

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

mean range count
Regressions ❌
(primary)
2.6% [2.4%, 2.8%] 2
Regressions ❌
(secondary)
2.4% [1.4%, 3.0%] 3
Improvements ✅
(primary)
-3.5% [-10.3%, -0.5%] 5
Improvements ✅
(secondary)
-4.2% [-4.2%, -4.2%] 1
All ❌✅ (primary) -1.7% [-10.3%, 2.8%] 7

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (primary -0.1%, secondary -0.1%)

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

mean range count
Regressions ❌
(primary)
0.1% [0.1%, 0.2%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.7%, -0.0%] 26
Improvements ✅
(secondary)
-0.1% [-0.7%, -0.0%] 16
All ❌✅ (primary) -0.1% [-0.7%, 0.2%] 30

Bootstrap: 474.437s -> 473.646s (-0.17%)
Artifact size: 383.26 MiB -> 383.28 MiB (0.00%)

@panstromek
Copy link
Contributor

perf triage:

pre-merge results roughly match the final results. Based on the assesment from #150309 (comment), I assume some small regressions in opt are expected.

@rustbot label: +perf-regression-triaged

Note that I can't asses very well whether this is justified or not as I don't have that much context. The original motivation for mir-opts was improving compile time, which is what I'm triaging here. From that standpoint this is now negative and it's only acceptable if it later leads to more improvements.

As far as I can tell, the motivation here is to improve runtime performance, for which we don't have much coverage in the benchmarks, so from that standpoint it's also not obvious to me whether this is worth the cost. Somebody with more knowledge than me should make that judgement.

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jan 19, 2026
@dianqk
Copy link
Member Author

dianqk commented Jan 20, 2026

At least for now, the pass probably cannot improve runtime performance because all things should have been done in LLVM. IMO, smaller MIR may be improvements or regressions. For instance, inlining :( or some new opportunities that have more constants are found in LLVM. The regressions are smaller. I don't think it's too bad.

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

Labels

A-mir-opt Area: MIR optimizations merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.

9 participants