diff --git a/llvm/include/llvm/Transforms/Utils/ValueMapper.h b/llvm/include/llvm/Transforms/Utils/ValueMapper.h --- a/llvm/include/llvm/Transforms/Utils/ValueMapper.h +++ b/llvm/include/llvm/Transforms/Utils/ValueMapper.h @@ -112,8 +112,9 @@ /// There are a number of top-level entry points: /// - \a mapValue() (and \a mapConstant()); /// - \a mapMetadata() (and \a mapMDNode()); -/// - \a remapInstruction(); and -/// - \a remapFunction(). +/// - \a remapInstruction(); +/// - \a remapFunction(); and +/// - \a remapGlobalObjectMetadata(). /// /// The \a ValueMaterializer can be used as a callback, but cannot invoke any /// of these top-level functions recursively. Instead, callbacks should use @@ -175,6 +176,7 @@ void remapInstruction(Instruction &I); void remapFunction(Function &F); + void remapGlobalObjectMetadata(GlobalObject &GO); void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID = 0); diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -409,6 +409,10 @@ std::vector Worklist; std::vector> RAUWWorklist; + /// Set of globals with eagerly copied metadata that may require remapping. + /// This remapping is performed after metadata linking. + DenseSet UnmappedMetadata; + void maybeAdd(GlobalValue *GV) { if (ValuesToLink.insert(GV).second) Worklist.push_back(GV); @@ -750,8 +754,11 @@ if (auto *NewGO = dyn_cast(NewGV)) { // Metadata for global variables and function declarations is copied eagerly. - if (isa(SGV) || SGV->isDeclaration()) + if (isa(SGV) || SGV->isDeclaration()) { NewGO->copyMetadata(cast(SGV), 0); + if (SGV->isDeclaration()) + UnmappedMetadata.insert(NewGO); + } } // Remove these copied constants in case this stays a declaration, since @@ -1056,6 +1063,7 @@ // as well. if (Function *F = dyn_cast(NewGV)) if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) { + UnmappedMetadata.erase(F); NewGV->eraseFromParent(); NewGV = *Remangled; NeedsRenaming = false; @@ -1651,6 +1659,13 @@ // are properly remapped. linkNamedMDNodes(); + // Clean up any global objects with potentially unmapped metadata. + // Specifically declarations which did not become definitions. + for (GlobalObject *NGO : UnmappedMetadata) { + if (NGO->isDeclaration()) + Mapper.remapGlobalObjectMetadata(*NGO); + } + if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) { // Append the module inline asm string. DstM.appendModuleInlineAsm(adjustInlineAsm(SrcM->getModuleInlineAsm(), diff --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp --- a/llvm/lib/Transforms/Utils/ValueMapper.cpp +++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp @@ -1181,6 +1181,10 @@ FlushingMapper(pImpl)->remapFunction(F); } +void ValueMapper::remapGlobalObjectMetadata(GlobalObject &GO) { + FlushingMapper(pImpl)->remapGlobalObjectMetadata(GO); +} + void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MCID) { diff --git a/llvm/test/Linker/Inputs/metadata-function.ll b/llvm/test/Linker/Inputs/metadata-function.ll --- a/llvm/test/Linker/Inputs/metadata-function.ll +++ b/llvm/test/Linker/Inputs/metadata-function.ll @@ -10,4 +10,13 @@ unreachable } +%AltHandle = type { i8* } +declare !types !1 %AltHandle @init.AltHandle() + +define void @uses.AltHandle() { + %.res = call %AltHandle @init.AltHandle() + unreachable +} + !0 = !{!"b"} +!1 = !{%AltHandle undef} diff --git a/llvm/test/Linker/metadata-function.ll b/llvm/test/Linker/metadata-function.ll --- a/llvm/test/Linker/metadata-function.ll +++ b/llvm/test/Linker/metadata-function.ll @@ -21,6 +21,15 @@ unreachable } +; CHECK-DAG: define %[[HandleType:[A-Za-z]+]] @init.Handle() { +; CHECK-DAG: declare !types ![[C:[0-9]+]] %[[HandleType]] @init.AltHandle() +; CHECK-DAG: define void @uses.AltHandle() { +%Handle = type { i8* } +define %Handle @init.Handle() { + unreachable +} + ; CHECK-DAG: ![[A]] = !{!"a"} ; CHECK-DAG: ![[B]] = !{!"b"} +; CHECK-DAG: ![[C]] = !{%[[HandleType]] undef} !0 = !{!"a"}