[PATCH] D36082: [Cloning] Move distinct GlobalVariable debug info metadata in CloneModule

Ewan Crawford via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 04:04:38 PDT 2017


EwanCrawford created this revision.

Duplicating the distinct Subprogram and CU metadata nodes seems like the incorrect thing to do in CloneModule for GlobalVariable debug info. As it results in the scope of the GlobalVariable DI no longer being consistent with the rest of the module, and the new CU is absent from llvm.dbg.cu.

Fixed by adding RF_MoveDistinctMDs to MapMetadata flags for GlobalVariables.

Current unit test IR after clone:

  @gv = global i32 1, comdat($comdat), !dbg !0, !type !5
  
  define private void @f() comdat($comdat) personality void ()* @persfn !dbg !14 {
  
  !llvm.dbg.cu = !{!10}
  
  !0 = !DIGlobalVariableExpression(var: !1)
  !1 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !2, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
  !2 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !6, variables: !5)
  !3 = !DIFile(filename: "filename.c", directory: "/file/dir/")
  !4 = !DISubroutineType(types: !5)
  !5 = !{}
  !6 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !8)
  !7 = !DIFile(filename: "filename.c", directory: "/file/dir")
  !8 = !{!0}
  !9 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "decltype(nullptr)")
  !10 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !11)
  !11 = !{!12}
  !12 = !DIGlobalVariableExpression(var: !13)
  !13 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !14, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
  !14 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !10, variables: !5)

Patched IR after clone:

  @gv = global i32 1, comdat($comdat), !dbg !0, !type !5
  
  define private void @f() comdat($comdat) personality void ()* @persfn !dbg !2 {
  
  !llvm.dbg.cu = !{!6}
  
  !0 = !DIGlobalVariableExpression(var: !1)
  !1 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !2, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
  !2 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !6, variables: !5)
  !3 = !DIFile(filename: "filename.c", directory: "/file/dir/")
  !4 = !DISubroutineType(types: !5)
  !5 = !{}
  !6 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !8)
  !7 = !DIFile(filename: "filename.c", directory: "/file/dir")
  !8 = !{!0}
  !9 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "decltype(nullptr)")


Repository:
  rL LLVM

https://reviews.llvm.org/D36082

Files:
  lib/Transforms/Utils/CloneModule.cpp
  unittests/Transforms/Utils/Cloning.cpp


Index: unittests/Transforms/Utils/Cloning.cpp
===================================================================
--- unittests/Transforms/Utils/Cloning.cpp
+++ unittests/Transforms/Utils/Cloning.cpp
@@ -506,6 +506,10 @@
                                 DINode::FlagZero, false);
     F->setSubprogram(Subprogram);
 
+    auto GVExpression = DBuilder.createGlobalVariableExpression(
+        Subprogram, "gv", "gv", File, 1, DBuilder.createNullPtrType(), false);
+    GV->addDebugInfo(GVExpression);
+
     auto *Entry = BasicBlock::Create(C, "", F);
     IBuilder.SetInsertPoint(Entry);
     IBuilder.CreateRetVoid();
@@ -545,6 +549,27 @@
   EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));
 }
 
+TEST_F(CloneModule, GlobalDebugInfo) {
+  GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
+  EXPECT_TRUE(NewGV != nullptr);
+
+  SmallVector<DIGlobalVariableExpression *, 1> GVs;
+  NewGV->getDebugInfo(GVs);
+  EXPECT_EQ(GVs.size(), 1U);
+
+  DIGlobalVariableExpression *GVExpr = GVs[0];
+  DIGlobalVariable *GV = GVExpr->getVariable();
+  EXPECT_TRUE(GV != nullptr);
+
+  EXPECT_EQ(GV->getName(), "gv");
+  EXPECT_EQ(GV->getLine(), 1U);
+
+  Function *NewF = NewM->getFunction("f");
+  DISubprogram *SP = NewF->getSubprogram();
+  EXPECT_TRUE(SP != nullptr);
+  EXPECT_EQ(GV->getScope(), SP);
+}
+
 TEST_F(CloneModule, Comdat) {
   GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
   auto *CD = NewGV->getComdat();
Index: lib/Transforms/Utils/CloneModule.cpp
===================================================================
--- lib/Transforms/Utils/CloneModule.cpp
+++ lib/Transforms/Utils/CloneModule.cpp
@@ -132,7 +132,8 @@
     SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
     I->getAllMetadata(MDs);
     for (auto MD : MDs)
-      GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
+      GV->addMetadata(MD.first,
+                      *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
 
     copyComdat(GV, &*I);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36082.108893.patch
Type: text/x-patch
Size: 1970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170731/30d26c03/attachment.bin>


More information about the llvm-commits mailing list