Blame 0001-Merging-r318289.patch

b47291c
From d61468959556ddc180c6c28edff476244fd0e8a6 Mon Sep 17 00:00:00 2001
b47291c
From: tstellar <tstellar@91177308-0d34-0410-b5e6-96231b3b80d8>
b47291c
Date: Fri, 17 Nov 2017 18:48:34 +0000
b47291c
Subject: [PATCH] Merging r318289:
b47291c
b47291c
------------------------------------------------------------------------
b47291c
r318289 | jdevlieghere | 2017-11-15 02:57:05 -0800 (Wed, 15 Nov 2017) | 14 lines
b47291c
b47291c
[DebugInfo] Fix potential CU mismatch for SubprogramScopeDIEs.
b47291c
b47291c
In constructAbstractSubprogramScopeDIE there can be a potential mismatch
b47291c
between `this` and the CU of ContextDIE when a scope is shared between
b47291c
two DISubprograms belonging to a different CU. In that case, `this` is
b47291c
the CU that was specified in the IR, but the CU of ContextDIE is that of
b47291c
the first subprogram that was emitted. This patch fixes the mismatch by
b47291c
looking up the CU of ContextDIE, and switching to use that.
b47291c
b47291c
This fixes PR35212 (https://bugs.llvm.org/show_bug.cgi?id=35212)
b47291c
b47291c
Patch by Philip Craig!
b47291c
b47291c
Differential revision: https://reviews.llvm.org/D39981
b47291c
------------------------------------------------------------------------
b47291c
b47291c
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@318542 91177308-0d34-0410-b5e6-96231b3b80d8
b47291c
---
b47291c
 lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp | 20 ++++++++----
b47291c
 lib/CodeGen/AsmPrinter/DwarfDebug.h         |  5 ++-
b47291c
 test/DebugInfo/cross-cu-scope.ll            | 50 +++++++++++++++++++++++++++++
b47291c
 3 files changed, 67 insertions(+), 8 deletions(-)
b47291c
 create mode 100644 test/DebugInfo/cross-cu-scope.ll
b47291c
b47291c
diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
b47291c
index 676c48f..333d14a 100644
b47291c
--- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
b47291c
+++ b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
b47291c
@@ -621,6 +621,7 @@ void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
b47291c
   auto *SP = cast<DISubprogram>(Scope->getScopeNode());
b47291c
 
b47291c
   DIE *ContextDIE;
b47291c
+  DwarfCompileUnit *ContextCU = this;
b47291c
 
b47291c
   if (includeMinimalInlineScopes())
b47291c
     ContextDIE = &getUnitDie();
b47291c
@@ -631,18 +632,23 @@ void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
b47291c
   else if (auto *SPDecl = SP->getDeclaration()) {
b47291c
     ContextDIE = &getUnitDie();
b47291c
     getOrCreateSubprogramDIE(SPDecl);
b47291c
-  } else
b47291c
+  } else {
b47291c
     ContextDIE = getOrCreateContextDIE(resolve(SP->getScope()));
b47291c
+    // The scope may be shared with a subprogram that has already been
b47291c
+    // constructed in another CU, in which case we need to construct this
b47291c
+    // subprogram in the same CU.
b47291c
+    ContextCU = DD->lookupCU(ContextDIE->getUnitDie());
b47291c
+  }
b47291c
 
b47291c
   // Passing null as the associated node because the abstract definition
b47291c
   // shouldn't be found by lookup.
b47291c
-  AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
b47291c
-  applySubprogramAttributesToDefinition(SP, *AbsDef);
b47291c
+  AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
b47291c
+  ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef);
b47291c
 
b47291c
-  if (!includeMinimalInlineScopes())
b47291c
-    addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
b47291c
-  if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef))
b47291c
-    addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
b47291c
+  if (!ContextCU->includeMinimalInlineScopes())
b47291c
+    ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
b47291c
+  if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef))
b47291c
+    ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
b47291c
 }
b47291c
 
b47291c
 DIE *DwarfCompileUnit::constructImportedEntityDIE(
b47291c
diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.h b/lib/CodeGen/AsmPrinter/DwarfDebug.h
b47291c
index 5dfe06c..78ee9a1 100644
b47291c
--- a/lib/CodeGen/AsmPrinter/DwarfDebug.h
b47291c
+++ b/lib/CodeGen/AsmPrinter/DwarfDebug.h
b47291c
@@ -283,7 +283,7 @@ class DwarfDebug : public DebugHandlerBase {
b47291c
   // 0, referencing the comp_dir of all the type units that use it.
b47291c
   MCDwarfDwoLineTable SplitTypeUnitFileTable;
b47291c
   /// @}
b47291c
-  
b47291c
+
b47291c
   /// True iff there are multiple CUs in this module.
b47291c
   bool SingleCU;
b47291c
   bool IsDarwin;
b47291c
@@ -562,6 +562,9 @@ public:
b47291c
   bool isLexicalScopeDIENull(LexicalScope *Scope);
b47291c
 
b47291c
   bool hasDwarfPubSections(bool includeMinimalInlineScopes) const;
b47291c
+
b47291c
+  /// Find the matching DwarfCompileUnit for the given CU DIE.
b47291c
+  DwarfCompileUnit *lookupCU(const DIE *Die) { return CUDieMap.lookup(Die); }
b47291c
 };
b47291c
 } // End of namespace llvm
b47291c
 
b47291c
diff --git a/test/DebugInfo/cross-cu-scope.ll b/test/DebugInfo/cross-cu-scope.ll
b47291c
new file mode 100644
b47291c
index 0000000..7c71f27
b47291c
--- /dev/null
b47291c
+++ b/test/DebugInfo/cross-cu-scope.ll
b47291c
@@ -0,0 +1,50 @@
b47291c
+; RUN: %llc_dwarf %s -filetype=obj -o %t
b47291c
+; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s
b47291c
+
b47291c
+; Reduced test case from PR35212. Two DISubprogram belong to a different CU but
b47291c
+; share a scope. Both are declarations and end up in the scope's CU. We want to
b47291c
+; check that the CU from the context DIE is used (rather than from the IR).
b47291c
+; This manifests itself by the DW_base_type ending up in the second CU, rather
b47291c
+; than in the first one as specified in the IR.
b47291c
+
b47291c
+; CHECK: DW_TAG_compile_unit
b47291c
+; CHECK-NEXT: discriminator 0
b47291c
+; CHECK: DW_TAG_compile_unit
b47291c
+; CHECK-NEXT: discriminator 1
b47291c
+; CHECK: DW_TAG_structure_type
b47291c
+; CHECK-NOT: NULL
b47291c
+; CHECK: DW_TAG_subprogram
b47291c
+; CHECK-NOT: NULL
b47291c
+; CHECK: DW_TAG_formal_parameter
b47291c
+; CHECK-NOT: NULL
b47291c
+; CHECK: DW_AT_type{{.*}}{[[USIZE_LABEL:0x[0-9a-f]+]]}
b47291c
+; CHECK: NULL
b47291c
+; CHECK: [[USIZE_LABEL]]: DW_TAG_base_type
b47291c
+; CHECK-NOT: NULL
b47291c
+; CHECK: DW_AT_name{{.*}}"usize"
b47291c
+
b47291c
+define hidden void @foo() !dbg !4 {
b47291c
+  ret void, !dbg !7
b47291c
+}
b47291c
+
b47291c
+!llvm.dbg.cu = !{!0, !2}
b47291c
+!llvm.module.flags = !{!3}
b47291c
+
b47291c
+!0 = distinct !DICompileUnit(language: DW_LANG_Rust, file: !1, producer: "clang LLVM (rustc version 1.23.0-nightly (discriminator 0))", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
b47291c
+!1 = !DIFile(filename: "../lib.rs", directory: "/home/alex/code/rust4/lol")
b47291c
+!2 = distinct !DICompileUnit(language: DW_LANG_Rust, file: !1, producer: "clang LLVM (rustc version 1.23.0-nightly (discriminator 1))", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
b47291c
+!3 = !{i32 2, !"Debug Info Version", i32 3}
b47291c
+!4 = distinct !DISubprogram(name: "clone<alloc::string::String>", linkageName: "_ZN5alloc3vec8{{impl}}28clone<alloc::string::String>E", scope: null, file: !1, line: 1519, type: !5, isLocal: false, isDefinition: true, scopeLine: 1519, flags: DIFlagPrototyped, isOptimized: true, unit: !0, templateParams: !6, variables: !6)
b47291c
+!5 = !DISubroutineType(types: !6)
b47291c
+!6 = !{}
b47291c
+!7 = !DILocation(line: 1612, scope: !8, inlinedAt: !11)
b47291c
+!8 = distinct !DILexicalBlock(scope: !9, file: !1, line: 86, column: 12)
b47291c
+!9 = distinct !DISubprogram(name: "allocate_in<alloc::string::String,alloc::heap::Heap>", linkageName: "_ZN5alloc7raw_vec8{{impl}}52allocate_in<alloc::string::String,alloc::heap::Heap>E", scope: !10, file: !1, line: 82, type: !5, isLocal: false, isDefinition: true, scopeLine: 82, flags: DIFlagPrototyped, isOptimized: true, unit: !2, templateParams: !6, variables: !6)
b47291c
+!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "RawVec<alloc::string::String, alloc::heap::Heap>", file: !1, size: 128, align: 64, elements: !6, identifier: "5c6e4db16d2c64555e40661d70c4d81e")
b47291c
+!11 = distinct !DILocation(line: 86, scope: !8, inlinedAt: !12)
b47291c
+!12 = distinct !DILocation(line: 141, scope: !13, inlinedAt: !17)
b47291c
+!13 = distinct !DISubprogram(name: "with_capacity<alloc::string::String>", linkageName: "_ZN5alloc7raw_vec8{{impl}}36with_capacity<alloc::string::String>E", scope: !10, file: !1, line: 140, type: !5, isLocal: false, isDefinition: true, scopeLine: 140, flags: DIFlagPrototyped, isOptimized: true, unit: !0, templateParams: !6, variables: !14)
b47291c
+!14 = !{!15}
b47291c
+!15 = !DILocalVariable(name: "cap", arg: 1, scope: !13, file: !1, line: 1, type: !16)
b47291c
+!16 = !DIBasicType(name: "usize", size: 64, encoding: DW_ATE_unsigned)
b47291c
+!17 = !DILocation(line: 1521, scope: !4)
b47291c
-- 
b47291c
1.8.3.1
b47291c