From cf26f429e818a308251ca9a54b88394e98642ccd Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 8 Nov 2016 04:17:11 +0000 Subject: [PATCH] Bitcode: Decouple block info block state from reader. As proposed on llvm-dev: http://lists.llvm.org/pipermail/llvm-dev/2016-October/106630.html Move block info block state to a new class, BitstreamBlockInfo. Clients may set the block info for a particular cursor with the BitstreamCursor::setBlockInfo() method. At this point BitstreamReader is not much more than a container for an ArrayRef, so remove it and replace all uses with direct uses of memory buffers. Differential Revision: https://reviews.llvm.org/D26259 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@286207 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../ObjectFilePCHContainerOperations.h | 6 ++--- .../clang/Frontend/PCHContainerOperations.h | 12 ++++------ include/clang/Serialization/Module.h | 4 ++-- include/clang/Serialization/ModuleManager.h | 2 +- .../ObjectFilePCHContainerOperations.cpp | 14 +++++------ lib/Frontend/PCHContainerOperations.cpp | 6 ++--- lib/Frontend/SerializedDiagnosticReader.cpp | 11 +++++---- lib/Serialization/ASTReader.cpp | 23 ++++++++----------- lib/Serialization/GlobalModuleIndex.cpp | 9 ++------ lib/Serialization/ModuleManager.cpp | 5 ++-- 10 files changed, 39 insertions(+), 53 deletions(-) diff --git a/include/clang/CodeGen/ObjectFilePCHContainerOperations.h b/include/clang/CodeGen/ObjectFilePCHContainerOperations.h index 6437f4f9b4..67be6718fe 100644 --- a/include/clang/CodeGen/ObjectFilePCHContainerOperations.h +++ b/include/clang/CodeGen/ObjectFilePCHContainerOperations.h @@ -35,10 +35,8 @@ class ObjectFilePCHContainerWriter : public PCHContainerWriter { class ObjectFilePCHContainerReader : public PCHContainerReader { StringRef getFormat() const override { return "obj"; } - /// Initialize an llvm::BitstreamReader with the serialized - /// AST inside the PCH container Buffer. - void ExtractPCH(llvm::MemoryBufferRef Buffer, - llvm::BitstreamReader &StreamFile) const override; + /// Returns the serialized AST inside the PCH container Buffer. + StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override; }; } diff --git a/include/clang/Frontend/PCHContainerOperations.h b/include/clang/Frontend/PCHContainerOperations.h index 0c1b28e9a5..d323fb3e8b 100644 --- a/include/clang/Frontend/PCHContainerOperations.h +++ b/include/clang/Frontend/PCHContainerOperations.h @@ -17,7 +17,6 @@ namespace llvm { class raw_pwrite_stream; -class BitstreamReader; } using llvm::StringRef; @@ -63,10 +62,8 @@ public: /// Equivalent to the format passed to -fmodule-format= virtual StringRef getFormat() const = 0; - /// Initialize an llvm::BitstreamReader with the serialized AST inside - /// the PCH container Buffer. - virtual void ExtractPCH(llvm::MemoryBufferRef Buffer, - llvm::BitstreamReader &StreamFile) const = 0; + /// Returns the serialized AST inside the PCH container Buffer. + virtual StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const = 0; }; /// Implements write operations for a raw pass-through PCH container. @@ -87,9 +84,8 @@ class RawPCHContainerWriter : public PCHContainerWriter { class RawPCHContainerReader : public PCHContainerReader { StringRef getFormat() const override { return "raw"; } - /// Initialize an llvm::BitstreamReader with Buffer. - void ExtractPCH(llvm::MemoryBufferRef Buffer, - llvm::BitstreamReader &StreamFile) const override; + /// Simply returns the buffer contained in Buffer. + StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override; }; /// A registry of PCHContainerWriter and -Reader objects for different formats. diff --git a/include/clang/Serialization/Module.h b/include/clang/Serialization/Module.h index 4e4bcc49fa..58b3149d40 100644 --- a/include/clang/Serialization/Module.h +++ b/include/clang/Serialization/Module.h @@ -173,8 +173,8 @@ public: /// \brief The global bit offset (or base) of this module uint64_t GlobalBitOffset; - /// \brief The bitstream reader from which we'll read the AST file. - llvm::BitstreamReader StreamFile; + /// \brief The serialized bitstream data for this file. + StringRef Data; /// \brief The main bitstream cursor for the main block. llvm::BitstreamCursor Stream; diff --git a/include/clang/Serialization/ModuleManager.h b/include/clang/Serialization/ModuleManager.h index 08e7d4049e..1c4d88e979 100644 --- a/include/clang/Serialization/ModuleManager.h +++ b/include/clang/Serialization/ModuleManager.h @@ -175,7 +175,7 @@ public: OutOfDate }; - typedef ASTFileSignature(*ASTFileSignatureReader)(llvm::BitstreamReader &); + typedef ASTFileSignature(*ASTFileSignatureReader)(StringRef); /// \brief Attempts to create a new module and add it to the list of known /// modules. diff --git a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp index 42a9782219..baf7811eed 100644 --- a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp +++ b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @@ -312,8 +312,9 @@ ObjectFilePCHContainerWriter::CreatePCHContainerGenerator( CI, MainFileName, OutputFileName, std::move(OS), Buffer); } -void ObjectFilePCHContainerReader::ExtractPCH( - llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { +StringRef +ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const { + StringRef PCH; auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer); if (OFOrErr) { auto &OF = OFOrErr.get(); @@ -323,10 +324,8 @@ void ObjectFilePCHContainerReader::ExtractPCH( StringRef Name; Section.getName(Name); if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) { - StringRef Buf; - Section.getContents(Buf); - StreamFile = llvm::BitstreamReader(Buf); - return; + Section.getContents(PCH); + return PCH; } } } @@ -334,8 +333,9 @@ void ObjectFilePCHContainerReader::ExtractPCH( if (EIB.convertToErrorCode() == llvm::object::object_error::invalid_file_type) // As a fallback, treat the buffer as a raw AST. - StreamFile = llvm::BitstreamReader(Buffer); + PCH = Buffer.getBuffer(); else EIB.log(llvm::errs()); }); + return PCH; } diff --git a/lib/Frontend/PCHContainerOperations.cpp b/lib/Frontend/PCHContainerOperations.cpp index 2c867e7c64..eebebf327a 100644 --- a/lib/Frontend/PCHContainerOperations.cpp +++ b/lib/Frontend/PCHContainerOperations.cpp @@ -58,9 +58,9 @@ std::unique_ptr RawPCHContainerWriter::CreatePCHContainerGenerator( return llvm::make_unique(std::move(OS), Buffer); } -void RawPCHContainerReader::ExtractPCH( - llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { - StreamFile = llvm::BitstreamReader(Buffer); +StringRef +RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const { + return Buffer.getBuffer(); } PCHContainerOperations::PCHContainerOperations() { diff --git a/lib/Frontend/SerializedDiagnosticReader.cpp b/lib/Frontend/SerializedDiagnosticReader.cpp index aefd0f8fbb..c4461d452e 100644 --- a/lib/Frontend/SerializedDiagnosticReader.cpp +++ b/lib/Frontend/SerializedDiagnosticReader.cpp @@ -24,8 +24,8 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { if (!Buffer) return SDError::CouldNotLoad; - llvm::BitstreamReader StreamFile(**Buffer); - llvm::BitstreamCursor Stream(StreamFile); + llvm::BitstreamCursor Stream(**Buffer); + Optional BlockInfo; // Sniff for the signature. if (Stream.Read(8) != 'D' || @@ -41,10 +41,13 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { std::error_code EC; switch (Stream.ReadSubBlockID()) { - case llvm::bitc::BLOCKINFO_BLOCK_ID: - if (Stream.ReadBlockInfoBlock()) + case llvm::bitc::BLOCKINFO_BLOCK_ID: { + BlockInfo = Stream.ReadBlockInfoBlock(); + if (!BlockInfo) return SDError::MalformedBlockInfoBlock; + Stream.setBlockInfo(&*BlockInfo); continue; + } case BLOCK_META: if ((EC = readMetaBlock(Stream))) return EC; diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 650c46e9ff..5c875ee731 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -1711,7 +1711,7 @@ void ASTReader::ReadDefinedMacros() { BitstreamCursor &MacroCursor = I->MacroCursor; // If there was no preprocessor block, skip this file. - if (!MacroCursor.getBitStreamReader()) + if (MacroCursor.getBitcodeBytes().empty()) continue; BitstreamCursor Cursor = MacroCursor; @@ -3798,7 +3798,7 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, return Success; } -static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile); +static ASTFileSignature readASTFileSignature(StringRef PCH); /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'. static bool startsWithASTFileMagic(BitstreamCursor &Stream) { @@ -3885,8 +3885,7 @@ ASTReader::ReadASTCore(StringRef FileName, ModuleFile &F = *M; BitstreamCursor &Stream = F.Stream; - PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile); - Stream.init(&F.StreamFile); + Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); F.SizeInBits = F.Buffer->getBufferSize() * 8; // Sniff for the signature. @@ -4175,10 +4174,10 @@ void ASTReader::finalizeForWriting() { // Nothing to do for now. } -/// \brief Reads and return the signature record from \p StreamFile's control -/// block, or else returns 0. -static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){ - BitstreamCursor Stream(StreamFile); +/// \brief Reads and return the signature record from \p PCH's control block, or +/// else returns 0. +static ASTFileSignature readASTFileSignature(StringRef PCH) { + BitstreamCursor Stream(PCH); if (!startsWithASTFileMagic(Stream)) return 0; @@ -4216,9 +4215,7 @@ std::string ASTReader::getOriginalSourceFile( } // Initialize the stream - llvm::BitstreamReader StreamFile; - PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile); - BitstreamCursor Stream(StreamFile); + BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); // Sniff for the signature. if (!startsWithASTFileMagic(Stream)) { @@ -4318,9 +4315,7 @@ bool ASTReader::readASTFileControlBlock( } // Initialize the stream - llvm::BitstreamReader StreamFile; - PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile); - BitstreamCursor Stream(StreamFile); + BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); // Sniff for the signature. if (!startsWithASTFileMagic(Stream)) diff --git a/lib/Serialization/GlobalModuleIndex.cpp b/lib/Serialization/GlobalModuleIndex.cpp index e55ed40b22..9f986d54a9 100644 --- a/lib/Serialization/GlobalModuleIndex.cpp +++ b/lib/Serialization/GlobalModuleIndex.cpp @@ -245,11 +245,8 @@ GlobalModuleIndex::readIndex(StringRef Path) { return std::make_pair(nullptr, EC_NotFound); std::unique_ptr Buffer = std::move(BufferOrErr.get()); - /// \brief The bitstream reader from which we'll read the AST file. - llvm::BitstreamReader Reader(*Buffer); - /// \brief The main bitstream cursor for the main block. - llvm::BitstreamCursor Cursor(Reader); + llvm::BitstreamCursor Cursor(*Buffer); // Sniff for the signature. if (Cursor.Read(8) != 'B' || @@ -503,9 +500,7 @@ bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { } // Initialize the input stream - llvm::BitstreamReader InStreamFile; - PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), InStreamFile); - llvm::BitstreamCursor InStream(InStreamFile); + llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer)); // Sniff for the signature. if (InStream.Read(8) != 'C' || diff --git a/lib/Serialization/ModuleManager.cpp b/lib/Serialization/ModuleManager.cpp index 0a176f6fa8..e50dcaab2f 100644 --- a/lib/Serialization/ModuleManager.cpp +++ b/lib/Serialization/ModuleManager.cpp @@ -135,15 +135,14 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type, } // Initialize the stream. - PCHContainerRdr.ExtractPCH(ModuleEntry->Buffer->getMemBufferRef(), - ModuleEntry->StreamFile); + ModuleEntry->Data = PCHContainerRdr.ExtractPCH(*ModuleEntry->Buffer); } if (ExpectedSignature) { // If we've not read the control block yet, read the signature eagerly now // so that we can check it. if (!ModuleEntry->Signature) - ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile); + ModuleEntry->Signature = ReadSignature(ModuleEntry->Data); if (ModuleEntry->Signature != ExpectedSignature) { ErrorStr = ModuleEntry->Signature ? "signature mismatch" -- 2.50.1