Index: include/llvm/LTO/LTO.h =================================================================== --- include/llvm/LTO/LTO.h +++ include/llvm/LTO/LTO.h @@ -290,6 +290,12 @@ ModuleSummaryIndex CombinedIndex; MapVector ModuleMap; DenseMap PrevailingModuleForGUID; + + StringMap> + ModuleToDefinedGVSummaries; + StringMap ImportLists; + StringMap ExportLists; + StringMap> ResolvedODR; } ThinLTO; // The global resolution for a particular (mangled) symbol name. This is in @@ -356,6 +362,7 @@ const SymbolResolution *&ResI, const SymbolResolution *ResE); Error runRegularLTO(AddStreamFn AddStream); + void prepareThinLTO(); Error runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, bool HasRegularLTO); Index: lib/LTO/LTO.cpp =================================================================== --- lib/LTO/LTO.cpp +++ lib/LTO/LTO.cpp @@ -632,6 +632,7 @@ // the CombinedModule will be moved at the end of runRegularLTO. bool HasRegularLTO = RegularLTO.CombinedModule != nullptr; // Invoke regular LTO if there was a regular LTO module to start with. + prepareThinLTO(); if (HasRegularLTO) if (auto E = runRegularLTO(AddStream)) return E; @@ -936,20 +937,14 @@ }; } -Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, - bool HasRegularLTO) { +void LTO::prepareThinLTO() { if (ThinLTO.ModuleMap.empty()) - return Error::success(); - - if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex)) - return Error::success(); + return; // Collect for each module the list of function it defines (GUID -> // Summary). - StringMap> - ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size()); ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule( - ModuleToDefinedGVSummaries); + ThinLTO.ModuleToDefinedGVSummaries); // Create entries for any modules that didn't have any GV summaries // (either they didn't have any GVs to start with, or we suppressed // generation of the summaries because they e.g. had inline assembly @@ -958,14 +953,8 @@ // is passed the map of summaries for the module, without any special // handling for this case. for (auto &Mod : ThinLTO.ModuleMap) - if (!ModuleToDefinedGVSummaries.count(Mod.first)) - ModuleToDefinedGVSummaries.try_emplace(Mod.first); - - StringMap ImportLists( - ThinLTO.ModuleMap.size()); - StringMap ExportLists( - ThinLTO.ModuleMap.size()); - StringMap> ResolvedODR; + if (!ThinLTO.ModuleToDefinedGVSummaries.count(Mod.first)) + ThinLTO.ModuleToDefinedGVSummaries.try_emplace(Mod.first); if (Conf.OptLevel > 0) { // Compute "dead" symbols, we don't want to import/export these! @@ -982,8 +971,9 @@ auto DeadSymbols = computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols); - ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, - ImportLists, ExportLists, &DeadSymbols); + ComputeCrossModuleImport( + ThinLTO.CombinedIndex, ThinLTO.ModuleToDefinedGVSummaries, + ThinLTO.ImportLists, ThinLTO.ExportLists, &DeadSymbols); std::set ExportedGUIDs; for (auto &Res : GlobalResolutions) { @@ -1003,8 +993,8 @@ } auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { - const auto &ExportList = ExportLists.find(ModuleIdentifier); - return (ExportList != ExportLists.end() && + const auto &ExportList = ThinLTO.ExportLists.find(ModuleIdentifier); + return (ExportList != ThinLTO.ExportLists.end() && ExportList->second.count(GUID)) || ExportedGUIDs.count(GUID); }; @@ -1018,14 +1008,23 @@ auto recordNewLinkage = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID, GlobalValue::LinkageTypes NewLinkage) { - ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; + ThinLTO.ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; }; thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing, recordNewLinkage); +} + +Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, + bool HasRegularLTO) { + if (ThinLTO.ModuleMap.empty()) + return Error::success(); + + if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex)) + return Error::success(); std::unique_ptr BackendProc = - ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, - AddStream, Cache); + ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, + ThinLTO.ModuleToDefinedGVSummaries, AddStream, Cache); // Task numbers start at ParallelCodeGenParallelismLevel if an LTO // module is present, as tasks 0 through ParallelCodeGenParallelismLevel-1 @@ -1033,9 +1032,10 @@ unsigned Task = HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0; for (auto &Mod : ThinLTO.ModuleMap) { - if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first], - ExportLists[Mod.first], - ResolvedODR[Mod.first], ThinLTO.ModuleMap)) + if (Error E = BackendProc->start( + Task, Mod.second, ThinLTO.ImportLists[Mod.first], + ThinLTO.ExportLists[Mod.first], ThinLTO.ResolvedODR[Mod.first], + ThinLTO.ModuleMap)) return E; ++Task; }