Index: include/llvm/Transforms/Utils/ValueMapper.h =================================================================== --- include/llvm/Transforms/Utils/ValueMapper.h +++ include/llvm/Transforms/Utils/ValueMapper.h @@ -69,6 +69,11 @@ /// Instruct the remapper to move distinct metadata instead of duplicating /// it when there are module-level changes. RF_MoveDistinctMDs = 4, + + /// RF_NullMapMissingGlobalValues - Any global values not in value map are + /// mapped to null instead of mapping to self. Illegal if + /// RF_IgnoreMissingEntries is also set. + RF_NullMapMissingGlobalValues = 8, }; static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) { Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -1628,8 +1628,9 @@ NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName()); // Add Src elements into Dest node. for (const MDNode *op : NMD.operands()) - DestNMD->addOperand(MapMetadata(op, ValueMap, RF_MoveDistinctMDs, - &TypeMap, &ValMaterializer)); + DestNMD->addOperand(MapMetadata( + op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues, + &TypeMap, &ValMaterializer)); } } Index: lib/Transforms/Utils/ValueMapper.cpp =================================================================== --- lib/Transforms/Utils/ValueMapper.cpp +++ lib/Transforms/Utils/ValueMapper.cpp @@ -42,9 +42,14 @@ // Global values do not need to be seeded into the VM if they // are using the identity mapping. - if (isa(V)) + if (isa(V)) { + if (Flags & RF_NullMapMissingGlobalValues) { + assert(!(Flags & RF_IgnoreMissingEntries)); + return nullptr; + } return VM[V] = const_cast(V); - + } + if (const InlineAsm *IA = dyn_cast(V)) { // Inline asm may need *type* remapping. FunctionType *NewTy = IA->getFunctionType(); @@ -74,7 +79,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // assert(MappedMD && "Referenced metadata value not in value map"); + // assert((MappedMD || (Flags & RF_NullMapMissingGlobalValues)) && + // "Referenced metadata value not in value map"); return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD); } @@ -184,7 +190,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // llvm_unreachable("Referenced metadata not in value map!"); + // assert((Flags & RF_NullMapMissingGlobalValues) && + // "Referenced metadata not in value map!"); return nullptr; } @@ -305,7 +312,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // assert(MappedV && "Referenced metadata not in value map!"); + // assert((MappedV || (Flags & RF_NullMapMissingGlobalValues)) && + // "Referenced metadata not in value map!"); if (MappedV) return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV)); return nullptr;