1use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt};
2use rustc_session::config::Lto;
3use rustc_span::bug;
4use rustc_symbol_mangling::typeid_for_trait_ref;
5use rustc_target::callconv::FnAbi;
6use rustc_trait_selection::traits::vtable;
7use tracing::{debug, instrument};
8
9use crate::traits::*;
10
11#[derive(#[automatically_derived]impl ::core::marker::Copy for VirtualIndex { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for VirtualIndex {
}
#[automatically_derived]impl ::core::clone::Clone for VirtualIndex {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<u64>;
*self
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for VirtualIndex {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "VirtualIndex",
&&self.0)
}
}Debug)]
12pub(crate) struct VirtualIndex(u64);
13
14impl<'a, 'tcx> VirtualIndex {
15 pub(crate) fn from_index(index: usize) -> Self {
16 VirtualIndex(index as u64)
17 }
18
19 fn load_field<Bx: BuilderMethods<'a, 'tcx>>(
21 self,
22 bx: &mut Bx,
23 llvtable: Bx::Value,
24 llty: Bx::Type,
25 nonnull: bool,
26 ) -> Bx::Value {
27 let ptr_size = bx.data_layout().pointer_size();
28 let vtable_byte_offset = self.0 * ptr_size.bytes();
29
30 let gep = bx.inbounds_ptradd(llvtable, bx.const_usize(vtable_byte_offset));
31 let ptr_align = bx.data_layout().pointer_align();
32 let ptr = bx.load(llty, gep, ptr_align);
33
34 bx.set_invariant_load(ptr);
36
37 if nonnull {
38 bx.nonnull_metadata(ptr);
39 }
40
41 ptr
42 }
43
44 fn get_fn_inner<Bx: BuilderMethods<'a, 'tcx>>(
45 self,
46 bx: &mut Bx,
47 llvtable: Bx::Value,
48 ty: Ty<'tcx>,
49 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
50 nonnull: bool,
51 ) -> Bx::Value {
52 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs:53",
"rustc_codegen_ssa::meth", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs"),
::tracing_core::__macro_support::Option::Some(53u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::meth"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_fn({0:?}, {1:?}, {2:?})",
llvtable, ty, self) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_fn({llvtable:?}, {ty:?}, {self:?})");
54
55 let llty = bx.fn_ptr_backend_type(fn_abi);
56 let ptr_size = bx.data_layout().pointer_size();
57 let vtable_byte_offset = self.0 * ptr_size.bytes();
58
59 if bx.cx().sess().opts.unstable_opts.virtual_function_elimination
60 && bx.cx().sess().lto() == Lto::Fat
61 {
62 if let Some(trait_ref) = dyn_trait_in_self(bx.tcx(), ty) {
63 let typeid = typeid_for_trait_ref(bx.tcx(), trait_ref);
64 let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid.as_bytes());
65 return func;
66 } else if nonnull {
67 ::rustc_span::macros::bug_impl(None,
format_args!("load nonnull value from a vtable without a principal trait"),
Location::caller())bug!("load nonnull value from a vtable without a principal trait")
68 }
69 }
70
71 self.load_field(bx, llvtable, llty, nonnull)
72 }
73
74 pub(crate) fn get_optional_fn<Bx: BuilderMethods<'a, 'tcx>>(
75 self,
76 bx: &mut Bx,
77 llvtable: Bx::Value,
78 ty: Ty<'tcx>,
79 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
80 ) -> Bx::Value {
81 self.get_fn_inner(bx, llvtable, ty, fn_abi, false)
82 }
83
84 pub(crate) fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
85 self,
86 bx: &mut Bx,
87 llvtable: Bx::Value,
88 ty: Ty<'tcx>,
89 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
90 ) -> Bx::Value {
91 self.get_fn_inner(bx, llvtable, ty, fn_abi, true)
92 }
93
94 pub(crate) fn get_data_ptr<Bx: BuilderMethods<'a, 'tcx>>(
96 self,
97 bx: &mut Bx,
98 llvtable: Bx::Value,
99 nonnull: bool,
100 ) -> Bx::Value {
101 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs:101",
"rustc_codegen_ssa::meth", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs"),
::tracing_core::__macro_support::Option::Some(101u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::meth"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_data_ptr({0:?}, {1:?})",
llvtable, self) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_data_ptr({:?}, {:?})", llvtable, self);
102 self.load_field(bx, llvtable, bx.type_ptr(), nonnull)
103 }
104
105 pub(crate) fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
106 self,
107 bx: &mut Bx,
108 llvtable: Bx::Value,
109 ) -> Bx::Value {
110 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs:110",
"rustc_codegen_ssa::meth", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs"),
::tracing_core::__macro_support::Option::Some(110u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::meth"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_usize({0:?}, {1:?})",
llvtable, self) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_usize({:?}, {:?})", llvtable, self);
111 self.load_field(bx, llvtable, bx.type_isize(), false)
112 }
113}
114
115fn dyn_trait_in_self<'tcx>(
118 tcx: TyCtxt<'tcx>,
119 ty: Ty<'tcx>,
120) -> Option<ty::ExistentialTraitRef<'tcx>> {
121 for arg in ty.peel_refs().walk() {
122 if let GenericArgKind::Type(ty) = arg.kind()
123 && let ty::Dynamic(data, _) = ty.kind()
124 {
125 return data
128 .principal()
129 .map(|principal| tcx.instantiate_bound_regions_with_erased(principal));
130 }
131 }
132
133 ::rustc_span::macros::bug_impl(None,
format_args!("expected a `dyn Trait` ty, found {0:?}", ty),
Location::caller())bug!("expected a `dyn Trait` ty, found {ty:?}")
134}
135
136{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("get_vtable",
"rustc_codegen_ssa::meth", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6c709eb428d472a3c3d93681c1ba37d44a1c27cf/compiler/rustc_codegen_ssa/src/meth.rs"),
::tracing_core::__macro_support::Option::Some(144u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::meth"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("self_ty")
}> =
::tracing::__macro_support::FieldName::new("self_ty");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("trait_ref")
}> =
::tracing::__macro_support::FieldName::new("trait_ref");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&trait_ref)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Cx::Value = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = cx.tcx();
if let Some(&val) =
cx.vtables().borrow().get(&(self_ty, trait_ref)) {
return val;
}
let vtable_alloc_id = tcx.vtable_allocation((self_ty, trait_ref));
let vtable_allocation =
tcx.global_alloc(vtable_alloc_id).unwrap_memory();
let vtable = cx.static_addr_of(vtable_allocation, Some("vtable"));
if let Some(trait_ref) = trait_ref &&
cx.sess().opts.unstable_opts.virtual_function_elimination &&
cx.sess().lto() == Lto::Fat {
let key = trait_ref.with_self_ty(tcx, self_ty);
let trait_hierarchy = vtable::vtable_prefix_traits(tcx, key);
cx.apply_vcall_visibility_metadata(&trait_hierarchy, vtable);
}
cx.create_vtable_debuginfo(self_ty, trait_ref, vtable);
cx.vtables().borrow_mut().insert((self_ty, trait_ref), vtable);
vtable
}
}
}#[instrument(level = "debug", skip(cx))]
145pub(crate) fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
146 cx: &Cx,
147 self_ty: Ty<'tcx>,
148 trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
149) -> Cx::Value {
150 let tcx = cx.tcx();
151
152 if let Some(&val) = cx.vtables().borrow().get(&(self_ty, trait_ref)) {
154 return val;
155 }
156
157 let vtable_alloc_id = tcx.vtable_allocation((self_ty, trait_ref));
158 let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
159 let vtable = cx.static_addr_of(vtable_allocation, Some("vtable"));
160
161 if let Some(trait_ref) = trait_ref
162 && cx.sess().opts.unstable_opts.virtual_function_elimination
163 && cx.sess().lto() == Lto::Fat
164 {
165 let key = trait_ref.with_self_ty(tcx, self_ty);
166 let trait_hierarchy = vtable::vtable_prefix_traits(tcx, key);
167
168 cx.apply_vcall_visibility_metadata(&trait_hierarchy, vtable);
169 }
170
171 cx.create_vtable_debuginfo(self_ty, trait_ref, vtable);
172 cx.vtables().borrow_mut().insert((self_ty, trait_ref), vtable);
173
174 vtable
175}