diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -982,7 +982,8 @@ DIE *ContextDIE; DwarfCompileUnit *ContextCU = this; - if (includeMinimalInlineScopes()) + // -fdebug-info-for-profiling requires a subprogram DIE. + if (includeMinimalInlineScopes() && !getCUNode()->getDebugInfoForProfiling()) ContextDIE = &getUnitDie(); // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with // the important distinction that the debug node is not associated with the @@ -996,7 +997,10 @@ // The scope may be shared with a subprogram that has already been // constructed in another CU, in which case we need to construct this // subprogram in the same CU. - ContextCU = DD->lookupCU(ContextDIE->getUnitDie()); + DwarfCompileUnit *OtherContextCU = DD->lookupCU(ContextDIE->getUnitDie()); + // If the context was not found, revert to the previous value. + if (OtherContextCU) + ContextCU = OtherContextCU; } // Passing null as the associated node because the abstract definition diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -600,8 +600,10 @@ if (DIE *TyDIE = getDIE(Ty)) return TyDIE; - return static_cast(ContextDIE->getUnit()) - ->createTypeDIE(Context, *ContextDIE, Ty); + auto *Unit = static_cast(ContextDIE->getUnit()); + if (!Unit) + Unit = this; + return Unit->createTypeDIE(Context, *ContextDIE, Ty); } void DwarfUnit::updateAcceleratorTables(const DIScope *Context, @@ -1098,8 +1100,10 @@ if (SP->isDefinition()) return &SPDie; - static_cast(SPDie.getUnit()) - ->applySubprogramAttributes(SP, SPDie); + auto *Unit = static_cast(SPDie.getUnit()); + if (!Unit) + Unit = this; + Unit->applySubprogramAttributes(SP, SPDie); return &SPDie; } diff --git a/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash-dwarf5.ll b/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash-dwarf5.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash-dwarf5.ll @@ -0,0 +1,77 @@ +; RUN: llc %s -split-dwarf-file=%t.dwo -filetype=obj -o - | llvm-dwarfdump -debug-info - | FileCheck %s + +;; IR simplified from: +; struct b { +; template +; void d(c e) { d(e); } +; }; +; void f() { b a; a.d(0); } +; +;; Which raises assertion failures when running: clang -fsplit-dwarf-inlining -gdwarf-5 -gsplit-dwarf -fdebug-info-for-profiling -O2 + +; CHECK: .debug_info contents: +; CHECK: DW_TAG_skeleton_unit +; CHECK: DW_TAG_structure_type +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_linkage_name ("_ZN1b1dIiEEvT_") +; CHECK: DW_AT_name ("d") +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_specification ({{.*}} "_ZN1b1dIiEEvT_") +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_linkage_name ("_Z1fv") +; CHECK: DW_AT_name ("f") +; CHECK: DW_TAG_inlined_subroutine +; CHECK: DW_AT_abstract_origin ({{.*}} "_ZN1b1dIiEEvT_") + +;; Note: .debug_info.dwo has the same basic structure as .debug_info. Make sure +;; the above CHECK lines match .debug_info, not .debug_info.dwo. + +; CHECK: .debug_info.dwo contents: + +; ModuleID = '/tmp/repro.cc' +source_filename = "/tmp/repro.cc" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define dso_local void @_Z1fv() local_unnamed_addr !dbg !7 { +entry: + br label %tailrecurse.i, !dbg !15 + +tailrecurse.i: ; preds = %tailrecurse.i, %entry + br label %tailrecurse.i, !dbg !15 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git 4c8c6368710ea58d940d7274dcc700eeb1c0a969)", isOptimized: true, runtimeVersion: 0, splitDebugFilename: "/tmp/repro.dwo", emissionKind: FullDebug, enums: !2, debugInfoForProfiling: true, nameTableKind: GNU) +!1 = !DIFile(filename: "/tmp/repro.cc", directory: "/tmp") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 5} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git 4c8c6368710ea58d940d7274dcc700eeb1c0a969)"} +!7 = distinct !DISubprogram(name: "f", linkageName: "_Z1fv", scope: !8, file: !8, line: 7, type: !9, scopeLine: 7, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) +!8 = !DIFile(filename: "/tmp/repro.cc", directory: "") +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !{!12} +!12 = !DILocalVariable(name: "a", scope: !7, file: !8, line: 8, type: !13) +!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "b", file: !8, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !2, identifier: "_ZTS1b") +!14 = !DILocation(line: 8, column: 5, scope: !7) +!15 = !DILocation(line: 4, column: 5, scope: !16, inlinedAt: !28) +!16 = distinct !DISubprogram(name: "d", linkageName: "_ZN1b1dIiEEvT_", scope: !13, file: !8, line: 3, type: !17, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, templateParams: !22, declaration: !21, retainedNodes: !24) +!17 = !DISubroutineType(types: !18) +!18 = !{null, !19, !20} +!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!20 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!21 = !DISubprogram(name: "d", linkageName: "_ZN1b1dIiEEvT_", scope: !13, file: !8, line: 3, type: !17, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, templateParams: !22) +!22 = !{!23} +!23 = !DITemplateTypeParameter(name: "c", type: !20) +!24 = !{!25, !27} +!25 = !DILocalVariable(name: "this", arg: 1, scope: !16, type: !26, flags: DIFlagArtificial | DIFlagObjectPointer) +!26 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64) +!27 = !DILocalVariable(name: "e", arg: 2, scope: !16, file: !8, line: 3, type: !20) +!28 = distinct !DILocation(line: 9, column: 5, scope: !7) +!29 = !DILocation(line: 0, scope: !16, inlinedAt: !28) diff --git a/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash.ll b/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/X86/debug-info-for-profiling-crash.ll @@ -0,0 +1,58 @@ +; RUN: llc %s -split-dwarf-file=%t.dwo -filetype=obj -o - | llvm-dwarfdump -debug-info - | FileCheck %s + +;; IR simplified from: +; void f1(); +; __attribute__((always_inline)) inline void f2() { f1(); } +; void f3() { f2(); } + +;; Which raises assertion failures when running: clang -fsplit-dwarf-inlining -g -gsplit-dwarf -fdebug-info-for-profiling -O2 + +; CHECK: .debug_info contents: +; CHECK: DW_TAG_compile_unit +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_linkage_name ("_Z2f2v") +; CHECK: DW_AT_name ("f2") +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_linkage_name ("_Z2f3v") +; CHECK: DW_AT_name ("f3") +; CHECK: DW_TAG_inlined_subroutine +; CHECK: DW_AT_abstract_origin ({{.*}} "_Z2f2v") + +;; Note: .debug_info.dwo has the same basic structure as .debug_info. Make sure +;; the above CHECK lines match .debug_info, not .debug_info.dwo. + +; CHECK: .debug_info.dwo contents: + +; ModuleID = '/tmp/repro.cc' +source_filename = "/tmp/repro.cc" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: noinline optnone uwtable mustprogress +define dso_local void @_Z2f3v() !dbg !7 { +entry: + call void @_Z2f1v(), !dbg !11 + ret void, !dbg !14 +} + +declare dso_local void @_Z2f1v() + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git 6834435362ef7830398ece43f527492e31e372b6)", isOptimized: false, runtimeVersion: 0, splitDebugFilename: "/tmp/crash.dwo", emissionKind: FullDebug, enums: !2, debugInfoForProfiling: true, nameTableKind: GNU) +!1 = !DIFile(filename: "/tmp/crash.cc", directory: "/tmp") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git 6834435362ef7830398ece43f527492e31e372b6)"} +!7 = distinct !DISubprogram(name: "f3", linkageName: "_Z2f3v", scope: !8, file: !8, line: 3, type: !9, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DIFile(filename: "/tmp/crash.cc", directory: "/tmp") +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !DILocation(line: 2, column: 51, scope: !12, inlinedAt: !13) +!12 = distinct !DISubprogram(name: "f2", linkageName: "_Z2f2v", scope: !8, file: !8, line: 2, type: !9, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!13 = distinct !DILocation(line: 3, column: 13, scope: !7) +!14 = !DILocation(line: 3, column: 19, scope: !7)