Skip to content

Identify trivial consts without building MIR - #165112

Open
nnethercote wants to merge 5 commits into
rust-lang:mainfrom
nnethercote:identify-trivial-consts
Open

nnethercote wants to merge 5 commits into
rust-lang:mainfrom
nnethercote:identify-trivial-consts

Conversation

@nnethercote

@nnethercote nnethercote commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This PR replaces the existing MIR-based trivial_const check with a HIR-based one, to avoid constructing the MIR unnecessarily for trivial consts. (MIR-based passes such as borrow checking, unsafety checking, etc., are skipped for trivial consts.)

Perf results are good: the best case is libc-0.2.172, where we see best-case wins of -6% icounts, -7% cycles and -7% max-rss.

r? @saethlin

@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 Oct 29, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

@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 Oct 29, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 29, 2026
Identify trivial consts without building MIR
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: fc37d66 (fc37d662ac8c0998e574a244a03f0fae1441488f)
Base parent: ce37c4f (ce37c4ff837c5c5dc45cddf9e1e3704e42ca67ea)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (fc37d66): 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 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.3% [0.0%, 0.5%] 7
Improvements ✅
(primary)
-1.3% [-6.4%, -0.1%] 82
Improvements ✅
(secondary)
-1.6% [-4.5%, -0.1%] 30
All ❌✅ (primary) -1.3% [-6.4%, -0.1%] 82

Max RSS (memory usage)

Results (primary -2.3%, 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.7% [0.4%, 1.1%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.7% [-6.9%, -0.6%] 31
Improvements ✅
(secondary)
-2.4% [-4.9%, -0.5%] 5
All ❌✅ (primary) -2.3% [-6.9%, 1.1%] 35

Cycles

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

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

mean range count
Regressions ❌
(primary)
1.3% [0.6%, 2.5%] 9
Regressions ❌
(secondary)
1.6% [1.6%, 1.6%] 1
Improvements ✅
(primary)
-3.7% [-7.6%, -0.6%] 20
Improvements ✅
(secondary)
-1.6% [-3.2%, -0.4%] 5
All ❌✅ (primary) -2.2% [-7.6%, 2.5%] 29

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 490.291s -> 489.459s (-0.17%)
Artifact size: 406.63 MiB -> 406.82 MiB (0.05%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 29, 2026
@panstromek

Copy link
Copy Markdown
Contributor

I wonder whether this interacts with const generics somehow, because there's #165070, which creates more MIR for type system constants.

@khyperia

Copy link
Copy Markdown
Member

I wonder whether this interacts with const generics somehow, because there's #165070, which creates more MIR for type system constants.

not really! this should all be compatible and nice. to be honest, this new trivial_const is amusingly similar to gca type system direct constants - trivial_const lowers a hir expr to a mir value, const_of_item lowers a hir expr to a ty::Const. It's probably possible to be cute here and merge handling code of something or other here, but, that's not particularly important (and this PR should probably not care about gca at all and just leave it up to const generics folks (me) to deal with any cleanup)

(also, gca type system direct constants probably ought to be considered trivial constants, but, afaik they are not right now, but whatever, doesn't matter, we're a ways away from considering perf with gca. I'll do that at some point!)

@nnethercote

nnethercote commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

I started a Zulip thread to talk about this because it seems like it warrants some wider attention.

jmikedupont2 pushed a commit to meta-introspector/ra-jit that referenced this pull request Nov 5, 2026
A const or static whose body is one scalar literal of a primitive type (or a
negated integer literal of a signed one), with no macro call, no adjustment and
a type check with no error, mismatch or diagnostic, gets its value read off the
literal in hir_ty::consteval::trivial_value: the literal's little-endian bytes at
its type's size, as lowering's lower_literal_to_operand encodes it and the
evaluator's write_allocation truncates it, with no memory map. Every other body
goes to evaluate_by_mir, the previous path, unchanged.

rustc's trivial_const, in the form of rust-lang/rust#165112 (decided before MIR
is built). The test consteval::trivial runs both paths on 21 trivial items and
asserts one interned allocation each, and checks 10 non-trivial bodies (an
operation, a path, a cast, a block, a float negation, type errors) are refused.
@rust-bors

This comment has been minimized.

@nnethercote
nnethercote force-pushed the identify-trivial-consts branch from bbcb9ea to 148c3bd Compare November 26, 2026 02:44
The MIR pretty printer prints trivial consts in a special way, which
turns out to be very useful for writing a test that detects which consts
are considered trivial and which are not.
Currently it has two paths after the MIR is built.
- `trivial_const` path: steal the body, dump MIR if necessary, return the body.
- Other path: dump MIR if necessary, run optimization passes, steal
  the body.

The differing order of steal/dump isn't meaningful.

This commit combines them into a single path with one optional step:
- dump MIR if necessary, run optimization passes if non-`trivial_const`,
  steal the body.

It's neater and will enable subsequent commits.
This avoids useless work: elaboration, creating an obligation, and
evaluating the obligation.
From `rustc_mir_build` to `rustc_middle`.

Necessary because the next commit will use it from
`rustc_mir_transform`. `rustc_middle` is the obvious spot for something
that is needed by both `rustc_mir_build` and `rustc_mir_transform`, and
it's next to `LitToConstInput`.

`parse_float_into_constval` and `parse_float_into_scalar` both must move
as well.
This improves compile times in some cases because less MIR is built.

It requires several changes.

- `trivial_const` now analyzes HIR instead of MIR. The HIR analysis
  gives very similar results but succeeds in slightly fewer cases,
  visible in the changes to `tests/ui/consts/trivial-consts.rs`.

- `mir_const_qualif`: it now checks `trivial_const` and handles that
  case specially with an early return to avoid building MIR.

- In `mir_promoted`, we no longer need
  `ensure_done().trivial_const(def)`. (It was there because
  `mir_promoted` comes immediately afterward and it steals the MIR body
  that `trivial_const` used to require.)

- In `run_required_analyses`, the `is_trivial_const` skip moved above
  `check_unsafety`, because the latter needs THIR and forced `mir_built`
  to run before stealing the THIR.

It also eliminates some of the awkward interactions between
`trivial_const` and MIR building, as seen from the deleted comments and
the removed `trivial_const_provider` function.

There were some other minor test changes.

- Some cycle errors lost a "...which requires building MIR" entry; the
  cycle error still occurs.

- `tests/mir-opt/unusual_item_types.rs` needs non-trivial consts for MIR
  to still be produced and dumped.
@nnethercote
nnethercote force-pushed the identify-trivial-consts branch from 148c3bd to 0b9a047 Compare November 26, 2026 11:01
@nnethercote

Copy link
Copy Markdown
Contributor Author

@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 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Identify trivial consts without building MIR
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6ac36b4 (6ac36b489af3dbf101fd7d93e2dc736aaae53732)
Base parent: 86e8c94 (86e8c946f12c16ad26be9e4813cec57232290d99)

@rust-timer

This comment has been minimized.

@JonathanBrouwer

Copy link
Copy Markdown
Member

This perf run might show false regressions, because of rust-lang/rustc-perf#2650
Please rerun the benchmark after it completes

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (6ac36b4): 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.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
-1.5% [-6.6%, -0.2%] 74
Improvements ✅
(secondary)
-1.7% [-4.8%, -0.2%] 29
All ❌✅ (primary) -1.5% [-6.6%, -0.2%] 74

Max RSS (memory usage)

Results (primary -3.0%, secondary -1.5%)

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

mean range count
Regressions ❌
(primary)
3.3% [3.3%, 3.3%] 1
Regressions ❌
(secondary)
1.3% [0.4%, 3.2%] 4
Improvements ✅
(primary)
-3.3% [-6.1%, -0.6%] 24
Improvements ✅
(secondary)
-2.9% [-5.6%, -1.2%] 8
All ❌✅ (primary) -3.0% [-6.1%, 3.3%] 25

Cycles

Results (primary -4.8%, 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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.8% [-8.1%, -2.0%] 15
Improvements ✅
(secondary)
-2.6% [-4.8%, -0.6%] 10
All ❌✅ (primary) -4.8% [-8.1%, -2.0%] 15

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 461.669s -> 461.947s (0.06%)
Artifact size: 405.59 MiB -> 405.48 MiB (-0.03%)

@rustbot rustbot removed perf-regression Performance regression. S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 26, 2026
@JonathanBrouwer

JonathanBrouwer commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Seems to have been fine, I think because the perf run started minutes before the problematic change actually landed
(that's a lovely perf improvement :3)

@nnethercote
nnethercote marked this pull request as ready for review November 26, 2026 22:41
@rustbot

rustbot commented Nov 26, 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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 26, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

This is now in a decent state, with some good testing.

Best reviewed one commit at a time.

@rust-bors

rust-bors Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

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

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.

8 participants