From abd9d876819b668da5d0b4da7dd3d9579ca333c1 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Sat, 14 Nov 2026 14:21:48 +0100 Subject: [PATCH 1/2] VFE: set `!type` for vtables in constants A vtable in a constant takes a different codegen path, and previously it did not apply the attributes that virtual function elimination needs. That could lead to miscompilations - GCC does not need to set the VFE metadata. I'd like it to also use get_vtable, but that requires more refactoring, and I'd prefer to do that in the `rustc_codegen_gcc` repo - Cranelift already shares the vtable creation code across places where it emits one. --- compiler/rustc_codegen_llvm/src/common.rs | 15 +++---- compiler/rustc_codegen_ssa/src/meth.rs | 2 +- tests/codegen-llvm/vfe-const.rs | 49 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 tests/codegen-llvm/vfe-const.rs diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index c6f2ea0f82ab3..777073463c185 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -216,15 +216,12 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { alloc } - 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)); + + return Ok(rustc_codegen_ssa::meth::get_vtable(self, self_ty, principal)); } }; 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) +} From 4e981f49a77625a0ed304b8863ae07e4e4a10986 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 18 Nov 2026 21:33:56 +0100 Subject: [PATCH 2/2] `fn alloc_to_backend`: stop using early returns --- compiler/rustc_codegen_llvm/src/common.rs | 51 +++++++++++------------ 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 777073463c185..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,34 +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(self_ty, dyn_ty) => { let principal = dyn_ty .principal() .map(|principal| self.tcx.instantiate_bound_regions_with_erased(principal)); - return Ok(rustc_codegen_ssa::meth::get_vtable(self, self_ty, 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) } }