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); Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ lib/Analysis/ModuleSummaryAnalysis.cpp @@ -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,13 @@ 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. ValueIdToValueInfoMap[ValueID] = std::make_pair( - TheIndex.getOrInsertValueInfo(ValueGUID, UseStrtab ? ValueName : ""), + TheIndex.getOrInsertValueInfo( + ValueGUID, + UseStrtab ? ValueName : TheIndex.saveString(ValueName.str())), OriginalNameID); } Index: test/ThinLTO/X86/autoupgrade.ll =================================================================== --- test/ThinLTO/X86/autoupgrade.ll +++ test/ThinLTO/X86/autoupgrade.ll @@ -12,6 +12,13 @@ ; CHECK: