LTO+Debug Info: correctly emit inlined_subroutine when the inlined callee is
authorManman Ren <mren@apple.com>
Tue, 28 May 2013 19:01:58 +0000 (19:01 +0000)
committerManman Ren <mren@apple.com>
Tue, 28 May 2013 19:01:58 +0000 (19:01 +0000)
from a different CU.

We used to print out an error message and fail to generate inlined_subroutine.

If we use ref_addr in the generated DWARF, the DWARF version should be 3 or
above.
rdar://13926659

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182791 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.h
test/DebugInfo/cross-cu-inline.ll [new file with mode: 0644]

index 90ca034c976ab3709012c218f50411261a3f446d..adee01bc830097a386d3d7441831a1a497b795be 100644 (file)
@@ -1504,9 +1504,14 @@ DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
   DIE *VariableDie = new DIE(Tag);
   DbgVariable *AbsVar = DV->getAbstractVariable();
   DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
-  if (AbsDIE)
+  if (AbsDIE) {
+    bool InSameCU = AbsDIE->getCompileUnit() == getCUDie();
     addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
-                            dwarf::DW_FORM_ref4, AbsDIE);
+                InSameCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
+                AbsDIE);
+    if (!InSameCU)
+      DD->setUseRefAddr(true);
+  }
   else {
     addString(VariableDie, dwarf::DW_AT_name, Name);
     addSourceLine(VariableDie, DV->getVariable());
index 2d42af8f83aaad53924a6e06f3d189ba57ea9ff2..e764e36f058cb3f12f64d1fedba79e7e2bcb6694 100644 (file)
@@ -190,6 +190,7 @@ DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
       IsDarwinGDBCompat = false;
   } else
     IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
+  UseRefAddr = false;
 
   if (DwarfAccelTables == Default) {
     if (IsDarwin)
@@ -369,6 +370,8 @@ DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
     SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
                       InSameCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
                       AbsSPDIE);
+    if (!InSameCU)
+      UseRefAddr = true;
     SPCU->addDie(SPDie);
   } else {
     DISubprogram SPDecl = SP.getFunctionDeclaration();
@@ -469,6 +472,17 @@ DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
   return ScopeDIE;
 }
 
+DIE *DwarfDebug::findSPDieInAllCUs(const MDNode *N) {
+  for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
+         CUE = CUMap.end(); CUI != CUE; ++CUI) {
+    CompileUnit *TheCU = CUI->second;
+    DIE *SPDie = TheCU->getDIE(N);
+    if (SPDie)
+      return SPDie;
+  }
+  return 0;
+}
+
 // This scope represents inlined body of a function. Construct DIE to
 // represent this concrete inlined copy of the function.
 DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
@@ -481,7 +495,7 @@ DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
     return NULL;
   DIScope DS(Scope->getScopeNode());
   DISubprogram InlinedSP = getDISubprogram(DS);
-  DIE *OriginDIE = TheCU->getDIE(InlinedSP);
+  DIE *OriginDIE = findSPDieInAllCUs(InlinedSP);
   if (!OriginDIE) {
     DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
     return NULL;
@@ -500,8 +514,12 @@ DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
          "Invalid end label for an inlined scope!");
 
   DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
+  bool InSameCU = OriginDIE->getCompileUnit() == TheCU->getCUDie();
   TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
-                     dwarf::DW_FORM_ref4, OriginDIE);
+                     InSameCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
+                     OriginDIE);
+  if (!InSameCU)
+    UseRefAddr = true;
 
   if (Ranges.size() > 1) {
     // .debug_range section has not been laid out yet. Emit offset in
@@ -558,9 +576,12 @@ DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
 
   DIScope DS(Scope->getScopeNode());
   // Early return to avoid creating dangling variable|scope DIEs.
-  if (!Scope->getInlinedAt() && DS.isSubprogram() && Scope->isAbstractScope() &&
-      !TheCU->getDIE(DS))
-    return NULL;
+  DIE *AbstractSPDie = 0;
+  if (!Scope->getInlinedAt() && DS.isSubprogram() && Scope->isAbstractScope()) {
+    AbstractSPDie = findSPDieInAllCUs(DS);
+    if (!AbstractSPDie)
+      return NULL;
+  }
 
   SmallVector<DIE *, 8> Children;
   DIE *ObjectPointer = NULL;
@@ -593,7 +614,7 @@ DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
   else if (DS.isSubprogram()) {
     ProcessedSPNodes.insert(DS);
     if (Scope->isAbstractScope()) {
-      ScopeDIE = TheCU->getDIE(DS);
+      ScopeDIE = AbstractSPDie;
       // Note down abstract DIE.
       if (ScopeDIE)
         AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
@@ -1683,7 +1704,9 @@ void DwarfDebug::endFunction(const MachineFunction *MF) {
       }
     }
     if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
-      constructScopeDIE(TheCU, AScope);
+      // Use the compile unit for the abstract scope to create scope DIEs and
+      // variable DIEs.
+      constructScopeDIE(SPMap.lookup(AScope->getScopeNode()), AScope);
   }
 
   DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
@@ -1973,7 +1996,8 @@ void DwarfUnits::emitUnits(DwarfDebug *DD,
     Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
     Asm->EmitInt32(ContentSize);
     Asm->OutStreamer.AddComment("DWARF version number");
-    Asm->EmitInt16(dwarf::DWARF_VERSION);
+    Asm->EmitInt16((DD->getUseRefAddr() && dwarf::DWARF_VERSION < 3) ?
+                   3 : dwarf::DWARF_VERSION);
     Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
     Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
                            ASectionSym);
@@ -2214,7 +2238,8 @@ void DwarfDebug::emitDebugPubnames() {
     Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
 
     Asm->OutStreamer.AddComment("DWARF Version");
-    Asm->EmitInt16(dwarf::DWARF_VERSION);
+    Asm->EmitInt16((UseRefAddr && dwarf::DWARF_VERSION < 3) ?
+                   3 : dwarf::DWARF_VERSION);
 
     Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
     Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
@@ -2261,7 +2286,8 @@ void DwarfDebug::emitDebugPubTypes() {
                                                   TheCU->getUniqueID()));
 
     if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
-    Asm->EmitInt16(dwarf::DWARF_VERSION);
+    Asm->EmitInt16((UseRefAddr && dwarf::DWARF_VERSION < 3) ?
+                   3 : dwarf::DWARF_VERSION);
 
     Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
     const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
@@ -2544,7 +2570,8 @@ void DwarfDebug::emitDebugInlineInfo() {
   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
 
   Asm->OutStreamer.AddComment("Dwarf Version");
-  Asm->EmitInt16(dwarf::DWARF_VERSION);
+  Asm->EmitInt16((UseRefAddr && dwarf::DWARF_VERSION < 3) ?
+                 3 : dwarf::DWARF_VERSION);
   Asm->OutStreamer.AddComment("Address Size (in bytes)");
   Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
 
index 6cc792b373172a68fc9d83fb605be9bb55175a57..65e4fa99a0b23982ccfc34a31726712ebf93e89c 100644 (file)
@@ -402,6 +402,9 @@ class DwarfDebug {
   // Whether or not we're emitting info for older versions of gdb on darwin.
   bool IsDarwinGDBCompat;
 
+  // Whether or not we use ref_addr in the generated DWARF.
+  unsigned UseRefAddr;
+
   // DWARF5 Experimental Options
   bool HasDwarfAccelTables;
   bool HasSplitDwarf;
@@ -601,6 +604,9 @@ private:
   /// \brief Return Label immediately following the instruction.
   MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
 
+  /// \brief Search all compile units to find the SP DIE for the given MDNode.
+  DIE *findSPDieInAllCUs(const MDNode *N);
+
 public:
   //===--------------------------------------------------------------------===//
   // Main entry points.
@@ -640,6 +646,9 @@ public:
   /// output to the limitations of darwin gdb.
   bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
 
+  bool getUseRefAddr() { return UseRefAddr; }
+  void setUseRefAddr(bool RefAddr) { UseRefAddr = RefAddr; }
+
   // Experimental DWARF5 features.
 
   /// \brief Returns whether or not to emit tables that dwarf consumers can
diff --git a/test/DebugInfo/cross-cu-inline.ll b/test/DebugInfo/cross-cu-inline.ll
new file mode 100644 (file)
index 0000000..3a721ba
--- /dev/null
@@ -0,0 +1,117 @@
+; RUN: llc -O0 -filetype=obj < %s > %t
+; RUN: llvm-dwarfdump %t | FileCheck %s
+
+; rdar://13926659
+; When a callee from a different CU is inlined, we should generate
+; inlined_subroutine with abstract_origin pointing to a DIE in a different
+; CU.
+; CHECK: debug_info contents
+; CHECK: DW_TAG_inlined_subroutine
+; CHECK: DW_AT_abstract_origin [DW_FORM_ref_addr] (0x{{[0]*}}[[ORIGIN:.*]])
+; CHECK: DW_TAG_formal_parameter
+; CHECK: DW_AT_abstract_origin [DW_FORM_ref_addr] (0x{{[0]*}}[[PARAM_ORIGIN:.*]])
+; CHECK: 0x{{[0]*}}[[ORIGIN]]: DW_TAG_subprogram
+; CHECK: DW_AT_name {{.*}} ( .debug_str[{{.*}}] = "bar2")
+; CHECK: DW_AT_inline
+; CHECK: 0x{{[0]*}}[[PARAM_ORIGIN]]: DW_TAG_formal_parameter
+; CHECK: DW_TAG_subprogram
+; CHECK: DW_AT_abstract_origin [DW_FORM_ref4] (cu + {{.*}} => {0x{{[0]*}}[[ORIGIN]]})
+
+target triple = "x86_64-apple-macosx"
+%struct.Foo = type { i32, i32 }
+
+; Function Attrs: noinline ssp uwtable
+define i32 @_Z3fooi(i32 %a) #0 {
+  %1 = alloca i32, align 4
+  %2 = alloca i32, align 4
+  %g = alloca %struct.Foo, align 4
+  store i32 %a, i32* %2, align 4
+  call void @llvm.dbg.declare(metadata !{i32* %2}, metadata !17), !dbg !18
+  call void @llvm.dbg.declare(metadata !{%struct.Foo* %g}, metadata !19), !dbg !24
+  %3 = getelementptr inbounds %struct.Foo* %g, i32 0, i32 0, !dbg !24
+  %4 = load i32* %2, align 4, !dbg !24
+  store i32 %4, i32* %3, align 4, !dbg !24
+  %5 = getelementptr inbounds %struct.Foo* %g, i32 0, i32 1, !dbg !24
+  %6 = load i32* %2, align 4, !dbg !24
+  store i32 %6, i32* %5, align 4, !dbg !24
+  %7 = load i32* %2, align 4, !dbg !25
+  %8 = load i32* %2, align 4, !dbg !25
+  %9 = bitcast i32* %1 to i8*
+  call void @llvm.lifetime.start(i64 4, i8* %9)
+  store i32 %8, i32* %1, align 4
+  call void @llvm.dbg.declare(metadata !{i32* %1}, metadata !26), !dbg !27
+  %10 = load i32* %1, align 4, !dbg !28
+  %11 = mul nsw i32 2, %10, !dbg !28
+  %12 = bitcast i32* %1 to i8*, !dbg !28
+  call void @llvm.lifetime.end(i64 4, i8* %12), !dbg !28
+  %13 = add nsw i32 %7, %11, !dbg !25
+  ret i32 %13, !dbg !25
+}
+
+; Function Attrs: nounwind readnone
+declare void @llvm.dbg.declare(metadata, metadata) #1
+
+; Function Attrs: alwaysinline nounwind ssp uwtable
+define i32 @_Z4bar2i(i32 %ab) #2 {
+  %1 = alloca i32, align 4
+  store i32 %ab, i32* %1, align 4
+  call void @llvm.dbg.declare(metadata !{i32* %1}, metadata !26), !dbg !29
+  %2 = load i32* %1, align 4, !dbg !30
+  %3 = mul nsw i32 2, %2, !dbg !30
+  ret i32 %3, !dbg !30
+}
+
+; Function Attrs: ssp uwtable
+define i32 @main() #3 {
+  %1 = alloca i32, align 4
+  store i32 0, i32* %1
+  %2 = call i32 @_Z3fooi(i32 44), !dbg !31
+  ret i32 %2, !dbg !31
+}
+
+; Function Attrs: nounwind
+declare void @llvm.lifetime.start(i64, i8* nocapture) #4
+
+; Function Attrs: nounwind
+declare void @llvm.lifetime.end(i64, i8* nocapture) #4
+
+attributes #0 = { noinline ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { nounwind readnone }
+attributes #2 = { alwaysinline nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #3 = { ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #4 = { nounwind }
+
+!llvm.dbg.cu = !{!0, !9}
+
+!0 = metadata !{i32 786449, metadata !1, i32 4, metadata !"clang version 3.4 (trunk 182336)", i1 false, metadata !"", i32 0, metadata !2, metadata !2, metadata !3, metadata !2, metadata !2, metadata !""} ; [ DW_TAG_compile_unit ] [foo.cpp] [DW_LANG_C_plus_plus]
+!1 = metadata !{metadata !"foo.cpp", metadata !"."}
+!2 = metadata !{i32 0}
+!3 = metadata !{metadata !4}
+!4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"foo", metadata !"foo", metadata !"_Z3fooi", i32 3, metadata !6, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 (i32)* @_Z3fooi, null, null, metadata !2, i32 3} ; [ DW_TAG_subprogram ] [line 3] [def] [foo]
+!5 = metadata !{i32 786473, metadata !1}          ; [ DW_TAG_file_type ]
+!6 = metadata !{i32 786453, i32 0, i32 0, metadata !"", i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !7, i32 0, i32 0} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
+!7 = metadata !{metadata !8, metadata !8}
+!8 = metadata !{i32 786468, null, null, metadata !"int", i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] [int] [line 0, size 32, align 32, offset 0, enc DW_ATE_signed]
+!9 = metadata !{i32 786449, metadata !10, i32 4, metadata !"clang version 3.4 (trunk 182336)", i1 false, metadata !"", i32 0, metadata !2, metadata !2, metadata !11, metadata !2, metadata !2, metadata !""} ; [ DW_TAG_compile_unit ] [bar.cpp] [DW_LANG_C_plus_plus]
+!10 = metadata !{metadata !"bar.cpp", metadata !"."}
+!11 = metadata !{metadata !12, metadata !14}
+!12 = metadata !{i32 786478, metadata !10, metadata !13, metadata !"bar2", metadata !"bar2", metadata !"_Z4bar2i", i32 2, metadata !6, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 (i32)* @_Z4bar2i, null, null, metadata !2, i32 2} ; [ DW_TAG_subprogram ] [line 2] [def] [bar2]
+!13 = metadata !{i32 786473, metadata !10}        ; [ DW_TAG_file_type ]
+!14 = metadata !{i32 786478, metadata !10, metadata !13, metadata !"main", metadata !"main", metadata !"", i32 15, metadata !15, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 ()* @main, null, null, metadata !2, i32 15} ; [ DW_TAG_subprogram ] [line 15] [def] [main]
+!15 = metadata !{i32 786453, i32 0, i32 0, metadata !"", i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !16, i32 0, i32 0} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
+!16 = metadata !{metadata !8}
+!17 = metadata !{i32 786689, metadata !4, metadata !"a", metadata !5, i32 16777219, metadata !8, i32 0, i32 0} ; [ DW_TAG_arg_variable ] [a] [line 3]
+!18 = metadata !{i32 3, i32 0, metadata !4, null}
+!19 = metadata !{i32 786688, metadata !4, metadata !"g", metadata !5, i32 9, metadata !20, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [g] [line 9]
+!20 = metadata !{i32 786451, metadata !1, metadata !4, metadata !"Foo", i32 4, i64 64, i64 32, i32 0, i32 0, null, metadata !21, i32 0, null, null} ; [ DW_TAG_structure_type ] [Foo] [line 4, size 64, align 32, offset 0] [from ]
+!21 = metadata !{metadata !22, metadata !23}
+!22 = metadata !{i32 786445, metadata !1, metadata !20, metadata !"a", i32 5, i64 32, i64 32, i64 0, i32 0, metadata !8} ; [ DW_TAG_member ] [a] [line 5, size 32, align 32, offset 0] [from int]
+!23 = metadata !{i32 786445, metadata !1, metadata !20, metadata !"b", i32 6, i64 32, i64 32, i64 32, i32 0, metadata !8} ; [ DW_TAG_member ] [b] [line 6, size 32, align 32, offset 32] [from int]
+!24 = metadata !{i32 9, i32 0, metadata !4, null}
+!25 = metadata !{i32 10, i32 0, metadata !4, null}
+!26 = metadata !{i32 786689, metadata !12, metadata !"ab", metadata !13, i32 16777218, metadata !8, i32 0, i32 0} ; [ DW_TAG_arg_variable ] [ab] [line 2]
+!27 = metadata !{i32 2, i32 0, metadata !12, metadata !25}
+!28 = metadata !{i32 11, i32 0, metadata !12, metadata !25}
+!29 = metadata !{i32 2, i32 0, metadata !12, null}
+!30 = metadata !{i32 11, i32 0, metadata !12, null}
+!31 = metadata !{i32 16, i32 0, metadata !14, null}