diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -150,6 +150,11 @@ /// in the GlobalValueMap. Requires a vector in the case of multiple /// COMDAT values of the same name. GlobalValueSummaryList SummaryList; + + /// Cached IsDSOLocal result for fast lookup during function importing. + /// Value is pre-populated in ModuleIndexSummary::populateDSOLocal() and + /// invalidated when SummaryList or any associated summary is updated. + Optional IsDSOLocal; }; /// Map from global value GUID to corresponding summary structures. Use a @@ -1313,6 +1318,9 @@ // by the non-const *this. const_cast(VI.getRef()) ->second.SummaryList.push_back(std::move(Summary)); + // Invalidate the cached result. + const_cast(VI.getRef()) + ->second.IsDSOLocal.reset(); } /// Add an original name for the value of the given GUID. @@ -1350,6 +1358,13 @@ return findSummaryInModule(CalleeInfo, ModuleId); } + /// Find the summary info for global \p GUID in index, or nullptr if not + /// found. + GlobalValueSummaryInfo *findSummaryInfo(GlobalValue::GUID ValueGUID) { + auto I = GlobalValueMap.find(ValueGUID); + return I == GlobalValueMap.end() ? nullptr : &(I->second); + } + /// Returns the first GlobalValueSummary for \p GV, asserting that there /// is only one if \p PerModuleIndex. GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, @@ -1518,6 +1533,10 @@ /// Checks if we can import global variable from another module. bool canImportGlobalVar(GlobalValueSummary *S, bool AnalyzeRefs) const; + + /// Compute IsDSOLocal for GUIDs in GlobalValueMap and save the result in + /// its corresponding GlobalValueSummaryInfo struct. + void populateDSOLocal(); }; /// GraphTraits definition to build SCC for the index diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -6979,5 +6979,8 @@ return errorCodeToError(FileOrErr.getError()); if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) return nullptr; - return getModuleSummaryIndex(**FileOrErr); + auto IndexOrErr = getModuleSummaryIndex(**FileOrErr); + if (IndexOrErr) + IndexOrErr.get()->populateDSOLocal(); + return IndexOrErr; } diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp --- a/llvm/lib/IR/ModuleSummaryIndex.cpp +++ b/llvm/lib/IR/ModuleSummaryIndex.cpp @@ -53,12 +53,9 @@ } bool ValueInfo::isDSOLocal() const { - // Need to check all summaries are local in case of hash collisions. - return getSummaryList().size() && - llvm::all_of(getSummaryList(), - [](const std::unique_ptr &Summary) { - return Summary->isDSOLocal(); - }); + // Get pre-populated IsDSOLocal result. + assert(getRef()->second.IsDSOLocal && "IsDSOLocal was not populated."); + return getRef()->second.IsDSOLocal.getValue(); } bool ValueInfo::canAutoHide() const { @@ -340,6 +337,18 @@ } } +void ModuleSummaryIndex::populateDSOLocal() { + for (auto &I : GlobalValueMap) { + auto &GI = I.second; + GI.IsDSOLocal = + !GI.IsDSOLocal && GI.SummaryList.size() && + llvm::all_of(GI.SummaryList, + [](const std::unique_ptr &Summary) { + return Summary->isDSOLocal(); + }); + } +} + namespace { struct Attributes { void add(const Twine &Name, const Twine &Value, diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -890,6 +890,9 @@ if (auto S = ThinLTO.CombinedIndex.findSummaryInModule( GUID, BM.getModuleIdentifier())) { S->setDSOLocal(true); + // Invalidate cached result. + if (auto *I = ThinLTO.CombinedIndex.findSummaryInfo(GUID)) + I->IsDSOLocal.reset(); } } } @@ -1480,6 +1483,8 @@ generateParamAccessSummary(ThinLTO.CombinedIndex); + ThinLTO.CombinedIndex.populateDSOLocal(); + std::unique_ptr BackendProc = ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, AddStream, Cache); diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -607,6 +607,7 @@ return nullptr; } } + CombinedIndex->populateDSOLocal(); return CombinedIndex; }