X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=lib%2FLinker%2FLinkModules.cpp;h=5419c22fbf7b69dea916910eccf7cb98eb8bc4fb;hp=88b019a6f92a1d2a0140bf3769167b69851f8b77;hb=78b70af914869966c1b90415d7f020c396699e8c;hpb=649084b00b5bb85507168f6a7939ef2bad3a1fd5 diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 88b019a6f92..5419c22fbf7 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -509,6 +509,8 @@ private: ComdatsChosen; bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK, bool &LinkFromSrc); + // Keep track of the global value members of each comdat in source. + DenseMap> ComdatMembers; /// Given a global in the source module, return the global in the /// destination module that is being linked to, if any. @@ -655,7 +657,9 @@ bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) { return false; return doImportAsDefinition(GA->getBaseObject()); } - // Always import GlobalVariable definitions. The linkage changes + // Always import GlobalVariable definitions, except for the special + // case of WeakAny which are imported as ExternalWeak declarations + // (see comments in ModuleLinker::getLinkage). The linkage changes // described in ModuleLinker::getLinkage ensure the correct behavior (e.g. // global variables with external linkage are transformed to // available_externally definitions, which are ultimately turned into @@ -773,7 +777,7 @@ GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) { // since it would cause global constructors/destructors to be // executed multiple times. This should have already been handled // by linkGlobalValueProto. - assert(false && "Cannot import appending linkage variable"); + llvm_unreachable("Cannot import appending linkage variable"); case GlobalValue::InternalLinkage: case GlobalValue::PrivateLinkage: @@ -1384,6 +1388,7 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) { std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; C = DstM->getOrInsertComdat(SC->getName()); C->setSelectionKind(SK); + ComdatMembers[SC].push_back(SGV); } else if (DGV) { if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV)) return true; @@ -1587,6 +1592,19 @@ void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) { bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) { Value *Dst = ValueMap[&Src]; assert(Dst); + if (const Comdat *SC = Src.getComdat()) { + // To ensure that we don't generate an incomplete comdat group, + // we must materialize and map in any other members that are not + // yet materialized in Dst, which also ensures their definitions + // are linked in. Otherwise, linkonce and other lazy linked GVs will + // not be materialized if they aren't referenced. + for (auto *SGV : ComdatMembers[SC]) { + if (ValueMap[SGV]) + continue; + Value *NewV = ValMaterializer.materializeValueFor(SGV); + ValueMap[SGV] = NewV; + } + } if (shouldInternalizeLinkedSymbols()) if (auto *DGV = dyn_cast(Dst)) DGV->setLinkage(GlobalValue::InternalLinkage); @@ -1610,8 +1628,9 @@ void ModuleLinker::linkNamedMDNodes() { 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)); } }