|
3 | 3 | use std::cmp::Ordering; |
4 | 4 |
|
5 | 5 | use cranelift_module::*; |
6 | | -use rustc_abi::Align; |
7 | | -use rustc_const_eval::interpret::{AllocInit, Allocation, alloc_range}; |
8 | 6 | use rustc_data_structures::fx::FxHashSet; |
9 | 7 | use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; |
10 | 8 | use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint}; |
@@ -177,11 +175,11 @@ pub(crate) fn codegen_const_value<'tcx>( |
177 | 175 | fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); |
178 | 176 | fx.bcx.ins().global_value(fx.pointer_type, local_data_id) |
179 | 177 | } |
180 | | - GlobalAlloc::Type { ty: type_id_ty, segment } => { |
| 178 | + GlobalAlloc::Type { .. } => { |
181 | 179 | return CValue::const_val( |
182 | 180 | fx, |
183 | 181 | layout, |
184 | | - type_id_segment(fx.tcx, type_id_ty, segment), |
| 182 | + ScalarInt::try_from_target_usize(offset.bytes(), fx.tcx).unwrap(), |
185 | 183 | ); |
186 | 184 | } |
187 | 185 | GlobalAlloc::Static(def_id) => { |
@@ -221,23 +219,6 @@ pub(crate) fn codegen_const_value<'tcx>( |
221 | 219 | } |
222 | 220 | } |
223 | 221 |
|
224 | | -fn type_id_segment<'tcx>(tcx: TyCtxt<'tcx>, type_id_ty: Ty<'tcx>, segment: u8) -> ScalarInt { |
225 | | - let type_id = tcx.type_id_hash(type_id_ty).as_u128(); |
226 | | - let mut alloc: Allocation = |
227 | | - Allocation::new(Size::from_bytes(16), Align::from_bytes(8).unwrap(), AllocInit::Uninit, ()); |
228 | | - alloc |
229 | | - .write_scalar( |
230 | | - &tcx, |
231 | | - alloc_range(Size::ZERO, Size::from_bytes(16)), |
232 | | - Scalar::from_u128(type_id), |
233 | | - ) |
234 | | - .unwrap(); |
235 | | - let pointer_size = tcx.data_layout.pointer_size; |
236 | | - let offset = pointer_size * u64::from(segment); |
237 | | - let value = alloc.read_scalar(&tcx, alloc_range(offset, pointer_size), false).unwrap(); |
238 | | - value.to_scalar_int().unwrap() |
239 | | -} |
240 | | - |
241 | 222 | fn pointer_for_allocation<'tcx>( |
242 | 223 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
243 | 224 | alloc_id: AllocId, |
@@ -498,16 +479,9 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant |
498 | 479 | .principal() |
499 | 480 | .map(|principal| tcx.instantiate_bound_regions_with_erased(principal)), |
500 | 481 | ), |
501 | | - GlobalAlloc::Type { ty, segment } => { |
502 | | - let val = type_id_segment(tcx, ty, segment); |
503 | | - let Init::Bytes { contents } = &mut data.init else { unreachable!() }; |
504 | | - let start = offset.bytes_usize(); |
505 | | - let ptr_size = tcx.data_layout.pointer_size; |
506 | | - let src = val.to_bits(ptr_size); |
507 | | - let src = u128::to_le_bytes(src); |
508 | | - let len = ptr_size.bytes_usize(); |
509 | | - assert!(src[len..].iter().all(|b| *b == 0)); |
510 | | - contents[start..(start + len)].copy_from_slice(&src[..len]); |
| 482 | + GlobalAlloc::Type { .. } => { |
| 483 | + // Nothing to do, the bytes/offset of this pointer have already been written together with all other bytes, |
| 484 | + // so we just need to drop this provenance. |
511 | 485 | continue; |
512 | 486 | } |
513 | 487 | GlobalAlloc::Static(def_id) => { |
|
0 commit comments