From 70ec64cb7235efbeb1a7c6c4f9b5810d866850ec Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Fri, 15 Mar 2019 15:11:38 +0000 Subject: [PATCH] [ThinLTO] Restructure AliasSummary to contain ValueInfo of Aliasee Summary: The AliasSummary previously contained the AliaseeGUID, which was only populated when reading the summary from bitcode. This patch changes it to instead hold the ValueInfo of the aliasee, and always populates it. This enables more efficient access to the ValueInfo (specifically in the recent patch r352438 which needed to perform an index hash table lookup using the aliasee GUID). As noted in the comments in AliasSummary, we no longer technically need to keep a pointer to the corresponding aliasee summary, since it could be obtained by walking the list of summaries on the ValueInfo looking for the summary in the same module. However, I am concerned that this would be inefficient when walking through the index during the thin link for various analyses. That can be reevaluated in the future. By always populating this new field, we can remove the guard and special handling for a 0 aliasee GUID when dumping the dot graph of the summary. An additional improvement in this patch is when reading the summaries from LLVM assembly we now set the AliaseeSummary field to the aliasee summary in that same module, which makes it consistent with the behavior when reading the summary from bitcode. Reviewers: pcc, mehdi_amini Subscribers: inglorion, eraman, steven_wu, dexonsmith, arphaman, llvm-commits Differential Revision: https://reviews.llvm.org/D57470 llvm-svn: 356268 --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 59 +++++++++++++++++++---------- llvm/lib/Analysis/ModuleSummaryAnalysis.cpp | 8 ++-- llvm/lib/AsmParser/LLParser.cpp | 18 +++++---- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 18 +++------ llvm/lib/IR/ModuleSummaryIndex.cpp | 12 +----- llvm/lib/Transforms/IPO/FunctionImport.cpp | 7 +--- llvm/test/ThinLTO/X86/dot-dumper.ll | 3 +- 7 files changed, 64 insertions(+), 61 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index a97f736..bf2aad1 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -380,25 +380,35 @@ public: /// Alias summary information. class AliasSummary : public GlobalValueSummary { + ValueInfo AliaseeValueInfo; + + /// This is the Aliasee in the same module as alias (could get from VI, trades + /// memory for time). Note that this pointer may be null (and the value info + /// empty) when we have a distributed index where the alias is being imported + /// (as a copy of the aliasee), but the aliasee is not. GlobalValueSummary *AliaseeSummary; - // AliaseeGUID is only set and accessed when we are building a combined index - // via the BitcodeReader. - GlobalValue::GUID AliaseeGUID; public: AliasSummary(GVFlags Flags) : GlobalValueSummary(AliasKind, Flags, ArrayRef{}), - AliaseeSummary(nullptr), AliaseeGUID(0) {} + AliaseeSummary(nullptr) {} /// Check if this is an alias summary. static bool classof(const GlobalValueSummary *GVS) { return GVS->getSummaryKind() == AliasKind; } - void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; } - void setAliaseeGUID(GlobalValue::GUID GUID) { AliaseeGUID = GUID; } + void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee) { + AliaseeValueInfo = AliaseeVI; + AliaseeSummary = Aliasee; + } - bool hasAliasee() const { return !!AliaseeSummary; } + bool hasAliasee() const { + assert(!!AliaseeSummary == (AliaseeValueInfo && + !AliaseeValueInfo.getSummaryList().empty()) && + "Expect to have both aliasee summary and summary list or neither"); + return !!AliaseeSummary; + } const GlobalValueSummary &getAliasee() const { assert(AliaseeSummary && "Unexpected missing aliasee summary"); @@ -409,10 +419,13 @@ public: return const_cast( static_cast(this)->getAliasee()); } - bool hasAliaseeGUID() const { return AliaseeGUID != 0; } - const GlobalValue::GUID &getAliaseeGUID() const { - assert(AliaseeGUID && "Unexpected missing aliasee GUID"); - return AliaseeGUID; + ValueInfo getAliaseeVI() const { + assert(AliaseeValueInfo && "Unexpected missing aliasee"); + return AliaseeValueInfo; + } + GlobalValue::GUID getAliaseeGUID() const { + assert(AliaseeValueInfo && "Unexpected missing aliasee"); + return AliaseeValueInfo.getGUID(); } }; @@ -1043,24 +1056,30 @@ public: OidGuidMap[OrigGUID] = ValueGUID; } - /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if + /// Find the summary for ValueInfo \p VI in module \p ModuleId, or nullptr if /// not found. - GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID, - StringRef ModuleId) const { - auto CalleeInfo = getValueInfo(ValueGUID); - if (!CalleeInfo) { - return nullptr; // This function does not have a summary - } + GlobalValueSummary *findSummaryInModule(ValueInfo VI, StringRef ModuleId) const { + auto SummaryList = VI.getSummaryList(); auto Summary = - llvm::find_if(CalleeInfo.getSummaryList(), + llvm::find_if(SummaryList, [&](const std::unique_ptr &Summary) { return Summary->modulePath() == ModuleId; }); - if (Summary == CalleeInfo.getSummaryList().end()) + if (Summary == SummaryList.end()) return nullptr; return Summary->get(); } + /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if + /// not found. + GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID, + StringRef ModuleId) const { + auto CalleeInfo = getValueInfo(ValueGUID); + if (!CalleeInfo) + return nullptr; // This function does not have a summary + return findSummaryInModule(CalleeInfo, ModuleId); + } + /// Returns the first GlobalValueSummary for \p GV, asserting that there /// is only one if \p PerModuleIndex. GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp index 58bf469..68eb1ef9 100644 --- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -441,9 +441,11 @@ computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, /* Live = */ false, A.isDSOLocal()); auto AS = llvm::make_unique(Flags); auto *Aliasee = A.getBaseObject(); - auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee); - assert(AliaseeSummary && "Alias expects aliasee summary to be parsed"); - AS->setAliasee(AliaseeSummary); + auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID()); + assert(AliaseeVI && "Alias expects aliasee summary to be available"); + assert(AliaseeVI.getSummaryList().size() == 1 && + "Expected a single entry per aliasee in per-module index"); + AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get()); if (NonRenamableLocal) CantBePromoted.insert(A.getGUID()); Index.addGlobalValueSummary(A, std::move(AS)); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index cba579d..5a875fe 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -7707,10 +7707,6 @@ void LLParser::AddGlobalValueToIndex( } } - // Add the summary if one was provided. - if (Summary) - Index->addGlobalValueSummary(VI, std::move(Summary)); - // Resolve forward references from calls/refs auto FwdRefVIs = ForwardRefValueInfos.find(ID); if (FwdRefVIs != ForwardRefValueInfos.end()) { @@ -7728,11 +7724,16 @@ void LLParser::AddGlobalValueToIndex( for (auto AliaseeRef : FwdRefAliasees->second) { assert(!AliaseeRef.first->hasAliasee() && "Forward referencing alias already has aliasee"); - AliaseeRef.first->setAliasee(VI.getSummaryList().front().get()); + assert(Summary && "Aliasee must be a definition"); + AliaseeRef.first->setAliasee(VI, Summary.get()); } ForwardRefAliasees.erase(FwdRefAliasees); } + // Add the summary if one was provided. + if (Summary) + Index->addGlobalValueSummary(VI, std::move(Summary)); + // Save the associated ValueInfo for use in later references by ID. if (ID == NumberedValueInfos.size()) NumberedValueInfos.push_back(VI); @@ -7976,8 +7977,11 @@ bool LLParser::ParseAliasSummary(std::string Name, GlobalValue::GUID GUID, auto FwdRef = ForwardRefAliasees.insert( std::make_pair(GVId, std::vector>())); FwdRef.first->second.push_back(std::make_pair(AS.get(), Loc)); - } else - AS->setAliasee(AliaseeVI.getSummaryList().front().get()); + } else { + auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath); + assert(Summary && "Aliasee must be a definition"); + AS->setAliasee(AliaseeVI, Summary); + } AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, std::move(AS)); diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index b9f93dc..5eff6e6 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -5482,14 +5482,11 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { // ownership. AS->setModulePath(getThisModule()->first()); - GlobalValue::GUID AliaseeGUID = - getValueInfoFromValueId(AliaseeID).first.getGUID(); - auto AliaseeInModule = - TheIndex.findSummaryInModule(AliaseeGUID, ModulePath); + auto AliaseeVI = getValueInfoFromValueId(AliaseeID).first; + auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath); if (!AliaseeInModule) return error("Alias expects aliasee summary to be parsed"); - AS->setAliasee(AliaseeInModule); - AS->setAliaseeGUID(AliaseeGUID); + AS->setAliasee(AliaseeVI, AliaseeInModule); auto GUID = getValueInfoFromValueId(ValueID); AS->setOriginalName(GUID.second); @@ -5592,12 +5589,9 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { LastSeenSummary = AS.get(); AS->setModulePath(ModuleIdMap[ModuleId]); - auto AliaseeGUID = - getValueInfoFromValueId(AliaseeValueId).first.getGUID(); - auto AliaseeInModule = - TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); - AS->setAliasee(AliaseeInModule); - AS->setAliaseeGUID(AliaseeGUID); + auto AliaseeVI = getValueInfoFromValueId(AliaseeValueId).first; + auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath()); + AS->setAliasee(AliaseeVI, AliaseeInModule); ValueInfo VI = getValueInfoFromValueId(ValueID).first; LastSeenGUID = VI.getGUID(); diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp index 6ae6285..4704215 100644 --- a/llvm/lib/IR/ModuleSummaryIndex.cpp +++ b/llvm/lib/IR/ModuleSummaryIndex.cpp @@ -418,17 +418,7 @@ void ModuleSummaryIndex::exportToDot(raw_ostream &OS) const { Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2); if (auto *AS = dyn_cast_or_null(SummaryIt.second)) { - GlobalValue::GUID AliaseeId; - if (AS->hasAliaseeGUID()) - AliaseeId = AS->getAliaseeGUID(); - else { - auto AliaseeOrigId = AS->getAliasee().getOriginalName(); - AliaseeId = getGUIDFromOriginalID(AliaseeOrigId); - if (!AliaseeId) - AliaseeId = AliaseeOrigId; - } - - Draw(SummaryIt.first, AliaseeId, -3); + Draw(SummaryIt.first, AS->getAliaseeGUID(), -3); continue; } diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp index dcd246d..ef8b152 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -821,12 +821,7 @@ void llvm::computeDeadSymbols( // If this is an alias, visit the aliasee VI to ensure that all copies // are marked live and it is added to the worklist for further // processing of its references. - // FIXME: The aliasee GUID is only populated in the summary when we - // read them from bitcode, which is currently the only way we can - // get here (we don't yet support reading the summary index directly - // from LLVM assembly code in tools that can perform a thin link). - // If that ever changes, the below call to getAliaseGUID will assert. - visit(Index.getValueInfo(AS->getAliaseeGUID())); + visit(AS->getAliaseeVI()); continue; } diff --git a/llvm/test/ThinLTO/X86/dot-dumper.ll b/llvm/test/ThinLTO/X86/dot-dumper.ll index e8bdc15..d546d90 100644 --- a/llvm/test/ThinLTO/X86/dot-dumper.ll +++ b/llvm/test/ThinLTO/X86/dot-dumper.ll @@ -23,10 +23,9 @@ ; PERMODULE-NEXT: M0_[[MAIN_ALIAS:[0-9]+]] [style="dotted,filled",shape="box",label="main_alias",fillcolor="red"]; // alias, dead ; PERMODULE-NEXT: M0_[[MAIN:[0-9]+]] [shape="record",label="main|extern (inst: 4, ffl: 00000)}",fillcolor="red"]; // function, dead ; PERMODULE-NEXT: // Edges: +; PERMODULE-NEXT: M0_[[MAIN_ALIAS]] -> M0_[[MAIN]] [style=dotted]; // alias ; PERMODULE-NEXT: } ; PERMODULE-NEXT: // Cross-module edges: -; PERMODULE-NEXT: 0 [label="@0"]; // defined externally -; PERMODULE-NEXT: M0_[[MAIN_ALIAS]] -> 0 [style=dotted]; // alias ; PERMODULE-NEXT: [[A:[0-9]+]] [label="A"]; // defined externally ; PERMODULE-NEXT: M0_[[MAIN]] -> [[A]] [style=dashed,color=forestgreen]; // const-ref ; PERMODULE-NEXT: [[FOO:[0-9]+]] [label="foo"]; // defined externally -- 2.7.4