Skip to content

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Jun 21, 2025

The current implementation of jump threading walks MIR CFG backwards from each SwitchInt terminator. This PR replaces this by a single postorder traversal of MIR. In theory, we could do a full fixpoint dataflow analysis, but this has low returns as we forbid threading through a loop header.

The second commit in this PR modifies the carried state to a lighter data structure. The current implementation uses some kind of IndexVec<ValueIndex, &[Condition]>. This is needlessly heavy, as the state rarely ever carries more than a few Conditions. The first commit replaces this state with a simpler &[Condition], and puts the corresponding ValueIndex inside Condition.

The three later commits are perf tweaks.

The sixth commit is the main change. Instead of carrying the goto target inside the condition, we maintain a set of conditions associated with each block, and their consequences in following blocks. Think: if this condition is fulfilled in this block, then that condition is fulfilled in that block. This makes the threading algorithm much easier to implement, without the extra bookkeeping of ThreadingOpportunity we had.

Later commits modify that algorithm to shrink the set of duplicated blocks. By propagating fulfilled conditions down the CFG, and trimming costly threads.

@rustbot
Copy link
Collaborator

rustbot commented Jun 21, 2025

r? @lcnr

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

@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 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 21, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 21, 2025

@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 Jun 21, 2025
bors added a commit that referenced this pull request Jun 21, 2025
Compute jump threading opportunities in a single pass

The current implementation of jump threading walks MIR CFG backwards from each `SwitchInt` terminator. This PR replaces this by a single postorder traversal of MIR. In theory, we could do a full fixpoint dataflow analysis, but this has low returns as we forbid threading through a loop header, and we do not merge TOs yet.

The second commit in this PR modifies the carried state to a lighter data structure. The current implementation uses some kind of `IndexVec<ValueIndex, &[Condition]>`. This is needlessly heavy, as the state rarely ever carries more than a few `Condition`s. The first commit replaces this state with a simpler `&[Condition]`, and puts the corresponding `ValueIndex` inside `Condition`.

The last commit is the main change. It needs a fair amount of data structure tweaks, as each condition now needs to carry its chain of blocks with it.
@bors
Copy link
Collaborator

bors commented Jun 21, 2025

⌛ Trying commit bdf9d85 with merge d27b44e...

@bors
Copy link
Collaborator

bors commented Jun 21, 2025

☀️ Try build successful - checks-actions
Build commit: d27b44e (d27b44ea37402dbd9462aa3b8eee9ff6d5e4c007)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d27b44e): 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.5% [0.1%, 3.1%] 54
Regressions ❌
(secondary)
1.4% [0.2%, 6.0%] 17
Improvements ✅
(primary)
-0.4% [-0.8%, -0.2%] 8
Improvements ✅
(secondary)
-0.4% [-0.6%, -0.2%] 8
All ❌✅ (primary) 0.4% [-0.8%, 3.1%] 62

Max RSS (memory usage)

Results (primary -1.9%, secondary -3.6%)

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

mean range count
Regressions ❌
(primary)
5.5% [5.5%, 5.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.7% [-4.7%, -2.7%] 4
Improvements ✅
(secondary)
-3.6% [-3.6%, -3.6%] 1
All ❌✅ (primary) -1.9% [-4.7%, 5.5%] 5

Cycles

Results (primary 1.6%, secondary 2.4%)

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

mean range count
Regressions ❌
(primary)
1.6% [0.9%, 2.9%] 3
Regressions ❌
(secondary)
2.4% [1.7%, 3.1%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.6% [0.9%, 2.9%] 3

Binary size

Results (primary -0.1%, 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)
0.2% [0.0%, 1.7%] 24
Regressions ❌
(secondary)
0.2% [0.0%, 0.6%] 8
Improvements ✅
(primary)
-0.3% [-0.8%, -0.0%] 49
Improvements ✅
(secondary)
-0.6% [-13.0%, -0.1%] 86
All ❌✅ (primary) -0.1% [-0.8%, 1.7%] 73

Bootstrap: 689.042s -> 688.964s (-0.01%)
Artifact size: 371.85 MiB -> 371.64 MiB (-0.06%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 21, 2025
@cjgillot cjgillot force-pushed the jump-threading-single branch from bdf9d85 to 3f66e3a Compare June 22, 2025 10:00
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jun 23, 2025

Some changes occurred in coverage tests.

cc @Zalathar

@lcnr
Copy link
Contributor

lcnr commented Jun 23, 2025

r? wg-mir-opt

@rustbot
Copy link
Collaborator

rustbot commented Jun 23, 2025

Failed to set assignee to vertexclique: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@cjgillot cjgillot added the A-mir-opt Area: MIR optimizations label Jun 23, 2025
@cjgillot cjgillot force-pushed the jump-threading-single branch from 325fee6 to b541dc6 Compare July 2, 2025 10:56
@lcnr
Copy link
Contributor

lcnr commented Jul 3, 2025

oh, there are people in the wg which can't actually be assigned for review 😅
r? wg-mir-opt

@rustbot rustbot assigned oli-obk and unassigned lcnr Jul 3, 2025
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

I can yolo-review it (check that the general design makes sense and appears to be doing what it is supposed to), but I am certain I cannot antagonistically review it in the way that we should be reviewing mir opts to make sure we don't have a misoptimization. I have tried the last two weeks but I don't think I am a good reviewer for such work

rustc_index::newtype_index!(
/// This index uniquely identifies a tracked place and therefore a slot in [`State`].
///
/// It is an implementation detail of this module.
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is now outdated

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

r? mir

@rustbot rustbot assigned saethlin and unassigned oli-obk Jul 7, 2025
@cjgillot cjgillot force-pushed the jump-threading-single branch from 31e6085 to 95e10dd Compare October 17, 2025 01:49
@rustbot

This comment has been minimized.

@cjgillot cjgillot force-pushed the jump-threading-single branch from 95e10dd to 87ac07f Compare October 19, 2025 02:12
@rustbot

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Nov 13, 2025

☔ The latest upstream changes (presumably #148789) made this pull request unmergeable. Please resolve the merge conflicts.

@cjgillot cjgillot force-pushed the jump-threading-single branch from 87ac07f to 223620f Compare November 17, 2025 23:41
@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@saethlin
Copy link
Member

saethlin commented Dec 1, 2025

I've been back and forth over this so many times that I think I've built enough confidence in it, even though I'll admit it is at the edge of my grasp.

This PR is really well-organized and well-described.

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 1, 2025

📌 Commit 223620f has been approved by saethlin

It is now in the queue for this repository.

@bors bors 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 Dec 1, 2025
@bors
Copy link
Collaborator

bors commented Dec 1, 2025

⌛ Testing commit 223620f with merge 4ad239f...

@bors
Copy link
Collaborator

bors commented Dec 2, 2025

☀️ Test successful - checks-actions
Approved by: saethlin
Pushing 4ad239f to main...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 2, 2025
@bors bors merged commit 4ad239f into rust-lang:main Dec 2, 2025
12 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Dec 2, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2025

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 1d60f9e (parent) -> 4ad239f (this PR)

Test differences

Show 2 test diffs

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

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 4ad239f4156aa4e7df5ac9eb90ff0ab3d0089d1c --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-apple-various: 3676.2s -> 4508.3s (+22.6%)
  2. x86_64-gnu-stable: 7524.2s -> 6449.8s (-14.3%)
  3. i686-gnu-2: 6044.0s -> 5198.3s (-14.0%)
  4. aarch64-msvc-2: 5566.9s -> 4862.6s (-12.7%)
  5. armhf-gnu: 5377.9s -> 4762.9s (-11.4%)
  6. x86_64-gnu-llvm-21-1: 3711.3s -> 3288.7s (-11.4%)
  7. pr-check-1: 1886.8s -> 1680.3s (-10.9%)
  8. i686-gnu-nopt-1: 8273.0s -> 7388.2s (-10.7%)
  9. x86_64-rust-for-linux: 2990.9s -> 2672.5s (-10.6%)
  10. x86_64-gnu-llvm-20-2: 6249.3s -> 5630.9s (-9.9%)
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.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4ad239f): 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.4% [0.1%, 0.8%] 18
Regressions ❌
(secondary)
0.2% [0.1%, 1.0%] 19
Improvements ✅
(primary)
-0.7% [-1.4%, -0.3%] 8
Improvements ✅
(secondary)
-0.3% [-0.8%, -0.1%] 27
All ❌✅ (primary) 0.1% [-1.4%, 0.8%] 26

Max RSS (memory usage)

Results (primary 1.5%, 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)
3.8% [1.1%, 5.8%] 3
Regressions ❌
(secondary)
2.5% [0.8%, 5.0%] 3
Improvements ✅
(primary)
-5.4% [-5.4%, -5.4%] 1
Improvements ✅
(secondary)
-2.4% [-2.4%, -2.4%] 1
All ❌✅ (primary) 1.5% [-5.4%, 5.8%] 4

Cycles

Results (primary 3.5%, secondary -2.2%)

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

mean range count
Regressions ❌
(primary)
3.5% [3.5%, 3.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.7%, -1.7%] 5
All ❌✅ (primary) 3.5% [3.5%, 3.5%] 1

Binary size

Results (primary -0.2%, 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)
0.2% [0.0%, 0.9%] 7
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 6
Improvements ✅
(primary)
-0.3% [-0.8%, -0.0%] 27
Improvements ✅
(secondary)
-0.5% [-0.7%, -0.0%] 56
All ❌✅ (primary) -0.2% [-0.8%, 0.9%] 34

Bootstrap: 471.751s -> 469.27s (-0.53%)
Artifact size: 386.95 MiB -> 386.74 MiB (-0.06%)

@Mark-Simulacrum Mark-Simulacrum added the perf-regression-triaged The performance regression has been triaged. label Dec 3, 2025
@cjgillot cjgillot deleted the jump-threading-single branch December 3, 2025 03:42
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. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

10 participants