Index: llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -951,8 +951,8 @@ addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target), MachineLocation(CallReg)); } else { - DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP); - assert(CalleeDIE && "Could not create DIE for call site entry origin"); + DIE *CalleeDIE = getDIE(CalleeSP); + assert(CalleeDIE && "Could not find DIE for call site entry origin"); addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin), *CalleeDIE); } Index: llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -437,6 +437,9 @@ /// Construct a DIE for this abstract scope. void constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, LexicalScope *Scope); + /// Construct a DIE for the subprogram definition \p SP and return it. + DIE &constructSubprogramDefinitionDIE(const DISubprogram *SP); + /// Construct DIEs for call site entries describing the calls in \p MF. void constructCallSiteEntryDIEs(const DISubprogram &SP, DwarfCompileUnit &CU, DIE &ScopeDIE, const MachineFunction &MF); Index: llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -535,6 +535,12 @@ } } +DIE &DwarfDebug::constructSubprogramDefinitionDIE(const DISubprogram *SP) { + assert(SP->isDefinition()); + auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); + return *CU.getOrCreateSubprogramDIE(SP); +} + /// Try to interpret values loaded into registers that forward parameters /// for \p CallMI. Store parameters with interpreted value into \p Params. static void collectCallSiteParameters(const MachineInstr *CallMI, @@ -748,6 +754,15 @@ if (!CalleeDecl || !CalleeDecl->getSubprogram()) continue; CalleeSP = CalleeDecl->getSubprogram(); + + if (CalleeSP->isDefinition()) { + // Ensure that a subprogram definition for the callee and a containing + // CU are available. + constructSubprogramDefinitionDIE(CalleeSP); + } else { + assert(CU.getDIE(CalleeSP) && + "Expected declaration subprogram DIE for callee"); + } } // TODO: Omit call site entries for runtime calls (objc_msgSend, etc). Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -188,8 +188,9 @@ /// Check whether the DIE for this MDNode can be shared across CUs. bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const { - // When the MDNode can be part of the type system, the DIE can be shared - // across CUs. + // When the MDNode can be part of the type system (this includes subprogram + // declarations *and* subprogram definitions, even local definitions), the + // DIE must be shared across CUs. // Combining type units and cross-CU DIE sharing is lower value (since // cross-CU DIE sharing is used in LTO and removes type redundancy at that // level already) but may be implementable for some value in projects @@ -197,9 +198,7 @@ // together. if (isDwoUnit() && !DD->shareAcrossDWOCUs()) return false; - return (isa(D) || - (isa(D) && !cast(D)->isDefinition())) && - !DD->generateTypeUnits(); + return (isa(D) || isa(D)) && !DD->generateTypeUnits(); } DIE *DwarfUnit::getDIE(const DINode *D) const { Index: llvm/test/DebugInfo/X86/lto-cross-cu-call-origin-ref.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/lto-cross-cu-call-origin-ref.ll @@ -0,0 +1,200 @@ +; RUN: llc -mtriple=x86_64-apple-darwin -filetype=obj < %s | llvm-dwarfdump - \ +; RUN: | FileCheck %s -implicit-check-not=DW_TAG_subprogram + +; Source: +; // a.c +; extern void helper_from_b(void); +; __attribute__((optnone)) void bar() {} +; __attribute__((optnone)) static void baz() {} +; __attribute__((always_inline)) void foo() { +; bar(); +; baz(); +; } +; __attribute__((noinline)) void noinline_in_a() { +; helper_from_b(); +; } +; // b.c +; extern void foo(void); +; extern void noinline_in_a(void); +; __attribute__((optnone)) void baz() {} +; __attribute__((always_inline)) void helper_from_b(void) { baz(); } +; int main(int argc, char **argv) { +; foo(); +; baz(); +; noinline_in_a(); +; return 0; +; } + +; Command: +; clang -O2 -Xclang -femit-debug-entry-values -g -flto -o a.o -c a.c +; clang -O2 -Xclang -femit-debug-entry-values -g -flto -o b.o -c b.c +; clang -O2 -Xclang -femit-debug-entry-values -g -flto -o main a.o b.o -Wl,-object_path_lto,lto.o,-save-temps + +; === CU for a.c === + +; CHECK: DW_TAG_compile_unit +; CHECK: DW_AT_name ("a.c") + +; Match & ignore the declaration of "helper_from_b". +; FIXME: There is probably no need for this declaration in an LTO setting. +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_name ("helper_from_b") +; CHECK: DW_AT_declaration (true) + +; Check for the definitions expected in a.c. Save the DIE references for these +; definitions so that, later, we can check that the cross-CU references in b.c +; are correct. +; CHECK: 0x{{0+}}[[BAR_IN_A_DIE:.*]]: DW_TAG_subprogram +; CHECK: DW_AT_call_all_calls (true) +; CHECK: DW_AT_name ("bar") + +; CHECK: 0x{{0+}}[[BAZ_IN_A_DIE:.*]]: DW_TAG_subprogram +; CHECK: DW_AT_call_all_calls (true) +; CHECK: DW_AT_name ("baz") + +; Check that `helper_from_b` is inlined into `noinline_in_a`, which in turn +; produces a cross-CU call into b.c. +; CHECK: 0x{{0+}}[[NOINLINE_IN_A_DIE:.*]]: DW_TAG_subprogram +; CHECK: DW_AT_call_all_calls (true) +; CHECK: DW_AT_name ("noinline_in_a") +; CHECK: DW_TAG_inlined_subroutine +; CHECK-NEXT: DW_AT_abstract_origin (0x{{0+}}[[HELPER_FROM_B_DIE:.*]] "helper_from_b") +; CHECK: DW_TAG_call_site +; CHECK-NEXT: DW_AT_call_origin (0x{{0+}}[[BAZ_IN_B_DIE:.*]]) + +; CHECK: 0x{{0+}}[[FOO_IN_A_DIE:.*]]: DW_TAG_subprogram +; CHECK: DW_AT_name ("foo") +; CHECK: DW_AT_inline (DW_INL_inlined) + +; === CU for b.c === + +; CHECK: DW_TAG_compile_unit +; CHECK: DW_AT_name ("b.c") + +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_name ("foo") +; CHECK: DW_AT_declaration (true) + +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_name ("noinline_in_a") +; CHECK: DW_AT_declaration (true) + +; CHECK: 0x{{0+}}[[HELPER_FROM_B_DIE]]: DW_TAG_subprogram +; CHECK: DW_AT_name ("helper_from_b") +; CHECK: DW_AT_inline (DW_INL_inlined) + +; Check for an external definition subprogram for "baz", distinct from the +; "baz" local to a.c. +; CHECK: 0x{{0+}}[[BAZ_IN_B_DIE]]: DW_TAG_subprogram +; CHECK: DW_AT_call_all_calls (true) +; CHECK: DW_AT_name ("baz") +; CHECK: DW_AT_external (true) + +; Check that "main" references, in order: +; +; 1) The definition of "bar" in "a.c". (Previously we would not share +; definition subprograms across CUs, so the call site info logic would create an +; empty 'definition' subprogram for "bar" which never got filled in. This breaks +; both entry value evaluation & artificial tail call frame synthesis in the LTO +; setting.) +; +; 2) The non-external definition of "baz" in "a.c". This is inlined via "foo". +; +; 3) The external definition of "baz" in "b.c". +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_name ("main") +; CHECK: DW_TAG_inlined_subroutine +; CHECK-NEXT: DW_AT_abstract_origin (0x{{0+}}[[FOO_IN_A_DIE]] "foo") +; CHECK: DW_TAG_call_site +; CHECK-NEXT: DW_AT_call_origin (0x{{0+}}[[BAR_IN_A_DIE]]) +; CHECK: DW_TAG_call_site +; CHECK-NEXT: DW_AT_call_origin (0x{{0+}}[[BAZ_IN_A_DIE]]) +; CHECK: DW_TAG_call_site +; CHECK-NEXT: DW_AT_call_origin (0x{{0+}}[[BAZ_IN_B_DIE]]) + +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.14.0" + +define internal fastcc void @bar() unnamed_addr #0 !dbg !18 { +entry: + ret void, !dbg !19 +} + +define internal fastcc void @baz.2() unnamed_addr #0 !dbg !20 { +entry: + ret void, !dbg !21 +} + +define internal fastcc void @noinline_in_a() unnamed_addr #0 !dbg !22 { +entry: + tail call fastcc void @baz(), !dbg !23 + ret void, !dbg !26 +} + +define internal fastcc void @baz() unnamed_addr #0 !dbg !27 { +entry: + ret void, !dbg !28 +} + +define i32 @main(i32 %argc, i8** nocapture readnone %argv) local_unnamed_addr !dbg !29 { +entry: + tail call fastcc void @bar(), !dbg !40 + tail call fastcc void @baz.2(), !dbg !43 + tail call fastcc void @baz(), !dbg !44 + tail call fastcc void @noinline_in_a(), !dbg !45 + ret i32 0, !dbg !46 +} + +attributes #0 = { noinline } + +!llvm.dbg.cu = !{!0, !7} +!llvm.ident = !{!12, !12} +!llvm.module.flags = !{!13, !14, !15, !16, !17} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0 (git@github.com:llvm/llvm-project.git 38530f9070cb06bb3d5bf6471befb70c7930da30)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, nameTableKind: None) +!1 = !DIFile(filename: "a.c", directory: "/Users/vsk/tmp/lto-entry-vals") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "helper_from_b", scope: !1, file: !1, line: 1, type: !5, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{null} +!7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !8, producer: "clang version 10.0.0 (git@github.com:llvm/llvm-project.git 38530f9070cb06bb3d5bf6471befb70c7930da30)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !9, nameTableKind: None) +!8 = !DIFile(filename: "b.c", directory: "/Users/vsk/tmp/lto-entry-vals") +!9 = !{!10, !11} +!10 = !DISubprogram(name: "foo", scope: !8, file: !8, line: 1, type: !5, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!11 = !DISubprogram(name: "noinline_in_a", scope: !8, file: !8, line: 2, type: !5, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!12 = !{!"clang version 10.0.0 (git@github.com:llvm/llvm-project.git 38530f9070cb06bb3d5bf6471befb70c7930da30)"} +!13 = !{i32 7, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{i32 1, !"wchar_size", i32 4} +!16 = !{i32 7, !"PIC Level", i32 2} +!17 = !{i32 1, !"LTOPostLink", i32 1} +!18 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 2, type: !5, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!19 = !DILocation(line: 2, column: 38, scope: !18) +!20 = distinct !DISubprogram(name: "baz", scope: !1, file: !1, line: 3, type: !5, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!21 = !DILocation(line: 3, column: 45, scope: !20) +!22 = distinct !DISubprogram(name: "noinline_in_a", scope: !1, file: !1, line: 8, type: !5, scopeLine: 8, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!23 = !DILocation(line: 4, column: 59, scope: !24, inlinedAt: !25) +!24 = distinct !DISubprogram(name: "helper_from_b", scope: !8, file: !8, line: 4, type: !5, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !7, retainedNodes: !2) +!25 = distinct !DILocation(line: 9, column: 3, scope: !22) +!26 = !DILocation(line: 10, column: 1, scope: !22) +!27 = distinct !DISubprogram(name: "baz", scope: !8, file: !8, line: 3, type: !5, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !7, retainedNodes: !2) +!28 = !DILocation(line: 3, column: 38, scope: !27) +!29 = distinct !DISubprogram(name: "main", scope: !8, file: !8, line: 5, type: !30, scopeLine: 5, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !7, retainedNodes: !36) +!30 = !DISubroutineType(types: !31) +!31 = !{!32, !32, !33} +!32 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!33 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !34, size: 64) +!34 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !35, size: 64) +!35 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!36 = !{!37, !38} +!37 = !DILocalVariable(name: "argc", arg: 1, scope: !29, file: !8, line: 5, type: !32) +!38 = !DILocalVariable(name: "argv", arg: 2, scope: !29, file: !8, line: 5, type: !33) +!39 = !DILocation(line: 0, scope: !29) +!40 = !DILocation(line: 5, column: 3, scope: !41, inlinedAt: !42) +!41 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 4, type: !5, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!42 = distinct !DILocation(line: 6, column: 3, scope: !29) +!43 = !DILocation(line: 6, column: 3, scope: !41, inlinedAt: !42) +!44 = !DILocation(line: 7, column: 3, scope: !29) +!45 = !DILocation(line: 8, column: 3, scope: !29) +!46 = !DILocation(line: 9, column: 3, scope: !29)