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

Side by Side Diff: lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp

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 nits. 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 | « include/llvm/Bitcode/NaCl/NaClBitstreamReader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- NaClBitstreamReader.cpp --------------------------------------------===// 1 //===- NaClBitstreamReader.cpp --------------------------------------------===//
2 // NaClBitstreamReader implementation 2 // NaClBitstreamReader implementation
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
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 //===----------------------------------------------------------------------===// 42 //===----------------------------------------------------------------------===//
43 // NaClBitstreamCursor implementation 43 // NaClBitstreamCursor implementation
44 //===----------------------------------------------------------------------===// 44 //===----------------------------------------------------------------------===//
45 45
46 void NaClBitstreamCursor::ErrorHandler:: 46 void NaClBitstreamCursor::ErrorHandler::
47 Fatal(const std::string &ErrorMessage) const { 47 Fatal(const std::string &ErrorMessage) const {
48 // Default implementation is simply print message, and the bit where 48 // Default implementation is simply print message, and the bit where
49 // the error occurred. 49 // the error occurred.
50 std::string Buffer; 50 std::string Buffer;
51 raw_string_ostream StrBuf(Buffer); 51 raw_string_ostream StrBuf(Buffer);
52 naclbitc::ErrorAt(StrBuf, naclbitc::Fatal, Cursor.GetCurrentBitNo()) 52 naclbitc::ErrorAt(StrBuf, naclbitc::Fatal, getCurrentBitNo()) << ErrorMessage;
53 << ErrorMessage;
54 report_fatal_error(StrBuf.str()); 53 report_fatal_error(StrBuf.str());
55 } 54 }
56 55
57 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const { 56 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const {
58 std::string Buffer; 57 std::string Buffer;
59 raw_string_ostream StrBuf(Buffer); 58 raw_string_ostream StrBuf(Buffer);
60 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record"; 59 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record";
61 ErrHandler->Fatal(StrBuf.str()); 60 ErrHandler->Fatal(StrBuf.str());
62 } 61 }
63 62
64 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const { 63 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const {
65 std::string Buffer; 64 std::string Buffer;
66 raw_string_ostream StrBuf(Buffer); 65 raw_string_ostream StrBuf(Buffer);
67 StrBuf << "Invalid jump to bit " << BitNo; 66 StrBuf << "Invalid jump to bit " << BitNo;
68 ErrHandler->Fatal(StrBuf.str()); 67 ErrHandler->Fatal(StrBuf.str());
69 } 68 }
70 69
71 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter 70 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
72 /// the block, and return true if the block has an error. 71 /// the block, and return true if the block has an error.
73 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { 72 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
74 const bool IsFixed = true; 73 const bool IsFixed = true;
75 NaClBitcodeSelectorAbbrev 74 NaClBitcodeSelectorAbbrev
76 CodeAbbrev(IsFixed, ReadVBR(naclbitc::CodeLenWidth)); 75 CodeAbbrev(IsFixed, ReadVBR(naclbitc::CodeLenWidth));
77 BlockScope.push_back(Block(&BitStream->getOrCreateBlockInfo(BlockID), 76 BlockScope.push_back(Block(BitStream->getBlockInfo(BlockID), CodeAbbrev));
78 CodeAbbrev));
79 SkipToFourByteBoundary(); 77 SkipToFourByteBoundary();
80 unsigned NumWords = Read(naclbitc::BlockSizeWidth); 78 unsigned NumWords = Read(naclbitc::BlockSizeWidth);
81 if (NumWordsP) *NumWordsP = NumWords; 79 if (NumWordsP) *NumWordsP = NumWords;
82 80
83 // Validate that this block is sane. 81 // Validate that this block is sane.
84 if (BlockScope.back().getCodeAbbrev().NumBits == 0 || AtEndOfStream()) 82 if (BlockScope.back().getCodeAbbrev().NumBits == 0 || AtEndOfStream())
85 return true; 83 return true;
86 84
87 return false; 85 return false;
88 } 86 }
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 continue; 321 continue;
324 } 322 }
325 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); 323 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3));
326 if (NaClBitCodeAbbrevOp::hasValue(E)) { 324 if (NaClBitCodeAbbrevOp::hasValue(E)) {
327 ReadVBR64(5); 325 ReadVBR64(5);
328 } 326 }
329 } 327 }
330 SkipToByteBoundaryIfAligned(); 328 SkipToByteBoundaryIfAligned();
331 } 329 }
332 330
331 namespace {
332
333 unsigned ValidBlockIDs[] = {
334 naclbitc::BLOCKINFO_BLOCK_ID,
335 naclbitc::CONSTANTS_BLOCK_ID,
336 naclbitc::FUNCTION_BLOCK_ID,
337 naclbitc::GLOBALVAR_BLOCK_ID,
338 naclbitc::MODULE_BLOCK_ID,
339 naclbitc::TOP_LEVEL_BLOCKID,
340 naclbitc::TYPE_BLOCK_ID_NEW,
341 naclbitc::VALUE_SYMTAB_BLOCK_ID
342 };
343
344 } // end of anonymous namespace
345
346 NaClBitstreamReader::BlockInfoRecordsMap::
347 BlockInfoRecordsMap() : IsFrozen(false) {
348 for (size_t BlockID : ValidBlockIDs) {
349 std::unique_ptr<BlockInfo> Info(new BlockInfo(BlockID));
350 KnownInfos.emplace(BlockID, std::move(Info));
351 }
352 }
353
354 NaClBitstreamReader::BlockInfo * NaClBitstreamReader::BlockInfoRecordsMap::
355 getOrCreateUnknownBlockInfo(unsigned BlockID) {
356 std::unique_lock<std::mutex> Lock(UnknownBlockInfoLock);
357 while (true) {
358 auto Pos = UnknownInfos.find(BlockID);
359 if (Pos != UnknownInfos.end())
360 return Pos->second.get();
361 // Install, then let next iteration find.
362 std::unique_ptr<BlockInfo> Info(new BlockInfo(BlockID));
363 UnknownInfos.emplace(BlockID, std::move(Info));
364 }
365 }
366
367 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock::
368 UpdateLock(BlockInfoRecordsMap &BlockInfoRecords)
369 : BlockInfoRecords(BlockInfoRecords),
370 Lock(BlockInfoRecords.UpdateRecordsLock) {}
371
372 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock::
373 ~UpdateLock() {
374 if (BlockInfoRecords.freeze())
375 report_fatal_error("Global abbreviations block frozen while building.");
376 }
377
333 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { 378 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) {
334 // If this is the second stream to get to the block info block, skip it. 379 // If this is the second read of the block info block, skip it.
335 if (BitStream->HasReadBlockInfoBlock) 380 if (BitStream->BlockInfoRecords->isFrozen())
336 return SkipBlock(); 381 return SkipBlock();
337 382
338 BitStream->HasReadBlockInfoBlock = true; 383 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock
339 384 Lock(*BitStream->BlockInfoRecords);
340 unsigned NumWords; 385 unsigned NumWords;
341 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; 386 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true;
342 387
343 if (Listener) Listener->BeginBlockInfoBlock(NumWords); 388 if (Listener) Listener->BeginBlockInfoBlock(NumWords);
344 389
345 NaClBitcodeRecordVector Record; 390 NaClBitcodeRecordVector Record;
346 Block &CurBlock = BlockScope.back(); 391 Block &CurBlock = BlockScope.back();
347 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = 392 NaClBitstreamReader::AbbrevList *UpdateAbbrevs =
348 &CurBlock.GlobalAbbrevs->getAbbrevs(); 393 &CurBlock.GlobalAbbrevs->getAbbrevs();
349 bool FoundSetBID = false; 394 bool FoundSetBID = false;
(...skipping 30 matching lines...) Expand all
380 // Read a record. 425 // Read a record.
381 Record.clear(); 426 Record.clear();
382 switch (readRecord(Entry.ID, Record)) { 427 switch (readRecord(Entry.ID, Record)) {
383 default: 428 default:
384 // No other records should be found! 429 // No other records should be found!
385 return true; 430 return true;
386 case naclbitc::BLOCKINFO_CODE_SETBID: 431 case naclbitc::BLOCKINFO_CODE_SETBID:
387 if (Record.size() < 1) return true; 432 if (Record.size() < 1) return true;
388 FoundSetBID = true; 433 FoundSetBID = true;
389 UpdateAbbrevs = 434 UpdateAbbrevs =
390 &BitStream->getOrCreateBlockInfo((unsigned)Record[0]).getAbbrevs(); 435 &BitStream->getBlockInfo((unsigned)Record[0])->getAbbrevs();
391 if (Listener) { 436 if (Listener) {
392 Listener->Values = Record; 437 Listener->Values = Record;
393 Listener->SetBID(); 438 Listener->SetBID();
394 } 439 }
395 break; 440 break;
396 } 441 }
397 } 442 }
398 } 443 }
OLDNEW
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitstreamReader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698