Index: lib/LTO/ThinLTOCodeGenerator.cpp =================================================================== --- lib/LTO/ThinLTOCodeGenerator.cpp +++ lib/LTO/ThinLTOCodeGenerator.cpp @@ -94,6 +94,120 @@ WriteBitcodeToFile(&TheModule, OS, true, false); } +bool IsFirstDefinitionForLinker(const GlobalValueInfoList &GVInfo, + const ModuleSummaryIndex &Index, + StringRef ModulePath) { + // Get the first *linker visible* definition for this global in the summary + // list. + auto FirstDefForLinker = llvm::find_if( + GVInfo, [](const std::unique_ptr &FuncInfo) { + auto Linkage = FuncInfo->summary()->linkage(); + return !GlobalValue::isAvailableExternallyLinkage(Linkage); + }); + // If \p GV is not the first definition, give up... + if ((*FirstDefForLinker)->summary()->modulePath() != ModulePath) + return false; + // If there is any strong definition anywhere, do not bother emitting this. + if (llvm::any_of( + GVInfo, [](const std::unique_ptr &FuncInfo) { + auto Linkage = FuncInfo->summary()->linkage(); + return !GlobalValue::isAvailableExternallyLinkage(Linkage) && + !GlobalValue::isWeakForLinker(Linkage); + })) + return false; + return true; +}; + +static void FixupLinkOnceODR(GlobalValue &GV, const ModuleSummaryIndex &Index, + StringRef ModulePath) { + if (GV.isDeclaration()) + return; + + auto HasMultipleCopies = + [&](const GlobalValueInfoList &GVInfo) { return GVInfo.size() > 1; }; + + auto getGVInfo = [&](GlobalValue &GV) -> const GlobalValueInfoList *{ + auto GUID = Function::getGlobalIdentifier(GV.getName(), GV.getLinkage(), + ModulePath); + auto It = Index.findGlobalValueInfoList(GV.getName()); + if (It == Index.end()) + return nullptr; + return &It->second; + }; + + switch (GV.getLinkage()) { + case GlobalValue::ExternalLinkage: + case GlobalValue::AvailableExternallyLinkage: + case GlobalValue::AppendingLinkage: + case GlobalValue::InternalLinkage: + case GlobalValue::PrivateLinkage: + case GlobalValue::ExternalWeakLinkage: + case GlobalValue::CommonLinkage: + break; + case GlobalValue::LinkOnceAnyLinkage: { + auto *GVInfo = getGVInfo(GV); + if (!GVInfo) + break; + // We need to emit only one of these, the first module will keep + // it, but turned into a weak while the others will drop it. + if (!HasMultipleCopies(*GVInfo)) + break; + if (IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath)) + GV.setLinkage(GlobalValue::WeakAnyLinkage); + else + GV.setLinkage(GlobalValue::AvailableExternallyLinkage); + break; + } + case GlobalValue::LinkOnceODRLinkage: { + auto *GVInfo = getGVInfo(GV); + if (!GVInfo) + break; + // We need to emit only one of these, the first module will keep + // it, but turned into a weak while the others will drop it. + if (!HasMultipleCopies(*GVInfo)) + break; + if (IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath)) + GV.setLinkage(GlobalValue::WeakODRLinkage); + else + GV.setLinkage(GlobalValue::AvailableExternallyLinkage); + break; + } + case GlobalValue::WeakAnyLinkage: { + auto *GVInfo = getGVInfo(GV); + if (!GVInfo) + break; + // We need to emit only one of these, the first module will keep + // it "as is" and the others will drop it + if (!IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath)) + GV.setLinkage(GlobalValue::AvailableExternallyLinkage); + break; + } + case GlobalValue::WeakODRLinkage: { + auto *GVInfo = getGVInfo(GV); + if (!GVInfo) + break; + // We need to emit only one of these, the first module will keep + // it "as is" and the others will drop it + if (!IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath)) + GV.setLinkage(GlobalValue::AvailableExternallyLinkage); + break; + } + } +} + +/// LinkOnceODR needs to be turned to weak ODR for correctness. +/// +static void FixupLinkOnceODR(Module &TheModule, + const ModuleSummaryIndex &Index) { + for (auto &GV : TheModule) { + FixupLinkOnceODR(GV, Index, TheModule.getModuleIdentifier()); + } + for (auto &GV : TheModule.globals()) { + FixupLinkOnceODR(GV, Index, TheModule.getModuleIdentifier()); + } + // Fixme: alias? +} + static StringMap generateModuleMap(const std::vector &Modules) { StringMap ModuleMap; @@ -199,6 +313,10 @@ if (!SingleModule) { promoteModule(TheModule, Index); + // Fixup the LinkOnceODR + // !! Need to be taken in account when handling the cache !! + FixupLinkOnceODR(TheModule, Index); + // Save temps: after promotion. saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc");