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
@rust-log-analyzer

This comment has been minimized.

@cjgillot cjgillot force-pushed the jump-threading-single branch from 282d317 to 394cb02 Compare September 14, 2025 20:05
@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

@bors try @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 Sep 14, 2025
Compute jump threading opportunities in a single pass
@rust-bors
Copy link

rust-bors bot commented Sep 14, 2025

☀️ Try build successful (CI)
Build commit: 4983996 (4983996d8ea483eeff53efe5cca3d6c27e3e1ff7, parent: 52618eb338609df44978b0ca4451ab7941fd1c7a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4983996): 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.6% [0.1%, 1.6%] 32
Regressions ❌
(secondary)
0.3% [0.1%, 1.7%] 19
Improvements ✅
(primary)
-0.4% [-0.7%, -0.1%] 21
Improvements ✅
(secondary)
-0.7% [-1.2%, -0.1%] 16
All ❌✅ (primary) 0.2% [-0.7%, 1.6%] 53

Max RSS (memory usage)

Results (primary 0.4%, 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)
2.7% [2.3%, 3.1%] 3
Regressions ❌
(secondary)
4.4% [4.2%, 4.6%] 7
Improvements ✅
(primary)
-1.8% [-4.1%, -0.5%] 3
Improvements ✅
(secondary)
-2.3% [-4.5%, -0.9%] 6
All ❌✅ (primary) 0.4% [-4.1%, 3.1%] 6

Cycles

Results (primary 2.6%, 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.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
3.4% [2.2%, 6.3%] 12
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-4.1%, -2.1%] 5
All ❌✅ (primary) 2.6% [2.6%, 2.6%] 1

Binary size

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

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.5%] 26
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 8
Improvements ✅
(primary)
-0.2% [-1.2%, -0.0%] 46
Improvements ✅
(secondary)
-0.7% [-13.0%, -0.0%] 53
All ❌✅ (primary) -0.1% [-1.2%, 1.5%] 72

Bootstrap: 474.182s -> 472.053s (-0.45%)
Artifact size: 390.15 MiB -> 389.62 MiB (-0.13%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 15, 2025
@bors
Copy link
Collaborator

bors commented Sep 17, 2025

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

@cjgillot cjgillot force-pushed the jump-threading-single branch from 1b74a55 to 3a5c954 Compare September 24, 2025 00:25
@cjgillot
Copy link
Contributor Author

@saethlin this new version implements a much cleaner (and very different algorithm) that the previous one. The perf results are much more balanced.

This version opens the way to tractable dfa threading if we want to go that way (for instance if the loop is annotated with loop_match)

@bors
Copy link
Collaborator

bors commented Sep 27, 2025

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

@cjgillot cjgillot force-pushed the jump-threading-single branch from 3a5c954 to f82bdcb Compare September 27, 2025 16:03
@cjgillot cjgillot marked this pull request as ready for review September 27, 2025 16:07
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 27, 2025
@cjgillot cjgillot force-pushed the jump-threading-single branch from f82bdcb to eab0865 Compare October 4, 2025 16:52
@rustbot
Copy link
Collaborator

rustbot commented Oct 4, 2025

This PR was rebased onto a different master 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.

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 perf-regression Performance regression. 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.

9 participants