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

Require type_map::stub callers to supply file information #104342

Merged
merged 25 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2f00b6a
Require `type_map::stub` callers to supply file information
mweber15 Nov 10, 2022
5b23c38
Add codegen test to validate IR for debuginfo
mweber15 Nov 16, 2022
94669d9
Add file and line metadata for closures
mweber15 Nov 16, 2022
f3da828
Refactor `type_map::stub` parameters
mweber15 Nov 17, 2022
c07797a
Add file and line metadata for coroutines
mweber15 Nov 17, 2022
af6b0de
Add file and line metadata for struct/union members
mweber15 Nov 18, 2022
aa485fc
Add file and line metadata for enum variant and fields
mweber15 Nov 19, 2022
27b1b01
Refactor `type_stub` from `DefId` to tuple
mweber15 Nov 23, 2022
cc4f214
Split metadata testing into multiple files
mweber15 Jan 21, 2023
937a00b
Separate NONMSVC and MSVC checks
mweber15 Jan 21, 2023
f92fc88
Allow check lines to exceed normal length limit
mweber15 Jan 21, 2023
aa1a16a
Add test for async function and async block
mweber15 Jan 21, 2023
fc59f2c
Refactor and expand enum test
mweber15 Jan 22, 2023
b6659b0
Move tests into issues directory
mweber15 Jul 30, 2023
a4833a8
Move additional source location info behind -Z option
mweber15 Jul 30, 2023
4692d46
Add additional option checks
mweber15 Jul 30, 2023
21c58b1
Rename option and add doc
mweber15 Mar 2, 2024
8200068
Rename generator test file
mweber15 Mar 2, 2024
7329111
Update compile flags formatting
mweber15 Mar 2, 2024
613ddc1
Restructure `compile-flags` for tests
mweber15 Mar 2, 2024
64dd582
Use -DAG to handle use of file before definition
wesleywiser Mar 14, 2024
f9ac7ac
Add location info for f16
mweber15 Nov 7, 2024
8286299
Clean up use requirements after rebasing
mweber15 Nov 7, 2024
d151593
Fix relative lines in coroutine test
mweber15 Nov 7, 2024
e9fbb6f
Fix tests when using MinGW
mweber15 Dec 3, 2024
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
96 changes: 86 additions & 10 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
Stub::Struct,
unique_type_id,
&ptr_type_debuginfo_name,
None,
cx.size_and_align_of(ptr_type),
NO_SCOPE_METADATA,
DIFlags::FlagZero,
Expand Down Expand Up @@ -260,6 +261,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
layout.fields.offset(abi::WIDE_PTR_ADDR),
DIFlags::FlagZero,
data_ptr_type_di_node,
None,
),
build_field_di_node(
cx,
Expand All @@ -269,6 +271,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
layout.fields.offset(abi::WIDE_PTR_EXTRA),
DIFlags::FlagZero,
type_di_node(cx, extra_field.ty),
None,
),
]
},
Expand Down Expand Up @@ -371,6 +374,7 @@ fn build_dyn_type_di_node<'ll, 'tcx>(
Stub::Struct,
unique_type_id,
&type_name,
None,
cx.size_and_align_of(dyn_type),
NO_SCOPE_METADATA,
DIFlags::FlagZero,
Expand Down Expand Up @@ -724,19 +728,36 @@ fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreation
// `f16`'s value to be displayed using a Natvis visualiser in `intrinsic.natvis`.
let float_ty = cx.tcx.types.f16;
let bits_ty = cx.tcx.types.u16;
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
match float_ty.kind() {
ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
_ => None,
}
} else {
None
};
type_map::build_type_with_children(
cx,
type_map::stub(
cx,
Stub::Struct,
UniqueTypeId::for_ty(cx.tcx, float_ty),
"f16",
def_location,
cx.size_and_align_of(float_ty),
NO_SCOPE_METADATA,
DIFlags::FlagZero,
),
// Fields:
|cx, float_di_node| {
let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
match bits_ty.kind() {
ty::Adt(def, _) => Some(def.did()),
_ => None,
}
} else {
None
};
smallvec![build_field_di_node(
cx,
float_di_node,
Expand All @@ -745,6 +766,7 @@ fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreation
Size::ZERO,
DIFlags::FlagZero,
type_di_node(cx, bits_ty),
def_id,
)]
},
NO_GENERICS,
Expand Down Expand Up @@ -841,6 +863,7 @@ fn build_foreign_type_di_node<'ll, 'tcx>(
Stub::Struct,
unique_type_id,
&compute_debuginfo_type_name(cx.tcx, t, false),
None,
cx.size_and_align_of(t),
Some(get_namespace_for_item(cx, def_id)),
DIFlags::FlagZero,
Expand Down Expand Up @@ -991,15 +1014,22 @@ fn build_field_di_node<'ll, 'tcx>(
offset: Size,
flags: DIFlags,
type_di_node: &'ll DIType,
def_id: Option<DefId>,
) -> &'ll DIType {
let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
{
file_metadata_from_def_id(cx, def_id)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
unsafe {
llvm::LLVMRustDIBuilderCreateMemberType(
DIB(cx),
owner,
name.as_c_char_ptr(),
name.len(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
file_metadata,
line_number,
size_and_align.0.bits(),
size_and_align.1.bits() as u32,
offset.bits(),
Expand Down Expand Up @@ -1043,6 +1073,11 @@ fn build_struct_type_di_node<'ll, 'tcx>(
let containing_scope = get_namespace_for_item(cx, adt_def.did());
let struct_type_and_layout = cx.layout_of(struct_type);
let variant_def = adt_def.non_enum_variant();
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
} else {
None
};

type_map::build_type_with_children(
cx,
Expand All @@ -1051,6 +1086,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
Stub::Struct,
unique_type_id,
&compute_debuginfo_type_name(cx.tcx, struct_type, false),
def_location,
size_and_align_of(struct_type_and_layout),
Some(containing_scope),
visibility_di_flags(cx, adt_def.did(), adt_def.did()),
Expand All @@ -1070,6 +1106,11 @@ fn build_struct_type_di_node<'ll, 'tcx>(
Cow::Borrowed(f.name.as_str())
};
let field_layout = struct_type_and_layout.field(cx, i);
let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
Some(f.did)
} else {
None
};
build_field_di_node(
cx,
owner,
Expand All @@ -1078,6 +1119,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
struct_type_and_layout.fields.offset(i),
visibility_di_flags(cx, f.did, adt_def.did()),
type_di_node(cx, field_layout.ty),
def_id,
)
})
.collect()
Expand Down Expand Up @@ -1129,6 +1171,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
layout.fields.offset(index),
DIFlags::FlagZero,
type_di_node(cx, up_var_ty),
None,
)
})
.collect()
Expand All @@ -1154,6 +1197,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
Stub::Struct,
unique_type_id,
&type_name,
None,
size_and_align_of(tuple_type_and_layout),
NO_SCOPE_METADATA,
DIFlags::FlagZero,
Expand All @@ -1172,6 +1216,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
tuple_type_and_layout.fields.offset(index),
DIFlags::FlagZero,
type_di_node(cx, component_type),
None,
)
})
.collect()
Expand All @@ -1193,13 +1238,20 @@ fn build_closure_env_di_node<'ll, 'tcx>(
let containing_scope = get_namespace_for_item(cx, def_id);
let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);

let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
Some(file_metadata_from_def_id(cx, Some(def_id)))
} else {
None
};

type_map::build_type_with_children(
cx,
type_map::stub(
cx,
Stub::Struct,
unique_type_id,
&type_name,
def_location,
cx.size_and_align_of(closure_env_type),
Some(containing_scope),
DIFlags::FlagZero,
Expand All @@ -1223,6 +1275,11 @@ fn build_union_type_di_node<'ll, 'tcx>(
let containing_scope = get_namespace_for_item(cx, union_def_id);
let union_ty_and_layout = cx.layout_of(union_type);
let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
Some(file_metadata_from_def_id(cx, Some(union_def_id)))
} else {
None
};

type_map::build_type_with_children(
cx,
Expand All @@ -1231,6 +1288,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
Stub::Union,
unique_type_id,
&type_name,
def_location,
size_and_align_of(union_ty_and_layout),
Some(containing_scope),
DIFlags::FlagZero,
Expand All @@ -1243,6 +1301,11 @@ fn build_union_type_di_node<'ll, 'tcx>(
.enumerate()
.map(|(i, f)| {
let field_layout = union_ty_and_layout.field(cx, i);
let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
Some(f.did)
} else {
None
};
build_field_di_node(
cx,
owner,
Expand All @@ -1251,6 +1314,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
Size::ZERO,
DIFlags::FlagZero,
type_di_node(cx, field_layout.ty),
def_id,
)
})
.collect()
Expand Down Expand Up @@ -1326,14 +1390,7 @@ pub(crate) fn build_global_var_di_node<'ll>(
// We may want to remove the namespace scope if we're in an extern block (see
// https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
let var_scope = get_namespace_for_item(cx, def_id);
let span = hygiene::walk_chain_collapsed(tcx.def_span(def_id), DUMMY_SP);

let (file_metadata, line_number) = if !span.is_dummy() {
let loc = cx.lookup_debug_loc(span.lo());
(file_metadata(cx, &loc.file), loc.line)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));

let is_local_to_unit = is_node_local_to_unit(cx, def_id);

Expand Down Expand Up @@ -1423,6 +1480,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
Stub::VTableTy { vtable_holder },
unique_type_id,
&vtable_type_name,
None,
(size, pointer_align),
NO_SCOPE_METADATA,
DIFlags::FlagArtificial,
Expand Down Expand Up @@ -1460,6 +1518,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
field_offset,
DIFlags::FlagZero,
field_type_di_node,
None,
))
})
.collect()
Expand Down Expand Up @@ -1611,3 +1670,20 @@ fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
.map(|s| Cow::from(*s))
.unwrap_or_else(|| Cow::from(format!("__{field_index}")))
}

pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);

pub(crate) fn file_metadata_from_def_id<'ll>(
cx: &CodegenCx<'ll, '_>,
def_id: Option<DefId>,
) -> DefinitionLocation<'ll> {
if let Some(def_id) = def_id
&& let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
&& !span.is_dummy()
{
let loc = cx.lookup_debug_loc(span.lo());
(file_metadata(cx, &loc.file), loc.line)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
}
}
Loading
Loading