Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: include/llvm/Bitcode/NaCl/NaClBitstreamReader.h

Issue 1843673003: Modify bitstream reader to allow parallel parses. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix InfosMap in BlockInfoRecordsMap. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===// 1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===//
2 // Low-level bitstream reader interface 2 // Low-level bitstream reader interface
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 // 10 //
11 // This header defines the BitstreamReader class. This class can be used to 11 // This header defines the BitstreamReader class. This class can be used to
12 // read an arbitrary bitstream, regardless of its contents. 12 // read an arbitrary bitstream, regardless of its contents.
13 // 13 //
14 //===----------------------------------------------------------------------===// 14 //===----------------------------------------------------------------------===//
15 15
16 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H 16 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H
17 #define LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H 17 #define LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H
18 18
19 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" 20 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
21 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" 21 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
22 #include "llvm/Support/Endian.h" 22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/StreamingMemoryObject.h" 23 #include "llvm/Support/StreamingMemoryObject.h"
24 #include <atomic>
24 #include <climits> 25 #include <climits>
26 #include <mutex>
27 #include <unordered_map>
25 #include <vector> 28 #include <vector>
26 29
27 namespace llvm { 30 namespace llvm {
28 31
29 class Deserializer; 32 class Deserializer;
30 class NaClBitstreamCursor; 33 class NaClBitstreamCursor;
31 34
32 namespace naclbitc { 35 namespace naclbitc {
33 36
34 /// Returns the Bit as a Byte:BitInByte string. 37 /// Returns the Bit as a Byte:BitInByte string.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 BlockInfo(const BlockInfo&) = default; 126 BlockInfo(const BlockInfo&) = default;
124 unsigned getBlockID() const { return BlockID; } 127 unsigned getBlockID() const { return BlockID; }
125 void setBlockID(unsigned ID) { BlockID = ID; } 128 void setBlockID(unsigned ID) { BlockID = ID; }
126 AbbrevList &getAbbrevs() { return Abbrevs; } 129 AbbrevList &getAbbrevs() { return Abbrevs; }
127 ~BlockInfo() {} 130 ~BlockInfo() {}
128 private: 131 private:
129 unsigned BlockID; 132 unsigned BlockID;
130 AbbrevList Abbrevs; 133 AbbrevList Abbrevs;
131 }; 134 };
132 135
136 // Holds the global abbreviations in the BlockInfo block of the bitcode file.
137 // Sharing is used to allow parallel parses. Share by using std::share_ptr's
138 // and std::shared_from_this().
139 //
140 // Note: The BlockInfo block must be parsed before sharing of the
141 // BlockInfoRecordsMap. Therefore, before changing to a parallel parse, the
142 // BlockInfoRecordsMap must be frozen. Failure to do so, can lead to
143 // unexpected behaviour.
144 //
145 // In practice, this means that only function blocks can be parsed in
146 // parallel.
147 class BlockInfoRecordsMap :
148 public std::enable_shared_from_this<BlockInfoRecordsMap> {
149 friend class NaClBitstreamReader;
150 BlockInfoRecordsMap(const BlockInfoRecordsMap&) = delete;
151 BlockInfoRecordsMap &operator=(const BlockInfoRecordsMap&) = delete;
152 public:
153 using InfosMap = std::unordered_map<unsigned, std::unique_ptr<BlockInfo>>;
154
155 static std::shared_ptr<BlockInfoRecordsMap> create() {
Jim Stichnoth 2016/03/30 17:57:02 The somewhat unwieldy "std::shared_ptr<BlockInfoRe
Karl 2016/03/30 19:47:30 Done.
156 return std::shared_ptr<BlockInfoRecordsMap>(new BlockInfoRecordsMap());
157 }
158 ~BlockInfoRecordsMap() = default;
159
160 bool isFrozen() const {
161 return IsFrozen.load();
162 }
163
164 // Returns true if already frozen.
165 bool freeze() {
166 return IsFrozen.exchange(true);
167 }
168
169 BlockInfo *getBlockInfo(unsigned BlockID) {
170 auto Pos = KnownInfos.find(BlockID);
171 if (Pos != KnownInfos.end())
172 return Pos->second.get();
173 return getOrCreateUnknownBlockInfo(BlockID);
174 }
175
176 // Locks the BlockInfoRecordsMap for the lifetime of the UpdateLock. Used
177 // to allow the parsing of a BlockInfo block, and install global
178 // abbreviations.
179 //
180 // Verifies that the BlockInfoRecordsMap didn't get frozen during the
181 // instance's lifetime as a safety precaution. That is, it checks that no
182 // bitstream reader was created to share the global abbreviations before the
183 // global abbreviations are defined.
184 class UpdateLock {
185 UpdateLock() = delete;
186 UpdateLock &operator=(const UpdateLock&) = delete;
Jim Stichnoth 2016/03/30 17:57:02 Delete the default copy ctor?
Karl 2016/03/30 19:47:30 Done.
187 public:
188 explicit UpdateLock(BlockInfoRecordsMap &BlockInfoRecords);
189 ~UpdateLock();
190 private:
191 // The BlockInfoRecordsMap to update.
192 BlockInfoRecordsMap &BlockInfoRecords;
193 // The locked mutex from BlockInfoRecordsMap;
194 std::unique_lock<std::mutex> Lock;
195 };
196
197 private:
198 // The set of known BlockInfo's. This map is prepopulated so that fast
199 // lookup can be performed thread safe (i.e. without using a lock).
200 InfosMap KnownInfos;
201 // The set of unknown BlockInfo's. This map is to handle unknown (and hence,
202 // invalid) PNaCl bitcode files. This map is updated incrementally, and uses
203 // UnknownLock to make it thread safe.
204 InfosMap UnknownInfos;
205 // True if the known BlockInfo blocks are frozen (i.e. the bitstream reader
206 // will ignore the BlockInfo block).
207 std::atomic_bool IsFrozen;
208 // Lock to use to update this data structure.
209 std::mutex UpdateRecordsLock;
210 // Lock to get/create an unknonw block info.
211 std::mutex UnknownBlockInfoLock;
212
213 BlockInfoRecordsMap();
214
215 BlockInfo *getOrCreateUnknownBlockInfo(unsigned BlockID);
216 };
217
133 private: 218 private:
134 friend class NaClBitstreamCursor; 219 friend class NaClBitstreamCursor;
135 220
136 std::unique_ptr<MemoryObject> BitcodeBytes; 221 std::unique_ptr<MemoryObject> BitcodeBytes;
137 222
138 std::vector<BlockInfo> BlockInfoRecords; 223 std::shared_ptr<BlockInfoRecordsMap> BlockInfoRecords;
139
140 // True if the BlockInfo block has been read.
141 bool HasReadBlockInfoBlock = false;
142 224
143 /// \brief Holds the offset of the first byte after the header. 225 /// \brief Holds the offset of the first byte after the header.
144 size_t InitialAddress; 226 size_t InitialAddress;
145 227
146 // True if filler should be added to byte align records. 228 // True if filler should be added to byte align records.
147 bool AlignBitcodeRecords = false; 229 bool AlignBitcodeRecords = false;
148 NaClBitstreamReader(const NaClBitstreamReader&) = delete; 230 NaClBitstreamReader(const NaClBitstreamReader&) = delete;
149 void operator=(const NaClBitstreamReader&) = delete; 231 void operator=(const NaClBitstreamReader&) = delete;
150 232
151 233
152 void initFromHeader(NaClBitcodeHeader &Header) { 234 void initFromHeader(NaClBitcodeHeader &Header) {
153 InitialAddress = Header.getHeaderSize(); 235 InitialAddress = Header.getHeaderSize();
154 AlignBitcodeRecords = Header.getAlignBitcodeRecords(); 236 AlignBitcodeRecords = Header.getAlignBitcodeRecords();
155 } 237 }
156 238
157 public: 239 public:
158 /// Read stream from sequence of bytes [Start .. End) after parsing 240 /// Read stream from sequence of bytes [Start .. End) after parsing
159 /// the given bitcode header. 241 /// the given bitcode header.
160 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, 242 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End,
161 NaClBitcodeHeader &Header) 243 NaClBitcodeHeader &Header)
162 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)) { 244 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)),
245 BlockInfoRecords(BlockInfoRecordsMap::create()) {
163 initFromHeader(Header); 246 initFromHeader(Header);
164 } 247 }
165 248
166 /// Read stream from Bytes, after parsing the given bitcode header. 249 /// Read stream from Bytes, after parsing the given bitcode header.
167 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header) 250 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header)
168 : BitcodeBytes(Bytes) { 251 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()) {
169 initFromHeader(Header); 252 initFromHeader(Header);
170 } 253 }
171 254
172 /// Read stream from bytes, starting at the given initial address. 255 /// Read stream from bytes, starting at the given initial address.
173 /// Provides simple API for unit testing. 256 /// Provides simple API for unit testing.
174 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress) 257 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress)
175 : BitcodeBytes(Bytes), InitialAddress(InitialAddress) { 258 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()),
259 InitialAddress(InitialAddress) {
176 } 260 }
177 261
262 /// Read stream from sequence of bytes [Start .. End), using the global
263 /// abbreviations of the given bitstream reader. Assumes that [Start .. End)
264 /// is copied from Reader's memory object.
265 NaClBitstreamReader(const unsigned char *Start,
266 const unsigned char *End, NaClBitstreamReader *Reader)
267 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)),
268 BlockInfoRecords(Reader->BlockInfoRecords), InitialAddress(0)
269 { BlockInfoRecords->freeze(); }
270
178 // Returns the memory object that is being read. 271 // Returns the memory object that is being read.
179 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } 272 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
180 273
181 ~NaClBitstreamReader() {} 274 ~NaClBitstreamReader() {}
182 275
183 /// \brief Returns the initial address (after the header) of the input stream. 276 /// \brief Returns the initial address (after the header) of the input stream.
184 size_t getInitialAddress() const { 277 size_t getInitialAddress() const {
185 return InitialAddress; 278 return InitialAddress;
186 } 279 }
187 280
188 //===--------------------------------------------------------------------===// 281 //===--------------------------------------------------------------------===//
189 // Block Manipulation 282 // Block Manipulation
190 //===--------------------------------------------------------------------===// 283 //===--------------------------------------------------------------------===//
191 284
192 /// If there is block info for the specified ID, return it, otherwise return 285 BlockInfo *getBlockInfo(unsigned BlockID) {
193 /// null. 286 return BlockInfoRecords->getBlockInfo(BlockID);
194 const BlockInfo *getBlockInfo(unsigned BlockID) const {
195 // Common case, the most recent entry matches BlockID.
196 if (!BlockInfoRecords.empty() &&
197 BlockInfoRecords.back().getBlockID() == BlockID)
198 return &BlockInfoRecords.back();
199
200 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
201 i != e; ++i)
202 if (BlockInfoRecords[i].getBlockID() == BlockID)
203 return &BlockInfoRecords[i];
204 return nullptr;
205 }
206
207 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
208 if (const BlockInfo *BI = getBlockInfo(BlockID))
209 return *const_cast<BlockInfo*>(BI);
210
211 // Otherwise, add a new record.
212 BlockInfoRecords.push_back(BlockInfo(BlockID));
213 return BlockInfoRecords.back();
214 } 287 }
215 }; 288 };
216 289
217 /// When advancing through a bitstream cursor, each advance can discover a few 290 /// When advancing through a bitstream cursor, each advance can discover a few
218 /// different kinds of entries: 291 /// different kinds of entries:
219 struct NaClBitstreamEntry { 292 struct NaClBitstreamEntry {
220 enum { 293 enum {
221 Error, // Malformed bitcode was found. 294 Error, // Malformed bitcode was found.
222 EndBlock, // We've reached the end of the current block, (or the end of the 295 EndBlock, // We've reached the end of the current block, (or the end of the
223 // file, which is treated like a series of EndBlock records. 296 // file, which is treated like a series of EndBlock records.
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 : ErrHandler(new ErrorHandler(*this)) { init(&R); } 479 : ErrHandler(new ErrorHandler(*this)) { init(&R); }
407 480
408 void init(NaClBitstreamReader *R) { 481 void init(NaClBitstreamReader *R) {
409 freeState(); 482 freeState();
410 BitStream = R; 483 BitStream = R;
411 NextChar = (BitStream == nullptr) ? 0 : BitStream->getInitialAddress(); 484 NextChar = (BitStream == nullptr) ? 0 : BitStream->getInitialAddress();
412 Size = 0; 485 Size = 0;
413 BitsInCurWord = 0; 486 BitsInCurWord = 0;
414 if (BitStream) { 487 if (BitStream) {
415 BlockScope.push_back( 488 BlockScope.push_back(
416 Block(&BitStream->getOrCreateBlockInfo(naclbitc::TOP_LEVEL_BLOCKID))); 489 Block(BitStream->getBlockInfo(naclbitc::TOP_LEVEL_BLOCKID)));
417 } 490 }
418 } 491 }
419 492
420 ~NaClBitstreamCursor() { 493 ~NaClBitstreamCursor() {
421 freeState(); 494 freeState();
422 } 495 }
423 496
424 void freeState() { 497 void freeState() {
425 while (!BlockScope.empty()) 498 while (!BlockScope.empty())
426 BlockScope.pop_back(); 499 BlockScope.pop_back();
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 NaClBitstreamEntry Entry = advance(Flags, 0); 589 NaClBitstreamEntry Entry = advance(Flags, 0);
517 if (Entry.Kind != NaClBitstreamEntry::SubBlock) 590 if (Entry.Kind != NaClBitstreamEntry::SubBlock)
518 return Entry; 591 return Entry;
519 592
520 // If we found a sub-block, just skip over it and check the next entry. 593 // If we found a sub-block, just skip over it and check the next entry.
521 if (SkipBlock()) 594 if (SkipBlock())
522 return NaClBitstreamEntry::getError(); 595 return NaClBitstreamEntry::getError();
523 } 596 }
524 } 597 }
525 598
599 /// Returns the starting byte of the word containing BitNo.
600 uintptr_t getStartWordByteForBit(uint64_t BitNo) const {
601 return uintptr_t(BitNo/8) & ~(sizeof(word_t)-1);
Jim Stichnoth 2016/03/30 17:57:02 Can these "8" magic constants be replaced with CHA
Karl 2016/03/30 19:47:30 Fixed all occurrences in file.
602 }
603
604 /// Returns the index of BitNo within the word it appears in.
605 unsigned getWordBitNo(uint64_t BitNo) const {
606 return unsigned(BitNo & (sizeof(word_t)*8-1));
607 }
608
609 /// Returns the ending byte of the word containing BitNo.
610 uintptr_t getEndWordByteForBit(uint64_t BitNo) const {
611 return getStartWordByteForBit(BitNo) +
612 (getWordBitNo(BitNo)
613 ? sizeof(word_t)
614 : 0);
615 }
616
617 /// Fills Buffer[Size] using bytes at Address (in the memory object being
618 /// read). Returns number of bytes filled (less than Size if at end of memory
619 /// object).
620 uint64_t fillBuffer(uint8_t *Buffer, size_t Size, size_t Address) const {
621 return BitStream->getBitcodeBytes().readBytes(Buffer, Size, Address);
622 }
623
526 /// Reset the stream to the specified bit number. 624 /// Reset the stream to the specified bit number.
527 void JumpToBit(uint64_t BitNo) { 625 void JumpToBit(uint64_t BitNo) {
528 uintptr_t ByteNo = uintptr_t(BitNo/8) & ~(sizeof(word_t)-1); 626 uintptr_t ByteNo = getStartWordByteForBit(BitNo);
Jim Stichnoth 2016/03/30 17:57:02 const?
Karl 2016/03/30 19:47:30 Done.
529 unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1)); 627 unsigned WordBitNo = getWordBitNo(BitNo);
530 if (!canSkipToPos(ByteNo)) 628 if (!canSkipToPos(ByteNo))
531 reportInvalidJumpToBit(BitNo); 629 reportInvalidJumpToBit(BitNo);
532 630
533 // Move the cursor to the right word. 631 // Move the cursor to the right word.
534 NextChar = ByteNo; 632 NextChar = ByteNo;
535 BitsInCurWord = 0; 633 BitsInCurWord = 0;
536 634
537 // Skip over any bits that are already consumed. 635 // Skip over any bits that are already consumed.
538 if (WordBitNo) 636 if (WordBitNo)
539 Read(WordBitNo); 637 Read(WordBitNo);
540 } 638 }
541 639
542 void fillCurWord() { 640 void fillCurWord() {
543 assert(Size == 0 || NextChar < (unsigned)Size); 641 assert(Size == 0 || NextChar < (unsigned)Size);
544 642
545 // Read the next word from the stream. 643 // Read the next word from the stream.
546 uint8_t Array[sizeof(word_t)] = {0}; 644 uint8_t Array[sizeof(word_t)] = {0};
547 645
548 uint64_t BytesRead = 646 uint64_t BytesRead = fillBuffer(Array, sizeof(Array), NextChar);
549 BitStream->getBitcodeBytes().readBytes(Array, sizeof(Array), NextChar);
550 647
551 // If we run out of data, stop at the end of the stream. 648 // If we run out of data, stop at the end of the stream.
552 if (BytesRead == 0) { 649 if (BytesRead == 0) {
553 Size = NextChar; 650 Size = NextChar;
554 return; 651 return;
555 } 652 }
556 653
557 CurWord = 654 CurWord =
558 support::endian::read<word_t, support::little, support::unaligned>( 655 support::endian::read<word_t, support::little, support::unaligned>(
559 Array); 656 Array);
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, 882 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord,
786 // except that no abbreviation is built. 883 // except that no abbreviation is built.
787 void SkipAbbrevRecord(); 884 void SkipAbbrevRecord();
788 885
789 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); 886 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener);
790 }; 887 };
791 888
792 } // End llvm namespace 889 } // End llvm namespace
793 890
794 #endif 891 #endif
OLDNEW
« no previous file with comments | « no previous file | lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698