diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index c6f2ea0f82ab3..b0b4400fb43b4 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -183,14 +183,12 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { global_alloc: GlobalAlloc<'tcx>, schema: Option<&PointerAuthSchema>, ) -> Result, u64> { - let alloc = match global_alloc { - GlobalAlloc::Function { instance, .. } => { - return Ok(self.get_fn_addr(instance, schema)); - } + match global_alloc { + GlobalAlloc::Function { instance, .. } => Ok(self.get_fn_addr(instance, schema)), GlobalAlloc::Static(def_id) => { assert!(self.tcx.is_static(def_id)); assert!(!self.tcx.is_thread_local_static(def_id)); - return Ok( + Ok( // `alloc_to_backend` might be called by `global_asm!` codegen. In which case // `global_asm!` would need to find the renamed statics to use for symbol name. self.renamed_statics @@ -198,12 +196,12 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { .get(&def_id) .copied() .unwrap_or_else(|| self.get_static(def_id)), - ); + ) } GlobalAlloc::TypeId { .. } => { // Drop the provenance, the offset contains the bytes of the hash, so // just return 0 as base address. - return Err(0); + Err(0) } GlobalAlloc::Memory(alloc) => { @@ -214,37 +212,33 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { return Err(alloc.inner().align.bytes()); } - alloc + let init = const_alloc_to_llvm(self, alloc.inner(), IsStatic::No, IsInitOrFini::No); + let alloc = alloc.inner(); + + let value = match alloc.mutability { + Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None), + Mutability::Not => self.static_addr_of_impl(init, alloc.align, None), + }; + + if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty() { + let hash = self.tcx.with_stable_hashing_context(|mut hcx| { + let mut hasher = StableHasher::new(); + alloc.stable_hash(&mut hcx, &mut hasher); + hasher.finish::() + }); + llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes()); + } + + Ok(value) } - GlobalAlloc::VTable(ty, dyn_ty) => { - self.tcx - .global_alloc(self.tcx.vtable_allocation(( - ty, - dyn_ty.principal().map(|principal| { - self.tcx.instantiate_bound_regions_with_erased(principal) - }), - ))) - .unwrap_memory() + GlobalAlloc::VTable(self_ty, dyn_ty) => { + let principal = dyn_ty + .principal() + .map(|principal| self.tcx.instantiate_bound_regions_with_erased(principal)); + + Ok(rustc_codegen_ssa::meth::get_vtable(self, self_ty, principal)) } - }; - - let init = const_alloc_to_llvm(self, alloc.inner(), IsStatic::No, IsInitOrFini::No); - let alloc = alloc.inner(); - - let value = match alloc.mutability { - Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None), - Mutability::Not => self.static_addr_of_impl(init, alloc.align, None), - }; - if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty() { - let hash = self.tcx.with_stable_hashing_context(|mut hcx| { - let mut hasher = StableHasher::new(); - alloc.stable_hash(&mut hcx, &mut hasher); - hasher.finish::() - }); - llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes()); } - - Ok(value) } } diff --git a/compiler/rustc_codegen_ssa/src/meth.rs b/compiler/rustc_codegen_ssa/src/meth.rs index ea1a7d9724d07..2fb8f0606315f 100644 --- a/compiler/rustc_codegen_ssa/src/meth.rs +++ b/compiler/rustc_codegen_ssa/src/meth.rs @@ -142,7 +142,7 @@ fn dyn_trait_in_self<'tcx>( /// making an object `Foo` from a value of type `Foo`, then /// `trait_ref` would map `T: Trait`. #[instrument(level = "debug", skip(cx))] -pub(crate) fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( +pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>( cx: &Cx, self_ty: Ty<'tcx>, trait_ref: Option>, diff --git a/tests/codegen-llvm/vfe-const.rs b/tests/codegen-llvm/vfe-const.rs new file mode 100644 index 0000000000000..278a01b05214b --- /dev/null +++ b/tests/codegen-llvm/vfe-const.rs @@ -0,0 +1,49 @@ +//@ compile-flags: -Zvirtual-function-elimination -Clto=fat -Zverify-llvm-ir -Cno-prepopulate-passes -Copt-level=0 +//@ no-prefer-dynamic +#![crate_type = "staticlib"] + +// Generating a constant vtable takes a different codegen path. Make sure that it also sets the +// !type and !vcall_visibility attributes. + +// CHECK: @vtable.{{[0-9]+}} = {{.*}}StructB{{.*}} !type ![[META_SIZED:[0-9]+]], !type ![[TRAIT:[0-9]+]], !vcall_visibility ![[VIS:[0-9]+]] +// CHECK: @vtable.{{[0-9]+}} = {{.*}}StructA{{.*}} !type ![[META_SIZED]], !type ![[TRAIT]], !vcall_visibility ![[VIS]] + +// CHECK-DAG: ![[META_SIZED]] = !{i64 0, !"{{.*}}9MetaSized"} +// CHECK-DAG: ![[TRAIT]] = !{i64 0, !"{{.*}}5Trait"} +// CHECK-DAG: ![[VIS]] = !{i64 1} + +trait Trait { + fn method(&self) -> i32; +} + +struct StructA; +struct StructB; + +impl Trait for StructA { + #[inline(never)] + fn method(&self) -> i32 { + 3 + } +} + +impl Trait for StructB { + #[inline(never)] + fn method(&self) -> i32 { + 4 + } +} + +const CONST: &dyn Trait = &StructA; + +#[unsafe(no_mangle)] +fn call(x: &dyn Trait) -> i32 { + x.method() +} + +#[unsafe(no_mangle)] +fn usage() -> i32 { + let a = CONST; + let b: &dyn Trait = &StructB; + + call(a) + call(b) +}