Skip to content

GVN: Only propagate borrows from SSA locals#150485

Open
dianqk wants to merge 4 commits intorust-lang:mainfrom
dianqk:gvn-ssa-borrow
Open

GVN: Only propagate borrows from SSA locals#150485
dianqk wants to merge 4 commits intorust-lang:mainfrom
dianqk:gvn-ssa-borrow

Conversation

@dianqk
Copy link
Member

@dianqk dianqk commented Dec 29, 2025

Fixes #141313. This is a more principled fix than #147886.

Using a reference that is not a borrowing of an SSA local at a new location may be UB.

The PR has two major changes.

The first one, when introducing a new dereference at a new location, is that the reference must point to an SSA local or be an immutable argument. dereference_address has handled SSA locals.

The second one, if we cannot guard to the reference point to an SSA local in visit_assign, we have to rewrite the value to opaque. This avoids unifying the following dereferences that also are references:

let b: &T = *a;
// ... `a` is allowed to be modified. `c` and `b` have different borrowing lifetime.
// Unifying them will extend the lifetime of `b`.
let c: &T = *a; 

See also #130853.

This still allows unifying non-reference dereferences:

let a: &T = ...;
let b: T = *a;
// ... a is NOT allowed to be modified.
let c: T = *a; 

r? @cjgillot

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

This comment has been minimized.

@dianqk
Copy link
Member Author

dianqk commented Jan 1, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jan 1, 2026
[DRAFT] GVN: Only propagate borrows from SSA-locals
@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 Jan 1, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 2, 2026

☀️ Try build successful (CI)
Build commit: f23e5de (f23e5de34a174f8bd281d69038b68bf6332b1af6, parent: fcd630976c460c819c4bbcaf107d0c94501205d8)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f23e5de): 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.1%, 0.9%] 20
Regressions ❌
(secondary)
0.4% [0.1%, 2.0%] 27
Improvements ✅
(primary)
-0.6% [-1.2%, -0.2%] 4
Improvements ✅
(secondary)
-0.2% [-0.4%, -0.1%] 7
All ❌✅ (primary) 0.1% [-1.2%, 0.9%] 24

Max RSS (memory usage)

Results (primary -1.1%, secondary -4.7%)

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

mean range count
Regressions ❌
(primary)
2.4% [1.9%, 2.8%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.4% [-6.8%, -0.6%] 3
Improvements ✅
(secondary)
-4.7% [-6.5%, -3.7%] 3
All ❌✅ (primary) -1.1% [-6.8%, 2.8%] 5

Cycles

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

Binary size

Results (primary 0.1%, secondary 0.0%)

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

mean range count
Regressions ❌
(primary)
0.7% [0.0%, 6.9%] 11
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 14
Improvements ✅
(primary)
-0.1% [-1.0%, -0.0%] 27
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 5
All ❌✅ (primary) 0.1% [-1.0%, 6.9%] 38

Bootstrap: 475.254s -> 472.456s (-0.59%)
Artifact size: 390.85 MiB -> 390.82 MiB (-0.01%)

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

dianqk commented Jan 2, 2026

@bors try parent=fcd630976c460c819c4bbcaf107d0c94501205d8 @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jan 2, 2026
[DRAFT] GVN: Only propagate borrows from SSA-locals
@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 Jan 2, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 2, 2026

☀️ Try build successful (CI)
Build commit: 2183340 (218334063dc5d70b7f9d69202bf22750167c6cbf, parent: fcd630976c460c819c4bbcaf107d0c94501205d8)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (2183340): 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.1%, 0.9%] 17
Regressions ❌
(secondary)
0.3% [0.1%, 0.8%] 23
Improvements ✅
(primary)
-0.6% [-1.2%, -0.2%] 4
Improvements ✅
(secondary)
-0.2% [-0.4%, -0.1%] 8
All ❌✅ (primary) 0.1% [-1.2%, 0.9%] 21

Max RSS (memory usage)

Results (primary -1.9%)

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

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.0% [-4.6%, -0.6%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.9% [-4.6%, 1.7%] 4

Cycles

Results (secondary -2.3%)

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.3% [-2.3%, -2.3%] 1
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.2%, 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.7% [0.0%, 6.9%] 11
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 14
Improvements ✅
(primary)
-0.2% [-0.9%, -0.0%] 11
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 5
All ❌✅ (primary) 0.2% [-0.9%, 6.9%] 22

Bootstrap: 475.254s -> 472.783s (-0.52%)
Artifact size: 390.85 MiB -> 390.84 MiB (-0.00%)

@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 17, 2026

@bors try @rust-timer queue

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 28, 2026

☀️ Try build successful (CI)
Build commit: 0e5b1a1 (0e5b1a1b4144d4e37e094f7d74087be1e0077bfb, parent: e96bb7e44fbcc23c1e6009e8d0ee8ab208668fb4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0e5b1a1): 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.2% [0.2%, 0.3%] 33
Regressions ❌
(secondary)
0.2% [0.1%, 0.3%] 25
Improvements ✅
(primary)
-0.6% [-1.2%, -0.2%] 3
Improvements ✅
(secondary)
-0.1% [-0.3%, -0.1%] 9
All ❌✅ (primary) 0.2% [-1.2%, 0.3%] 36

Max RSS (memory usage)

Results (primary -0.9%, secondary -2.6%)

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

mean range count
Regressions ❌
(primary)
3.0% [2.3%, 3.6%] 2
Regressions ❌
(secondary)
2.1% [1.5%, 3.1%] 3
Improvements ✅
(primary)
-2.2% [-3.7%, -1.4%] 6
Improvements ✅
(secondary)
-4.4% [-5.4%, -0.5%] 8
All ❌✅ (primary) -0.9% [-3.7%, 3.6%] 8

Cycles

Results (primary 2.9%, secondary 3.8%)

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

mean range count
Regressions ❌
(primary)
2.9% [1.6%, 4.5%] 22
Regressions ❌
(secondary)
4.2% [1.9%, 8.8%] 41
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.2% [-4.2%, -4.1%] 2
All ❌✅ (primary) 2.9% [1.6%, 4.5%] 22

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.1% [0.0%, 0.3%] 8
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 15
Improvements ✅
(secondary)
-0.1% [-0.2%, -0.0%] 9
All ❌✅ (primary) 0.0% [-0.1%, 0.3%] 23

Bootstrap: 470.891s -> 474.803s (0.83%)
Artifact size: 383.44 MiB -> 385.80 MiB (0.62%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 28, 2026
@dianqk
Copy link
Member Author

dianqk commented Jan 28, 2026

It's weird.
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 28, 2026
GVN: Only propagate borrows from SSA locals
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 28, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 28, 2026

☀️ Try build successful (CI)
Build commit: f1385c1 (f1385c1681402a00f99d1eb766520242f9fde309, parent: 466ea4e6c39f8a43727edcc726ca86b499e14d83)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f1385c1): 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.3%] 6
Regressions ❌
(secondary)
0.6% [0.1%, 1.0%] 10
Improvements ✅
(primary)
-0.6% [-1.1%, -0.1%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-1.1%, 0.3%] 8

Max RSS (memory usage)

Results (primary -1.9%, 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.1% [3.1%, 3.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.7% [-5.3%, -1.7%] 6
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -1.9% [-5.3%, 3.1%] 7

Cycles

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

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)
2.8% [2.8%, 2.8%] 1
Improvements ✅
(primary)
-1.9% [-1.9%, -1.9%] 1
Improvements ✅
(secondary)
-2.8% [-2.8%, -2.8%] 1
All ❌✅ (primary) -1.9% [-1.9%, -1.9%] 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.1% [0.0%, 0.4%] 7
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 15
Improvements ✅
(secondary)
-0.1% [-0.2%, -0.0%] 8
All ❌✅ (primary) 0.0% [-0.1%, 0.4%] 22

Bootstrap: 476.499s -> 476.268s (-0.05%)
Artifact size: 397.91 MiB -> 397.83 MiB (-0.02%)

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

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Feb 4, 2026

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.

@dianqk
Copy link
Member Author

dianqk commented Feb 4, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 4, 2026
GVN: Only propagate borrows from SSA locals
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 4, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 5, 2026

☀️ Try build successful (CI)
Build commit: f2def88 (f2def887c3d08e9845884a5677473348acc2e23f, parent: db3e99bbab28c6ca778b13222becdea54533d908)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f2def88): 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)
1.5% [0.9%, 2.1%] 2
Regressions ❌
(secondary)
0.7% [0.3%, 2.0%] 5
Improvements ✅
(primary)
-1.5% [-1.9%, -1.0%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-1.9%, 2.1%] 4

Max RSS (memory usage)

Results (primary -0.6%, secondary 0.3%)

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

mean range count
Regressions ❌
(primary)
2.5% [1.7%, 3.5%] 3
Regressions ❌
(secondary)
1.5% [0.5%, 2.5%] 2
Improvements ✅
(primary)
-5.3% [-7.6%, -3.1%] 2
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) -0.6% [-7.6%, 3.5%] 5

Cycles

Results (primary 2.8%)

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

mean range count
Regressions ❌
(primary)
2.8% [2.8%, 2.8%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.8% [2.8%, 2.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.0%, 1.2%] 5
Regressions ❌
(secondary)
1.2% [1.2%, 1.2%] 1
Improvements ✅
(primary)
-0.3% [-1.4%, -0.0%] 6
Improvements ✅
(secondary)
-0.1% [-0.2%, -0.0%] 8
All ❌✅ (primary) 0.0% [-1.4%, 1.2%] 11

Bootstrap: 472.94s -> 474.175s (0.26%)
Artifact size: 398.12 MiB -> 398.12 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 5, 2026
@dianqk
Copy link
Member Author

dianqk commented Feb 5, 2026

The two latest perf results that were built with LLVM 22 are different. I am not sure why, but the latest result seems acceptable to me.

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

Labels

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.

GVN misunderstands aliasing, can create overlapping assignments (again)

6 participants