Index: llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -580,9 +580,6 @@ /// emit it here if we don't have a skeleton CU for split dwarf. void addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const; - /// Create new DwarfCompileUnit for the given metadata node with tag - /// DW_TAG_compile_unit. - DwarfCompileUnit &getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit); void finishUnitAttributes(const DICompileUnit *DIUnit, DwarfCompileUnit &NewCU); @@ -749,6 +746,10 @@ /// * DW_FORM_data4 for 32-bit DWARFv3 and DWARFv2. dwarf::Form getDwarfSectionOffsetForm() const; + /// Create new DwarfCompileUnit for the given metadata node with tag + /// DW_TAG_compile_unit. + DwarfCompileUnit &getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit); + /// Returns the previous CU that was being updated const DwarfCompileUnit *getPrevCU() const { return PrevCU; } void setPrevCU(const DwarfCompileUnit *PrevCU) { this->PrevCU = PrevCU; } Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1074,6 +1074,17 @@ } DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { + // If this unit is neither the SPs compile unit, nor its skeleton, then + // create the SP in the compile unit it specifies. + if (const DICompileUnit *SPCU = SP->getUnit()) { + DwarfCompileUnit &SPUnit = DD->getOrCreateDwarfCompileUnit(SPCU); + DwarfCompileUnit *Skeleton = SPUnit.getSkeleton(); + const DIE *ThisDie = &getUnitDie(); + if (&SPUnit.getUnitDie() != ThisDie && // SP in this unit? + (!Skeleton || &Skeleton->getUnitDie() != ThisDie)) // This units skel? + return SPUnit.getOrCreateSubprogramDIE(SP, Minimal); + } + // Construct the context before querying for the existence of the DIE in case // such construction creates the DIE (as is the case for member function // declarations). Index: llvm/test/DebugInfo/X86/subprogram-across-cus.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/subprogram-across-cus.ll @@ -0,0 +1,114 @@ +; RUN: llc %s -mtriple=x86_64-unknown-linux-gnu -filetype=obj -o %t +; RUN: llvm-dwarfdump -verify %t +; RUN: llvm-dwarfdump -debug-info %t | FileCheck %s +; RUN: rm %t +; +;$ cat -n 1.cpp +; 1 struct HHH; +; 2 +; 3 struct xxx { +; 4 HHH yyy(); +; 5 } zzz; +; 6 +; +;$ cat -n 2.cpp +; 1 int ggg; +; 2 +; 3 struct HHH { +; 4 template int ccc(bbb) { +; 5 while (--ggg) +; 6 ; +; 7 } +; 8 }; +; 9 +; 10 void eee() { +; 11 HHH fff; +; 12 fff.ccc([] {}); +; 13 } +; 14 +; +; Given this input, LLVM attempts to create a DIE for subprogram "eee" in the +; wrong context. The definition of struct "HHH" is placed in the CU for 1.cpp. +; While creating the template instance in "HHH", function "eee" is referenced +; via the inlined lambda type, and "eee" is created in the CU for 1.cpp, which +; is incorrect. +; +; See PR48790 for more discussion and original compile commands. +; +; Check that there are no verifier failures, and that the SP for "eee" appears +; in the correct CU. +; CHECK-LABEL: DW_TAG_compile_unit +; CHECK: DW_AT_name ("1.cpp") +; CHECK-NOT: DW_AT_name ("eee") +; CHECK-LABEL: DW_TAG_compile_unit +; CHECK: DW_AT_name ("2.cpp") +; CHECK: DW_TAG_subprogram +; CHECK: DW_AT_name ("eee") + +source_filename = "ld-temp.o" +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-scei-ps4" + +%struct.xxx = type { i8 } + +@zzz = hidden local_unnamed_addr global %struct.xxx zeroinitializer, align 1, !dbg !0 +@ggg = hidden local_unnamed_addr global i32 0, align 4, !dbg !14 + +; Function Attrs: norecurse noreturn nounwind readnone sspstrong uwtable willreturn mustprogress +define hidden void @_Z3eeev() local_unnamed_addr #0 !dbg !27 { +entry: + unreachable, !dbg !32 +} + +attributes #0 = { norecurse noreturn nounwind readnone sspstrong uwtable willreturn mustprogress "denormal-fp-math"="preserve-sign,preserve-sign" "denormal-fp-math-f32"="ieee,ieee" "disable-tail-calls"="false" "frame-pointer"="non-leaf" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="btver2" "target-features"="+aes,+avx,+bmi,+cx16,+cx8,+f16c,+fxsr,+lzcnt,+mmx,+movbe,+pclmul,+popcnt,+prfchw,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+sse4a,+ssse3,+x87,+xsave,+xsaveopt" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!2, !16} +!llvm.ident = !{!19, !19} +!llvm.module.flags = !{!20, !21, !22, !23, !24, !25, !26} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "zzz", scope: !2, file: !3, line: 5, type: !6, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "1.cpp", directory: "nou") +!4 = !{} +!5 = !{!0} +!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "xxx", file: !3, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !7, identifier: "_ZTS3xxx") +!7 = !{!8} +!8 = !DISubprogram(name: "yyy", linkageName: "_ZN3xxx3yyyEv", scope: !6, file: !3, line: 4, type: !9, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) +!9 = !DISubroutineType(types: !10) +!10 = !{!11, !13} +!11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "HHH", file: !12, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !4, identifier: "_ZTS3HHH") +!12 = !DIFile(filename: "2.cpp", directory: "nou") +!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!14 = !DIGlobalVariableExpression(var: !15, expr: !DIExpression()) +!15 = distinct !DIGlobalVariable(name: "ggg", scope: !16, file: !12, line: 1, type: !18, isLocal: false, isDefinition: true) +!16 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !12, producer: "clang version 12.0.0 (git@github.com:llvm/llvm-project 93592b726c7587aa86548cc74268346e25a4a7f2)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !17, splitDebugInlining: false, nameTableKind: None) +!17 = !{!14} +!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!19 = !{!"clang version 12.0.0 (git@github.com:llvm/llvm-project 93592b726c7587aa86548cc74268346e25a4a7f2)"} +!20 = !{i32 7, !"Dwarf Version", i32 4} +!21 = !{i32 2, !"Debug Info Version", i32 3} +!22 = !{i32 1, !"wchar_size", i32 2} +!23 = !{i32 7, !"PIC Level", i32 2} +!24 = !{i32 1, !"ThinLTO", i32 0} +!25 = !{i32 1, !"EnableSplitLTOUnit", i32 1} +!26 = !{i32 1, !"LTOPostLink", i32 1} +!27 = distinct !DISubprogram(name: "eee", linkageName: "_Z3eeev", scope: !12, file: !12, line: 10, type: !28, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !16, retainedNodes: !30) +!28 = !DISubroutineType(types: !29) +!29 = !{null} +!30 = !{!31} +!31 = !DILocalVariable(name: "fff", scope: !27, file: !12, line: 11, type: !11) +!32 = !DILocation(line: 5, scope: !33, inlinedAt: !45) +!33 = distinct !DISubprogram(name: "ccc<(lambda at 2.cpp:12:11)>", linkageName: "_ZN3HHH3cccIZ3eeevE3$_0EEiT_", scope: !11, file: !12, line: 4, type: !34, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !16, templateParams: !39, declaration: !38, retainedNodes: !41) +!34 = !DISubroutineType(types: !35) +!35 = !{!18, !36, !37} +!36 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!37 = distinct !DICompositeType(tag: DW_TAG_class_type, scope: !27, file: !12, line: 12, size: 8, flags: DIFlagTypePassByValue | DIFlagNonTrivial, elements: !4) +!38 = !DISubprogram(name: "ccc<(lambda at 2.cpp:12:11)>", linkageName: "_ZN3HHH3cccIZ3eeevE3$_0EEiT_", scope: !11, file: !12, line: 4, type: !34, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, templateParams: !39) +!39 = !{!40} +!40 = !DITemplateTypeParameter(name: "bbb", type: !37) +!41 = !{!42, !44} +!42 = !DILocalVariable(name: "this", arg: 1, scope: !33, type: !43, flags: DIFlagArtificial | DIFlagObjectPointer) +!43 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64) +!44 = !DILocalVariable(arg: 2, scope: !33, file: !12, line: 4, type: !37) +!45 = distinct !DILocation(line: 12, scope: !27)