Skip to content

Commit 50ad68a

Browse files
committed
use AllocId instead of Allocation in ConstValue::ByRef
1 parent 5d62ab8 commit 50ad68a

File tree

15 files changed

+75
-57
lines changed

15 files changed

+75
-57
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,15 @@ pub(crate) fn codegen_const_value<'tcx>(
222222
CValue::by_val(val, layout)
223223
}
224224
},
225-
ConstValue::ByRef { alloc, offset } => CValue::by_ref(
226-
pointer_for_allocation(fx, alloc)
227-
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
228-
layout,
229-
),
225+
ConstValue::ByRef { alloc_id, offset } => {
226+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
227+
// FIXME: avoid creating multiple allocations for the same AllocId?
228+
CValue::by_ref(
229+
pointer_for_allocation(fx, alloc)
230+
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
231+
layout,
232+
)
233+
}
230234
ConstValue::Slice { data, start, end } => {
231235
let ptr = pointer_for_allocation(fx, data)
232236
.offset_i64(fx, i64::try_from(start).unwrap())

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
172172
.expect("simd_shuffle idx not const");
173173

174174
let idx_bytes = match idx_const {
175-
ConstValue::ByRef { alloc, offset } => {
175+
ConstValue::ByRef { alloc_id, offset } => {
176+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
176177
let size = Size::from_bytes(
177178
4 * ret_lane_count, /* size_of([u32; ret_lane_count]) */
178179
);

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
116116
let b_llval = bx.const_usize((end - start) as u64);
117117
OperandValue::Pair(a_llval, b_llval)
118118
}
119-
ConstValue::ByRef { alloc, offset } => {
119+
ConstValue::ByRef { alloc_id, offset } => {
120+
let alloc = bx.tcx().global_alloc(alloc_id).unwrap_memory();
121+
// FIXME: should we attempt to avoid building the same AllocId multiple times?
120122
return Self::from_const_alloc(bx, layout, alloc, offset);
121123
}
122124
};

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ pub(super) fn op_to_const<'tcx>(
148148
let to_const_value = |mplace: &MPlaceTy<'_>| {
149149
debug!("to_const_value(mplace: {:?})", mplace);
150150
match mplace.ptr().into_parts() {
151-
(Some(alloc_id), offset) => {
152-
let alloc = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
153-
ConstValue::ByRef { alloc, offset }
154-
}
151+
(Some(alloc_id), offset) => ConstValue::ByRef { alloc_id, offset },
155152
(None, offset) => {
156153
assert!(mplace.layout.is_zst());
157154
assert_eq!(

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use rustc_middle::ty::{self, layout::TyAndLayout, Ty};
2424
use rustc_ast::Mutability;
2525

2626
use super::{
27-
AllocId, Allocation, ConstAllocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy,
28-
Projectable, ValueVisitor,
27+
AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy, Projectable,
28+
ValueVisitor,
2929
};
3030
use crate::const_eval;
3131
use crate::errors::{DanglingPtrInFinal, UnsupportedUntypedPointer};
@@ -455,19 +455,23 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
455455
{
456456
/// A helper function that allocates memory for the layout given and gives you access to mutate
457457
/// it. Once your own mutation code is done, the backing `Allocation` is removed from the
458-
/// current `Memory` and returned.
458+
/// current `Memory` and interned as read-only into the global memory.
459459
pub fn intern_with_temp_alloc(
460460
&mut self,
461461
layout: TyAndLayout<'tcx>,
462462
f: impl FnOnce(
463463
&mut InterpCx<'mir, 'tcx, M>,
464464
&PlaceTy<'tcx, M::Provenance>,
465465
) -> InterpResult<'tcx, ()>,
466-
) -> InterpResult<'tcx, ConstAllocation<'tcx>> {
466+
) -> InterpResult<'tcx, AllocId> {
467+
// `allocate` picks a fresh AllocId that we will associate with its data below.
467468
let dest = self.allocate(layout, MemoryKind::Stack)?;
468469
f(self, &dest.clone().into())?;
469470
let mut alloc = self.memory.alloc_map.remove(&dest.ptr().provenance.unwrap()).unwrap().1;
470471
alloc.mutability = Mutability::Not;
471-
Ok(self.tcx.mk_const_alloc(alloc))
472+
let alloc = self.tcx.mk_const_alloc(alloc);
473+
let alloc_id = dest.ptr().provenance.unwrap(); // this was just allocated, it must have provenance
474+
self.tcx.set_alloc_id_memory(alloc_id, alloc);
475+
Ok(alloc_id)
472476
}
473477
}

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
756756
};
757757
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
758758
let op = match val_val {
759-
ConstValue::ByRef { alloc, offset } => {
760-
let id = self.tcx.create_memory_alloc(alloc);
759+
ConstValue::ByRef { alloc_id, offset } => {
761760
// We rely on mutability being set correctly in that allocation to prevent writes
762761
// where none should happen.
763-
let ptr = self.global_base_pointer(Pointer::new(id, offset))?;
762+
let ptr = self.global_base_pointer(Pointer::new(alloc_id, offset))?;
764763
Operand::Indirect(MemPlace::from_ptr(ptr.into()))
765764
}
766765
ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ pub enum ConstValue<'tcx> {
4343

4444
/// A value not represented/representable by `Scalar` or `Slice`
4545
ByRef {
46-
/// The backing memory of the value, may contain more memory than needed for just the value
47-
/// in order to share `ConstAllocation`s between values
48-
alloc: ConstAllocation<'tcx>,
46+
/// The backing memory of the value. May contain more memory than needed for just the value
47+
/// if this points into some other larger ConstValue.
48+
///
49+
/// We use an `AllocId` here instead of a `ConstAllocation<'tcx>` to make sure that when a
50+
/// raw constant (which is basically just an `AllocId`) is turned into a `ConstValue` and
51+
/// back, we can preserve the original `AllocId`.
52+
alloc_id: AllocId,
4953
/// Offset into `alloc`
5054
offset: Size,
5155
},

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,9 @@ fn pretty_print_const_value<'tcx>(
28832883
_ => {}
28842884
}
28852885
}
2886-
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
2886+
(ConstValue::ByRef { alloc_id, offset }, ty::Array(t, n)) if *t == u8_type => {
28872887
let n = n.try_to_bits(tcx.data_layout.pointer_size).unwrap();
2888+
let alloc = tcx.global_alloc(alloc_id).unwrap_memory();
28882889
// cast is ok because we already checked for pointer size (32 or 64 bit) above
28892890
let range = AllocRange { start: offset, size: Size::from_bytes(n) };
28902891
let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,9 @@ pub fn write_allocations<'tcx>(
707707
Either::Left(Either::Right(std::iter::empty()))
708708
}
709709
ConstValue::ZeroSized => Either::Left(Either::Right(std::iter::empty())),
710-
ConstValue::ByRef { alloc, .. } | ConstValue::Slice { data: alloc, .. } => {
711-
Either::Right(alloc_ids_from_alloc(alloc))
710+
ConstValue::Slice { data, .. } => Either::Right(alloc_ids_from_alloc(data)),
711+
ConstValue::ByRef { alloc_id, .. } => {
712+
Either::Left(Either::Left(std::iter::once(alloc_id)))
712713
}
713714
}
714715
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ TrivialTypeTraversalAndLiftImpls! {
502502
::rustc_span::symbol::Ident,
503503
::rustc_errors::ErrorGuaranteed,
504504
interpret::Scalar,
505+
interpret::AllocId,
505506
rustc_target::abi::Size,
506507
ty::BoundVar,
507508
}

0 commit comments

Comments
 (0)