Index: llvm/trunk/include/llvm/IR/GlobalObject.h =================================================================== --- llvm/trunk/include/llvm/IR/GlobalObject.h +++ llvm/trunk/include/llvm/IR/GlobalObject.h @@ -114,6 +114,9 @@ /// Erase all metadata attachments with the given kind. void eraseMetadata(unsigned KindID); + /// Copy metadata from Src. + void copyMetadata(const GlobalObject *Src); + void copyAttributesFrom(const GlobalValue *Src) override; // Methods for support type inquiry through isa, cast, and dyn_cast: Index: llvm/trunk/lib/IR/Metadata.cpp =================================================================== --- llvm/trunk/lib/IR/Metadata.cpp +++ llvm/trunk/lib/IR/Metadata.cpp @@ -1393,6 +1393,13 @@ return getMetadata(getContext().getMDKindID(Kind)); } +void GlobalObject::copyMetadata(const GlobalObject *Other) { + SmallVector, 8> MDs; + Other->getAllMetadata(MDs); + for (auto &MD : MDs) + addMetadata(MD.first, *MD.second); +} + void Function::setSubprogram(DISubprogram *SP) { setMetadata(LLVMContext::MD_dbg, SP); } Index: llvm/trunk/lib/Linker/IRMover.cpp =================================================================== --- llvm/trunk/lib/Linker/IRMover.cpp +++ llvm/trunk/lib/Linker/IRMover.cpp @@ -638,6 +638,12 @@ NewGV->copyAttributesFrom(SGV); + if (auto *NewGO = dyn_cast(NewGV)) { + // Metadata for global variables and function declarations is copied eagerly. + if (isa(SGV) || SGV->isDeclaration()) + NewGO->copyMetadata(cast(SGV)); + } + // Remove these copied constants in case this stays a declaration, since // they point to the source module. If the def is linked the values will // be mapped in during linkFunctionBody. @@ -961,10 +967,7 @@ Dst.setPersonalityFn(Src.getPersonalityFn()); // Copy over the metadata attachments without remapping. - SmallVector, 8> MDs; - Src.getAllMetadata(MDs); - for (const auto &I : MDs) - Dst.setMetadata(I.first, I.second); + Dst.copyMetadata(&Src); // Steal arguments and splice the body of Src into Dst. Dst.stealArgumentListFrom(Src); Index: llvm/trunk/test/Linker/metadata-attach.ll =================================================================== --- llvm/trunk/test/Linker/metadata-attach.ll +++ llvm/trunk/test/Linker/metadata-attach.ll @@ -0,0 +1,19 @@ +; RUN: llvm-link %s -S -o - | FileCheck %s + +; CHECK: @g1 = global i32 0, !attach !0 +@g1 = global i32 0, !attach !0 + +; CHECK: @g2 = external global i32, !attach !0 +@g2 = external global i32, !attach !0 + +; CHECK: define void @f1() !attach !0 +define void @f1() !attach !0 { + call void @f2() + store i32 0, i32* @g2 + ret void +} + +; CHECK: declare !attach !0 void @f2() +declare !attach !0 void @f2() + +!0 = !{}