Skip to content

Commit

Permalink
Remap metadata attached to global variables.
Browse files Browse the repository at this point in the history
Fix for PR32577.
Global variables may have !associated metadata, which includes a reference to another global. It needs remapping.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302203 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
eugenis committed May 4, 2017
1 parent 197e49d commit 1565077
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/Transforms/Utils/ValueMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class Mapper {

void addFlags(RemapFlags Flags);

void remapGlobalObjectMetadata(GlobalObject &GO);

Value *mapValue(const Value *V);
void remapInstruction(Instruction *I);
void remapFunction(Function &F);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -892,18 +895,22 @@ void Mapper::remapInstruction(Instruction *I) {
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)
Expand Down
11 changes: 11 additions & 0 deletions test/Linker/metadata-global.ll
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 1565077

Please sign in to comment.