From c40d5a1d2a4582c1caee9b49462a66eb0a08c7bd Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Thu, 4 May 2017 23:29:39 +0000 Subject: [PATCH] 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. llvm-svn: 302203 --- lib/Transforms/Utils/ValueMapper.cpp | 17 ++++++++++++----- test/Linker/metadata-global.ll | 11 +++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/Linker/metadata-global.ll diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index f77c10b6dd4..84d89f103a2 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -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(Instruction *I) { I->mutateType(TypeMapper->remapType(I->getType())); } +void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) { + SmallVector, 8> MDs; + GO.getAllMetadata(MDs); + GO.clearMetadata(); + for (const auto &I : MDs) + GO.addMetadata(I.first, *cast(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, 8> MDs; - F.getAllMetadata(MDs); - F.clearMetadata(); - for (const auto &I : MDs) - F.addMetadata(I.first, *cast(mapMetadata(I.second))); + remapGlobalObjectMetadata(F); // Remap the argument types. if (TypeMapper) diff --git a/test/Linker/metadata-global.ll b/test/Linker/metadata-global.ll new file mode 100644 index 00000000000..56d77e128bd --- /dev/null +++ b/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} + +@a = global i32 0 +@b = global i32 0, !associated !0 + +!0 = !{i32* @b}