[llvm] 512534b - [Cloning] Clone metadata on function declarations

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 1 15:40:38 PST 2021


Author: Arthur Eubanks
Date: 2021-12-01T15:40:05-08:00
New Revision: 512534bc16d2bebb0fb02e92a5ebd35eff9fb220

URL: https://github.com/llvm/llvm-project/commit/512534bc16d2bebb0fb02e92a5ebd35eff9fb220
DIFF: https://github.com/llvm/llvm-project/commit/512534bc16d2bebb0fb02e92a5ebd35eff9fb220.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/CloneModule.cpp
    llvm/unittests/Transforms/Utils/CloningTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CloneModule.cpp b/llvm/lib/Transforms/Utils/CloneModule.cpp
index 200deca4b3172..57c273a0e3c52 100644
--- a/llvm/lib/Transforms/Utils/CloneModule.cpp
+++ b/llvm/lib/Transforms/Utils/CloneModule.cpp
@@ -135,10 +135,18 @@ std::unique_ptr<Module> llvm::CloneModule(
   // Similarly, copy over function bodies now...
   //
   for (const Function &I : M) {
-    if (I.isDeclaration())
+    Function *F = cast<Function>(VMap[&I]);
+
+    if (I.isDeclaration()) {
+      // Copy over metadata for declarations since we're not doing it below in
+      // CloneFunctionInto().
+      SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
+      I.getAllMetadata(MDs);
+      for (auto MD : MDs)
+        F->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
       continue;
+    }
 
-    Function *F = cast<Function>(VMap[&I]);
     if (!ShouldCloneDefinition(&I)) {
       // Skip after setting the correct linkage for an external reference.
       F->setLinkage(GlobalValue::ExternalLinkage);

diff  --git a/llvm/unittests/Transforms/Utils/CloningTest.cpp b/llvm/unittests/Transforms/Utils/CloningTest.cpp
index 34802b63aae84..77b2c2ce2cf8b 100644
--- a/llvm/unittests/Transforms/Utils/CloningTest.cpp
+++ b/llvm/unittests/Transforms/Utils/CloningTest.cpp
@@ -921,6 +921,10 @@ class CloneModule : public ::testing::Test {
     IBuilder.SetInsertPoint(Entry);
     IBuilder.CreateRetVoid();
 
+    auto *G =
+        Function::Create(FuncType, GlobalValue::ExternalLinkage, "g", OldM);
+    G->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
+
     // Finalize the debug info
     DBuilder.finalize();
   }
@@ -934,10 +938,10 @@ class CloneModule : public ::testing::Test {
 
 TEST_F(CloneModule, Verify) {
   // Confirm the old module is (still) valid.
-  EXPECT_FALSE(verifyModule(*OldM));
+  EXPECT_FALSE(verifyModule(*OldM, &errs()));
 
   // Check the new module.
-  EXPECT_FALSE(verifyModule(*NewM));
+  EXPECT_FALSE(verifyModule(*NewM, &errs()));
 }
 
 TEST_F(CloneModule, OldModuleUnchanged) {
@@ -955,6 +959,11 @@ TEST_F(CloneModule, Subprogram) {
   EXPECT_EQ(SP->getLine(), (unsigned)4);
 }
 
+TEST_F(CloneModule, FunctionDeclarationMetadata) {
+  Function *NewF = NewM->getFunction("g");
+  EXPECT_NE(nullptr, NewF->getMetadata(LLVMContext::MD_type));
+}
+
 TEST_F(CloneModule, GlobalMetadata) {
   GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
   EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));


        


More information about the llvm-commits mailing list