[llvm-commits] [llvm] r68465 - in /llvm/trunk: docs/BitCodeFormat.html include/llvm/Bitcode/BitCodes.h include/llvm/Bitcode/BitstreamReader.h include/llvm/Bitcode/BitstreamWriter.h

Chris Lattner sabre at nondot.org
Mon Apr 6 14:50:40 PDT 2009


Author: lattner
Date: Mon Apr  6 16:50:39 2009
New Revision: 68465

URL: http://llvm.org/viewvc/llvm-project?rev=68465&view=rev
Log:
add a new Blob encoding abbreviation for bitcode files that emits
elements in a form that is efficient for the reader to just get a
pointer in memory and start reading.  APIs to do efficient reading
and writing are still todo.

Modified:
    llvm/trunk/docs/BitCodeFormat.html
    llvm/trunk/include/llvm/Bitcode/BitCodes.h
    llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
    llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h

Modified: llvm/trunk/docs/BitCodeFormat.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/BitCodeFormat.html?rev=68465&r1=68464&r2=68465&view=diff

==============================================================================
--- llvm/trunk/docs/BitCodeFormat.html (original)
+++ llvm/trunk/docs/BitCodeFormat.html Mon Apr  6 16:50:39 2009
@@ -478,6 +478,13 @@
 <li value="4">Char6: This field should be emitted as
     a <a href="#char6">char6-encoded value</a>.  This operand type takes no
     extra data.</li>
+<li value="5">Blob: This field is emitted as a vbr6, followed by padding to a
+    32-bit boundary (for alignment) and an array of 8-bit objects.  The array of
+    bytes is further followed by tail padding to ensure that its total length is
+    a multiple of 4 bytes.  This makes it very efficient for the reader to
+    decode the data without having to make a copy of it: it can use a pointer to
+    the data in the mapped in file and poke directly at it.  A blob may only
+    occur as the last operand of an abbreviation.</li>
 </ol>
 
 <p>

Modified: llvm/trunk/include/llvm/Bitcode/BitCodes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitCodes.h?rev=68465&r1=68464&r2=68465&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitCodes.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitCodes.h Mon Apr  6 16:50:39 2009
@@ -88,7 +88,8 @@
     Fixed = 1,  // A fixed width field, Val specifies number of bits.
     VBR   = 2,  // A VBR field where Val specifies the width of each chunk.
     Array = 3,  // A sequence of fields, next field species elt encoding.
-    Char6 = 4   // A 6-bit fixed field which maps to [a-zA-Z0-9._].
+    Char6 = 4,  // A 6-bit fixed field which maps to [a-zA-Z0-9._].
+    Blob  = 5   // 8-bit aligned array of 8-bit characters.
   };
 
   explicit BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
@@ -117,6 +118,7 @@
       return true;
     case Array:
     case Char6:
+    case Blob:
       return false;
     }
   }

Modified: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=68465&r1=68464&r2=68465&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h Mon Apr  6 16:50:39 2009
@@ -149,7 +149,7 @@
     }
 
     // If we run out of data, stop at the end of the stream.
-    if (LastChar == NextChar) {
+    if (NextChar == LastChar) {
       CurWord = 0;
       BitsInCurWord = 0;
       return 0;
@@ -380,9 +380,7 @@
       const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
       if (Op.isLiteral()) {
         ReadAbbreviatedLiteral(Op, Vals); 
-      } else if (Op.getEncoding() != BitCodeAbbrevOp::Array) {
-        ReadAbbreviatedField(Op, Vals);
-      } else {
+      } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
         // Array case.  Read the number of elements as a vbr6.
         unsigned NumElts = ReadVBR(6);
 
@@ -393,6 +391,29 @@
         // Read all the elements.
         for (; NumElts; --NumElts)
           ReadAbbreviatedField(EltEnc, Vals);
+      } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
+        // Blob case.  Read the number of bytes as a vbr6.
+        unsigned NumElts = ReadVBR(6);
+        SkipToWord();  // 32-bit alignment
+
+        // Figure out where the end of this blob will be including tail padding.
+        const unsigned char *NewEnd = NextChar+((NumElts+3)&~3);
+        
+        // If this would read off the end of the bitcode file, just set the
+        // record to empty and return.
+        if (NewEnd > LastChar) {
+          Vals.append(NumElts, 0);
+          NextChar = LastChar;
+          break;
+        }
+        
+        // Otherwise, read the number of bytes.
+        for (; NumElts; ++NextChar, --NumElts)
+          Vals.push_back(*NextChar);
+        // Skip over tail padding.
+        NextChar = NewEnd;
+      } else {
+        ReadAbbreviatedField(Op, Vals);
       }
     }
 

Modified: llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h?rev=68465&r1=68464&r2=68465&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h Mon Apr  6 16:50:39 2009
@@ -319,11 +319,7 @@
         assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
         EmitAbbreviatedLiteral(Op, Vals[RecordIdx]);
         ++RecordIdx;
-      } else if (Op.getEncoding() != BitCodeAbbrevOp::Array) {
-        assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
-        EmitAbbreviatedField(Op, Vals[RecordIdx]);
-        ++RecordIdx;
-      } else {
+      } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
         // Array case.
         assert(i+2 == e && "array op not second to last?");
         const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
@@ -334,6 +330,26 @@
         // Emit each field.
         for (; RecordIdx != Vals.size(); ++RecordIdx)
           EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
+      } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
+        // Emit a vbr6 to indicate the number of elements present.
+        EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
+        // Flush to a 32-bit alignment boundary.
+        FlushToWord();
+        assert((Out.size() & 3) == 0 && "Not 32-bit aligned");
+
+        // Emit each field as a literal byte.
+        for (; RecordIdx != Vals.size(); ++RecordIdx) {
+          assert(Vals[RecordIdx] < 256 && "Value too large to emit as blob");
+          Out.push_back((unsigned char)Vals[RecordIdx]);
+        }
+        // Align end to 32-bits.
+        while (Out.size() & 3)
+          Out.push_back(0);
+        
+      } else {  // Single scalar field.
+        assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
+        EmitAbbreviatedField(Op, Vals[RecordIdx]);
+        ++RecordIdx;
       }
     }
     assert(RecordIdx == Vals.size() && "Not all record operands emitted!");





More information about the llvm-commits mailing list