Skip to content

Commit 551f481

Browse files
committed
use AllocId instead of Allocation in ConstValue::ByRef
1 parent c728bf3 commit 551f481

File tree

15 files changed

+80
-63
lines changed

15 files changed

+80
-63
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,15 @@ pub(crate) fn codegen_const_value<'tcx>(
200200
CValue::by_val(val, layout)
201201
}
202202
},
203-
ConstValue::ByRef { alloc, offset } => CValue::by_ref(
204-
pointer_for_allocation(fx, alloc)
205-
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
206-
layout,
207-
),
203+
ConstValue::ByRef { alloc_id, offset } => {
204+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
205+
// FIXME: avoid creating multiple allocations for the same AllocId?
206+
CValue::by_ref(
207+
pointer_for_allocation(fx, alloc)
208+
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
209+
layout,
210+
)
211+
}
208212
ConstValue::Slice { data, start, end } => {
209213
let ptr = pointer_for_allocation(fx, data)
210214
.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
@@ -2914,8 +2914,9 @@ fn pretty_print_const_value<'tcx>(
29142914
_ => {}
29152915
}
29162916
}
2917-
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
2917+
(ConstValue::ByRef { alloc_id, offset }, ty::Array(t, n)) if *t == u8_type => {
29182918
let n = n.try_to_target_usize(tcx).unwrap();
2919+
let alloc = tcx.global_alloc(alloc_id).unwrap_memory();
29192920
// cast is ok because we already checked for pointer size (32 or 64 bit) above
29202921
let range = AllocRange { start: offset, size: Size::from_bytes(n) };
29212922
let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,15 @@ pub fn write_allocations<'tcx>(
701701
fn alloc_ids_from_const_val(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
702702
match val {
703703
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _)) => {
704-
Either::Left(Either::Left(std::iter::once(ptr.provenance)))
704+
Either::Left(std::iter::once(ptr.provenance))
705705
}
706-
ConstValue::Scalar(interpret::Scalar::Int { .. }) => {
707-
Either::Left(Either::Right(std::iter::empty()))
708-
}
709-
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))
706+
ConstValue::Scalar(interpret::Scalar::Int { .. }) => Either::Right(std::iter::empty()),
707+
ConstValue::ZeroSized => Either::Right(std::iter::empty()),
708+
ConstValue::Slice { .. } => {
709+
// `u8`/`str` slices, shouldn't contain pointers that we want to print.
710+
Either::Right(std::iter::empty())
712711
}
712+
ConstValue::ByRef { alloc_id, .. } => Either::Left(std::iter::once(alloc_id)),
713713
}
714714
}
715715
struct CollectAllocIds(BTreeSet<AllocId>);

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ TrivialTypeTraversalAndLiftImpls! {
510510
::rustc_span::symbol::Ident,
511511
::rustc_errors::ErrorGuaranteed,
512512
interpret::Scalar,
513+
interpret::AllocId,
513514
rustc_target::abi::Size,
514515
ty::BoundVar,
515516
}

0 commit comments

Comments
 (0)