Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 29 additions & 35 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,25 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
global_alloc: GlobalAlloc<'tcx>,
schema: Option<&PointerAuthSchema>,
) -> Result<Value<'ll>, 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
.borrow()
.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) => {
Expand All @@ -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::<Hash128>()
});
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::<Hash128>()
});
llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes());
}

Ok(value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn dyn_trait_in_self<'tcx>(
/// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, 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<ty::ExistentialTraitRef<'tcx>>,
Expand Down
49 changes: 49 additions & 0 deletions tests/codegen-llvm/vfe-const.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Loading