Skip to content

Add post-mono MIR optimizations#156858

Open
cjgillot wants to merge 11 commits into
rust-lang:mainfrom
cjgillot:post-mono-mir-opts
Open

Add post-mono MIR optimizations#156858
cjgillot wants to merge 11 commits into
rust-lang:mainfrom
cjgillot:post-mono-mir-opts

Conversation

@cjgillot

@cjgillot cjgillot commented May 23, 2026

Copy link
Copy Markdown
Contributor

View all comments

This is mostly a rebase of #131650 by @saethlin.

MIR optimizations are limited since they run on polymorphic code. They cannot know of all types nor of their layout.

To work around this limitation @saethlin added a MIR traversal which monomorphizes during traversal (#121421). We also already have a pass #139088 which is explicitly waiting for post-mono MIR passes to happen.

This PR creates a build_codegen_mir query. That query has a peculiar Steal<Cow<'tcx, Body<'tcx>>> return type. This allows reusing optimized_mir when the body is already monomorphic, and also to free memory when we need to clone it. With this device we still have a sizeable max-rss regression.

All this allows to remove just-in-time monomorphization from codegen code. Follow-up PRs can try migrating transforms that happen at codegen time to a post-mono MIR pass.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 23, 2026
@cjgillot

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@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 May 23, 2026
rust-bors Bot pushed a commit that referenced this pull request May 23, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented May 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: c40ae76 (c40ae76fdfbb0d687aafd24fdcd2354ede04422c, parent: 54333ff079780f803f65dcee30c544050b35f544)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c40ae76): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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.9% [0.1%, 3.4%] 80
Regressions ❌
(secondary)
0.7% [0.1%, 3.1%] 70
Improvements ✅
(primary)
-0.4% [-0.7%, -0.3%] 4
Improvements ✅
(secondary)
-0.4% [-1.4%, -0.0%] 6
All ❌✅ (primary) 0.8% [-0.7%, 3.4%] 84

Max RSS (memory usage)

Results (primary 11.7%, secondary 2.8%)

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

mean range count
Regressions ❌
(primary)
12.1% [0.8%, 53.9%] 48
Regressions ❌
(secondary)
3.3% [0.7%, 6.7%] 18
Improvements ✅
(primary)
-8.1% [-8.1%, -8.1%] 1
Improvements ✅
(secondary)
-5.8% [-5.8%, -5.8%] 1
All ❌✅ (primary) 11.7% [-8.1%, 53.9%] 49

Cycles

Results (primary 2.7%, 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)
3.6% [1.8%, 5.5%] 15
Regressions ❌
(secondary)
3.4% [1.9%, 4.4%] 10
Improvements ✅
(primary)
-4.1% [-4.6%, -3.6%] 2
Improvements ✅
(secondary)
-3.3% [-4.5%, -2.1%] 2
All ❌✅ (primary) 2.7% [-4.6%, 5.5%] 17

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.2% [0.0%, 0.6%] 15
Regressions ❌
(secondary)
0.3% [0.0%, 0.5%] 5
Improvements ✅
(primary)
-0.3% [-3.0%, -0.0%] 46
Improvements ✅
(secondary)
-0.1% [-0.4%, -0.0%] 34
All ❌✅ (primary) -0.2% [-3.0%, 0.6%] 61

Bootstrap: 510.282s -> 521.256s (2.15%)
Artifact size: 400.55 MiB -> 398.41 MiB (-0.53%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels May 23, 2026
@jieyouxu jieyouxu self-assigned this May 23, 2026
@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from 73c6c56 to c885a8d Compare May 23, 2026 22:00
@cjgillot

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@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 May 23, 2026
rust-bors Bot pushed a commit that referenced this pull request May 23, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 0a3713a (0a3713a7df23eb1f82606bf484689d5bf5886931, parent: 54333ff079780f803f65dcee30c544050b35f544)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0a3713a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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.0% [0.2%, 16.5%] 27
Regressions ❌
(secondary)
0.3% [0.1%, 0.7%] 16
Improvements ✅
(primary)
-0.4% [-0.8%, -0.1%] 17
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.0%] 8
All ❌✅ (primary) 0.5% [-0.8%, 16.5%] 44

Max RSS (memory usage)

Results (primary 7.3%, secondary 1.9%)

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

mean range count
Regressions ❌
(primary)
8.4% [1.0%, 36.4%] 35
Regressions ❌
(secondary)
2.5% [0.8%, 4.2%] 8
Improvements ✅
(primary)
-2.5% [-7.8%, -0.7%] 4
Improvements ✅
(secondary)
-2.7% [-2.7%, -2.7%] 1
All ❌✅ (primary) 7.3% [-7.8%, 36.4%] 39

Cycles

Results (primary 2.0%, secondary 4.8%)

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

mean range count
Regressions ❌
(primary)
5.4% [1.7%, 17.4%] 5
Regressions ❌
(secondary)
4.8% [3.5%, 7.0%] 5
Improvements ✅
(primary)
-3.5% [-4.6%, -2.3%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.0% [-4.6%, 17.4%] 8

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.2% [0.0%, 0.6%] 16
Regressions ❌
(secondary)
0.3% [0.0%, 0.5%] 5
Improvements ✅
(primary)
-0.3% [-3.0%, -0.0%] 46
Improvements ✅
(secondary)
-0.1% [-0.4%, -0.0%] 34
All ❌✅ (primary) -0.2% [-3.0%, 0.6%] 62

Bootstrap: 510.282s -> 514.101s (0.75%)
Artifact size: 400.55 MiB -> 398.40 MiB (-0.54%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 24, 2026
@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from ec1aa52 to 17bf2a0 Compare May 24, 2026 08:33
@cjgillot

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@Kobzol

Kobzol commented May 25, 2026

Copy link
Copy Markdown
Member

rustc-perf supports dhat, massif and bytehound (https://github.com/rust-lang/rustc-perf/tree/master/collector#preparation).

@RalfJung

Copy link
Copy Markdown
Member

To work around this limitation @saethlin added a MIR traversal which monomorphizes one the run (#121421).

I can't parse this sentence, could you fix the grammar please? :)

This PR creates a build_codegen_mir query. That query has a peculiar Steal<Cow<'tcx, Body<'tcx>>> return type. This allows reusing optimized_mir when the body is already monomorphic, and also to free memory when we need to clone it. With this device we still have a sizeable max-rss regression.

At some point in the future it could be interesting to use this query in Miri, so that we don't monomorphize the same function / MIR block over and over again.

@rust-bors

This comment has been minimized.

@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from 9aed921 to c1e8eef Compare May 29, 2026 20:24
@rustbot

This comment has been minimized.

@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from c1e8eef to 8cd3e31 Compare May 30, 2026 14:46
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from 8cd3e31 to 7d9196b Compare June 4, 2026 09:08
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@cjgillot cjgillot force-pushed the post-mono-mir-opts branch 2 times, most recently from 2c1d69e to 9a5a530 Compare June 5, 2026 10:09
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@cjgillot cjgillot force-pushed the post-mono-mir-opts branch from 9a5a530 to 4341afc Compare June 12, 2026 00:05
@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

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.

@rust-bors

rust-bors Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

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

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. 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