mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Create a convenience wrapper for get_global_alloc(id).unwrap()
This commit is contained in:
parent
4572d32810
commit
43fcd7d55e
@ -244,8 +244,8 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Scalar::Ptr(ptr) => {
|
Scalar::Ptr(ptr) => {
|
||||||
let base_addr = match self.tcx.get_global_alloc(ptr.alloc_id) {
|
let base_addr = match self.tcx.global_alloc(ptr.alloc_id) {
|
||||||
Some(GlobalAlloc::Memory(alloc)) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
let init = const_alloc_to_llvm(self, alloc);
|
let init = const_alloc_to_llvm(self, alloc);
|
||||||
let value = match alloc.mutability {
|
let value = match alloc.mutability {
|
||||||
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
|
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
|
||||||
@ -256,12 +256,11 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|||||||
}
|
}
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
Some(GlobalAlloc::Function(fn_instance)) => self.get_fn_addr(fn_instance),
|
GlobalAlloc::Function(fn_instance) => self.get_fn_addr(fn_instance),
|
||||||
Some(GlobalAlloc::Static(def_id)) => {
|
GlobalAlloc::Static(def_id) => {
|
||||||
assert!(self.tcx.is_static(def_id));
|
assert!(self.tcx.is_static(def_id));
|
||||||
self.get_static(def_id)
|
self.get_static(def_id)
|
||||||
}
|
}
|
||||||
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
|
||||||
};
|
};
|
||||||
let llval = unsafe {
|
let llval = unsafe {
|
||||||
llvm::LLVMConstInBoundsGEP(
|
llvm::LLVMConstInBoundsGEP(
|
||||||
|
@ -197,7 +197,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
|
|||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
alloc_id: AllocId,
|
alloc_id: AllocId,
|
||||||
) -> Result<(), E::Error> {
|
) -> Result<(), E::Error> {
|
||||||
match tcx.get_global_alloc(alloc_id).expect("no value for given alloc ID") {
|
match tcx.global_alloc(alloc_id) {
|
||||||
GlobalAlloc::Memory(alloc) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
|
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
|
||||||
AllocDiscriminant::Alloc.encode(encoder)?;
|
AllocDiscriminant::Alloc.encode(encoder)?;
|
||||||
@ -513,6 +513,15 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||||||
self.alloc_map.lock().alloc_map.get(&id).cloned()
|
self.alloc_map.lock().alloc_map.get(&id).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[track_caller]
|
||||||
|
pub fn global_alloc(&self, id: AllocId) -> GlobalAlloc<'tcx> {
|
||||||
|
match self.get_global_alloc(id) {
|
||||||
|
Some(alloc) => alloc,
|
||||||
|
None => bug!("could not find allocation for {}", id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
|
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
|
||||||
/// call this function twice, even with the same `Allocation` will ICE the compiler.
|
/// call this function twice, even with the same `Allocation` will ICE the compiler.
|
||||||
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {
|
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {
|
||||||
|
@ -2410,13 +2410,9 @@ pub struct Constant<'tcx> {
|
|||||||
impl Constant<'tcx> {
|
impl Constant<'tcx> {
|
||||||
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
|
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
|
||||||
match self.literal.val.try_to_scalar() {
|
match self.literal.val.try_to_scalar() {
|
||||||
Some(Scalar::Ptr(ptr)) => match tcx.get_global_alloc(ptr.alloc_id) {
|
Some(Scalar::Ptr(ptr)) => match tcx.global_alloc(ptr.alloc_id) {
|
||||||
Some(GlobalAlloc::Static(def_id)) => Some(def_id),
|
GlobalAlloc::Static(def_id) => Some(def_id),
|
||||||
Some(_) => None,
|
_ => None,
|
||||||
None => {
|
|
||||||
tcx.sess.delay_span_bug(DUMMY_SP, "MIR cannot contain dangling const pointers");
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -956,8 +956,7 @@ pub trait PrettyPrinter<'tcx>:
|
|||||||
) => {
|
) => {
|
||||||
let byte_str = self
|
let byte_str = self
|
||||||
.tcx()
|
.tcx()
|
||||||
.get_global_alloc(ptr.alloc_id)
|
.global_alloc(ptr.alloc_id)
|
||||||
.unwrap()
|
|
||||||
.unwrap_memory()
|
.unwrap_memory()
|
||||||
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
|
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -1021,7 +1020,7 @@ pub trait PrettyPrinter<'tcx>:
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
|
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
|
||||||
let instance = self.tcx().get_global_alloc(ptr.alloc_id).unwrap().unwrap_fn();
|
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
|
||||||
self = self.typed_value(
|
self = self.typed_value(
|
||||||
|this| this.print_value_path(instance.def_id(), instance.substs),
|
|this| this.print_value_path(instance.def_id(), instance.substs),
|
||||||
|this| this.print_type(ty),
|
|this| this.print_type(ty),
|
||||||
|
@ -549,10 +549,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
|
|||||||
if a_val == b_val {
|
if a_val == b_val {
|
||||||
Ok(ConstValue::Scalar(a_val))
|
Ok(ConstValue::Scalar(a_val))
|
||||||
} else if let ty::FnPtr(_) = a.ty.kind {
|
} else if let ty::FnPtr(_) = a.ty.kind {
|
||||||
let a_instance =
|
let a_instance = tcx.global_alloc(a_val.assert_ptr().alloc_id).unwrap_fn();
|
||||||
tcx.get_global_alloc(a_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
|
let b_instance = tcx.global_alloc(b_val.assert_ptr().alloc_id).unwrap_fn();
|
||||||
let b_instance =
|
|
||||||
tcx.get_global_alloc(b_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
|
|
||||||
if a_instance == b_instance {
|
if a_instance == b_instance {
|
||||||
Ok(ConstValue::Scalar(a_val))
|
Ok(ConstValue::Scalar(a_val))
|
||||||
} else {
|
} else {
|
||||||
|
@ -130,7 +130,7 @@ pub(super) fn op_to_const<'tcx>(
|
|||||||
|
|
||||||
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
|
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
|
||||||
Scalar::Ptr(ptr) => {
|
Scalar::Ptr(ptr) => {
|
||||||
let alloc = ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory();
|
let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
|
||||||
ConstValue::ByRef { alloc, offset: ptr.offset }
|
ConstValue::ByRef { alloc, offset: ptr.offset }
|
||||||
}
|
}
|
||||||
Scalar::Raw { data, .. } => {
|
Scalar::Raw { data, .. } => {
|
||||||
@ -154,10 +154,9 @@ pub(super) fn op_to_const<'tcx>(
|
|||||||
},
|
},
|
||||||
Immediate::ScalarPair(a, b) => {
|
Immediate::ScalarPair(a, b) => {
|
||||||
let (data, start) = match a.not_undef().unwrap() {
|
let (data, start) = match a.not_undef().unwrap() {
|
||||||
Scalar::Ptr(ptr) => (
|
Scalar::Ptr(ptr) => {
|
||||||
ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
|
(ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
|
||||||
ptr.offset.bytes(),
|
}
|
||||||
),
|
|
||||||
Scalar::Raw { .. } => (
|
Scalar::Raw { .. } => (
|
||||||
ecx.tcx
|
ecx.tcx
|
||||||
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
|
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
|
||||||
@ -204,7 +203,7 @@ fn validate_and_turn_into_const<'tcx>(
|
|||||||
if is_static || cid.promoted.is_some() {
|
if is_static || cid.promoted.is_some() {
|
||||||
let ptr = mplace.ptr.assert_ptr();
|
let ptr = mplace.ptr.assert_ptr();
|
||||||
Ok(ConstValue::ByRef {
|
Ok(ConstValue::ByRef {
|
||||||
alloc: ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
|
alloc: ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(),
|
||||||
offset: ptr.offset,
|
offset: ptr.offset,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -467,7 +467,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
|||||||
})?;
|
})?;
|
||||||
// Make sure we use the ID of the resolved memory, not the lazy one!
|
// Make sure we use the ID of the resolved memory, not the lazy one!
|
||||||
let id = raw_const.alloc_id;
|
let id = raw_const.alloc_id;
|
||||||
let allocation = tcx.get_global_alloc(id).unwrap().unwrap_memory();
|
let allocation = tcx.global_alloc(id).unwrap_memory();
|
||||||
|
|
||||||
(allocation, Some(def_id))
|
(allocation, Some(def_id))
|
||||||
}
|
}
|
||||||
|
@ -1101,7 +1101,7 @@ where
|
|||||||
raw: RawConst<'tcx>,
|
raw: RawConst<'tcx>,
|
||||||
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
|
||||||
// This must be an allocation in `tcx`
|
// This must be an allocation in `tcx`
|
||||||
assert!(self.tcx.get_global_alloc(raw.alloc_id).is_some());
|
let _ = self.tcx.global_alloc(raw.alloc_id);
|
||||||
let ptr = self.tag_global_base_pointer(Pointer::from(raw.alloc_id));
|
let ptr = self.tag_global_base_pointer(Pointer::from(raw.alloc_id));
|
||||||
let layout = self.layout_of(raw.ty)?;
|
let layout = self.layout_of(raw.ty)?;
|
||||||
Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
|
Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
|
||||||
|
@ -1136,15 +1136,15 @@ fn create_mono_items_for_default_impls<'tcx>(
|
|||||||
|
|
||||||
/// Scans the miri alloc in order to find function calls, closures, and drop-glue.
|
/// Scans the miri alloc in order to find function calls, closures, and drop-glue.
|
||||||
fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<MonoItem<'tcx>>) {
|
fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<MonoItem<'tcx>>) {
|
||||||
match tcx.get_global_alloc(alloc_id) {
|
match tcx.global_alloc(alloc_id) {
|
||||||
Some(GlobalAlloc::Static(def_id)) => {
|
GlobalAlloc::Static(def_id) => {
|
||||||
let instance = Instance::mono(tcx, def_id);
|
let instance = Instance::mono(tcx, def_id);
|
||||||
if should_monomorphize_locally(tcx, &instance) {
|
if should_monomorphize_locally(tcx, &instance) {
|
||||||
trace!("collecting static {:?}", def_id);
|
trace!("collecting static {:?}", def_id);
|
||||||
output.push(MonoItem::Static(def_id));
|
output.push(MonoItem::Static(def_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(GlobalAlloc::Memory(alloc)) => {
|
GlobalAlloc::Memory(alloc) => {
|
||||||
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
|
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
|
||||||
for &((), inner) in alloc.relocations().values() {
|
for &((), inner) in alloc.relocations().values() {
|
||||||
rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||||
@ -1152,13 +1152,12 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(GlobalAlloc::Function(fn_instance)) => {
|
GlobalAlloc::Function(fn_instance) => {
|
||||||
if should_monomorphize_locally(tcx, &fn_instance) {
|
if should_monomorphize_locally(tcx, &fn_instance) {
|
||||||
trace!("collecting {:?} with {:#?}", alloc_id, fn_instance);
|
trace!("collecting {:?} with {:#?}", alloc_id, fn_instance);
|
||||||
output.push(create_fn_mono_item(fn_instance));
|
output.push(create_fn_mono_item(fn_instance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => bug!("alloc id without corresponding allocation: {}", alloc_id),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ impl<'tcx> LiteralExpander<'tcx> {
|
|||||||
(ConstValue::Scalar(p), x, y) if x == y => {
|
(ConstValue::Scalar(p), x, y) if x == y => {
|
||||||
match p {
|
match p {
|
||||||
Scalar::Ptr(p) => {
|
Scalar::Ptr(p) => {
|
||||||
let alloc = self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory();
|
let alloc = self.tcx.global_alloc(p.alloc_id).unwrap_memory();
|
||||||
ConstValue::ByRef { alloc, offset: p.offset }
|
ConstValue::ByRef { alloc, offset: p.offset }
|
||||||
}
|
}
|
||||||
Scalar::Raw { .. } => {
|
Scalar::Raw { .. } => {
|
||||||
@ -305,7 +305,7 @@ impl<'tcx> LiteralExpander<'tcx> {
|
|||||||
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
|
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
|
||||||
assert_eq!(t, u);
|
assert_eq!(t, u);
|
||||||
ConstValue::Slice {
|
ConstValue::Slice {
|
||||||
data: self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory(),
|
data: self.tcx.global_alloc(p.alloc_id).unwrap_memory(),
|
||||||
start: p.offset.bytes().try_into().unwrap(),
|
start: p.offset.bytes().try_into().unwrap(),
|
||||||
end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(),
|
end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user