[llvm] r302203 - Remap metadata attached to global variables.

Evgeniy Stepanov via llvm-commits llvm-commits at lists.llvm.org
Thu May 4 16:29:39 PDT 2017


Author: eugenis
Date: Thu May  4 18:29:39 2017
New Revision: 302203

URL: http://llvm.org/viewvc/llvm-project?rev=302203&view=rev
Log:
Remap metadata attached to global variables.

Fix for PR32577.
Global variables may have !associated metadata, which includes a reference to another global. It needs remapping.

Added:
    llvm/trunk/test/Linker/metadata-global.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp

Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=302203&r1=302202&r2=302203&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Thu May  4 18:29:39 2017
@@ -121,6 +121,8 @@ public:
 
   void addFlags(RemapFlags Flags);
 
+  void remapGlobalObjectMetadata(GlobalObject &GO);
+
   Value *mapValue(const Value *V);
   void remapInstruction(Instruction *I);
   void remapFunction(Function &F);
@@ -802,6 +804,7 @@ void Mapper::flush() {
     switch (E.Kind) {
     case WorklistEntry::MapGlobalInit:
       E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
+      remapGlobalObjectMetadata(*E.Data.GVInit.GV);
       break;
     case WorklistEntry::MapAppendingVar: {
       unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
@@ -892,6 +895,14 @@ void Mapper::remapInstruction(Instructio
   I->mutateType(TypeMapper->remapType(I->getType()));
 }
 
+void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
+  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
+  GO.getAllMetadata(MDs);
+  GO.clearMetadata();
+  for (const auto &I : MDs)
+    GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
+}
+
 void Mapper::remapFunction(Function &F) {
   // Remap the operands.
   for (Use &Op : F.operands())
@@ -899,11 +910,7 @@ void Mapper::remapFunction(Function &F)
       Op = mapValue(Op);
 
   // Remap the metadata attachments.
-  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
-  F.getAllMetadata(MDs);
-  F.clearMetadata();
-  for (const auto &I : MDs)
-    F.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
+  remapGlobalObjectMetadata(F);
 
   // Remap the argument types.
   if (TypeMapper)

Added: llvm/trunk/test/Linker/metadata-global.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/metadata-global.ll?rev=302203&view=auto
==============================================================================
--- llvm/trunk/test/Linker/metadata-global.ll (added)
+++ llvm/trunk/test/Linker/metadata-global.ll Thu May  4 18:29:39 2017
@@ -0,0 +1,11 @@
+; RUN: llvm-link %s -S | FileCheck %s
+
+; CHECK-DAG: @a = global i32 0
+; CHECK-DAG: @b = global i32 0, !associated !0
+
+; CHECK-DAG: !0 = !{i32* @b}
+
+ at a = global i32 0
+ at b = global i32 0, !associated !0
+
+!0 = !{i32* @b}




More information about the llvm-commits mailing list