Skip to content

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
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<'tcx> Cx<'tcx> {
950950
let kind = if self.tcx.is_thread_local_static(id) {
951951
ExprKind::ThreadLocalRef(id)
952952
} else {
953-
let alloc_id = self.tcx.create_static_alloc(id);
953+
let alloc_id = self.tcx.reserve_and_set_static_alloc(id);
954954
ExprKind::StaticRef { alloc_id, ty, def_id: id }
955955
};
956956
ExprKind::Deref {

compiler/rustc_mir_transform/src/large_enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl EnumSizeOpt {
114114
tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi,
115115
Mutability::Not,
116116
);
117-
let alloc = tcx.create_memory_alloc(tcx.mk_const_alloc(alloc));
117+
let alloc = tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(alloc));
118118
Some((*adt_def, num_discrs, *alloc_cache.entry(ty).or_insert(alloc)))
119119
}
120120
fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_smir/src/rustc_smir/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn new_allocation<'tcx>(
4545
new_empty_allocation(align.abi)
4646
}
4747
ConstValue::Slice { data, start, end } => {
48-
let alloc_id = tables.tcx.create_memory_alloc(data);
48+
let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data);
4949
let ptr = Pointer::new(alloc_id, rustc_target::abi::Size::from_bytes(start));
5050
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
5151
let scalar_len = rustc_middle::mir::interpret::Scalar::from_target_usize(

src/tools/miri/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
382382
.unwrap()
383383
.unwrap();
384384

385-
let main_ptr = ecx.create_fn_alloc_ptr(FnVal::Instance(entry_instance));
385+
let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
386386

387387
// Inlining of `DEFAULT` from
388388
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_session/src/config/sigpipe.rs.

src/tools/miri/src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
711711
let layout = this.machine.layouts.const_raw_ptr;
712712
let dlsym = Dlsym::from_str("signal".as_bytes(), &this.tcx.sess.target.os)?
713713
.expect("`signal` must be an actual dlsym on android");
714-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
714+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
715715
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
716716
Self::alloc_extern_static(this, "signal", val)?;
717717
// A couple zero-initialized pointer-sized extern statics.

src/tools/miri/src/shims/backtrace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6363
// to reconstruct the needed frame information in `handle_miri_resolve_frame`.
6464
// Note that we never actually read or write anything from/to this pointer -
6565
// all of the data is represented by the pointer value itself.
66-
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(instance));
66+
let fn_ptr = this.fn_ptr(FnVal::Instance(instance));
6767
fn_ptr.wrapping_offset(Size::from_bytes(pos.0), this)
6868
})
6969
.collect();
@@ -159,7 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
159159

160160
// Reconstruct the original function pointer,
161161
// which we pass to user code.
162-
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(fn_instance));
162+
let fn_ptr = this.fn_ptr(FnVal::Instance(fn_instance));
163163

164164
let num_fields = dest.layout.fields.count();
165165

src/tools/miri/src/shims/unix/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
232232
let symbol = this.read_pointer(symbol)?;
233233
let symbol_name = this.read_c_str(symbol)?;
234234
if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.os)? {
235-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
235+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
236236
this.write_pointer(ptr, dest)?;
237237
} else {
238238
this.write_null(dest)?;

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
335335
this.read_target_isize(hModule)?;
336336
let name = this.read_c_str(this.read_pointer(lpProcName)?)?;
337337
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? {
338-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
338+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
339339
this.write_pointer(ptr, dest)?;
340340
} else {
341341
this.write_null(dest)?;

0 commit comments

Comments
 (0)