diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 851f25091402..5566eaafa2be 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/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 @@ -766,6 +768,11 @@ private: 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; @@ -777,7 +784,7 @@ private: public: // See HaveGVs variable comment. - ModuleSummaryIndex(bool HaveGVs) : HaveGVs(HaveGVs) {} + ModuleSummaryIndex(bool HaveGVs) : HaveGVs(HaveGVs), Saver(Alloc) {} bool haveGVs() const { return HaveGVs; } @@ -886,6 +893,11 @@ public: return ValueInfo(HaveGVs, 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(!HaveGVs); diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp index 6a589b4ff258..17dae20ce3a1 100644 --- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -607,14 +607,14 @@ ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { auto &PSI = *getAnalysis().getPSI(); - Index = buildModuleSummaryIndex( + Index.emplace(buildModuleSummaryIndex( M, [this](const Function &F) { return &(this->getAnalysis( *const_cast(&F)) .getBFI()); }, - &PSI); + &PSI)); return false; } diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index ce2eb0774507..b0e040755163 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -4836,11 +4836,14 @@ void ModuleSummaryIndexBitcodeReader::setValueGUID( 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); } diff --git a/llvm/test/ThinLTO/X86/autoupgrade.ll b/llvm/test/ThinLTO/X86/autoupgrade.ll index 2188d031c439..77f7eb0e0e02 100644 --- a/llvm/test/ThinLTO/X86/autoupgrade.ll +++ b/llvm/test/ThinLTO/X86/autoupgrade.ll @@ -12,6 +12,13 @@ ; CHECK: