From 3163962305052afa72dcfad52e5fc3c9649ecff5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 12 Sep 2023 08:42:36 +0200 Subject: [PATCH] make it more clear which functions create fresh AllocId --- compiler/rustc_codegen_cranelift/src/constant.rs | 2 +- compiler/rustc_const_eval/src/interpret/cast.rs | 4 ++-- .../rustc_const_eval/src/interpret/intrinsics.rs | 2 +- .../rustc_const_eval/src/interpret/machine.rs | 2 +- compiler/rustc_const_eval/src/interpret/memory.rs | 7 ++----- compiler/rustc_const_eval/src/interpret/traits.rs | 2 +- compiler/rustc_middle/src/mir/interpret/mod.rs | 15 ++++++++------- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/vtable.rs | 6 +++--- .../rustc_mir_build/src/build/expr/as_constant.rs | 6 +++--- compiler/rustc_mir_build/src/thir/cx/expr.rs | 2 +- compiler/rustc_mir_transform/src/large_enums.rs | 2 +- src/tools/miri/src/eval.rs | 2 +- src/tools/miri/src/machine.rs | 2 +- src/tools/miri/src/shims/backtrace.rs | 4 ++-- src/tools/miri/src/shims/unix/foreign_items.rs | 2 +- src/tools/miri/src/shims/windows/foreign_items.rs | 2 +- 17 files changed, 31 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/constant.rs b/compiler/rustc_codegen_cranelift/src/constant.rs index dec407e66dcff..35b0c422cf199 100644 --- a/compiler/rustc_codegen_cranelift/src/constant.rs +++ b/compiler/rustc_codegen_cranelift/src/constant.rs @@ -259,7 +259,7 @@ fn pointer_for_allocation<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, alloc: ConstAllocation<'tcx>, ) -> crate::pointer::Pointer { - let alloc_id = fx.tcx.create_memory_alloc(alloc); + let alloc_id = fx.tcx.reserve_and_set_memory_alloc(alloc); let data_id = data_id_for_alloc_id( &mut fx.constants_cx, &mut *fx.module, diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index 25c74b98611ba..4c826239eca47 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -84,7 +84,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) .ok_or_else(|| err_inval!(TooGeneric))?; - let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance)); + let fn_ptr = self.fn_ptr(FnVal::Instance(instance)); self.write_pointer(fn_ptr, dest)?; } _ => 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> { ty::ClosureKind::FnOnce, ) .ok_or_else(|| err_inval!(TooGeneric))?; - let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance)); + let fn_ptr = self.fn_ptr(FnVal::Instance(instance)); self.write_pointer(fn_ptr, dest)?; } _ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty), diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 7cc3b59d35908..457084ac7b55b 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -57,7 +57,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>( let path = crate::util::type_name(tcx, tp_ty); let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes()); let alloc = tcx.mk_const_alloc(alloc); - let alloc_id = tcx.create_memory_alloc(alloc); + let alloc_id = tcx.reserve_and_set_memory_alloc(alloc); ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: alloc.inner().len() } } sym::needs_drop => { diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs index 9fda6b037c8ec..3733379c2777b 100644 --- a/compiler/rustc_const_eval/src/interpret/machine.rs +++ b/compiler/rustc_const_eval/src/interpret/machine.rs @@ -552,7 +552,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) { def_id: DefId, ) -> InterpResult<$tcx, Pointer> { // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail. - Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO)) + Ok(Pointer::new(ecx.tcx.reserve_and_set_static_alloc(def_id), Size::ZERO)) } #[inline(always)] diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 11bffedf54a61..436c4d521a51c 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -176,12 +176,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { M::adjust_alloc_base_pointer(self, ptr) } - pub fn create_fn_alloc_ptr( - &mut self, - fn_val: FnVal<'tcx, M::ExtraFnVal>, - ) -> Pointer { + pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer { let id = match fn_val { - FnVal::Instance(instance) => self.tcx.create_fn_alloc(instance), + FnVal::Instance(instance) => self.tcx.reserve_and_set_fn_alloc(instance), FnVal::Other(extra) => { // FIXME(RalfJung): Should we have a cache here? let id = self.tcx.reserve_alloc_id(); diff --git a/compiler/rustc_const_eval/src/interpret/traits.rs b/compiler/rustc_const_eval/src/interpret/traits.rs index fa15d466ac125..a9ca268a2a96f 100644 --- a/compiler/rustc_const_eval/src/interpret/traits.rs +++ b/compiler/rustc_const_eval/src/interpret/traits.rs @@ -27,7 +27,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ensure_monomorphic_enough(*self.tcx, ty)?; ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?; - let vtable_symbolic_allocation = self.tcx.create_vtable_alloc(ty, poly_trait_ref); + let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, poly_trait_ref); let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_symbolic_allocation))?; Ok(vtable_ptr.into()) } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index d6bf7e311126c..44d1dcbbe1791 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -389,7 +389,7 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating fn alloc ID"); let instance = ty::Instance::decode(decoder); trace!("decoded fn alloc instance: {:?}", instance); - let alloc_id = decoder.interner().create_fn_alloc(instance); + let alloc_id = decoder.interner().reserve_and_set_fn_alloc(instance); alloc_id } AllocDiscriminant::VTable => { @@ -399,7 +399,8 @@ impl<'s> AllocDecodingSession<'s> { let poly_trait_ref = > as Decodable>::decode(decoder); trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}"); - let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref); + let alloc_id = + decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref); alloc_id } AllocDiscriminant::Static => { @@ -407,7 +408,7 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating extern static alloc ID"); let did = >::decode(decoder); trace!("decoded static def-ID: {:?}", did); - let alloc_id = decoder.interner().create_static_alloc(did); + let alloc_id = decoder.interner().reserve_and_set_static_alloc(did); alloc_id } } @@ -544,13 +545,13 @@ impl<'tcx> TyCtxt<'tcx> { /// Generates an `AllocId` for a static or return a cached one in case this function has been /// called on the same static before. - pub fn create_static_alloc(self, static_id: DefId) -> AllocId { + pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId { self.reserve_and_set_dedup(GlobalAlloc::Static(static_id)) } /// Generates an `AllocId` for a function. Depending on the function type, /// this might get deduplicated or assigned a new ID each time. - pub fn create_fn_alloc(self, instance: Instance<'tcx>) -> AllocId { + pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>) -> AllocId { // Functions cannot be identified by pointers, as asm-equal functions can get deduplicated // by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be // duplicated across crates. @@ -575,7 +576,7 @@ impl<'tcx> TyCtxt<'tcx> { } /// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated. - pub fn create_vtable_alloc( + pub fn reserve_and_set_vtable_alloc( self, ty: Ty<'tcx>, poly_trait_ref: Option>, @@ -588,7 +589,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Statics with identical content will still point to the same `Allocation`, i.e., /// their data will be deduplicated through `Allocation` interning -- but they /// are different places in memory and as such need different IDs. - pub fn create_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId { + pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId { let id = self.reserve_alloc_id(); self.set_alloc_id_memory(id, mem); id diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index eeb87d5561dbc..f748404875759 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -647,7 +647,7 @@ impl<'tcx> TyCtxt<'tcx> { // Create an allocation that just contains these bytes. let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes); let alloc = self.mk_const_alloc(alloc); - self.create_memory_alloc(alloc) + self.reserve_and_set_memory_alloc(alloc) } /// Returns a range of the start/end indices specified with the diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 97402caa0013b..62f41921d888a 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -84,7 +84,7 @@ pub(super) fn vtable_allocation_provider<'tcx>( let scalar = match entry { VtblEntry::MetadataDropInPlace => { let instance = ty::Instance::resolve_drop_in_place(tcx, ty); - let fn_alloc_id = tcx.create_fn_alloc(instance); + let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance); let fn_ptr = Pointer::from(fn_alloc_id); Scalar::from_pointer(fn_ptr, &tcx) } @@ -94,7 +94,7 @@ pub(super) fn vtable_allocation_provider<'tcx>( VtblEntry::Method(instance) => { // Prepare the fn ptr we write into the vtable. let instance = instance.polymorphize(tcx); - let fn_alloc_id = tcx.create_fn_alloc(instance); + let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance); let fn_ptr = Pointer::from(fn_alloc_id); Scalar::from_pointer(fn_ptr, &tcx) } @@ -112,5 +112,5 @@ pub(super) fn vtable_allocation_provider<'tcx>( } vtable.mutability = Mutability::Not; - tcx.create_memory_alloc(tcx.mk_const_alloc(vtable)) + tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(vtable)) } diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 038f4c71d9f05..a7b706d02271b 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -133,7 +133,7 @@ fn lit_to_mir_constant<'tcx>( let s = s.as_str(); let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes()); let allocation = tcx.mk_const_alloc(allocation); - let alloc_id = tcx.create_memory_alloc(allocation); + let alloc_id = tcx.reserve_and_set_memory_alloc(allocation); ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: s.len() } } (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) @@ -141,7 +141,7 @@ fn lit_to_mir_constant<'tcx>( { let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]); let allocation = tcx.mk_const_alloc(allocation); - let alloc_id = tcx.create_memory_alloc(allocation); + let alloc_id = tcx.reserve_and_set_memory_alloc(allocation); ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: data.len() } } (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => { @@ -152,7 +152,7 @@ fn lit_to_mir_constant<'tcx>( { let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]); let allocation = tcx.mk_const_alloc(allocation); - let alloc_id = tcx.create_memory_alloc(allocation); + let alloc_id = tcx.reserve_and_set_memory_alloc(allocation); ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: data.len() } } (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => { diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 6c1f7d7a60667..18b0e9a643e8a 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -950,7 +950,7 @@ impl<'tcx> Cx<'tcx> { let kind = if self.tcx.is_thread_local_static(id) { ExprKind::ThreadLocalRef(id) } else { - let alloc_id = self.tcx.create_static_alloc(id); + let alloc_id = self.tcx.reserve_and_set_static_alloc(id); ExprKind::StaticRef { alloc_id, ty, def_id: id } }; ExprKind::Deref { diff --git a/compiler/rustc_mir_transform/src/large_enums.rs b/compiler/rustc_mir_transform/src/large_enums.rs index f6526b5de1edc..8afbe418502c5 100644 --- a/compiler/rustc_mir_transform/src/large_enums.rs +++ b/compiler/rustc_mir_transform/src/large_enums.rs @@ -114,7 +114,7 @@ impl EnumSizeOpt { tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi, Mutability::Not, ); - let alloc = tcx.create_memory_alloc(tcx.mk_const_alloc(alloc)); + let alloc = tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(alloc)); Some((*adt_def, num_discrs, *alloc_cache.entry(ty).or_insert(alloc))) } fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { diff --git a/src/tools/miri/src/eval.rs b/src/tools/miri/src/eval.rs index beb13ebdfe6fd..3946ce8ef9df2 100644 --- a/src/tools/miri/src/eval.rs +++ b/src/tools/miri/src/eval.rs @@ -382,7 +382,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( .unwrap() .unwrap(); - let main_ptr = ecx.create_fn_alloc_ptr(FnVal::Instance(entry_instance)); + let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance)); // Inlining of `DEFAULT` from // https://github.com/rust-lang/rust/blob/master/compiler/rustc_session/src/config/sigpipe.rs. diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs index 80f8318044802..ce7f47b5b4fee 100644 --- a/src/tools/miri/src/machine.rs +++ b/src/tools/miri/src/machine.rs @@ -711,7 +711,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> { let layout = this.machine.layouts.const_raw_ptr; let dlsym = Dlsym::from_str("signal".as_bytes(), &this.tcx.sess.target.os)? .expect("`signal` must be an actual dlsym on android"); - let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym)); + let ptr = this.fn_ptr(FnVal::Other(dlsym)); let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout); Self::alloc_extern_static(this, "signal", val)?; // A couple zero-initialized pointer-sized extern statics. diff --git a/src/tools/miri/src/shims/backtrace.rs b/src/tools/miri/src/shims/backtrace.rs index c4c9f77fab7f9..bfec4833ac901 100644 --- a/src/tools/miri/src/shims/backtrace.rs +++ b/src/tools/miri/src/shims/backtrace.rs @@ -63,7 +63,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // to reconstruct the needed frame information in `handle_miri_resolve_frame`. // Note that we never actually read or write anything from/to this pointer - // all of the data is represented by the pointer value itself. - let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(instance)); + let fn_ptr = this.fn_ptr(FnVal::Instance(instance)); fn_ptr.wrapping_offset(Size::from_bytes(pos.0), this) }) .collect(); @@ -159,7 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // Reconstruct the original function pointer, // which we pass to user code. - let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(fn_instance)); + let fn_ptr = this.fn_ptr(FnVal::Instance(fn_instance)); let num_fields = dest.layout.fields.count(); diff --git a/src/tools/miri/src/shims/unix/foreign_items.rs b/src/tools/miri/src/shims/unix/foreign_items.rs index 865f01931f706..4bcca5076cac2 100644 --- a/src/tools/miri/src/shims/unix/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/foreign_items.rs @@ -232,7 +232,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let symbol = this.read_pointer(symbol)?; let symbol_name = this.read_c_str(symbol)?; if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.os)? { - let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym)); + let ptr = this.fn_ptr(FnVal::Other(dlsym)); this.write_pointer(ptr, dest)?; } else { this.write_null(dest)?; diff --git a/src/tools/miri/src/shims/windows/foreign_items.rs b/src/tools/miri/src/shims/windows/foreign_items.rs index 28999268ba052..d76d01b07891a 100644 --- a/src/tools/miri/src/shims/windows/foreign_items.rs +++ b/src/tools/miri/src/shims/windows/foreign_items.rs @@ -335,7 +335,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { this.read_target_isize(hModule)?; let name = this.read_c_str(this.read_pointer(lpProcName)?)?; if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? { - let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym)); + let ptr = this.fn_ptr(FnVal::Other(dlsym)); this.write_pointer(ptr, dest)?; } else { this.write_null(dest)?;