Skip to content

Commit 1b17650

Browse files
aeubankspfez
authored andcommitted
[Cloning] Clone metadata on function declarations
Previously we missed cloning metadata on function declarations because we don't call CloneFunctionInto() on declarations in CloneModule(). Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D113812
1 parent 79a55d0 commit 1b17650

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

llvm/lib/Transforms/Utils/CloneModule.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,18 @@ std::unique_ptr<Module> llvm::CloneModule(
142142
// Similarly, copy over function bodies now...
143143
//
144144
for (const Function &I : M) {
145-
if (I.isDeclaration())
145+
Function *F = cast<Function>(VMap[&I]);
146+
147+
if (I.isDeclaration()) {
148+
// Copy over metadata for declarations since we're not doing it below in
149+
// CloneFunctionInto().
150+
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
151+
I.getAllMetadata(MDs);
152+
for (auto MD : MDs)
153+
F->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
146154
continue;
155+
}
147156

148-
Function *F = cast<Function>(VMap[&I]);
149157
if (!ShouldCloneDefinition(&I)) {
150158
// Skip after setting the correct linkage for an external reference.
151159
F->setLinkage(GlobalValue::ExternalLinkage);

llvm/unittests/Transforms/Utils/CloningTest.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,10 @@ class CloneModule : public ::testing::Test {
882882
IBuilder.SetInsertPoint(Entry);
883883
IBuilder.CreateRetVoid();
884884

885+
auto *G =
886+
Function::Create(FuncType, GlobalValue::ExternalLinkage, "g", OldM);
887+
G->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
888+
885889
// Finalize the debug info
886890
DBuilder.finalize();
887891
}
@@ -894,7 +898,11 @@ class CloneModule : public ::testing::Test {
894898
};
895899

896900
TEST_F(CloneModule, Verify) {
897-
EXPECT_FALSE(verifyModule(*NewM));
901+
// Confirm the old module is (still) valid.
902+
EXPECT_FALSE(verifyModule(*OldM, &errs()));
903+
904+
// Check the new module.
905+
EXPECT_FALSE(verifyModule(*NewM, &errs()));
898906
}
899907

900908
TEST_F(CloneModule, OldModuleUnchanged) {
@@ -912,6 +920,11 @@ TEST_F(CloneModule, Subprogram) {
912920
EXPECT_EQ(SP->getLine(), (unsigned)4);
913921
}
914922

923+
TEST_F(CloneModule, FunctionDeclarationMetadata) {
924+
Function *NewF = NewM->getFunction("g");
925+
EXPECT_NE(nullptr, NewF->getMetadata(LLVMContext::MD_type));
926+
}
927+
915928
TEST_F(CloneModule, GlobalMetadata) {
916929
GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
917930
EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));

0 commit comments

Comments
 (0)