-
Notifications
You must be signed in to change notification settings - Fork 13.6k
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
base: master
Are you sure you want to change the base?
Conversation
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
Some changes occurred in 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 |
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)); |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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::Const
s. 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
There was a problem hiding this comment.
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.^^
There was a problem hiding this comment.
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 🤔
let max = ty.numeric_max_val(cx.tcx).unwrap(); | ||
let max = ty::ValTree::from_scalar_int(cx.tcx, max.try_to_scalar_int().unwrap()); |
There was a problem hiding this comment.
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
?
ee610cc
to
b39c019
Compare
@rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Patterns: represent constants as valtrees
This comment has been minimized.
This comment has been minimized.
Given that we evaluate all const patterns by going through a Regardless, with this in mind I think using |
Finished benchmarking commit (e111586): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking 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 Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -3.3%, secondary 2.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 468.526s -> 467.78s (-0.16%) |
52c6e8f
to
1870bd2
Compare
@@ -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>; |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
@bors try |
This comment has been minimized.
This comment has been minimized.
36591d9
to
3ebc541
Compare
@bors try |
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)) |
There was a problem hiding this comment.
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?
3ebc541
to
0a1ffd9
Compare
@bors try |
Patterns: represent constants as valtrees
0a1ffd9
to
5e53d76
Compare
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (7380d99): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking 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 @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -0.2%, secondary 4.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (secondary -0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 468.885s -> 469.633s (0.16%) |
@bors try |
This comment has been minimized.
This comment has been minimized.
Patterns: represent constants as valtrees
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (51ac280): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking 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 @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (secondary 2.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 467.638s -> 467.767s (0.03%) |
Well, uh, I don't know why this could be slower than the version with separate types and valtrees... |
r? BoxyUwU |
|
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 usety::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. UsingValue
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