Skip to content

Comments

GVN: consider constants of primitive types as deterministic#149366

Merged
rust-bors[bot] merged 6 commits intorust-lang:mainfrom
cjgillot:gvn-primitive
Feb 23, 2026
Merged

GVN: consider constants of primitive types as deterministic#149366
rust-bors[bot] merged 6 commits intorust-lang:mainfrom
cjgillot:gvn-primitive

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Nov 26, 2025

View all comments

GVN separates MIR constants into deterministic and non-deterministic constants. Deterministic constants are defined here as: gives the exact same value each time it is evaluated.

This was mainly useful because of ConstValue::Slice that generated an extra AllocId each time it appeared in the MIR. That variant has been removed. This is still useful for valtrees that hold references, which generate a fresh AllocId for each evaluation.

This PR proposes to consider all constants of primitive type to be deterministic. If a constant of primitive type passes validation, then it does not contain provenance, so we have no risk of having a reference becoming different AllocIds. In particular, valtrees only are leaves.

r? @ghost for perf

@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 Nov 26, 2025
@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 Nov 26, 2025
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 26, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Nov 27, 2025

☀️ Try build successful (CI)
Build commit: 576a61d (576a61d19539ab75e0e4d0799b5c43a37f899cca, parent: 1be6b13be73dc12e98e51b403add4c41a0b77759)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (576a61d): 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.3%, 1.0%] 3
Regressions ❌
(secondary)
0.2% [0.1%, 0.2%] 3
Improvements ✅
(primary)
-0.3% [-0.5%, -0.2%] 15
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.1%] 2
All ❌✅ (primary) -0.1% [-0.5%, 1.0%] 18

Max RSS (memory usage)

Results (primary 0.4%)

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

mean range count
Regressions ❌
(primary)
4.4% [3.8%, 5.0%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-3.7%, -0.9%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [-3.7%, 5.0%] 5

Cycles

Results (secondary 2.9%)

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

Binary size

Results (primary -0.0%, 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.2% [0.0%, 0.5%] 11
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 51
Improvements ✅
(primary)
-0.2% [-0.5%, -0.1%] 18
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.1%] 11
All ❌✅ (primary) -0.0% [-0.5%, 0.5%] 29

Bootstrap: 468.01s -> 469.007s (0.21%)
Artifact size: 386.86 MiB -> 386.97 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 27, 2025
@cjgillot cjgillot changed the title Gvn primitive GVN: consider constants of primitive types as deterministic Nov 27, 2025
@RalfJung
Copy link
Member

RalfJung commented Dec 3, 2025

This PR proposes to consider all constants of primitive type to be deterministic. If a constant of primitive type passes validation, then it does not contain provenance, so we have no risk of having an AllocId becoming different pointers.

What is your notion of "primitive type"? For me that includes references and raw pointers and function pointers, but apparently that is not included in your definition?

EDIT: Ah, ty.is_primitive()... yeah I've been confused by that in the past.^^

| ConstValue::Indirect { .. },
_,
) => true,
Const::Val(..) => true,
Copy link
Member

@RalfJung RalfJung Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this true? It can contain provenance after all, so it can be affected by #128775.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that my PR description and the comments around this function do not reflect how it is used. I'll rephrase everything as a "we can duplicate the const inside a given MIR body".

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

@RalfJung I reworded the PR description and the comments to make the two separate concepts ("const can be cloned" and "const can be created") more explicit. I'd like to hear your suggestions.

@RalfJung
Copy link
Member

The PR description doesn't make either of these concepts explicit I think? The comment in the file helps a lot, thanks.

This was mainly useful because of ConstValue::Slice that generated an extra AllocId each time it appeared in the MIR. That variant has been removed. This is still useful for valtrees that hold references, which generate a fresh AllocId for each evaluation.

It still exists, but it is not non-deterministic any more.

//! different runtime values each time they are evaluated. This used to be the case with
//! `ConstValue::Slice` which have a new pointer each time they are evaluated, and is still the
//! case with valtrees that generate a new allocation each time they are used. This is checked by
//! `is_deterministic`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason to keep a comment for a case that does not exist any more. What is a case that is still relevant today?

Comment on lines 84 to 86
//! Conversely, some constants cannot cross crate boundaries, which could happen because of
//! inlining. For instance, constants that contain a fn pointer (`AllocId` pointing to a
//! `GlobalAlloc::Function`) point to a different symbol in each codegen unit. To avoid this,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"codegen unit" is a smaller boundary than "crate boundary" -- this means they have to stay in the same function, not just in the same crate, right?

@@ -1767,7 +1809,16 @@ fn op_to_prop_const<'tcx>(
// Check that we do not leak a pointer.
// Those pointers may lose part of their identity in codegen.
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#79738 is fixed. But #128775 is still open so maybe we still need this hack?

@@ -1809,10 +1874,9 @@ impl<'tcx> VnState<'_, '_, 'tcx> {
// Check that we do not leak a pointer.
// Those pointers may lose part of their identity in codegen.
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reference to the same closed issue

//! inlining. For instance, constants that contain a fn pointer (`AllocId` pointing to a
//! `GlobalAlloc::Function`) point to a different symbol in each codegen unit. To avoid this,
//! when writing constants in MIR, we do not write `Const`s that contain `AllocId`s. This is
//! checked by `may_have_provenance`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may_have_provenance is only used in an assertion, so -- that can't really be the place where this is checked, right? It's only the sanity check.

//!
//! Second, when writing constants in MIR, we do not write `Const::Slice` or `Const`
//! that contain `AllocId`s.
//! Conversely, some constants cannot cross crate boundaries, which could happen because of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Conversely, some constants cannot cross crate boundaries, which could happen because of
//! Conversely, some constants change their value when moved across crate boundaries, which could happen because of

It'd probably also be good to reference #128775 here

@bors
Copy link
Collaborator

bors commented Dec 22, 2025

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

@cjgillot cjgillot force-pushed the gvn-primitive branch 2 times, most recently from e0e7199 to 04cb159 Compare January 11, 2026 15:41
@cjgillot cjgillot marked this pull request as ready for review January 11, 2026 15:46
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot
Copy link
Collaborator

rustbot commented Feb 22, 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.

@cjgillot
Copy link
Contributor Author

@bors r=RalfJung

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 22, 2026

📌 Commit a2c3ebf has been approved by RalfJung

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 22, 2026
@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
GVN: consider constants of primitive types as deterministic

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/149366)*

GVN separates MIR constants into deterministic and non-deterministic constants. Deterministic constants are defined here as: gives the exact same value each time it is evaluated.

This was mainly useful because of `ConstValue::Slice` that generated an extra `AllocId` each time it appeared in the MIR. That variant has been removed. This is still useful for valtrees that hold references, which generate a fresh `AllocId` for each evaluation.

This PR proposes to consider all constants of primitive type to be deterministic. If a constant of primitive type passes validation, then it does not contain provenance, so we have no risk of having a reference becoming different `AllocId`s. In particular, valtrees only are leaves.

r? @ghost for perf
@Zalathar
Copy link
Member

CI note: The x86_64-gnu-aux job stopped updating its logs hours ago, which suggests that it's on a cursed slow/stalled runner and probably won't finish.

@jhpratt
Copy link
Member

jhpratt commented Feb 23, 2026

That job has been problematic recently. The queue isn't very long (only 3 including this, assuming the rollup succeeds), so let's just move on.

@bors yield

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 23, 2026

Auto build cancelled. Cancelled workflows:

The next pull request likely to be tested is #153000.

@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 23, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 23, 2026

☀️ Test successful - CI
Approved by: RalfJung
Duration: 3h 39m 26s
Pushing eeb94be to main...

@rust-bors rust-bors bot merged commit eeb94be into rust-lang:main Feb 23, 2026
12 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 23, 2026
@github-actions
Copy link
Contributor

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing d3e8bd9 (parent) -> eeb94be (this PR)

Test differences

Show 6 test diffs

6 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard eeb94be79adc9df7a09ad0b2421f16e60e6d932c --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-x86_64-apple: 2h 2m -> 2h 35m (+27.6%)
  2. dist-apple-various: 2h 15m -> 1h 41m (-25.1%)
  3. dist-aarch64-linux: 2h 29m -> 1h 52m (-24.8%)
  4. dist-aarch64-msvc: 2h 1m -> 1h 41m (-16.0%)
  5. x86_64-mingw-2: 2h 24m -> 2h 42m (+12.0%)
  6. dist-aarch64-llvm-mingw: 1h 35m -> 1h 25m (-11.0%)
  7. i686-gnu-nopt-1: 2h 28m -> 2h 12m (-11.0%)
  8. tidy: 2m 40s -> 2m 57s (+10.5%)
  9. i686-gnu-1: 2h 29m -> 2h 14m (-9.9%)
  10. x86_64-msvc-1: 2h 28m -> 2h 42m (+9.8%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (eeb94be): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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.7% [0.6%, 0.8%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.7%, -0.2%] 15
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) -0.1% [-0.7%, 0.8%] 18

Max RSS (memory usage)

Results (primary -0.4%, secondary 2.0%)

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

mean range count
Regressions ❌
(primary)
2.2% [0.6%, 3.2%] 4
Regressions ❌
(secondary)
2.9% [2.6%, 3.3%] 3
Improvements ✅
(primary)
-3.9% [-5.8%, -2.1%] 3
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) -0.4% [-5.8%, 3.2%] 7

Cycles

Results (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)
- - 0
Regressions ❌
(secondary)
4.6% [4.6%, 4.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.8% [-3.0%, -2.6%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.1%, 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.3% [0.0%, 1.6%] 7
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 15
Improvements ✅
(primary)
-0.2% [-0.5%, -0.1%] 19
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.1%] 8
All ❌✅ (primary) -0.1% [-0.5%, 1.6%] 26

Bootstrap: 480.206s -> 481.498s (0.27%)
Artifact size: 397.91 MiB -> 395.88 MiB (-0.51%)

@cjgillot cjgillot deleted the gvn-primitive branch February 23, 2026 23:35
@Mark-Simulacrum
Copy link
Member

Some nice wins on a number of benchmarks. The regressions look potentially real but are generally outweighed by the improvements.

@rustbot label +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.