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

Side by Side Diff: lib/Bitcode/NaCl/Analysis/NaClCompress.cpp

Issue 1122423005: Add (unsupported experimental) feature allowing byte aligned bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix issues in patch 2. Created 5 years, 7 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
OLDNEW
1 //===-- NaClCompress.cpp - Bitcode (abbrev) compression -----------------===// 1 //===-- NaClCompress.cpp - Bitcode (abbrev) compression -----------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // Analyzes the data in memory buffer, and determines what 10 // Analyzes the data in memory buffer, and determines what
(...skipping 1140 matching lines...) Expand 10 before | Expand all | Expand 10 after
1151 MemoryBuffer *MemBuf, 1151 MemoryBuffer *MemBuf,
1152 raw_ostream &Output, 1152 raw_ostream &Output,
1153 BlockAbbrevsMapType &BlockAbbrevsMap) { 1153 BlockAbbrevsMapType &BlockAbbrevsMap) {
1154 // TODO(kschimpf): The current code only extracts abbreviations 1154 // TODO(kschimpf): The current code only extracts abbreviations
1155 // defined in the bitcode file. This code needs to be updated to 1155 // defined in the bitcode file. This code needs to be updated to
1156 // collect data distributions and figure out better (global) 1156 // collect data distributions and figure out better (global)
1157 // abbreviations to use. 1157 // abbreviations to use.
1158 1158
1159 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); 1159 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
1160 const unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize(); 1160 const unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
1161 const unsigned char *HeaderPtr = BufPtr;
1161 1162
1162 // First read header and verify it is good. 1163 // First read header and verify it is good.
1163 NaClBitcodeHeader Header; 1164 NaClBitcodeHeader Header;
1164 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) 1165 if (Header.Read(HeaderPtr, EndBufPtr))
1165 return Error("Invalid PNaCl bitcode header"); 1166 return Error("Invalid PNaCl bitcode header");
1167 if (!Header.IsSupported()) {
1168 errs() << Header.Unsupported();
1169 if (!Header.IsReadable())
1170 return Error("Invalid PNaCl bitcode header");
1171 }
1166 1172
1167 // Create a bitstream reader to read the bitcode file. 1173 // Create a bitstream reader to read the bitcode file.
1168 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr); 1174 NaClBitstreamReader StreamFile(
1175 getNonStreamedMemoryObject(BufPtr, EndBufPtr), Header);
1169 NaClBitstreamCursor Stream(StreamFile); 1176 NaClBitstreamCursor Stream(StreamFile);
1170 1177
1171 // Parse the the bitcode file. 1178 // Parse the the bitcode file.
1172 NaClAnalyzeParser Parser(Flags, Stream, BlockAbbrevsMap); 1179 NaClAnalyzeParser Parser(Flags, Stream, BlockAbbrevsMap);
1173 while (!Stream.AtEndOfStream()) { 1180 while (!Stream.AtEndOfStream()) {
1174 if (Parser.Parse()) return true; 1181 if (Parser.Parse()) return true;
1175 } 1182 }
1176 1183
1177 if (Flags.ShowAbbreviationFrequencies) 1184 if (Flags.ShowAbbreviationFrequencies)
1178 displayAbbreviationFrequencies(Output, Parser.BlockDist, BlockAbbrevsMap); 1185 displayAbbreviationFrequencies(Output, Parser.BlockDist, BlockAbbrevsMap);
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 return Parser.ParseThisBlock(); 1381 return Parser.ParseThisBlock();
1375 } 1382 }
1376 1383
1377 // Reads the bitcode in MemBuf, using the abbreviations in AbbrevsMap, 1384 // Reads the bitcode in MemBuf, using the abbreviations in AbbrevsMap,
1378 // and queues the selected abbrevations for each record into 1385 // and queues the selected abbrevations for each record into
1379 // AbbrevsQueueMap. 1386 // AbbrevsQueueMap.
1380 static bool chooseAbbrevs(MemoryBuffer *MemBuf, BlockAbbrevsMapType &AbbrevsMap, 1387 static bool chooseAbbrevs(MemoryBuffer *MemBuf, BlockAbbrevsMapType &AbbrevsMap,
1381 BlockAbbrevsQueueMap &AbbrevsQueueMap) { 1388 BlockAbbrevsQueueMap &AbbrevsQueueMap) {
1382 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); 1389 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
1383 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); 1390 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
1391 const unsigned char *HeaderPtr = BufPtr;
1384 1392
1385 // Read header. No verification is needed since AnalyzeBitcode has 1393 // Read header. No verification is needed since AnalyzeBitcode has
1386 // already checked it. 1394 // already checked it.
1387 NaClBitcodeHeader Header; 1395 NaClBitcodeHeader Header;
1388 if (Header.Read(BufPtr, EndBufPtr)) 1396 if (Header.Read(HeaderPtr, EndBufPtr))
1389 return Error("Invalid PNaCl bitcode header"); 1397 return Error("Invalid PNaCl bitcode header");
1390 1398
1391 // Create the bitcode reader. 1399 // Create the bitcode reader.
1392 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr); 1400 NaClBitstreamReader StreamFile(
1401 getNonStreamedMemoryObject(BufPtr, EndBufPtr), Header);
1393 NaClBitstreamCursor Stream(StreamFile); 1402 NaClBitstreamCursor Stream(StreamFile);
1394 1403
1395 // Set up the parser. 1404 // Set up the parser.
1396 NaClAssignAbbrevsParser Parser(Stream, AbbrevsMap, AbbrevsQueueMap); 1405 NaClAssignAbbrevsParser Parser(Stream, AbbrevsMap, AbbrevsQueueMap);
1397 1406
1398 // Parse the bitcode and choose abbreviations for records. 1407 // Parse the bitcode and choose abbreviations for records.
1399 while (!Stream.AtEndOfStream()) { 1408 while (!Stream.AtEndOfStream()) {
1400 if (Parser.Parse()) { 1409 if (Parser.Parse()) {
1401 installFrequentlyUsedAbbrevs(AbbrevsMap, AbbrevsQueueMap); 1410 installFrequentlyUsedAbbrevs(AbbrevsMap, AbbrevsQueueMap);
1402 return true; 1411 return true;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1572 // Read in bitcode, and write it back out using the abbreviations in 1581 // Read in bitcode, and write it back out using the abbreviations in
1573 // BlockAbbrevsMap, from memory buffer MemBuf containing the input 1582 // BlockAbbrevsMap, from memory buffer MemBuf containing the input
1574 // bitcode file. The bitcode is copied to Output. 1583 // bitcode file. The bitcode is copied to Output.
1575 static bool copyBitcode(const NaClBitcodeCompressor::CompressFlags &Flags, 1584 static bool copyBitcode(const NaClBitcodeCompressor::CompressFlags &Flags,
1576 MemoryBuffer *MemBuf, raw_ostream &Output, 1585 MemoryBuffer *MemBuf, raw_ostream &Output,
1577 BlockAbbrevsMapType &BlockAbbrevsMap, 1586 BlockAbbrevsMapType &BlockAbbrevsMap,
1578 BlockAbbrevsQueueMap &AbbrevsQueueMap) { 1587 BlockAbbrevsQueueMap &AbbrevsQueueMap) {
1579 1588
1580 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); 1589 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
1581 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); 1590 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
1591 const unsigned char *HeaderPtr = BufPtr;
1582 1592
1583 // Read header. No verification is needed since AnalyzeBitcode has 1593 // Read header. No verification is needed since AnalyzeBitcode has
1584 // already checked it. 1594 // already checked it.
1585 NaClBitcodeHeader Header; 1595 NaClBitcodeHeader Header;
1586 if (Header.Read(BufPtr, EndBufPtr)) 1596 if (Header.Read(HeaderPtr, EndBufPtr))
1587 return Error("Invalid PNaCl bitcode header"); 1597 return Error("Invalid PNaCl bitcode header");
1588 1598
1589 // Create the bitcode reader. 1599 // Create the bitcode reader.
1590 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr); 1600 NaClBitstreamReader StreamFile(
1601 getNonStreamedMemoryObject(BufPtr, EndBufPtr), Header);
1591 NaClBitstreamCursor Stream(StreamFile); 1602 NaClBitstreamCursor Stream(StreamFile);
1592 1603
1593 // Create the bitcode writer. 1604 // Create the bitcode writer.
1594 SmallVector<char, 0> OutputBuffer; 1605 SmallVector<char, 0> OutputBuffer;
1595 OutputBuffer.reserve(256 * 1024); 1606 OutputBuffer.reserve(256 * 1024);
1596 NaClBitstreamWriter StreamWriter(OutputBuffer); 1607 NaClBitstreamWriter StreamWriter(OutputBuffer);
1597 1608
1598 // Emit the file header. 1609 // Emit the file header.
1599 NaClWriteHeader(Header, StreamWriter); 1610 NaClWriteHeader(Header, StreamWriter);
1600 1611
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1643 BlockAbbrevsQueueMap AbbrevsQueueMap; 1654 BlockAbbrevsQueueMap AbbrevsQueueMap;
1644 bool Result = true; 1655 bool Result = true;
1645 if (chooseAbbrevs(MemBuf, BlockAbbrevsMap, AbbrevsQueueMap)) 1656 if (chooseAbbrevs(MemBuf, BlockAbbrevsMap, AbbrevsQueueMap))
1646 Result = false; 1657 Result = false;
1647 else if (copyBitcode(Flags, MemBuf, BitcodeOutput, BlockAbbrevsMap, 1658 else if (copyBitcode(Flags, MemBuf, BitcodeOutput, BlockAbbrevsMap,
1648 AbbrevsQueueMap)) 1659 AbbrevsQueueMap))
1649 Result = false; 1660 Result = false;
1650 DeleteContainerSeconds(AbbrevsQueueMap); 1661 DeleteContainerSeconds(AbbrevsQueueMap);
1651 return Result; 1662 return Result;
1652 } 1663 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698