llvm.org GIT mirror llvm / 10dbf12
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301843 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
7 changed file(s) with 84 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
9292
9393 /// Parse the specified bitcode buffer, returning the module summary index.
9494 Expected> getSummary();
95
96 /// Parse the specified bitcode buffer and merge its module summary index
97 /// into CombinedIndex.
98 Error readSummary(ModuleSummaryIndex &CombinedIndex, unsigned ModuleId);
9599 };
96100
97101 /// Returns a list of modules in the specified bitcode buffer.
139143 /// Parse the specified bitcode buffer, returning the module summary index.
140144 Expected>
141145 getModuleSummaryIndex(MemoryBufferRef Buffer);
146
147 /// Parse the specified bitcode buffer and merge the index into CombinedIndex.
148 Error readModuleSummaryIndex(MemoryBufferRef Buffer,
149 ModuleSummaryIndex &CombinedIndex,
150 unsigned ModuleId);
142151
143152 /// Parse the module summary index out of an IR file and return the module
144153 /// summary index object if found, or an empty summary if not. If Path refers
643643 return It->second.second;
644644 }
645645
646 /// Add the given per-module index into this module index/summary,
647 /// assigning it the given module ID. Each module merged in should have
648 /// a unique ID, necessary for consistent renaming of promoted
649 /// static (local) variables.
650 void mergeFrom(std::unique_ptr Other,
651 uint64_t NextModuleId);
652
653646 /// Convenience method for creating a promoted global name
654647 /// for the given value name of a local, and its original module's ID.
655648 static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
712712 /// Original source file name recorded in a bitcode record.
713713 std::string SourceFileName;
714714
715 /// The string identifier given to this module by the client, normally the
716 /// path to the bitcode file.
717 StringRef ModulePath;
718
719 /// For per-module summary indexes, the unique numerical identifier given to
720 /// this module by the client.
721 unsigned ModuleId;
722
715723 public:
716724 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
717 ModuleSummaryIndex &TheIndex);
718
719 Error parseModule(StringRef ModulePath);
725 ModuleSummaryIndex &TheIndex,
726 StringRef ModulePath, unsigned ModuleId);
727
728 Error parseModule();
720729
721730 private:
722731 void setValueGUID(uint64_t ValueID, StringRef ValueName,
729738 std::vector makeCallList(ArrayRef Record,
730739 bool IsOldProfileFormat,
731740 bool HasProfile);
732 Error parseEntireSummary(StringRef ModulePath);
741 Error parseEntireSummary();
733742 Error parseModuleStringTable();
734743
735744 std::pair
736745 getGUIDFromValueId(unsigned ValueId);
746
747 ModulePathStringTableTy::iterator addThisModulePath();
737748 };
738749
739750 } // end anonymous namespace
46754686 }
46764687
46774688 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
4678 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex)
4679 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex) {}
4689 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,
4690 StringRef ModulePath, unsigned ModuleId)
4691 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),
4692 ModulePath(ModulePath), ModuleId(ModuleId) {}
4693
4694 ModulePathStringTableTy::iterator
4695 ModuleSummaryIndexBitcodeReader::addThisModulePath() {
4696 return TheIndex.addModulePath(ModulePath, ModuleId);
4697 }
46804698
46814699 std::pair
46824700 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
47864804 // Parse just the blocks needed for building the index out of the module.
47874805 // At the end of this routine the module Index is populated with a map
47884806 // from global value id to GlobalValueSummary objects.
4789 Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) {
4807 Error ModuleSummaryIndexBitcodeReader::parseModule() {
47904808 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
47914809 return error("Invalid record");
47924810
48374855 SeenValueSymbolTable = true;
48384856 }
48394857 SeenGlobalValSummary = true;
4840 if (Error Err = parseEntireSummary(ModulePath))
4858 if (Error Err = parseEntireSummary())
48414859 return Err;
48424860 break;
48434861 case bitc::MODULE_STRTAB_BLOCK_ID:
48704888 case bitc::MODULE_CODE_HASH: {
48714889 if (Record.size() != 5)
48724890 return error("Invalid hash length " + Twine(Record.size()).str());
4873 if (TheIndex.modulePaths().empty())
4874 // We always seed the index with the module.
4875 TheIndex.addModulePath(ModulePath, 0);
4876 if (TheIndex.modulePaths().size() != 1)
4877 return error("Don't expect multiple modules defined?");
4878 auto &Hash = TheIndex.modulePaths().begin()->second.second;
4891 auto &Hash = addThisModulePath()->second.second;
48794892 int Pos = 0;
48804893 for (auto &Val : Record) {
48814894 assert(!(Val >> 32) && "Unexpected high bits set");
49504963
49514964 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
49524965 // objects in the index.
4953 Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(
4954 StringRef ModulePath) {
4966 Error ModuleSummaryIndexBitcodeReader::parseEntireSummary() {
49554967 if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID))
49564968 return error("Invalid record");
49574969 SmallVector Record;
50565068 PendingTypeTestAssumeConstVCalls.clear();
50575069 PendingTypeCheckedLoadConstVCalls.clear();
50585070 auto GUID = getGUIDFromValueId(ValueID);
5059 FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5071 FS->setModulePath(addThisModulePath()->first());
50605072 FS->setOriginalName(GUID.second);
50615073 TheIndex.addGlobalValueSummary(GUID.first, std::move(FS));
50625074 break;
50765088 // string table section in the per-module index, we create a single
50775089 // module path string table entry with an empty (0) ID to take
50785090 // ownership.
5079 AS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5091 AS->setModulePath(addThisModulePath()->first());
50805092
50815093 GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first;
5082 auto *AliaseeSummary = TheIndex.getGlobalValueSummary(AliaseeGUID);
5083 if (!AliaseeSummary)
5094 auto AliaseeInModule =
5095 TheIndex.findSummaryInModule(AliaseeGUID, ModulePath);
5096 if (!AliaseeInModule)
50845097 return error("Alias expects aliasee summary to be parsed");
5085 AS->setAliasee(AliaseeSummary);
5098 AS->setAliasee(AliaseeInModule);
50865099
50875100 auto GUID = getGUIDFromValueId(ValueID);
50885101 AS->setOriginalName(GUID.second);
50975110 std::vector Refs =
50985111 makeRefList(ArrayRef(Record).slice(2));
50995112 auto FS = llvm::make_unique(Flags, std::move(Refs));
5100 FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first());
5113 FS->setModulePath(addThisModulePath()->first());
51015114 auto GUID = getGUIDFromValueId(ValueID);
51025115 FS->setOriginalName(GUID.second);
51035116 TheIndex.addGlobalValueSummary(GUID.first, std::move(FS));
54815494 return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting);
54825495 }
54835496
5497 // Parse the specified bitcode buffer and merge the index into CombinedIndex.
5498 Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex,
5499 unsigned ModuleId) {
5500 BitstreamCursor Stream(Buffer);
5501 Stream.JumpToBit(ModuleBit);
5502
5503 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex,
5504 ModuleIdentifier, ModuleId);
5505
5506 if (Error Err = R.parseModule())
5507 return std::move(Err);
5508
5509 return Error::success();
5510 }
5511
54845512 // Parse the specified bitcode buffer, returning the function info index.
54855513 Expected> BitcodeModule::getSummary() {
54865514 BitstreamCursor Stream(Buffer);
54875515 Stream.JumpToBit(ModuleBit);
54885516
54895517 auto Index = llvm::make_unique();
5490 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index);
5491
5492 if (Error Err = R.parseModule(ModuleIdentifier))
5518 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index,
5519 ModuleIdentifier, 0);
5520
5521 if (Error Err = R.parseModule())
54935522 return std::move(Err);
54945523
54955524 return std::move(Index);
55975626 return StreamOrErr.takeError();
55985627
55995628 return readIdentificationCode(*StreamOrErr);
5629 }
5630
5631 Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer,
5632 ModuleSummaryIndex &CombinedIndex,
5633 unsigned ModuleId) {
5634 Expected BM = getSingleModule(Buffer);
5635 if (!BM)
5636 return BM.takeError();
5637
5638 return BM->readSummary(CombinedIndex, ModuleId);
56005639 }
56015640
56025641 Expected>
1414 #include "llvm/IR/ModuleSummaryIndex.h"
1515 #include "llvm/ADT/StringMap.h"
1616 using namespace llvm;
17
18 // Create the combined module index/summary from multiple
19 // per-module instances.
20 void ModuleSummaryIndex::mergeFrom(std::unique_ptr Other,
21 uint64_t NextModuleId) {
22 if (Other->modulePaths().empty())
23 return;
24
25 assert(Other->modulePaths().size() == 1 &&
26 "Can only merge from an single-module index at that time");
27
28 StringRef OtherModPath = Other->modulePaths().begin()->first();
29 StringRef ModPath = addModulePath(OtherModPath, NextModuleId,
30 Other->getModuleHash(OtherModPath))
31 ->first();
32
33 for (auto &OtherGlobalValSummaryLists : *Other) {
34 GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first;
35 GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second;
36
37 // Assert that the value summary list only has one entry, since we shouldn't
38 // have duplicate names within a single per-module index.
39 assert(List.size() == 1);
40 std::unique_ptr Summary = std::move(List.front());
41
42 // Note the module path string ref was copied above and is still owned by
43 // the original per-module index. Reset it to the new module path
44 // string reference owned by the combined index.
45 Summary->setModulePath(ModPath);
46
47 // Add new value summary to existing list. There may be duplicates when
48 // combining GlobalValueMap entries, due to COMDAT values. Any local
49 // values were given unique global IDs.
50 addGlobalValueSummary(ValueGUID, std::move(Summary));
51 }
52 }
5317
5418 // Collect for the given module the list of function it defines
5519 // (GUID -> Summary).
590590 ArrayRef Syms,
591591 const SymbolResolution *&ResI,
592592 const SymbolResolution *ResE) {
593 Expected> SummaryOrErr = BM.getSummary();
594 if (!SummaryOrErr)
595 return SummaryOrErr.takeError();
596 ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr),
597 ThinLTO.ModuleMap.size());
593 if (Error Err =
594 BM.readSummary(ThinLTO.CombinedIndex, ThinLTO.ModuleMap.size()))
595 return Err;
598596
599597 for (const InputFile::Symbol &Sym : Syms) {
600598 assert(ResI != ResE);
564564 * "thin-link".
565565 */
566566 std::unique_ptr ThinLTOCodeGenerator::linkCombinedIndex() {
567 std::unique_ptr CombinedIndex;
567 std::unique_ptr CombinedIndex =
568 llvm::make_unique();
568569 uint64_t NextModuleId = 0;
569570 for (auto &ModuleBuffer : Modules) {
570 Expected> IndexOrErr =
571 getModuleSummaryIndex(ModuleBuffer.getMemBuffer());
572 if (!IndexOrErr) {
571 if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
572 *CombinedIndex, NextModuleId++)) {
573573 // FIXME diagnose
574574 logAllUnhandledErrors(
575 IndexOrErr.takeError(), errs(),
575 std::move(Err), errs(),
576576 "error: can't create module summary index for buffer: ");
577577 return nullptr;
578 }
579 if (CombinedIndex) {
580 CombinedIndex->mergeFrom(std::move(*IndexOrErr), ++NextModuleId);
581 } else {
582 CombinedIndex = std::move(*IndexOrErr);
583578 }
584579 }
585580 return CombinedIndex;
330330 uint64_t NextModuleId = 0;
331331 for (auto &Filename : InputFilenames) {
332332 ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': ");
333 std::unique_ptr Index =
334 ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename));
335 // Skip files without a module summary.
336 if (!Index)
337 continue;
338 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
333 std::unique_ptr MB =
334 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Filename)));
335 ExitOnErr(readModuleSummaryIndex(*MB, CombinedIndex, ++NextModuleId));
339336 }
340337 std::error_code EC;
341338 assert(!OutputFilename.empty());