Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit a580131

Browse files
committed
Merging r309928:
------------------------------------------------------------------------ r309928 | ewancrawford | 2017-08-03 02:23:03 -0700 (Thu, 03 Aug 2017) | 56 lines [Cloning] Move distinct GlobalVariable debug info metadata in CloneModule 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)") ``` Reviewers: aprantl, probinson, dblaikie, echristo, loladiro Reviewed By: aprantl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D36082 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@309965 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9423112 commit a580131

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/Transforms/Utils/CloneModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ std::unique_ptr<Module> llvm::CloneModule(
132132
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
133133
I->getAllMetadata(MDs);
134134
for (auto MD : MDs)
135-
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
135+
GV->addMetadata(MD.first,
136+
*MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
136137

137138
copyComdat(GV, &*I);
138139
}

unittests/Transforms/Utils/Cloning.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,19 @@ class CloneModule : public ::testing::Test {
507507
DINode::FlagZero, false);
508508
F->setSubprogram(Subprogram);
509509

510+
// Create and assign DIGlobalVariableExpression to gv
511+
auto GVExpression = DBuilder.createGlobalVariableExpression(
512+
Subprogram, "gv", "gv", File, 1, DBuilder.createNullPtrType(), false);
513+
GV->addDebugInfo(GVExpression);
514+
515+
// DIGlobalVariableExpression not attached to any global variable
516+
auto Expr = DBuilder.createExpression(
517+
ArrayRef<uint64_t>{dwarf::DW_OP_constu, 42U, dwarf::DW_OP_stack_value});
518+
519+
DBuilder.createGlobalVariableExpression(
520+
Subprogram, "unattached", "unattached", File, 1,
521+
DBuilder.createNullPtrType(), false, Expr);
522+
510523
auto *Entry = BasicBlock::Create(C, "", F);
511524
IBuilder.SetInsertPoint(Entry);
512525
IBuilder.CreateRetVoid();
@@ -546,6 +559,52 @@ TEST_F(CloneModule, GlobalMetadata) {
546559
EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));
547560
}
548561

562+
TEST_F(CloneModule, GlobalDebugInfo) {
563+
GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
564+
EXPECT_TRUE(NewGV != nullptr);
565+
566+
// Find debug info expression assigned to global
567+
SmallVector<DIGlobalVariableExpression *, 1> GVs;
568+
NewGV->getDebugInfo(GVs);
569+
EXPECT_EQ(GVs.size(), 1U);
570+
571+
DIGlobalVariableExpression *GVExpr = GVs[0];
572+
DIGlobalVariable *GV = GVExpr->getVariable();
573+
EXPECT_TRUE(GV != nullptr);
574+
575+
EXPECT_EQ(GV->getName(), "gv");
576+
EXPECT_EQ(GV->getLine(), 1U);
577+
578+
// Assert that the scope of the debug info attached to
579+
// global variable matches the cloned function.
580+
DISubprogram *SP = NewM->getFunction("f")->getSubprogram();
581+
EXPECT_TRUE(SP != nullptr);
582+
EXPECT_EQ(GV->getScope(), SP);
583+
}
584+
585+
TEST_F(CloneModule, CompileUnit) {
586+
// Find DICompileUnit listed in llvm.dbg.cu
587+
auto *NMD = NewM->getNamedMetadata("llvm.dbg.cu");
588+
EXPECT_TRUE(NMD != nullptr);
589+
EXPECT_EQ(NMD->getNumOperands(), 1U);
590+
591+
DICompileUnit *CU = dyn_cast<llvm::DICompileUnit>(NMD->getOperand(0));
592+
EXPECT_TRUE(CU != nullptr);
593+
594+
// Assert this CU is consistent with the cloned function debug info
595+
DISubprogram *SP = NewM->getFunction("f")->getSubprogram();
596+
EXPECT_TRUE(SP != nullptr);
597+
EXPECT_EQ(SP->getUnit(), CU);
598+
599+
// Check globals listed in CU have the correct scope
600+
DIGlobalVariableExpressionArray GlobalArray = CU->getGlobalVariables();
601+
EXPECT_EQ(GlobalArray.size(), 2U);
602+
for (DIGlobalVariableExpression *GVExpr : GlobalArray) {
603+
DIGlobalVariable *GV = GVExpr->getVariable();
604+
EXPECT_EQ(GV->getScope(), SP);
605+
}
606+
}
607+
549608
TEST_F(CloneModule, Comdat) {
550609
GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
551610
auto *CD = NewGV->getComdat();

0 commit comments

Comments
 (0)