Index: lib/Transforms/Utils/CloneModule.cpp =================================================================== --- lib/Transforms/Utils/CloneModule.cpp +++ lib/Transforms/Utils/CloneModule.cpp @@ -132,7 +132,8 @@ SmallVector, 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); } 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 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();