Skip to content

Commit 74d22dd

Browse files
committed
Bitcode: Make the summary reader responsible for merging. NFCI.
This is to prepare for an upcoming change which uses pointers instead of GUIDs to represent references. Differential Revision: https://reviews.llvm.org/D32469 llvm-svn: 301843
1 parent 864a363 commit 74d22dd

File tree

7 files changed

+82
-87
lines changed

7 files changed

+82
-87
lines changed

llvm/include/llvm/Bitcode/BitcodeReader.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ namespace llvm {
9393

9494
/// Parse the specified bitcode buffer, returning the module summary index.
9595
Expected<std::unique_ptr<ModuleSummaryIndex>> getSummary();
96+
97+
/// Parse the specified bitcode buffer and merge its module summary index
98+
/// into CombinedIndex.
99+
Error readSummary(ModuleSummaryIndex &CombinedIndex, unsigned ModuleId);
96100
};
97101

98102
/// Returns a list of modules in the specified bitcode buffer.
@@ -141,6 +145,11 @@ namespace llvm {
141145
Expected<std::unique_ptr<ModuleSummaryIndex>>
142146
getModuleSummaryIndex(MemoryBufferRef Buffer);
143147

148+
/// Parse the specified bitcode buffer and merge the index into CombinedIndex.
149+
Error readModuleSummaryIndex(MemoryBufferRef Buffer,
150+
ModuleSummaryIndex &CombinedIndex,
151+
unsigned ModuleId);
152+
144153
/// Parse the module summary index out of an IR file and return the module
145154
/// summary index object if found, or an empty summary if not. If Path refers
146155
/// to an empty file and the -ignore-empty-index-file cl::opt flag is passed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,13 +644,6 @@ class ModuleSummaryIndex {
644644
return It->second.second;
645645
}
646646

647-
/// Add the given per-module index into this module index/summary,
648-
/// assigning it the given module ID. Each module merged in should have
649-
/// a unique ID, necessary for consistent renaming of promoted
650-
/// static (local) variables.
651-
void mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
652-
uint64_t NextModuleId);
653-
654647
/// Convenience method for creating a promoted global name
655648
/// for the given value name of a local, and its original module's ID.
656649
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,20 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
713713
/// Original source file name recorded in a bitcode record.
714714
std::string SourceFileName;
715715

716+
/// The string identifier given to this module by the client, normally the
717+
/// path to the bitcode file.
718+
StringRef ModulePath;
719+
720+
/// For per-module summary indexes, the unique numerical identifier given to
721+
/// this module by the client.
722+
unsigned ModuleId;
723+
716724
public:
717725
ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
718-
ModuleSummaryIndex &TheIndex);
726+
ModuleSummaryIndex &TheIndex,
727+
StringRef ModulePath, unsigned ModuleId);
719728

720-
Error parseModule(StringRef ModulePath);
729+
Error parseModule();
721730

722731
private:
723732
void setValueGUID(uint64_t ValueID, StringRef ValueName,
@@ -730,11 +739,13 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
730739
std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
731740
bool IsOldProfileFormat,
732741
bool HasProfile);
733-
Error parseEntireSummary(StringRef ModulePath);
742+
Error parseEntireSummary();
734743
Error parseModuleStringTable();
735744

736745
std::pair<GlobalValue::GUID, GlobalValue::GUID>
737746
getGUIDFromValueId(unsigned ValueId);
747+
748+
ModulePathStringTableTy::iterator addThisModulePath();
738749
};
739750

740751
} // end anonymous namespace
@@ -4676,8 +4687,15 @@ std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
46764687
}
46774688

46784689
ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
4679-
BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex)
4680-
: BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex) {}
4690+
BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,
4691+
StringRef ModulePath, unsigned ModuleId)
4692+
: BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),
4693+
ModulePath(ModulePath), ModuleId(ModuleId) {}
4694+
4695+
ModulePathStringTableTy::iterator
4696+
ModuleSummaryIndexBitcodeReader::addThisModulePath() {
4697+
return TheIndex.addModulePath(ModulePath, ModuleId);
4698+
}
46814699

46824700
std::pair<GlobalValue::GUID, GlobalValue::GUID>
46834701
ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
@@ -4787,7 +4805,7 @@ Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
47874805
// Parse just the blocks needed for building the index out of the module.
47884806
// At the end of this routine the module Index is populated with a map
47894807
// from global value id to GlobalValueSummary objects.
4790-
Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) {
4808+
Error ModuleSummaryIndexBitcodeReader::parseModule() {
47914809
if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
47924810
return error("Invalid record");
47934811

@@ -4838,7 +4856,7 @@ Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) {
48384856
SeenValueSymbolTable = true;
48394857
}
48404858
SeenGlobalValSummary = true;
4841-
if (Error Err = parseEntireSummary(ModulePath))
4859+
if (Error Err = parseEntireSummary())
48424860
return Err;
48434861
break;
48444862
case bitc::MODULE_STRTAB_BLOCK_ID:
@@ -4871,12 +4889,7 @@ Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) {
48714889
case bitc::MODULE_CODE_HASH: {
48724890
if (Record.size() != 5)
48734891
return error("Invalid hash length " + Twine(Record.size()).str());
4874-
if (TheIndex.modulePaths().empty())
4875-
// We always seed the index with the module.
4876-
TheIndex.addModulePath(ModulePath, 0);
4877-
if (TheIndex.modulePaths().size() != 1)
4878-
return error("Don't expect multiple modules defined?");
4879-
auto &Hash = TheIndex.modulePaths().begin()->second.second;
4892+
auto &Hash = addThisModulePath()->second.second;
48804893
int Pos = 0;
48814894
for (auto &Val : Record) {
48824895
assert(!(Val >> 32) && "Unexpected high bits set");
@@ -4951,8 +4964,7 @@ std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallLi
49514964

49524965
// Eagerly parse the entire summary block. This populates the GlobalValueSummary
49534966
// objects in the index.
4954-
Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(
4955-
StringRef ModulePath) {
4967+
Error ModuleSummaryIndexBitcodeReader::parseEntireSummary() {
49564968
if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID))
49574969
return error("Invalid record");
49584970
SmallVector<uint64_t, 64> Record;
@@ -5057,7 +5069,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(
50575069
PendingTypeTestAssumeConstVCalls.clear();
50585070
PendingTypeCheckedLoadConstVCalls.clear();
50595071
auto GUID = getGUIDFromValueId(ValueID);
5060-
FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5072+
FS->setModulePath(addThisModulePath()->first());
50615073
FS->setOriginalName(GUID.second);
50625074
TheIndex.addGlobalValueSummary(GUID.first, std::move(FS));
50635075
break;
@@ -5077,13 +5089,14 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(
50775089
// string table section in the per-module index, we create a single
50785090
// module path string table entry with an empty (0) ID to take
50795091
// ownership.
5080-
AS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5092+
AS->setModulePath(addThisModulePath()->first());
50815093

50825094
GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first;
5083-
auto *AliaseeSummary = TheIndex.getGlobalValueSummary(AliaseeGUID);
5084-
if (!AliaseeSummary)
5095+
auto AliaseeInModule =
5096+
TheIndex.findSummaryInModule(AliaseeGUID, ModulePath);
5097+
if (!AliaseeInModule)
50855098
return error("Alias expects aliasee summary to be parsed");
5086-
AS->setAliasee(AliaseeSummary);
5099+
AS->setAliasee(AliaseeInModule);
50875100

50885101
auto GUID = getGUIDFromValueId(ValueID);
50895102
AS->setOriginalName(GUID.second);
@@ -5098,7 +5111,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(
50985111
std::vector<ValueInfo> Refs =
50995112
makeRefList(ArrayRef<uint64_t>(Record).slice(2));
51005113
auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs));
5101-
FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5114+
FS->setModulePath(addThisModulePath()->first());
51025115
auto GUID = getGUIDFromValueId(ValueID);
51035116
FS->setOriginalName(GUID.second);
51045117
TheIndex.addGlobalValueSummary(GUID.first, std::move(FS));
@@ -5482,15 +5495,31 @@ BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,
54825495
return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting);
54835496
}
54845497

5498+
// Parse the specified bitcode buffer and merge the index into CombinedIndex.
5499+
Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex,
5500+
unsigned ModuleId) {
5501+
BitstreamCursor Stream(Buffer);
5502+
Stream.JumpToBit(ModuleBit);
5503+
5504+
ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex,
5505+
ModuleIdentifier, ModuleId);
5506+
5507+
if (Error Err = R.parseModule())
5508+
return std::move(Err);
5509+
5510+
return Error::success();
5511+
}
5512+
54855513
// Parse the specified bitcode buffer, returning the function info index.
54865514
Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() {
54875515
BitstreamCursor Stream(Buffer);
54885516
Stream.JumpToBit(ModuleBit);
54895517

54905518
auto Index = llvm::make_unique<ModuleSummaryIndex>();
5491-
ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index);
5519+
ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index,
5520+
ModuleIdentifier, 0);
54925521

5493-
if (Error Err = R.parseModule(ModuleIdentifier))
5522+
if (Error Err = R.parseModule())
54945523
return std::move(Err);
54955524

54965525
return std::move(Index);
@@ -5600,6 +5629,16 @@ Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) {
56005629
return readIdentificationCode(*StreamOrErr);
56015630
}
56025631

5632+
Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer,
5633+
ModuleSummaryIndex &CombinedIndex,
5634+
unsigned ModuleId) {
5635+
Expected<BitcodeModule> BM = getSingleModule(Buffer);
5636+
if (!BM)
5637+
return BM.takeError();
5638+
5639+
return BM->readSummary(CombinedIndex, ModuleId);
5640+
}
5641+
56035642
Expected<std::unique_ptr<ModuleSummaryIndex>>
56045643
llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {
56055644
Expected<BitcodeModule> BM = getSingleModule(Buffer);

llvm/lib/IR/ModuleSummaryIndex.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,6 @@
1616
#include "llvm/ADT/StringMap.h"
1717
using namespace llvm;
1818

19-
// Create the combined module index/summary from multiple
20-
// per-module instances.
21-
void ModuleSummaryIndex::mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
22-
uint64_t NextModuleId) {
23-
if (Other->modulePaths().empty())
24-
return;
25-
26-
assert(Other->modulePaths().size() == 1 &&
27-
"Can only merge from an single-module index at that time");
28-
29-
StringRef OtherModPath = Other->modulePaths().begin()->first();
30-
StringRef ModPath = addModulePath(OtherModPath, NextModuleId,
31-
Other->getModuleHash(OtherModPath))
32-
->first();
33-
34-
for (auto &OtherGlobalValSummaryLists : *Other) {
35-
GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first;
36-
GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second;
37-
38-
// Assert that the value summary list only has one entry, since we shouldn't
39-
// have duplicate names within a single per-module index.
40-
assert(List.size() == 1);
41-
std::unique_ptr<GlobalValueSummary> Summary = std::move(List.front());
42-
43-
// Note the module path string ref was copied above and is still owned by
44-
// the original per-module index. Reset it to the new module path
45-
// string reference owned by the combined index.
46-
Summary->setModulePath(ModPath);
47-
48-
// Add new value summary to existing list. There may be duplicates when
49-
// combining GlobalValueMap entries, due to COMDAT values. Any local
50-
// values were given unique global IDs.
51-
addGlobalValueSummary(ValueGUID, std::move(Summary));
52-
}
53-
}
54-
5519
// Collect for the given module the list of function it defines
5620
// (GUID -> Summary).
5721
void ModuleSummaryIndex::collectDefinedFunctionsForModule(

llvm/lib/LTO/LTO.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,9 @@ Error LTO::addThinLTO(BitcodeModule BM,
591591
ArrayRef<InputFile::Symbol> Syms,
592592
const SymbolResolution *&ResI,
593593
const SymbolResolution *ResE) {
594-
Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr = BM.getSummary();
595-
if (!SummaryOrErr)
596-
return SummaryOrErr.takeError();
597-
ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr),
598-
ThinLTO.ModuleMap.size());
594+
if (Error Err =
595+
BM.readSummary(ThinLTO.CombinedIndex, ThinLTO.ModuleMap.size()))
596+
return Err;
599597

600598
for (const InputFile::Symbol &Sym : Syms) {
601599
assert(ResI != ResE);

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,23 +565,18 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
565565
* "thin-link".
566566
*/
567567
std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
568-
std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
568+
std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
569+
llvm::make_unique<ModuleSummaryIndex>();
569570
uint64_t NextModuleId = 0;
570571
for (auto &ModuleBuffer : Modules) {
571-
Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
572-
getModuleSummaryIndex(ModuleBuffer.getMemBuffer());
573-
if (!IndexOrErr) {
572+
if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
573+
*CombinedIndex, NextModuleId++)) {
574574
// FIXME diagnose
575575
logAllUnhandledErrors(
576-
IndexOrErr.takeError(), errs(),
576+
std::move(Err), errs(),
577577
"error: can't create module summary index for buffer: ");
578578
return nullptr;
579579
}
580-
if (CombinedIndex) {
581-
CombinedIndex->mergeFrom(std::move(*IndexOrErr), ++NextModuleId);
582-
} else {
583-
CombinedIndex = std::move(*IndexOrErr);
584-
}
585580
}
586581
return CombinedIndex;
587582
}

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,9 @@ static void createCombinedModuleSummaryIndex() {
331331
uint64_t NextModuleId = 0;
332332
for (auto &Filename : InputFilenames) {
333333
ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': ");
334-
std::unique_ptr<ModuleSummaryIndex> Index =
335-
ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename));
336-
// Skip files without a module summary.
337-
if (!Index)
338-
continue;
339-
CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
334+
std::unique_ptr<MemoryBuffer> MB =
335+
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Filename)));
336+
ExitOnErr(readModuleSummaryIndex(*MB, CombinedIndex, ++NextModuleId));
340337
}
341338
std::error_code EC;
342339
assert(!OutputFilename.empty());

0 commit comments

Comments
 (0)