3 use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
4 use rustc_session::config::Lto;
5 use rustc_symbol_mangling::typeid_for_trait_ref;
6 use rustc_target::abi::call::FnAbi;
8 #[derive(Copy, Clone, Debug)]
9 pub struct VirtualIndex(u64);
11 impl<'a, 'tcx> VirtualIndex {
12 pub fn from_index(index: usize) -> Self {
13 VirtualIndex(index as u64)
16 pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
21 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
23 // Load the data pointer from the object.
24 debug!("get_fn({llvtable:?}, {ty:?}, {self:?})");
25 let llty = bx.fn_ptr_backend_type(fn_abi);
26 let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
28 if bx.cx().sess().opts.unstable_opts.virtual_function_elimination
29 && bx.cx().sess().lto() == Lto::Fat
32 bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)));
33 let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
34 let type_checked_load = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
35 let func = bx.extract_value(type_checked_load, 0);
36 bx.pointercast(func, llty)
38 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
39 let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
40 let ptr = bx.load(llty, gep, ptr_align);
41 bx.nonnull_metadata(ptr);
42 // VTable loads are invariant.
43 bx.set_invariant_load(ptr);
48 pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
53 // Load the data pointer from the object.
54 debug!("get_int({:?}, {:?})", llvtable, self);
56 let llty = bx.type_isize();
57 let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
58 let usize_align = bx.tcx().data_layout.pointer_align.abi;
59 let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
60 let ptr = bx.load(llty, gep, usize_align);
61 // VTable loads are invariant.
62 bx.set_invariant_load(ptr);
67 /// This takes a valid `self` receiver type and extracts the principal trait
69 fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
70 for arg in ty.peel_refs().walk() {
71 if let GenericArgKind::Type(ty) = arg.unpack() {
72 if let ty::Dynamic(data, _, _) = ty.kind() {
73 return data.principal().expect("expected principal trait object");
78 bug!("expected a `dyn Trait` ty, found {ty:?}")
81 /// Creates a dynamic vtable for the given type and vtable origin.
82 /// This is used only for objects.
84 /// The vtables are cached instead of created on every call.
86 /// The `trait_ref` encodes the erased self type. Hence if we are
87 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
88 /// `trait_ref` would map `T: Trait`.
89 #[instrument(level = "debug", skip(cx))]
90 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
93 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
98 if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
102 let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
103 let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
104 let vtable_const = cx.const_data_from_alloc(vtable_allocation);
105 let align = cx.data_layout().pointer_align.abi;
106 let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
108 cx.create_vtable_debuginfo(ty, trait_ref, vtable);
109 cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);