-
Notifications
You must be signed in to change notification settings - Fork 13.4k
always back ty::Const
by an Allocation
#58486
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
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7c966de
Setup `ty::Const` to allow `ConstValue::Scalar` to be backed by an al…
oli-obk a5a5ef1
Fiddle the corresponding allocation through all `RawConst` uses
oli-obk f5fb34c
Make const eval not use the hacky `op_to_const` function
oli-obk db168bc
Fix equality check for constants
oli-obk 9a63fc8
Promoteds always have a backing allocation
oli-obk 78c36b9
Fixup: statics are always backed by an allocation
oli-obk 785302d
Update comment and make it truer by actually doing what it says
oli-obk 8b0dd6d
Explain the `val` caching in `ty::Const`
oli-obk 6906379
Remove a now-dead function argument
oli-obk 1b93eda
Mark a function private that is unused anywhere else
oli-obk b3731ee
Fold an if on a match returning a bool directly into the match
oli-obk b0857a7
Synchronize StableHash with the updated PartialEq impl
oli-obk 1495b6b
Don't create an intermediate place when reading discriminants
oli-obk 92e4471
Note to self
oli-obk 0075868
Insert satisfying `op_to_const` bbqing sound here.
oli-obk a13cfdb
Make validity checking use `MPlaceTy` instead of `OpTy`
oli-obk 981a79e
Eliminate another use of `lazy_const_to_op`
oli-obk c07162c
Simplify `raw_const_to_mplace`
oli-obk defa8d5
Update a comment
oli-obk b653dc2
Another bad function bites the dust
oli-obk d3d145e
Update an outdated comment
oli-obk 1f718b0
Keep MPlaceTy internals private
oli-obk 5ef183d
Don't access the stack if not strictly necessary
oli-obk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,9 @@ use crate::ty::subst::{Substs, Subst, Kind, UnpackedKind}; | |
use crate::ty::{self, AdtDef, TypeFlags, Ty, TyCtxt, TypeFoldable}; | ||
use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv}; | ||
use crate::util::captures::Captures; | ||
use crate::mir::interpret::{Scalar, Pointer}; | ||
use crate::mir::interpret::{Scalar, Pointer, Allocation}; | ||
|
||
use std::hash::{Hash, Hasher}; | ||
use smallvec::SmallVec; | ||
use std::iter; | ||
use std::cmp::Ordering; | ||
|
@@ -2065,7 +2066,7 @@ pub enum LazyConst<'tcx> { | |
} | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
static_assert!(LAZY_CONST_SIZE: ::std::mem::size_of::<LazyConst<'static>>() == 56); | ||
static_assert!(LAZY_CONST_SIZE: ::std::mem::size_of::<LazyConst<'static>>() == 80); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this isn't a problem? Let's do a perf run. |
||
|
||
impl<'tcx> LazyConst<'tcx> { | ||
pub fn map_evaluated<R>(self, f: impl FnOnce(Const<'tcx>) -> Option<R>) -> Option<R> { | ||
|
@@ -2086,15 +2087,68 @@ impl<'tcx> LazyConst<'tcx> { | |
} | ||
|
||
/// Typed constant value. | ||
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq, Ord, PartialOrd)] | ||
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, Eq, Ord, PartialOrd)] | ||
pub struct Const<'tcx> { | ||
pub ty: Ty<'tcx>, | ||
|
||
pub val: ConstValue<'tcx>, | ||
/// This field is an optimization for caching commonly needed values of constants like `usize` | ||
/// (or other integers for enum discriminants) and slices (e.g. from `b"foo"` and `"foo"` | ||
/// literals) | ||
pub val: ConstValue, | ||
|
||
/// The actual backing storage of the constant and a pointer which can be resolved back to the | ||
/// `allocation` field | ||
/// | ||
/// Can be `None` for trivial constants created from literals or directly. Is always `Some` for | ||
/// aggregate constants or any named constant that you can actually end up taking a reference | ||
/// to. This will get unwrapped in situations where we do know that it's a referencable | ||
pub alloc: Option<(&'tcx Allocation, Pointer)>, | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl<'tcx> PartialEq for Const<'tcx> { | ||
fn eq(&self, other: &Self) -> bool { | ||
|
||
self.ty == other.ty && match (self.val, other.val) { | ||
(ConstValue::ByRef, ConstValue::ByRef) => { | ||
let (a, pa) = self.alloc.unwrap(); | ||
let (b, pb) = other.alloc.unwrap(); | ||
// only use the alloc ids to not have to compare the full allocations | ||
// the ids may differ if the allocation is the same | ||
(pa.offset == pb.offset) && (pa.alloc_id == pb.alloc_id || a == b) | ||
}, | ||
// ignore the actual allocation, just compare the values | ||
(ConstValue::Scalar(a), ConstValue::Scalar(b)) => a == b, | ||
(ConstValue::Slice(a, an), ConstValue::Slice(b, bn)) => an == bn && a == b, | ||
// if the values don't match, the consts can't be equal and the type equality should | ||
// have already failed, because we make the decision for non-byref solely based on the | ||
// type | ||
_ => bug!("same type but different value kind in constant: {:#?} {:#?}", self, other), | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> Hash for Const<'tcx> { | ||
fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
let Const { ty, val, alloc } = self; | ||
ty.hash(hasher); | ||
val.hash(hasher); | ||
// don't hash the memory for `Scalar` and `Slice`. There's nothing to be gained | ||
// by it. All the relevant info is contained in the value. | ||
if let ConstValue::ByRef = val { | ||
let (alloc, ptr) = alloc.unwrap(); | ||
// type check for future changes | ||
let alloc: &'tcx Allocation = alloc; | ||
alloc.hash(hasher); | ||
ptr.offset.hash(hasher); | ||
// do not hash the alloc id in the pointer. It does not add anything new to the hash. | ||
// If the hash of the alloc id is the same, then the hash of the allocation would also | ||
// be the same. | ||
} | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
static_assert!(CONST_SIZE: ::std::mem::size_of::<Const<'static>>() == 48); | ||
static_assert!(CONST_SIZE: ::std::mem::size_of::<Const<'static>>() == 72); | ||
|
||
impl<'tcx> Const<'tcx> { | ||
#[inline] | ||
|
@@ -2105,6 +2159,7 @@ impl<'tcx> Const<'tcx> { | |
Self { | ||
val: ConstValue::Scalar(val), | ||
ty, | ||
alloc: None, | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.