Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: added caching schema for debuginfo type names #88898

Closed
wants to merge 11 commits into from
29 changes: 19 additions & 10 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ macro_rules! return_if_metadata_created_in_meantime {
};
}

fn check_type_name_cache(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, qualified: bool) -> String {
compute_debuginfo_type_name(
cx.tcx,
ty,
qualified,
&mut debug_context(cx).type_name_cache.borrow_mut(),
)
}

fn fixed_vec_metadata(
cx: &CodegenCx<'ll, 'tcx>,
unique_type_id: UniqueTypeId,
Expand Down Expand Up @@ -422,7 +431,7 @@ fn vec_slice_metadata(

return_if_metadata_created_in_meantime!(cx, unique_type_id);

let slice_type_name = compute_debuginfo_type_name(cx.tcx, slice_ptr_type, true);
let slice_type_name = check_type_name_cache(cx, slice_ptr_type, true);

let (pointer_size, pointer_align) = cx.size_and_align_of(data_ptr_type);
let (usize_size, usize_align) = cx.size_and_align_of(cx.tcx.types.usize);
Expand Down Expand Up @@ -520,10 +529,10 @@ fn trait_pointer_metadata(
Some(trait_object_type) => match trait_object_type.kind() {
ty::Adt(def, _) => (
Some(get_namespace_for_item(cx, def.did)),
compute_debuginfo_type_name(cx.tcx, trait_object_type, false),
check_type_name_cache(cx, trait_object_type, false),
),
ty::RawPtr(_) | ty::Ref(..) => {
(NO_SCOPE_METADATA, compute_debuginfo_type_name(cx.tcx, trait_object_type, true))
(NO_SCOPE_METADATA, check_type_name_cache(cx, trait_object_type, true))
}
_ => {
bug!(
Expand All @@ -536,7 +545,7 @@ fn trait_pointer_metadata(

// No object type, use the trait type directly (no scope here since the type
// will be wrapped in the dyn$ synthetic type).
None => (NO_SCOPE_METADATA, compute_debuginfo_type_name(cx.tcx, trait_type, true)),
None => (NO_SCOPE_METADATA, check_type_name_cache(cx, trait_type, true)),
};

let file_metadata = unknown_file_metadata(cx);
Expand Down Expand Up @@ -987,7 +996,7 @@ fn foreign_type_metadata(
) -> &'ll DIType {
debug!("foreign_type_metadata: {:?}", t);

let name = compute_debuginfo_type_name(cx.tcx, t, false);
let name = check_type_name_cache(cx, t, false);
create_struct_stub(cx, t, &name, unique_type_id, NO_SCOPE_METADATA, DIFlags::FlagZero)
}

Expand All @@ -997,7 +1006,7 @@ fn pointer_type_metadata(
pointee_type_metadata: &'ll DIType,
) -> &'ll DIType {
let (pointer_size, pointer_align) = cx.size_and_align_of(pointer_type);
let name = compute_debuginfo_type_name(cx.tcx, pointer_type, false);
let name = check_type_name_cache(cx, pointer_type, false);
unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
Expand Down Expand Up @@ -1300,7 +1309,7 @@ fn prepare_struct_metadata(
unique_type_id: UniqueTypeId,
span: Span,
) -> RecursiveTypeDescription<'ll, 'tcx> {
let struct_name = compute_debuginfo_type_name(cx.tcx, struct_type, false);
let struct_name = check_type_name_cache(cx, struct_type, false);

let (struct_def_id, variant) = match struct_type.kind() {
ty::Adt(def, _) => (def.did, def.non_enum_variant()),
Expand Down Expand Up @@ -1406,7 +1415,7 @@ fn prepare_tuple_metadata(
span: Span,
containing_scope: Option<&'ll DIScope>,
) -> RecursiveTypeDescription<'ll, 'tcx> {
let tuple_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
let tuple_name = check_type_name_cache(cx, tuple_type, false);

let struct_stub = create_struct_stub(
cx,
Expand Down Expand Up @@ -1470,7 +1479,7 @@ fn prepare_union_metadata(
unique_type_id: UniqueTypeId,
span: Span,
) -> RecursiveTypeDescription<'ll, 'tcx> {
let union_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
let union_name = check_type_name_cache(cx, union_type, false);

let (union_def_id, variant) = match union_type.kind() {
ty::Adt(def, _) => (def.did, def.non_enum_variant()),
Expand Down Expand Up @@ -2025,7 +2034,7 @@ fn prepare_enum_metadata(
outer_field_tys: Vec<Ty<'tcx>>,
) -> RecursiveTypeDescription<'ll, 'tcx> {
let tcx = cx.tcx;
let enum_name = compute_debuginfo_type_name(tcx, enum_type, false);
let enum_name = check_type_name_cache(cx, enum_type, false);

let containing_scope = get_namespace_for_item(cx, enum_def_id);
// FIXME: This should emit actual file metadata for the enum, but we
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use self::utils::{create_DIArray, is_node_local_to_unit, DIB};
use crate::abi::FnAbi;
use crate::builder::Builder;
use crate::common::CodegenCx;
use crate::debuginfo::utils::debug_context;
use crate::llvm;
use crate::llvm::debuginfo::{
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
Expand Down Expand Up @@ -38,6 +39,7 @@ use libc::c_uint;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::iter;
use std::rc::Rc;
use tracing::debug;

mod create_scope_map;
Expand All @@ -62,6 +64,7 @@ pub struct CrateDebugContext<'a, 'tcx> {
builder: &'a mut DIBuilder<'a>,
created_files: RefCell<FxHashMap<(Option<String>, Option<String>), &'a DIFile>>,
created_enum_disr_types: RefCell<FxHashMap<(DefId, Primitive), &'a DIType>>,
type_name_cache: RefCell<FxHashMap<(Ty<'tcx>, bool), Rc<str>>>,

type_map: RefCell<TypeMap<'a, 'tcx>>,
namespace_map: RefCell<DefIdMap<&'a DIScope>>,
Expand Down Expand Up @@ -91,6 +94,7 @@ impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> {
builder,
created_files: Default::default(),
created_enum_disr_types: Default::default(),
type_name_cache: Default::default(),
type_map: Default::default(),
namespace_map: RefCell::new(Default::default()),
composite_types_completed: Default::default(),
Expand Down Expand Up @@ -439,6 +443,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
cx.tcx,
cx.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substs),
name_to_append_suffix_to,
&mut debug_context(cx).type_name_cache.borrow_mut(),
);

if substs.types().next().is_none() {
Expand Down
Loading