Skip to content

Patterns: represent constants as valtrees #144591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jul 28, 2025

Const patterns are always valtrees now. Let's represent that in the types.

It turns out we do have to carry along the type with the valtree (many places have another ty somewhere, but it's not always the same type, and I wasn't able to disentangle this). We could use ty::Value which nicely pairs up a valtree and a value -- however, a const pattern can be a raw pointer, which is not a valid "type system value". So I went for separate fields instead. I think by now I am regretting that choice, but I decided to wait what the reviewer would say. Using Value would avoid risking mixing the valtree with the wrong type. It also has some convenient methods, though TBH they aren't very needed here since they often double-check the type which is unnecessary work most of the time.

Cc @Nadrieril @BoxyUwU

@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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

@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 Jul 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in match checking

cc @Nadrieril

@RalfJung RalfJung changed the title Pattern valtrees Patterns: represent constants as valtrees Jul 28, 2025
let tcx = self.tcx;
let success_block = target_block(TestBranch::Success);
let fail_block = target_block(TestBranch::Failure);

let mut expect_ty = value.ty();
let mut expect_ty = value_ty;
let value = Const::Ty(value_ty, ty::Const::new_value(tcx, value, value_ty));
Copy link
Member Author

Choose a reason for hiding this comment

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

Oddly, constructing a mir::Const from a valtree requires the type twice...

Copy link
Member

@BoxyUwU BoxyUwU Jul 29, 2025

Choose a reason for hiding this comment

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

Const::Ty storing more than just a ty::Const is kinda meh yeah. This came about when we stopped storing a Ty alongside all ty::Consts. Removing the ty field from Const::Ty would require updating the function mir::Const::ty( to take either a TypingEnv so we can lookup the type of const parameters, and so that we can normalize the resulting type from type_of unevaluated constants and const generic parameter declarations

Copy link
Member Author

Choose a reason for hiding this comment

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

It is odd that mir::Const and ty::Value know their type but ty::Const does not... but I won't touch this now, there's probably a reason we ended up here.^^

Copy link
Member

@BoxyUwU BoxyUwU Jul 29, 2025

Choose a reason for hiding this comment

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

you can likely find a bit of context in #125958 as to how ty::Const wound up the way it did. For ty::Const only ty::Value needs a Ty because everything else we can "figure out" the type from the environment we're in or from the ty::Const itself.

I would expect that mir::Const::Unevaluated doesnt really need to store a Ty either as it should just be equivalent to type_of(uv.def).instantiate(uv.args) and then a normalization call 🤔

Comment on lines +759 to +760
let max = ty.numeric_max_val(cx.tcx).unwrap();
let max = ty::ValTree::from_scalar_int(cx.tcx, max.try_to_scalar_int().unwrap());
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a kind of silly dance. Maybe numeric_max_val should just return a ScalarInt?

@RalfJung
Copy link
Member Author

@rust-timer queue
@bors try

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 28, 2025

⌛ Trying commit b39c019 with merge e111586

To cancel the try build, run the command @bors try cancel.

rust-bors bot added a commit that referenced this pull request Jul 28, 2025
Patterns: represent constants as valtrees
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 28, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 28, 2025

☀️ Try build successful (CI)
Build commit: e111586 (e1115864e9f2387f19e755d0a8a30df5a8933f7c, parent: e3514bde96d2d13586337a48db77fa64b850d249)

@rust-timer

This comment has been minimized.

@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 29, 2025

We could use ty::Value which nicely pairs up a valtree and a value -- however, a const pattern can be a raw pointer, which is not a valid "type system value".

Given that we evaluate all const patterns by going through a ty::Const or ty::UnevaluatedConst we already don't get the property that ty::Const s only have to deal with stuff in const generics. I'm not sure if this is desirable or not and I think I need to finish adt_const_params before I'll be able to tell for sure 🤔

Regardless, with this in mind I think using ty::Value is fine, we can always swap it out for a different Ty/ValTree tuple down the road if it winds up being a problem. It doesn't seem worth making code-quality worse in order to try and maintain a property that doesn't even hold rn and I don't know how we even would do so nicely :D

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e111586): comparison URL.

Overall result: ✅ improvements - no action needed

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.

@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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.8%, -0.1%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.0% [-2.8%, -0.1%] 7

Max RSS (memory usage)

Results (secondary -0.8%)

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.3% [2.3%, 2.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.9% [-3.1%, -2.7%] 3
All ❌✅ (primary) - - 0

Cycles

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

Binary size

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

Bootstrap: 468.526s -> 467.78s (-0.16%)
Artifact size: 376.81 MiB -> 376.82 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@@ -907,7 +891,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
type Ty = RevealedTy<'tcx>;
type Error = ErrorGuaranteed;
type VariantIdx = VariantIdx;
type StrLit = Const<'tcx>;
type StrLit = ty::Value<'tcx>;
Copy link
Member Author

Choose a reason for hiding this comment

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

Oddly it seems like I can just change this type and nobody cares...

@@ -1373,16 +1373,16 @@ enum TestBranch<'tcx> {
/// Success branch, used for tests with two possible outcomes.
Success,
/// Branch corresponding to this constant.
Constant(Const<'tcx>, u128),
Constant(ty::Value<'tcx>, u128),
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the one place where dropping the type and just using a ValTree would work... but for consistency I kept it here, too.

@RalfJung
Copy link
Member Author

@bors try
@rust-timer queue

@rust-timer

This comment has been minimized.

@RalfJung
Copy link
Member Author

@bors try

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

⌛ Trying commit 3ebc541 with merge 44fbf1c

(The previously running try build was automatically cancelled.)

To cancel the try build, run the command @bors try cancel.

rust-bors bot added a commit that referenced this pull request Jul 29, 2025
Patterns: represent constants as valtrees
let bits = value.eval_bits(self.tcx, self.typing_env());
Some(TestBranch::Constant(value, bits))
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
Some(TestBranch::Constant(value.valtree, bits))
Copy link
Member Author

@RalfJung RalfJung Jul 29, 2025

Choose a reason for hiding this comment

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

Something very odd is happening here... this seems to be the only place that constructs a TestBranch::Constant, so bits here is always the result of calling unwrap_leaf().to_bits_unchecked() on the valtree. So, uh, why do we store both the valtree and its bits?

@RalfJung
Copy link
Member Author

@bors try

rust-bors bot added a commit that referenced this pull request Jul 29, 2025
Patterns: represent constants as valtrees
@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

⌛ Trying commit 0a1ffd9 with merge 7380d99

(The previously running try build was automatically cancelled.)

To cancel the try build, run the command @bors try cancel.

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

☀️ Try build successful (CI)
Build commit: 7380d99 (7380d99b508c50a3825bd54044d573ec8dd97f5f, parent: 552904134b564a74489db50aebe7070fdcce895c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7380d99): 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
Regressions ❌
(secondary)
0.9% [0.8%, 1.0%] 6
Improvements ✅
(primary)
-2.3% [-3.3%, -0.1%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-3.3%, -0.1%] 7

Max RSS (memory usage)

Results (primary 3.7%, secondary -0.9%)

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

mean range count
Regressions ❌
(primary)
3.7% [3.7%, 3.7%] 1
Regressions ❌
(secondary)
2.6% [2.5%, 2.7%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-4.7%, -1.9%] 4
All ❌✅ (primary) 3.7% [3.7%, 3.7%] 1

Cycles

Results (primary -0.2%, secondary 4.6%)

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

mean range count
Regressions ❌
(primary)
2.3% [2.3%, 2.3%] 1
Regressions ❌
(secondary)
4.6% [4.5%, 4.7%] 2
Improvements ✅
(primary)
-2.7% [-2.7%, -2.7%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-2.7%, 2.3%] 2

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

Bootstrap: 468.885s -> 469.633s (0.16%)
Artifact size: 376.79 MiB -> 376.87 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@RalfJung
Copy link
Member Author

@bors try
@rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

⌛ Trying commit cbb5fc1 with merge 51ac280

To cancel the try build, run the command @bors try cancel.

rust-bors bot added a commit that referenced this pull request Jul 29, 2025
Patterns: represent constants as valtrees
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

☀️ Try build successful (CI)
Build commit: 51ac280 (51ac2805763b973371abc5ea5c9aced2149b18c6, parent: 7278554d82fa474a4e8b5c67afb009e11e41a841)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (51ac280): 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
Regressions ❌
(secondary)
0.9% [0.8%, 1.0%] 6
Improvements ✅
(primary)
-2.7% [-3.3%, -2.1%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.7% [-3.3%, -2.1%] 6

Max RSS (memory usage)

Results (primary -1.3%, secondary -1.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)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
-1.3% [-1.3%, -1.3%] 1
Improvements ✅
(secondary)
-2.6% [-2.8%, -2.2%] 3
All ❌✅ (primary) -1.3% [-1.3%, -1.3%] 1

Cycles

Results (secondary 2.5%)

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

Binary size

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

Bootstrap: 467.638s -> 467.767s (0.03%)
Artifact size: 376.88 MiB -> 376.87 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@RalfJung
Copy link
Member Author

Well, uh, I don't know why this could be slower than the version with separate types and valtrees...

@fee1-dead
Copy link
Member

r? BoxyUwU

@rustbot rustbot assigned BoxyUwU and unassigned fee1-dead Aug 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 2, 2025

BoxyUwU is currently at their maximum review capacity.
They may take a while to respond.

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.

5 participants