From 098fc9715e0f0cc3aa5c6ff0512e389c24d09254 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 18 Sep 2023 16:29:50 +0000 Subject: [PATCH 1/2] Make FnDef 1-ZST in LLVM debuginfo. --- .../src/debuginfo/metadata.rs | 12 ++++++++++-- tests/codegen/debug-fndef-size.rs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/codegen/debug-fndef-size.rs diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index ed938761694dc..11874898a5adb 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -335,12 +335,20 @@ 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, + ), + _ => unreachable!(), + }; let di_node = unsafe { llvm::LLVMRustDIBuilderCreatePointerType( DIB(cx), fn_di_node, - cx.tcx.data_layout.pointer_size.bits(), - cx.tcx.data_layout.pointer_align.abi.bits() as u32, + size, + align, 0, // Ignore DWARF address space. name.as_ptr().cast(), name.len(), diff --git a/tests/codegen/debug-fndef-size.rs b/tests/codegen/debug-fndef-size.rs new file mode 100644 index 0000000000000..2bfd77bade23e --- /dev/null +++ b/tests/codegen/debug-fndef-size.rs @@ -0,0 +1,17 @@ +// Verify that `i32::cmp` FnDef type is declared with size 0 and align 1 in LLVM debuginfo. +// compile-flags: -O -g -Cno-prepopulate-passes + +use std::cmp::Ordering; + +fn foo Ordering>(v1: i32, v2: i32, compare: F) -> Ordering { + compare(&v1, &v2) +} + +pub fn main() { + foo(0, 1, i32::cmp); +} + +// CHECK: %compare.dbg.spill = alloca {}, align 1 +// CHECK: call void @llvm.dbg.declare(metadata ptr %compare.dbg.spill, metadata ![[VAR:.*]], metadata !DIExpression()), !dbg !{{.*}} +// 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) From 9d211b044d3198f8e2c9a3e5678fafd2a61a5308 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 8 Oct 2023 16:43:57 +0000 Subject: [PATCH 2/2] Ignore MSVC in test. --- tests/codegen/debug-fndef-size.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/codegen/debug-fndef-size.rs b/tests/codegen/debug-fndef-size.rs index 2bfd77bade23e..80eb35fa32a96 100644 --- a/tests/codegen/debug-fndef-size.rs +++ b/tests/codegen/debug-fndef-size.rs @@ -1,5 +1,6 @@ // Verify that `i32::cmp` FnDef type is declared with size 0 and align 1 in LLVM debuginfo. // compile-flags: -O -g -Cno-prepopulate-passes +// ignore-msvc the types are mangled differently use std::cmp::Ordering;