Index: include/llvm/Analysis/ModuleSummaryAnalysis.h =================================================================== --- include/llvm/Analysis/ModuleSummaryAnalysis.h +++ include/llvm/Analysis/ModuleSummaryAnalysis.h @@ -14,7 +14,6 @@ #ifndef LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H #define LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H -#include "llvm/ADT/Optional.h" #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" @@ -53,7 +52,7 @@ /// Legacy wrapper pass to provide the ModuleSummaryIndex object. class ModuleSummaryIndexWrapperPass : public ModulePass { - Optional Index; + std::unique_ptr Index; public: static char ID; Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -25,8 +25,10 @@ #include "llvm/ADT/StringRef.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/ScaledNumber.h" +#include "llvm/Support/StringSaver.h" #include #include #include @@ -764,6 +766,11 @@ std::set CfiFunctionDefs; std::set CfiFunctionDecls; + // Used in cases where we want to record the name of a global, but + // don't have the string owned elsewhere (e.g. the Strtab on a module). + StringSaver Saver; + BumpPtrAllocator Alloc; + // YAML I/O support. friend yaml::MappingTraits; @@ -775,7 +782,7 @@ public: // See IsAnalysis variable comment. ModuleSummaryIndex(bool IsPerformingAnalysis) - : IsAnalysis(IsPerformingAnalysis) {} + : IsAnalysis(IsPerformingAnalysis), Saver(Alloc) {} bool isPerformingAnalysis() const { return IsAnalysis; } @@ -884,6 +891,11 @@ return ValueInfo(IsAnalysis, getOrInsertValuePtr(GUID)); } + // Save a string in the Index. Use before passing Name to + // getOrInsertValueInfo when the string isn't owned elsewhere (e.g. on the + // module's Strtab). + StringRef saveString(std::string String) { return Saver.save(String); } + /// Return a ValueInfo for \p GUID setting value \p Name. ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) { assert(!IsAnalysis); @@ -912,6 +924,12 @@ std::set &cfiFunctionDecls() { return CfiFunctionDecls; } const std::set &cfiFunctionDecls() const { return CfiFunctionDecls; } + /// Add a global value summary for a value. + void addGlobalValueSummary(const GlobalValue &GV, + std::unique_ptr Summary) { + addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary)); + } + /// Add a global value summary for a value of the given name. void addGlobalValueSummary(StringRef ValueName, std::unique_ptr Summary) { @@ -963,8 +981,7 @@ GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex = true) const { assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name"); - return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()), - PerModuleIndex); + return getGlobalValueSummary(GV.getGUID(), PerModuleIndex); } /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ lib/Analysis/ModuleSummaryAnalysis.cpp @@ -361,7 +361,7 @@ TypeCheckedLoadConstVCalls.takeVector()); if (NonRenamableLocal) CantBePromoted.insert(F.getGUID()); - Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary)); + Index.addGlobalValueSummary(F, std::move(FuncSummary)); } static void @@ -377,7 +377,7 @@ llvm::make_unique(Flags, RefEdges.takeVector()); if (NonRenamableLocal) CantBePromoted.insert(V.getGUID()); - Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary)); + Index.addGlobalValueSummary(V, std::move(GVarSummary)); } static void @@ -393,7 +393,7 @@ AS->setAliasee(AliaseeSummary); if (NonRenamableLocal) CantBePromoted.insert(A.getGUID()); - Index.addGlobalValueSummary(A.getName(), std::move(AS)); + Index.addGlobalValueSummary(A, std::move(AS)); } // Set LiveRoot flag on entries matching the given value name. @@ -455,7 +455,7 @@ /* NotEligibleToImport = */ true, /* Live = */ true, /* Local */ GV->isDSOLocal()); - CantBePromoted.insert(GlobalValue::getGUID(Name)); + CantBePromoted.insert(GV->getGUID()); // Create the appropriate summary type. if (Function *F = dyn_cast(GV)) { std::unique_ptr Summary = @@ -472,12 +472,12 @@ ArrayRef{}, ArrayRef{}, ArrayRef{}); - Index.addGlobalValueSummary(Name, std::move(Summary)); + Index.addGlobalValueSummary(*GV, std::move(Summary)); } else { std::unique_ptr Summary = llvm::make_unique(GVFlags, ArrayRef{}); - Index.addGlobalValueSummary(Name, std::move(Summary)); + Index.addGlobalValueSummary(*GV, std::move(Summary)); } }); } @@ -607,14 +607,14 @@ bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { auto &PSI = *getAnalysis().getPSI(); - Index = buildModuleSummaryIndex( + Index = llvm::make_unique(buildModuleSummaryIndex( M, [this](const Function &F) { return &(this->getAnalysis( *const_cast(&F)) .getBFI()); }, - &PSI); + &PSI)); return false; } Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -4831,11 +4831,14 @@ if (PrintSummaryGUIDs) dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " << ValueName << "\n"; - + // UseStrtab is false for legacy summary formats and value names are - // created on stack. We can't use them outside of parseValueSymbolTable. + // created on stack. In that case we save the name in a string saver in + // the index so that the value name can be recorded. ValueIdToValueInfoMap[ValueID] = std::make_pair( - TheIndex.getOrInsertValueInfo(ValueGUID, UseStrtab ? ValueName : ""), + TheIndex.getOrInsertValueInfo( + ValueGUID, + UseStrtab ? ValueName : TheIndex.saveString(ValueName.str())), OriginalNameID); } Index: lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriter.cpp +++ lib/Bitcode/Writer/BitcodeWriter.cpp @@ -3483,7 +3483,7 @@ void ModuleBitcodeWriterBase::writeModuleLevelReferences( const GlobalVariable &V, SmallVector &NameVals, unsigned FSModRefsAbbrev) { - auto VI = Index->getValueInfo(GlobalValue::getGUID(V.getName())); + auto VI = Index->getValueInfo(V.getGUID()); if (!VI || VI.getSummaryList().empty()) { // Only declarations should not have a summary (a declaration might however // have a summary if the def was in module level asm). @@ -3592,7 +3592,7 @@ if (!F.hasName()) report_fatal_error("Unexpected anonymous function when writing summary"); - ValueInfo VI = Index->getValueInfo(GlobalValue::getGUID(F.getName())); + ValueInfo VI = Index->getValueInfo(F.getGUID()); if (!VI || VI.getSummaryList().empty()) { // Only declarations should not have a summary (a declaration might // however have a summary if the def was in module level asm). Index: test/ThinLTO/X86/autoupgrade.ll =================================================================== --- test/ThinLTO/X86/autoupgrade.ll +++ test/ThinLTO/X86/autoupgrade.ll @@ -12,6 +12,13 @@ ; CHECK: