llvm.org GIT mirror llvm / 4a4558e
Use std::unique_ptr to manage the DataStreamer in bitcode parsing. We were already deleting it, this just makes it explicit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239867 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
8 changed file(s) with 55 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
4040 /// Read the header of the specified stream and prepare for lazy
4141 /// deserialization and streaming of function bodies.
4242 ErrorOr> getStreamedBitcodeModule(
43 StringRef Name, DataStreamer *Streamer, LLVMContext &Context,
43 StringRef Name, std::unique_ptr Streamer,
44 LLVMContext &Context,
4445 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
4546
4647 /// Read the header of the specified bitcode buffer and extract just the
1616 #ifndef LLVM_SUPPORT_DATASTREAM_H
1717 #define LLVM_SUPPORT_DATASTREAM_H
1818
19 #include
1920 #include
2021
2122 namespace llvm {
2930 virtual ~DataStreamer();
3031 };
3132
32 DataStreamer *getDataFileStreamer(const std::string &Filename,
33 std::string *Err);
34
33 std::unique_ptr getDataFileStreamer(const std::string &Filename,
34 std::string *Err);
3535 }
3636
3737 #endif // LLVM_SUPPORT_DATASTREAM_H_
2323 /// setKnownObjectSize methods which are not applicable to non-streamed objects.
2424 class StreamingMemoryObject : public MemoryObject {
2525 public:
26 StreamingMemoryObject(DataStreamer *streamer);
26 StreamingMemoryObject(std::unique_ptr Streamer);
2727 uint64_t getExtent() const override;
2828 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
2929 uint64_t Address) const override;
135135 std::unique_ptr Buffer;
136136 std::unique_ptr StreamFile;
137137 BitstreamCursor Stream;
138 DataStreamer *Streamer;
138 bool IsStreamed;
139139 uint64_t NextUnreadBit = 0;
140140 bool SeenValueSymbolTable = false;
141141
222222
223223 BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
224224 DiagnosticHandlerFunction DiagnosticHandler);
225 BitcodeReader(DataStreamer *Streamer, LLVMContext &Context,
225 BitcodeReader(LLVMContext &Context,
226226 DiagnosticHandlerFunction DiagnosticHandler);
227227 ~BitcodeReader() override { freeState(); }
228228
240240
241241 /// \brief Main interface to parsing a bitcode buffer.
242242 /// \returns true if an error occurred.
243 std::error_code parseBitcodeInto(Module *M,
243 std::error_code parseBitcodeInto(std::unique_ptr Streamer,
244 Module *M,
244245 bool ShouldLazyLoadMetadata = false);
245246
246247 /// \brief Cheap mechanism to just extract module triple
367368 std::error_code parseMetadataAttachment(Function &F);
368369 ErrorOr parseModuleTriple();
369370 std::error_code parseUseLists();
370 std::error_code initStream();
371 std::error_code initStream(std::unique_ptr Streamer);
371372 std::error_code initStreamFromBuffer();
372 std::error_code initLazyStream();
373 std::error_code initLazyStream(std::unique_ptr Streamer);
373374 std::error_code findFunctionInStream(
374375 Function *F,
375376 DenseMap::iterator DeferredFunctionInfoIterator);
425426 DiagnosticHandlerFunction DiagnosticHandler)
426427 : Context(Context),
427428 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
428 Buffer(Buffer), Streamer(nullptr), ValueList(Context),
429 Buffer(Buffer), IsStreamed(false), ValueList(Context),
429430 MDValueList(Context) {}
430431
431 BitcodeReader::BitcodeReader(DataStreamer *Streamer, LLVMContext &Context,
432 BitcodeReader::BitcodeReader(LLVMContext &Context,
432433 DiagnosticHandlerFunction DiagnosticHandler)
433434 : Context(Context),
434435 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
435 Buffer(nullptr), Streamer(Streamer), ValueList(Context),
436 Buffer(nullptr), IsStreamed(true), ValueList(Context),
436437 MDValueList(Context) {}
437438
438439 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
27772778 // the bitcode. If the bitcode file is old, the symbol table will be
27782779 // at the end instead and will not have been seen yet. In this case,
27792780 // just finish the parse now.
2780 if (Streamer && SeenValueSymbolTable) {
2781 if (IsStreamed && SeenValueSymbolTable) {
27812782 NextUnreadBit = Stream.GetCurrentBitNo();
27822783 return std::error_code();
27832784 }
30283029 if (!isProto) {
30293030 Func->setIsMaterializable(true);
30303031 FunctionsWithBodies.push_back(Func);
3031 if (Streamer)
3032 if (IsStreamed)
30323033 DeferredFunctionInfo[Func] = 0;
30333034 }
30343035 break;
30763077 }
30773078 }
30783079
3079 std::error_code BitcodeReader::parseBitcodeInto(Module *M,
3080 bool ShouldLazyLoadMetadata) {
3080 std::error_code
3081 BitcodeReader::parseBitcodeInto(std::unique_ptr Streamer,
3082 Module *M, bool ShouldLazyLoadMetadata) {
30813083 TheModule = M;
30823084
3083 if (std::error_code EC = initStream())
3085 if (std::error_code EC = initStream(std::move(Streamer)))
30843086 return EC;
30853087
30863088 // Sniff for the signature.
31533155 }
31543156
31553157 ErrorOr BitcodeReader::parseTriple() {
3156 if (std::error_code EC = initStream())
3158 if (std::error_code EC = initStream(nullptr))
31573159 return EC;
31583160
31593161 // Sniff for the signature.
43984400 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
43994401 // If its position is recorded as 0, its body is somewhere in the stream
44004402 // but we haven't seen it yet.
4401 if (DFII->second == 0 && Streamer)
4403 if (DFII->second == 0 && IsStreamed)
44024404 if (std::error_code EC = findFunctionInStream(F, DFII))
44034405 return EC;
44044406
45134515 return IdentifiedStructTypes;
45144516 }
45154517
4516 std::error_code BitcodeReader::initStream() {
4518 std::error_code
4519 BitcodeReader::initStream(std::unique_ptr Streamer) {
45174520 if (Streamer)
4518 return initLazyStream();
4521 return initLazyStream(std::move(Streamer));
45194522 return initStreamFromBuffer();
45204523 }
45214524
45384541 return std::error_code();
45394542 }
45404543
4541 std::error_code BitcodeReader::initLazyStream() {
4544 std::error_code
4545 BitcodeReader::initLazyStream(std::unique_ptr Streamer) {
45424546 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
45434547 // see it.
4544 auto OwnedBytes = llvm::make_unique(Streamer);
4548 auto OwnedBytes =
4549 llvm::make_unique(std::move(Streamer));
45454550 StreamingMemoryObject &Bytes = *OwnedBytes;
45464551 StreamFile = llvm::make_unique(std::move(OwnedBytes));
45474552 Stream.init(&*StreamFile);
46164621 };
46174622
46184623 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
4619 if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
4624 if (std::error_code EC =
4625 R->parseBitcodeInto(nullptr, M.get(), ShouldLazyLoadMetadata))
46204626 return cleanupOnError(EC);
46214627
46224628 if (!WillMaterializeAll)
46354641 DiagnosticHandler, ShouldLazyLoadMetadata);
46364642 }
46374643
4638 ErrorOr>
4639 llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
4640 LLVMContext &Context,
4641 DiagnosticHandlerFunction DiagnosticHandler) {
4644 ErrorOr> llvm::getStreamedBitcodeModule(
4645 StringRef Name, std::unique_ptr Streamer,
4646 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) {
46424647 std::unique_ptr M = make_unique(Name, Context);
4643 BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler);
4648 BitcodeReader *R = new BitcodeReader(Context, DiagnosticHandler);
46444649 M->setMaterializer(R);
4645 if (std::error_code EC = R->parseBitcodeInto(M.get()))
4650 if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get()))
46464651 return EC;
46474652 return std::move(M);
46484653 }
1515
1616 #include "llvm/Support/DataStream.h"
1717 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/STLExtras.h"
1819 #include "llvm/Support/FileSystem.h"
1920 #include "llvm/Support/Program.h"
2021 #include
7273
7374 }
7475
75 namespace llvm {
76 DataStreamer *getDataFileStreamer(const std::string &Filename,
77 std::string *StrError) {
78 DataFileStreamer *s = new DataFileStreamer();
76 std::unique_ptr
77 llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
78 std::unique_ptr s = make_unique();
7979 if (std::error_code e = s->OpenFile(Filename)) {
8080 *StrError = std::string("Could not open ") + Filename + ": " +
8181 e.message() + "\n";
8282 return nullptr;
8383 }
84 return s;
84 return std::move(s);
8585 }
86
87 }
122122 return new RawMemoryObject(Start, End);
123123 }
124124
125 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
126 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
127 ObjectSize(0), EOFReached(false) {
128 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
125 StreamingMemoryObject::StreamingMemoryObject(
126 std::unique_ptr Streamer)
127 : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
128 BytesSkipped(0), ObjectSize(0), EOFReached(false) {
129 BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
129130 }
130131 }
147147 std::unique_ptr M;
148148
149149 // Use the bitcode streaming interface
150 DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
150 std::unique_ptr Streamer =
151 getDataFileStreamer(InputFilename, &ErrorMessage);
151152 if (Streamer) {
152153 std::string DisplayFilename;
153154 if (InputFilename == "-")
155156 else
156157 DisplayFilename = InputFilename;
157158 ErrorOr> MOrErr =
158 getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
159 getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context);
159160 M = std::move(*MOrErr);
160161 M->materializeAllPermanently();
161162 } else {
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/ADT/STLExtras.h"
910 #include "llvm/Support/StreamingMemoryObject.h"
1011 #include "gtest/gtest.h"
1112 #include
2223 }
2324
2425 TEST(StreamingMemoryObject, Test) {
25 auto *DS = new NullDataStreamer();
26 StreamingMemoryObject O(DS);
26 auto DS = make_unique();
27 StreamingMemoryObject O(std::move(DS));
2728 EXPECT_TRUE(O.isValidAddress(32 * 1024));
2829 }
2930
3031 TEST(StreamingMemoryObject, TestSetKnownObjectSize) {
31 auto *DS = new NullDataStreamer();
32 StreamingMemoryObject O(DS);
32 auto DS = make_unique();
33 StreamingMemoryObject O(std::move(DS));
3334 uint8_t Buf[32];
3435 EXPECT_EQ((uint64_t) 16, O.readBytes(Buf, 16, 0));
3536 O.setKnownObjectSize(24);