[PATCH] D93451: [Cloning] Copy metadata of global declarations

Ruiling, Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 04:34:12 PST 2020


ruiling created this revision.
ruiling added reviewers: pcc, tpr.
Herald added a subscriber: hiraditya.
ruiling requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We have modules with metadata on declarations, and out-of-tree passes
use that metadata, and we need to clone those modules. We really expect
such metadata is kept during the clone operation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93451

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


Index: llvm/unittests/Transforms/Utils/CloningTest.cpp
===================================================================
--- llvm/unittests/Transforms/Utils/CloningTest.cpp
+++ llvm/unittests/Transforms/Utils/CloningTest.cpp
@@ -840,6 +840,11 @@
     GV->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
     GV->setComdat(CD);
 
+    // global declare
+    auto GVD = new GlobalVariable(*OldM, Type::getInt32Ty(C), false,
+                                  GlobalValue::ExternalLinkage, nullptr, "gvd");
+    GVD->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
+
     DIBuilder DBuilder(*OldM);
     IRBuilder<> IBuilder(C);
 
@@ -915,6 +920,9 @@
 TEST_F(CloneModule, GlobalMetadata) {
   GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
   EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));
+  // copy metadata for global declare
+  GlobalVariable *NewGVD = NewM->getGlobalVariable("gvd");
+  EXPECT_NE(nullptr, NewGVD->getMetadata(LLVMContext::MD_type));
 }
 
 TEST_F(CloneModule, GlobalDebugInfo) {
Index: llvm/lib/Transforms/Utils/CloneModule.cpp
===================================================================
--- llvm/lib/Transforms/Utils/CloneModule.cpp
+++ llvm/lib/Transforms/Utils/CloneModule.cpp
@@ -117,10 +117,17 @@
   //
   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
        I != E; ++I) {
+    GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
+
+    SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
+    I->getAllMetadata(MDs);
+    for (auto MD : MDs)
+      GV->addMetadata(MD.first,
+                      *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
+
     if (I->isDeclaration())
       continue;
 
-    GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
     if (!ShouldCloneDefinition(&*I)) {
       // Skip after setting the correct linkage for an external reference.
       GV->setLinkage(GlobalValue::ExternalLinkage);
@@ -129,12 +136,6 @@
     if (I->hasInitializer())
       GV->setInitializer(MapValue(I->getInitializer(), VMap));
 
-    SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
-    I->getAllMetadata(MDs);
-    for (auto MD : MDs)
-      GV->addMetadata(MD.first,
-                      *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
-
     copyComdat(GV, &*I);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93451.312448.patch
Type: text/x-patch
Size: 2305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201217/2c865806/attachment.bin>


More information about the llvm-commits mailing list