[llvm-branch-commits] [llvm] 8dddcc7 - [Cloning] Copy metadata of global declarations

Ruiling Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jan 7 16:31:13 PST 2021


Author: Ruiling Song
Date: 2021-01-08T08:21:18+08:00
New Revision: 8dddcc762dd98d53b9406b36e92f62502834187c

URL: https://github.com/llvm/llvm-project/commit/8dddcc762dd98d53b9406b36e92f62502834187c
DIFF: https://github.com/llvm/llvm-project/commit/8dddcc762dd98d53b9406b36e92f62502834187c.diff

LOG: [Cloning] Copy metadata of global declarations

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.

Reviewed by: arsenm, aprantl

Differential Revision: https://reviews.llvm.org/D93451

Added: 
    llvm/test/Other/copy-metadata-of-declaration.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CloneModule.cpp b/llvm/lib/Transforms/Utils/CloneModule.cpp
index 2c8c3abb2922..a6327bbf21bc 100644
--- a/llvm/lib/Transforms/Utils/CloneModule.cpp
+++ b/llvm/lib/Transforms/Utils/CloneModule.cpp
@@ -117,10 +117,17 @@ std::unique_ptr<Module> llvm::CloneModule(
   //
   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 @@ std::unique_ptr<Module> llvm::CloneModule(
     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);
   }
 

diff  --git a/llvm/test/Other/copy-metadata-of-declaration.ll b/llvm/test/Other/copy-metadata-of-declaration.ll
new file mode 100644
index 000000000000..aed8db75f6d8
--- /dev/null
+++ b/llvm/test/Other/copy-metadata-of-declaration.ll
@@ -0,0 +1,10 @@
+; RUN: opt -run-twice -verify -S -o - %s | FileCheck %s
+
+; This test is used to check metadata attached to global variable declarations
+; are copied when CloneModule(). This is required by out-of-tree passes.
+
+; CHECK: @g = external addrspace(64) global i32, !spirv.InOut !0
+
+ at g = external addrspace(64) global i32, !spirv.InOut !0
+
+!0 = !{i32 1}


        


More information about the llvm-branch-commits mailing list