Add typeid_for_trait_ref function

This function computes a Itanium-like typeid for a trait_ref. This is
required for the VFE optimization in LLVM. It is used to map
`llvm.type.checked.load` invocations, that is loading the function from
a vtable, to the vtables this function could be from.

It is important to note that `typeid`s are not unique. So multiple
vtables of the same trait can share `typeid`s.
This commit is contained in:
flip1995 2022-04-21 13:42:46 +01:00 committed by Philipp Krones
parent 20f597ffcd
commit d55787a155
No known key found for this signature in database
GPG Key ID: 1CA0DF2AF59D68A5
2 changed files with 25 additions and 0 deletions

View File

@ -155,6 +155,13 @@ pub fn typeid_for_fnabi<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>)
v0::mangle_typeid_for_fnabi(tcx, fn_abi)
}
pub fn typeid_for_trait_ref<'tcx>(
tcx: TyCtxt<'tcx>,
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
) -> String {
v0::mangle_typeid_for_trait_ref(tcx, trait_ref)
}
/// Computes the symbol name for the given instance. This function will call
/// `compute_instantiating_crate` if it needs to factor the instantiating crate
/// into the symbol name.

View File

@ -95,6 +95,24 @@ pub(super) fn mangle_typeid_for_fnabi<'tcx>(
format!("typeid{}", arg_count)
}
pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
tcx: TyCtxt<'tcx>,
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
) -> String {
// FIXME(flip1995): See comment in `mangle_typeid_for_fnabi`.
let mut cx = &mut SymbolMangler {
tcx,
start_offset: 0,
paths: FxHashMap::default(),
types: FxHashMap::default(),
consts: FxHashMap::default(),
binders: vec![],
out: String::new(),
};
cx = cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
std::mem::take(&mut cx.out)
}
struct BinderLevel {
/// The range of distances from the root of what's
/// being printed, to the lifetimes in a binder.