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/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,55 @@ +; 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") + +; 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)