[PATCH] D32777: Remap metadata attached to global variables.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 2 17:41:39 PDT 2017


eugenis created this revision.

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


Repository:
  rL LLVM

https://reviews.llvm.org/D32777

Files:
  lib/Transforms/Utils/ValueMapper.cpp
  test/Linker/metadata-global.ll


Index: test/Linker/metadata-global.ll
===================================================================
--- /dev/null
+++ test/Linker/metadata-global.ll
@@ -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}
Index: lib/Transforms/Utils/ValueMapper.cpp
===================================================================
--- lib/Transforms/Utils/ValueMapper.cpp
+++ lib/Transforms/Utils/ValueMapper.cpp
@@ -121,6 +121,8 @@
 
   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 @@
     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,18 +895,22 @@
   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())
     if (Op)
       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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32777.97531.patch
Type: text/x-patch
Size: 1984 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170503/8509d9f9/attachment.bin>


More information about the llvm-commits mailing list