llvm.org GIT mirror llvm / 51b7aa5
Bitcode: Introduce BitcodeModule::{has,get}Summary(). These are equivalent to hasGlobalValueSummary() and getModuleSummaryIndex(). Differential Revision: https://reviews.llvm.org/D27242 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288317 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
2 changed file(s) with 99 addition(s) and 108 deletion(s). Raw diff Collapse all Expand all
7676
7777 /// Read the entire bitcode module and return it.
7878 Expected> parseModule(LLVMContext &Context);
79
80 /// Check if the given bitcode buffer contains a summary block.
81 Expected hasSummary();
82
83 /// Parse the specified bitcode buffer, returning the module summary index.
84 Expected> getSummary();
7985 };
8086
8187 /// Returns a list of modules in the specified bitcode buffer.
761761 /// Class to manage reading and parsing function summary index bitcode
762762 /// files/sections.
763763 class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
764 /// Eventually points to the module index built during parsing.
765 ModuleSummaryIndex *TheIndex = nullptr;
766
767 /// Used to indicate whether caller only wants to check for the presence
768 /// of the global value summary bitcode section. All blocks are skipped,
769 /// but the SeenGlobalValSummary boolean is set.
770 bool CheckGlobalValSummaryPresenceOnly = false;
764 /// The module index built during parsing.
765 ModuleSummaryIndex *TheIndex;
771766
772767 /// Indicates whether we have encountered a global value summary section
773 /// yet during parsing, used when checking if file contains global value
774 /// summary section.
768 /// yet during parsing.
775769 bool SeenGlobalValSummary = false;
776770
777771 /// Indicates whether we have already parsed the VST, used for error checking.
802796
803797 public:
804798 ModuleSummaryIndexBitcodeReader(
805 BitstreamCursor Stream, bool CheckGlobalValSummaryPresenceOnly = false);
806
807 /// Check if the parser has encountered a summary section.
808 bool foundGlobalValSummary() { return SeenGlobalValSummary; }
809
810 /// \brief Main interface to parsing a bitcode buffer.
811 /// \returns true if an error occurred.
812 Error parseSummaryIndexInto(ModuleSummaryIndex *I, StringRef ModulePath);
799 BitstreamCursor Stream, ModuleSummaryIndex *TheIndex);
800
801 Error parseModule(StringRef ModulePath);
813802
814803 private:
815 Error parseModule(StringRef ModulePath);
816804 Error parseValueSymbolTable(
817805 uint64_t Offset,
818806 DenseMap &ValueIdToLinkageMap);
58945882 }
58955883
58965884 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5897 BitstreamCursor Cursor, bool CheckGlobalValSummaryPresenceOnly)
5898 : BitcodeReaderBase(std::move(Cursor)),
5899 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
5885 BitstreamCursor Cursor, ModuleSummaryIndex *TheIndex)
5886 : BitcodeReaderBase(std::move(Cursor)), TheIndex(TheIndex) {}
59005887
59015888 std::pair
59025889 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
60266013 return Error::success();
60276014
60286015 case BitstreamEntry::SubBlock:
6029 if (CheckGlobalValSummaryPresenceOnly) {
6030 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) {
6031 SeenGlobalValSummary = true;
6032 // No need to parse the rest since we found the summary.
6033 return Error::success();
6034 }
6035 if (Stream.SkipBlock())
6036 return error("Invalid record");
6037 continue;
6038 }
60396016 switch (Entry.ID) {
60406017 default: // Skip unknown content.
60416018 if (Stream.SkipBlock())
60966073 case bitc::MODULE_CODE_HASH: {
60976074 if (Record.size() != 5)
60986075 return error("Invalid hash length " + Twine(Record.size()).str());
6099 if (!TheIndex)
6100 break;
61016076 if (TheIndex->modulePaths().empty())
61026077 // We always seed the index with the module.
61036078 TheIndex->addModulePath(ModulePath, 0);
61156090 case bitc::MODULE_CODE_VSTOFFSET:
61166091 if (Record.size() < 1)
61176092 return error("Invalid record");
6118 VSTOffset = Record[0];
6093 // Note that we subtract 1 here because the offset is relative to one
6094 // word before the start of the identification or module block, which
6095 // was historically always the start of the regular bitcode header.
6096 VSTOffset = Record[0] - 1;
61196097 break;
61206098 // GLOBALVAR: [pointer type, isconst, initid,
61216099 // linkage, alignment, section, visibility, threadlocal,
64876465 llvm_unreachable("Exit infinite loop");
64886466 }
64896467
6490 // Parse the function info index from the bitcode streamer into the given index.
6491 Error ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto(
6492 ModuleSummaryIndex *I, StringRef ModulePath) {
6493 TheIndex = I;
6494
6495 // We expect a number of well-defined blocks, though we don't necessarily
6496 // need to understand them all.
6497 while (true) {
6498 if (Stream.AtEndOfStream()) {
6499 // We didn't really read a proper Module block.
6500 return error("Malformed block");
6501 }
6502
6503 BitstreamEntry Entry =
6504 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
6505
6506 if (Entry.Kind != BitstreamEntry::SubBlock)
6507 return error("Malformed block");
6508
6509 // If we see a MODULE_BLOCK, parse it to find the blocks needed for
6510 // building the function summary index.
6511 if (Entry.ID == bitc::MODULE_BLOCK_ID)
6512 return parseModule(ModulePath);
6513
6514 if (Stream.SkipBlock())
6515 return error("Invalid record");
6516 }
6517 }
6518
65196468 namespace {
65206469
65216470 // FIXME: This class is only here to support the transition to llvm::Error. It
66606609 return getModuleImpl(Context, false, ShouldLazyLoadMetadata);
66616610 }
66626611
6612 // Parse the specified bitcode buffer, returning the function info index.
6613 Expected> BitcodeModule::getSummary() {
6614 BitstreamCursor Stream(Buffer);
6615 Stream.JumpToBit(ModuleBit);
6616
6617 auto Index = llvm::make_unique();
6618 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Index.get());
6619
6620 if (Error Err = R.parseModule(ModuleIdentifier))
6621 return std::move(Err);
6622
6623 return std::move(Index);
6624 }
6625
6626 // Check if the given bitcode buffer contains a global value summary block.
6627 Expected BitcodeModule::hasSummary() {
6628 BitstreamCursor Stream(Buffer);
6629 Stream.JumpToBit(ModuleBit);
6630
6631 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
6632 return error("Invalid record");
6633
6634 while (true) {
6635 BitstreamEntry Entry = Stream.advance();
6636
6637 switch (Entry.Kind) {
6638 case BitstreamEntry::Error:
6639 return error("Malformed block");
6640 case BitstreamEntry::EndBlock:
6641 return false;
6642
6643 case BitstreamEntry::SubBlock:
6644 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID)
6645 return true;
6646
6647 // Ignore other sub-blocks.
6648 if (Stream.SkipBlock())
6649 return error("Malformed block");
6650 continue;
6651
6652 case BitstreamEntry::Record:
6653 Stream.skipRecord(Entry.ID);
6654 continue;
6655 }
6656 }
6657 }
6658
6659 static Expected getSingleModule(MemoryBufferRef Buffer) {
6660 Expected> MsOrErr = getBitcodeModuleList(Buffer);
6661 if (!MsOrErr)
6662 return MsOrErr.takeError();
6663
6664 if (MsOrErr->size() != 1)
6665 return error("Expected a single module");
6666
6667 return (*MsOrErr)[0];
6668 }
6669
66636670 Expected>
66646671 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer,
66656672 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
6666 Expected> MsOrErr = getBitcodeModuleList(Buffer);
6667 if (!MsOrErr)
6668 return MsOrErr.takeError();
6669
6670 if (MsOrErr->size() != 1)
6671 return error("Expected a single module");
6672
6673 return (*MsOrErr)[0].getLazyModule(Context, ShouldLazyLoadMetadata);
6673 Expected BM = getSingleModule(Buffer);
6674 if (!BM)
6675 return BM.takeError();
6676
6677 return BM->getLazyModule(Context, ShouldLazyLoadMetadata);
66746678 }
66756679
66766680 Expected>
66926696
66936697 Expected> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
66946698 LLVMContext &Context) {
6695 Expected> MsOrErr = getBitcodeModuleList(Buffer);
6696 if (!MsOrErr)
6697 return MsOrErr.takeError();
6698
6699 if (MsOrErr->size() != 1)
6700 return error("Expected a single module");
6701
6702 return (*MsOrErr)[0].parseModule(Context);
6699 Expected BM = getSingleModule(Buffer);
6700 if (!BM)
6701 return BM.takeError();
6702
6703 return BM->parseModule(Context);
67036704 }
67046705
67056706 Expected llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) {
67266727 return readIdentificationCode(*StreamOrErr);
67276728 }
67286729
6729 // Parse the specified bitcode buffer, returning the function info index.
67306730 Expected>
67316731 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {
6732 Expected StreamOrErr = initStream(Buffer);
6733 if (!StreamOrErr)
6734 return StreamOrErr.takeError();
6735
6736 ModuleSummaryIndexBitcodeReader R(std::move(*StreamOrErr));
6737
6738 auto Index = llvm::make_unique();
6739
6740 if (Error Err =
6741 R.parseSummaryIndexInto(Index.get(), Buffer.getBufferIdentifier()))
6742 return std::move(Err);
6743
6744 return std::move(Index);
6745 }
6746
6747 // Check if the given bitcode buffer contains a global value summary block.
6732 Expected BM = getSingleModule(Buffer);
6733 if (!BM)
6734 return BM.takeError();
6735
6736 return BM->getSummary();
6737 }
6738
67486739 Expected llvm::hasGlobalValueSummary(MemoryBufferRef Buffer) {
6749 Expected StreamOrErr = initStream(Buffer);
6750 if (!StreamOrErr)
6751 return StreamOrErr.takeError();
6752
6753 ModuleSummaryIndexBitcodeReader R(std::move(*StreamOrErr), true);
6754
6755 if (Error Err =
6756 R.parseSummaryIndexInto(nullptr, Buffer.getBufferIdentifier()))
6757 return std::move(Err);
6758
6759 return R.foundGlobalValSummary();
6760 }
6740 Expected BM = getSingleModule(Buffer);
6741 if (!BM)
6742 return BM.takeError();
6743
6744 return BM->hasSummary();
6745 }