-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Convert Const to Allocation in smir #114587
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
use rustc_middle::mir::interpret::{alloc_range, ConstValue, Pointer}; | ||
|
||
use super::{mir::Mutability, mir::Safety, with, DefId}; | ||
use crate::rustc_internal::Opaque; | ||
use crate::{ | ||
rustc_internal::Opaque, | ||
rustc_smir::{Stable, Tables}, | ||
}; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct Ty(pub usize); | ||
|
@@ -105,6 +110,9 @@ pub struct AliasDef(pub(crate) DefId); | |
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub struct TraitDef(pub(crate) DefId); | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub struct ConstDef(pub(crate) DefId); | ||
|
||
impl TraitDef { | ||
pub fn trait_decl(&self) -> TraitDecl { | ||
with(|cx| cx.trait_decl(self)) | ||
|
@@ -248,6 +256,7 @@ pub type Bytes = Vec<Option<u8>>; | |
pub type Size = usize; | ||
pub type Prov = Opaque; | ||
pub type Align = u64; | ||
pub type Promoted = u32; | ||
pub type InitMaskMaterialized = Vec<u64>; | ||
|
||
/// Stores the provenance information of pointers stored in memory. | ||
|
@@ -266,6 +275,109 @@ pub struct Allocation { | |
pub mutability: Mutability, | ||
} | ||
|
||
impl Allocation { | ||
/// Creates new empty `Allocation` from given `Align`. | ||
fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation { | ||
Allocation { | ||
bytes: Vec::new(), | ||
provenance: ProvenanceMap { ptrs: Vec::new() }, | ||
align: align.bytes(), | ||
mutability: Mutability::Not, | ||
} | ||
} | ||
} | ||
|
||
// We need this method instead of a Stable implementation | ||
// because we need to get `Ty` of the const we are trying to create, to do that | ||
// we need to have access to `ConstantKind` but we can't access that inside Stable impl. | ||
pub fn new_allocation<'tcx>( | ||
const_kind: &rustc_middle::mir::ConstantKind<'tcx>, | ||
const_value: ConstValue<'tcx>, | ||
tables: &mut Tables<'tcx>, | ||
) -> Allocation { | ||
match const_value { | ||
ConstValue::Scalar(scalar) => { | ||
let size = scalar.size(); | ||
let align = tables | ||
.tcx | ||
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty())) | ||
.unwrap() | ||
.align; | ||
let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi); | ||
allocation | ||
.write_scalar(&tables.tcx, alloc_range(rustc_target::abi::Size::ZERO, size), scalar) | ||
.unwrap(); | ||
allocation.stable(tables) | ||
} | ||
ConstValue::ZeroSized => { | ||
let align = tables | ||
.tcx | ||
.layout_of(rustc_middle::ty::ParamEnv::empty().and(const_kind.ty())) | ||
.unwrap() | ||
.align; | ||
Allocation::new_empty_allocation(align.abi) | ||
} | ||
ConstValue::Slice { data, start, end } => { | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let alloc_id = tables.tcx.create_memory_alloc(data); | ||
let ptr = Pointer::new(alloc_id, rustc_target::abi::Size::from_bytes(start)); | ||
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx); | ||
let scalar_len = rustc_middle::mir::interpret::Scalar::from_target_usize( | ||
(end - start) as u64, | ||
&tables.tcx, | ||
); | ||
let layout = tables | ||
.tcx | ||
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty())) | ||
.unwrap(); | ||
let mut allocation = | ||
rustc_middle::mir::interpret::Allocation::uninit(layout.size, layout.align.abi); | ||
allocation | ||
.write_scalar( | ||
&tables.tcx, | ||
alloc_range(rustc_target::abi::Size::ZERO, tables.tcx.data_layout.pointer_size), | ||
scalar_ptr, | ||
) | ||
.unwrap(); | ||
allocation | ||
.write_scalar( | ||
&tables.tcx, | ||
alloc_range(tables.tcx.data_layout.pointer_size, scalar_len.size()), | ||
scalar_len, | ||
) | ||
.unwrap(); | ||
allocation.stable(tables) | ||
} | ||
ConstValue::ByRef { alloc, offset } => { | ||
let ty_size = tables | ||
.tcx | ||
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty())) | ||
.unwrap() | ||
.size; | ||
let bytes = alloc.0.get_bytes_unchecked(alloc_range(offset, ty_size)); | ||
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. This loses the initialization information and the relocation information. It may be easier to pull out the logic of the |
||
let offset_allocation = rustc_middle::mir::interpret::Allocation::from_bytes( | ||
bytes, | ||
alloc.0.align, | ||
alloc.0.mutability, | ||
); | ||
offset_allocation.stable(tables) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum ConstantKind { | ||
Allocated(Allocation), | ||
Unevaluated(UnevaluatedConst), | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct UnevaluatedConst { | ||
pub ty: Ty, | ||
pub def: ConstDef, | ||
pub args: GenericArgs, | ||
pub promoted: Option<Promoted>, | ||
} | ||
|
||
pub enum TraitSpecializationKind { | ||
None, | ||
Marker, | ||
|
Uh oh!
There was an error while loading. Please reload this page.