Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 430c386

Browse files
committed
make it more clear which functions create fresh AllocId
1 parent 0f8908d commit 430c386

File tree

18 files changed

+45
-45
lines changed

18 files changed

+45
-45
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc_middle::mir::interpret::{
6-
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
6+
read_target_uint, AllocId, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
77
};
88

99
use cranelift_module::*;
@@ -200,17 +200,14 @@ pub(crate) fn codegen_const_value<'tcx>(
200200
CValue::by_val(val, layout)
201201
}
202202
},
203-
ConstValue::Indirect { 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-
}
203+
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
204+
pointer_for_allocation(fx, alloc_id)
205+
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
206+
layout,
207+
),
212208
ConstValue::Slice { data, start, end } => {
213-
let ptr = pointer_for_allocation(fx, data)
209+
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
210+
let ptr = pointer_for_allocation(fx, alloc_id)
214211
.offset_i64(fx, i64::try_from(start).unwrap())
215212
.get_addr(fx);
216213
let len = fx
@@ -224,9 +221,9 @@ pub(crate) fn codegen_const_value<'tcx>(
224221

225222
fn pointer_for_allocation<'tcx>(
226223
fx: &mut FunctionCx<'_, '_, 'tcx>,
227-
alloc: ConstAllocation<'tcx>,
224+
alloc_id: AllocId,
228225
) -> crate::pointer::Pointer {
229-
let alloc_id = fx.tcx.create_memory_alloc(alloc);
226+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
230227
let data_id = data_id_for_alloc_id(
231228
&mut fx.constants_cx,
232229
&mut *fx.module,
@@ -357,6 +354,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
357354
unreachable!()
358355
}
359356
};
357+
// FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
360358
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
361359
module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
362360
});

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
105105
bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
106106
};
107107
let a = Scalar::from_pointer(
108-
Pointer::new(bx.tcx().create_memory_alloc(data), Size::from_bytes(start)),
108+
Pointer::new(
109+
bx.tcx().reserve_and_set_memory_alloc(data),
110+
Size::from_bytes(start),
111+
),
109112
&bx.tcx(),
110113
);
111114
let a_llval = bx.scalar_to_backend(
@@ -118,7 +121,6 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
118121
}
119122
ConstValue::Indirect { alloc_id, offset } => {
120123
let alloc = bx.tcx().global_alloc(alloc_id).unwrap_memory();
121-
// FIXME: should we attempt to avoid building the same AllocId multiple times?
122124
return Self::from_const_alloc(bx, layout, alloc, offset);
123125
}
124126
};
@@ -184,6 +186,8 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
184186
_ if layout.is_zst() => OperandRef::zero_sized(layout),
185187
_ => {
186188
// Neither a scalar nor scalar pair. Load from a place
189+
// FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
190+
// same `ConstAllocation`?
187191
let init = bx.const_data_from_alloc(alloc);
188192
let base_addr = bx.static_addr_of(init, alloc_align, None);
189193

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8484
)
8585
.ok_or_else(|| err_inval!(TooGeneric))?;
8686

87-
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
87+
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
8888
self.write_pointer(fn_ptr, dest)?;
8989
}
9090
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
@@ -116,7 +116,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
116116
ty::ClosureKind::FnOnce,
117117
)
118118
.ok_or_else(|| err_inval!(TooGeneric))?;
119-
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
119+
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
120120
self.write_pointer(fn_ptr, dest)?;
121121
}
122122
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
555555
def_id: DefId,
556556
) -> InterpResult<$tcx, Pointer> {
557557
// Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
558-
Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
558+
Ok(Pointer::new(ecx.tcx.reserve_and_set_static_alloc(def_id), Size::ZERO))
559559
}
560560

561561
#[inline(always)]

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
176176
M::adjust_alloc_base_pointer(self, ptr)
177177
}
178178

179-
pub fn create_fn_alloc_ptr(
180-
&mut self,
181-
fn_val: FnVal<'tcx, M::ExtraFnVal>,
182-
) -> Pointer<M::Provenance> {
179+
pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
183180
let id = match fn_val {
184-
FnVal::Instance(instance) => self.tcx.create_fn_alloc(instance),
181+
FnVal::Instance(instance) => self.tcx.reserve_and_set_fn_alloc(instance),
185182
FnVal::Other(extra) => {
186183
// FIXME(RalfJung): Should we have a cache here?
187184
let id = self.tcx.reserve_alloc_id();

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
768768
// We rely on mutability being set correctly in `data` to prevent writes
769769
// where none should happen.
770770
let ptr = Pointer::new(
771-
self.tcx.create_memory_alloc(data),
771+
self.tcx.reserve_and_set_memory_alloc(data),
772772
Size::from_bytes(start), // offset: `start`
773773
);
774774
Operand::Immediate(Immediate::new_slice(

compiler/rustc_const_eval/src/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2727
ensure_monomorphic_enough(*self.tcx, ty)?;
2828
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
2929

30-
let vtable_symbolic_allocation = self.tcx.create_vtable_alloc(ty, poly_trait_ref);
30+
let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, poly_trait_ref);
3131
let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_symbolic_allocation))?;
3232
Ok(vtable_ptr.into())
3333
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'s> AllocDecodingSession<'s> {
389389
trace!("creating fn alloc ID");
390390
let instance = ty::Instance::decode(decoder);
391391
trace!("decoded fn alloc instance: {:?}", instance);
392-
let alloc_id = decoder.interner().create_fn_alloc(instance);
392+
let alloc_id = decoder.interner().reserve_and_set_fn_alloc(instance);
393393
alloc_id
394394
}
395395
AllocDiscriminant::VTable => {
@@ -399,15 +399,16 @@ impl<'s> AllocDecodingSession<'s> {
399399
let poly_trait_ref =
400400
<Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
401401
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
402-
let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref);
402+
let alloc_id =
403+
decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref);
403404
alloc_id
404405
}
405406
AllocDiscriminant::Static => {
406407
assert!(alloc_id.is_none());
407408
trace!("creating extern static alloc ID");
408409
let did = <DefId as Decodable<D>>::decode(decoder);
409410
trace!("decoded static def-ID: {:?}", did);
410-
let alloc_id = decoder.interner().create_static_alloc(did);
411+
let alloc_id = decoder.interner().reserve_and_set_static_alloc(did);
411412
alloc_id
412413
}
413414
}
@@ -544,13 +545,13 @@ impl<'tcx> TyCtxt<'tcx> {
544545

545546
/// Generates an `AllocId` for a static or return a cached one in case this function has been
546547
/// called on the same static before.
547-
pub fn create_static_alloc(self, static_id: DefId) -> AllocId {
548+
pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
548549
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
549550
}
550551

551552
/// Generates an `AllocId` for a function. Depending on the function type,
552553
/// this might get deduplicated or assigned a new ID each time.
553-
pub fn create_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
554+
pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
554555
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
555556
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
556557
// duplicated across crates.
@@ -575,7 +576,7 @@ impl<'tcx> TyCtxt<'tcx> {
575576
}
576577

577578
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
578-
pub fn create_vtable_alloc(
579+
pub fn reserve_and_set_vtable_alloc(
579580
self,
580581
ty: Ty<'tcx>,
581582
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
@@ -588,7 +589,7 @@ impl<'tcx> TyCtxt<'tcx> {
588589
/// Statics with identical content will still point to the same `Allocation`, i.e.,
589590
/// their data will be deduplicated through `Allocation` interning -- but they
590591
/// are different places in memory and as such need different IDs.
591-
pub fn create_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
592+
pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
592593
let id = self.reserve_alloc_id();
593594
self.set_alloc_id_memory(id, mem);
594595
id

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<'tcx> TyCtxt<'tcx> {
647647
// Create an allocation that just contains these bytes.
648648
let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes);
649649
let alloc = self.mk_const_alloc(alloc);
650-
self.create_memory_alloc(alloc)
650+
self.reserve_and_set_memory_alloc(alloc)
651651
}
652652

653653
/// Returns a range of the start/end indices specified with the

compiler/rustc_middle/src/ty/vtable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
8484
let scalar = match entry {
8585
VtblEntry::MetadataDropInPlace => {
8686
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
87-
let fn_alloc_id = tcx.create_fn_alloc(instance);
87+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
8888
let fn_ptr = Pointer::from(fn_alloc_id);
8989
Scalar::from_pointer(fn_ptr, &tcx)
9090
}
@@ -94,7 +94,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
9494
VtblEntry::Method(instance) => {
9595
// Prepare the fn ptr we write into the vtable.
9696
let instance = instance.polymorphize(tcx);
97-
let fn_alloc_id = tcx.create_fn_alloc(instance);
97+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
9898
let fn_ptr = Pointer::from(fn_alloc_id);
9999
Scalar::from_pointer(fn_ptr, &tcx)
100100
}
@@ -112,5 +112,5 @@ pub(super) fn vtable_allocation_provider<'tcx>(
112112
}
113113

114114
vtable.mutability = Mutability::Not;
115-
tcx.create_memory_alloc(tcx.mk_const_alloc(vtable))
115+
tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(vtable))
116116
}

0 commit comments

Comments
 (0)