llvm.org GIT mirror llvm / dd77c44
Add a method to the BitcodeReader to parse only the identification block Summary: Mimic parseTriple(); and exposes it to LTOModule.cpp Reviewers: dexonsmith, rafael Subscribers: llvm-commits From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252442 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 4 years ago
4 changed file(s) with 70 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
5252 std::string
5353 getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
5454 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
55
56 /// Read the header of the specified bitcode buffer and extract just the
57 /// producer string information. If successful, this returns a string. On
58 /// error, this returns "".
59 std::string getBitcodeProducerString(
60 MemoryBufferRef Buffer, LLVMContext &Context,
61 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
5562
5663 /// Read the specified bitcode file, returning the module.
5764 ErrorOr>
7272 /// triple.
7373 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
7474 StringRef triplePrefix);
75
76 /// Returns a string representing the producer identification stored in the
77 /// bitcode, or "" if the bitcode does not contains any.
78 ///
79 static std::string getProducerString(MemoryBuffer *Buffer);
7580
7681 /// Create a MemoryBuffer from a memory range with an optional name.
7782 static std::unique_ptr
269269 /// \brief Cheap mechanism to just extract module triple
270270 /// \returns true if an error occurred.
271271 ErrorOr parseTriple();
272
273 /// Cheap mechanism to just extract the identification block out of bitcode.
274 ErrorOr parseIdentificationBlock();
272275
273276 static uint64_t decodeSignRotatedValue(uint64_t V);
274277
37283731 }
37293732 }
37303733
3734 ErrorOr BitcodeReader::parseIdentificationBlock() {
3735 if (std::error_code EC = initStream(nullptr))
3736 return EC;
3737
3738 // Sniff for the signature.
3739 if (!hasValidBitcodeHeader(Stream))
3740 return error("Invalid bitcode signature");
3741
3742 // We expect a number of well-defined blocks, though we don't necessarily
3743 // need to understand them all.
3744 while (1) {
3745 BitstreamEntry Entry = Stream.advance();
3746 switch (Entry.Kind) {
3747 case BitstreamEntry::Error:
3748 return error("Malformed block");
3749 case BitstreamEntry::EndBlock:
3750 return std::error_code();
3751
3752 case BitstreamEntry::SubBlock:
3753 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3754 if (std::error_code EC = parseBitcodeVersion())
3755 return EC;
3756 return ProducerIdentification;
3757 }
3758 // Ignore other sub-blocks.
3759 if (Stream.SkipBlock())
3760 return error("Malformed block");
3761 continue;
3762 case BitstreamEntry::Record:
3763 Stream.skipRecord(Entry.ID);
3764 continue;
3765 }
3766 }
3767 }
3768
37313769 /// Parse metadata attachments.
37323770 std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
37333771 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
58345872 return Triple.get();
58355873 }
58365874
5875 std::string
5876 llvm::getBitcodeProducerString(MemoryBufferRef Buffer, LLVMContext &Context,
5877 DiagnosticHandlerFunction DiagnosticHandler) {
5878 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5879 BitcodeReader R(Buf.release(), Context, DiagnosticHandler);
5880 ErrorOr ProducerString = R.parseIdentificationBlock();
5881 if (ProducerString.getError())
5882 return "";
5883 return ProducerString.get();
5884 }
5885
58375886 // Parse the specified bitcode buffer, returning the function info index.
58385887 // If IsLazy is false, parse the entire function summary into
58395888 // the index. Otherwise skip the function summary section, and only create
9090 return StringRef(Triple).startswith(TriplePrefix);
9191 }
9292
93 std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
94 ErrorOr BCOrErr =
95 IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
96 if (!BCOrErr)
97 return "";
98 LLVMContext Context;
99 return getBitcodeProducerString(*BCOrErr, Context);
100 }
101
93102 LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
94103 std::string &errMsg) {
95104 ErrorOr> BufferOrErr =