Skip to content

Commit

Permalink
Rollup merge of rust-lang#136640 - Zalathar:debuginfo-align-bits, r=c…
Browse files Browse the repository at this point in the history
…ompiler-errors

Debuginfo for function ZSTs should have alignment of 8 bits, not 1 bit

In rust-lang#116096, function ZSTs were made to have debuginfo that gives them an alignment of “1”. But because alignment in LLVM debuginfo is denoted in *bits*, not bytes, this resulted in an alignment specification of 1 bit instead of 1 byte.

I don't know whether this has any practical consequences, but I noticed that a test started failing when I accidentally fixed the mistake while working on rust-lang#136632, so I extracted the fix (and the test adjustment) to this PR.
  • Loading branch information
matthiaskrgr authored Feb 7, 2025
2 parents 64e06c0 + 4385a9e commit 2de58b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 4 additions & 7 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,16 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
// This is actually a function pointer, so wrap it in pointer DI.
let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
let (size, align) = match fn_ty.kind() {
ty::FnDef(..) => (0, 1),
ty::FnPtr(..) => (
cx.tcx.data_layout.pointer_size.bits(),
cx.tcx.data_layout.pointer_align.abi.bits() as u32,
),
ty::FnDef(..) => (Size::ZERO, Align::ONE),
ty::FnPtr(..) => (cx.tcx.data_layout.pointer_size, cx.tcx.data_layout.pointer_align.abi),
_ => unreachable!(),
};
let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
fn_di_node,
size,
align,
size.bits(),
align.bits() as u32,
0, // Ignore DWARF address space.
name.as_c_char_ptr(),
name.len(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
true,
DIFlags::FlagZero,
argument_index,
align.bytes() as u32,
align.bits() as u32,
)
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/codegen/debug-fndef-size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Verify that `i32::cmp` FnDef type is declared with size 0 and align 1 in LLVM debuginfo.
// Verify that `i32::cmp` FnDef type is declared with a size of 0 and an
// alignment of 8 bits (1 byte) in LLVM debuginfo.

//@ compile-flags: -O -g -Cno-prepopulate-passes
//@ ignore-msvc the types are mangled differently

Expand All @@ -14,5 +16,5 @@ pub fn main() {

// CHECK: %compare.dbg.spill = alloca [0 x i8], align 1
// CHECK: dbg{{.}}declare({{(metadata )?}}ptr %compare.dbg.spill, {{(metadata )?}}![[VAR:.*]], {{(metadata )?}}!DIExpression()
// CHECK: ![[TYPE:.*]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "fn(&i32, &i32) -> core::cmp::Ordering", baseType: !{{.*}}, align: 1, dwarfAddressSpace: {{.*}})
// CHECK: ![[VAR]] = !DILocalVariable(name: "compare", scope: !{{.*}}, file: !{{.*}}, line: {{.*}}, type: ![[TYPE]], align: 1)
// CHECK: ![[TYPE:.*]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "fn(&i32, &i32) -> core::cmp::Ordering", baseType: !{{.*}}, align: 8, dwarfAddressSpace: {{.*}})
// CHECK: ![[VAR]] = !DILocalVariable(name: "compare", scope: !{{.*}}, file: !{{.*}}, line: {{.*}}, type: ![[TYPE]], align: 8)

0 comments on commit 2de58b4

Please # to comment.