Skip to content

Keep hot Copy locals out of memory when only a panic path borrows them. - #165362

Open
stephenduong1004 wants to merge 1 commit into
rust-lang:mainfrom
stephenduong1004:prepare-cold
Open

stephenduong1004 wants to merge 1 commit into
rust-lang:mainfrom
stephenduong1004:prepare-cold

Conversation

@stephenduong1004

@stephenduong1004 stephenduong1004 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Add a MIR pass PrepareColdBlocks that keeps hot Copy locals in registers when they are only borrowed on cold panic paths (like panic!("{x}") or assert!(c, "{x}")).

Currently, borrowing &x inside a formatting panic forces non_ssa_locals to allocate a stack slot for x across the entire function, spilling x on the hot path. This pass fixes this by copying x into a fresh temporary and borrowing &copy right before a cold &x instead.

AI Usage Disclosure: Used an LLM to generate the initial prototype (#164821) and help draft tests. I implemented this split change myself based on the prototype and design feedback, and reviewed and verified all code and test outputs.

@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Nov 2, 2026
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

r? @khyperia

rustbot has assigned @khyperia.
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, mir, mir-opt
  • compiler, mir, mir-opt expanded to 77 candidates
  • Random selection from 21 candidates

@maxdexh

maxdexh commented Nov 2, 2026

Copy link
Copy Markdown
Member

@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 Nov 2, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 2, 2026
Keep hot Copy locals out of memory when only a panic path borrows them.
@rust-bors

rust-bors Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8e92508 (8e9250848565f47d620195e660a85a293ae0d759)
Base parent: ae36fe2 (ae36fe22c7e590df40ffd53ac25fea35597c3f3d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (8e92508): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@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
Improvements ✅
(primary)
-0.3% [-0.8%, -0.1%] 24
Improvements ✅
(secondary)
-0.3% [-0.8%, -0.1%] 15
All ❌✅ (primary) -0.3% [-0.8%, -0.1%] 24

Max RSS (memory usage)

Results (primary -3.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)
- - 0
Improvements ✅
(primary)
-3.0% [-4.3%, -1.7%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.0% [-4.3%, -1.7%] 2

Cycles

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

Binary size

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

Bootstrap: 483.826s -> 485.567s (0.36%)
Artifact size: 406.63 MiB -> 406.68 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 2, 2026
@khyperia

khyperia commented Nov 4, 2026

Copy link
Copy Markdown
Member

I'm very much not knowledgeable about mir opts, so, reassigning (I think mir-opt is the right group, lmk if I should have rolled mir or just compiler or something instead):

r? mir-opt

@rustbot rustbot assigned wesleywiser and unassigned khyperia Nov 4, 2026

This branch has not been deployed

No deployments
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.

6 participants