LLVM Bytecode File Format
  1. Abstract
  2. General Concepts
    1. Blocks
    2. Lists
    3. Fields
    4. Slots
    5. Encoding Rules
    6. Alignment
  3. Detailed Layout
    1. Notation
    2. Blocks Types
    3. Signature Block
    4. Module Block
    5. Global Type Pool
    6. Module Info Block
    7. Global Constant Pool
    8. Function Blocks
    9. Module Symbol Table

Written by Reid Spencer

Abstract

This document describes the LLVM bytecode file format. It specifies the binary encoding rules of the bytecode file format so that equivalent systems can encode bytecode files correctly. The LLVM bytecode representation is used to store the intermediate representation on disk in compacted form.

General Concepts

This section describes the general concepts of the bytecode file format without getting into bit and byte level specifics. Note that the LLVM bytecode format may change in the future, but will always be backwards compatible with older formats. This document only describes the most current version of the bytecode format.

Blocks

LLVM bytecode files consist simply of a sequence of blocks of bytes. Each block begins with an identification value that determines the type of the next block. The possible types of blocks are described below in the section Block Types. The block identifier is used because it is possible for entire blocks to be omitted from the file if they are empty. The block identifier helps the reader determine which kind of block is next in the file.

The following block identifiers are currently in use (from llvm/Bytecode/Format.h):

  1. Module (0x01).
  2. Function (0x11).
  3. ConstantPool (0x12).
  4. SymbolTable (0x13).
  5. ModuleGlobalInfo (0x14).
  6. GlobalTypePlane (0x15).
  7. BasicBlock (0x31).
  8. InstructionList (0x32).
  9. CompactionTable (0x33).

All blocks are variable length, and the block header specifies the size of the block. All blocks are rounded aligned to even 32-bit boundaries, so they always start and end of this boundary. Each block begins with an integer identifier and the length of the block, which does not include the padding bytes needed for alignment.

Lists

Most blocks are constructed of lists of information. Lists can be constructed of other lists, etc. This decomposition of information follows the containment hierarchy of the LLVM Intermediate Representation. For example, a function contains a list of instructions (the terminator instructions implicitly define the end of the basic blocks).

A list is encoded into the file simply by encoding the number of entries as an integer followed by each of the entries. The reader knows when the list is done because it will have filled the list with the required numbe of entries.

Fields

Fields are units of information that LLVM knows how to write atomically. Most fields have a uniform length or some kind of length indication built into their encoding. For example, a constant string (array of bytes) is written simply as the length followed by the characters. Although this is similar to a list, constant strings are treated atomically and are thus fields.

Fields use a condensed bit format specific to the type of information they must contain. As few bits as possible are written for each field. The sections that follow will provide the details on how these fields are written and how the bits are to be interpreted.

Slots

The bytecode format uses the notion of a "slot" to reference Types and Values. Since the bytecode file is a direct representation of LLVM's intermediate representation, there is a need to represent pointers in the file. Slots are used for this purpose. For example, if one has the following assembly:

%MyType = type { int, sbyte }
%MyVar = external global %MyType

there are two definitions. The definition of %MyVar uses %MyType. In the C++ IR this linkage between %MyVar and %MyType is explicit through the use of C++ pointers. In bytecode, however, there's no ability to store memory addresses. Instead, we compute and write out slot numbers for every type and Value written to the file.

A slot number is simply an unsigned 32-bit integer encoded in the variable bit rate scheme (see encoding below). This ensures that low slot numbers are encoded in one byte. Through various bits of magic LLVM attempts to always keep the slot numbers low. The first attempt is to associate slot numbers with their "type plane". That is, Values of the same type are written to the bytecode file in a list (sequentially). Their order in that list determines their slot number. This means that slot #1 doesn't mean anything unless you also specify for which type you want slot #1. Types are handled specially and are always written to the file first (in the Global Type Pool) and in such a way that both forward and backward references of the types can often be resolved with a single pass through the type pool.

Slot numbers are also kept small by rearranging their order. Because of the structure of LLVM, certain values are much more likely to be used frequently in the body of a function. For this reason, a compaction table is provided in the body of a function if its use would make the function body smaller. Suppose you have a function body that uses just the types "int*" and "{double}" but uses them thousands of time. Its worthwhile to ensure that the slot number for these types are low so they can be encoded in a single byte (via vbr). This is exactly what the compaction table does.

Encoding Primitives

Each field that can be put out is encoded into the file using a small set of primitives. The rules for these primitives are described below.

Variable Bit Rate Encoding

Most of the values written to LLVM bytecode files are small integers. To minimize the number of bytes written for these quantities, an encoding scheme similar to UTF-8 is used to write integer data. The scheme is known as variable bit rate (vbr) encoding. In this encoding, the high bit of each byte is used to indicate if more bytes follow. If (byte & 0x80) is non-zero in any given byte, it means there is another byte immediately following that also contributes to the value. For the final byte (byte & 0x80) is false (the high bit is not set). In each byte only the low seven bits contribute to the value. Consequently 32-bit quantities can take from one to five bytes to encode. In general, smaller quantities will encode in fewer bytes, as follows:

Byte # Significant Bits Maximum Value
10-6127
27-1316,383
314-202,097,151
421-27268,435,455
528-3434,359,738,367
635-414,398,046,511,103
742-48562,949,953,421,311
849-5572,057,594,037,927,935
956-629,223,372,036,854,775,807
1063-691,180,591,620,717,411,303,423

Note that in practice, the tenth byte could only encode bit 63 since the maximum quantity to use this encoding is a 64-bit integer.

Signed VBR values are encoded with the standard vbr encoding, but with the sign bit as the low order bit instead of the high order bit. This allows small negative quantities to be encoded efficiently. For example, -3 is encoded as "((3 << 1) | 1)" and 3 is encoded as "(3 << 1) | 0)", emitted with the standard vbr encoding above.

The table below defines the encoding rules for type names used in the descriptions of blocks and fields in the next section. Any type name with the suffix _vbr indicate a quantity that is encoded using variable bit rate encoding as described above.

Type Rule
unsigned A 32-bit unsigned integer that always occupies four consecutive bytes. The unsigned integer is encoded using LSB first ordering. That is bits 20 through 27 are in the byte with the lowest file offset (little endian).
uint_vbr A 32-bit unsigned integer that occupies from one to five bytes using variable bit rate encoding.
uint64_vbr A 64-bit unsigned integer that occupies from one to ten bytes using variable bit rate encoding.
int64_vbr A 64-bit signed integer that occupies from one to ten bytes using the signed variable bit rate encoding.
char A single unsigned character encoded into one byte
bit A single bit within a byte.
string A uint_vbr indicating the length of the character string immediately followed by the characters of the string. There is no terminating null byte in the string.
data An arbitrarily long segment of data to which no interpretation is implied. This is used for float, double, and constant initializers.
Alignment

To support cross-platform differences, the bytecode file is aligned on certain boundaries. This means that a small amount of padding (at most 3 bytes) will be added to ensure that the next entry is aligned to a 32-bit boundary.

Detailed Layout

This section provides the detailed layout of the LLVM bytecode file format. bit and byte level specifics.

Notation

The descriptions of the bytecode format that follow describe the bit fields in detail. These descriptions are provided in tabular form. Each table has four columns that specify:

  1. Byte(s): The offset in bytes of the field from the start of its container (block, list, other field).
  2. Bit(s): The offset in bits of the field from the start of the byte field. Bits are always little endian. That is, bit addresses with smaller values have smaller address (i.e. 20 is at bit 0, 21 at 1, etc.)
  3. Align?: Indicates if this field is aligned to 32 bits or not. This indicates where the next field starts, always on a 32 bit boundary.
  4. Type: The basic type of information contained in the field.
  5. Description: Describes the contents of the field.
Block Types

The bytecode format encodes the intermediate representation into groups of bytes known as blocks. The blocks are written sequentially to the file in the following order:

  1. Signature: This contains the file signature (magic number) that identifies the file as LLVM bytecode and the bytecode version number.
  2. Module Block: This is the top level block in a bytecode file. It contains all the other blocks.
  3. Global Type Pool: This block contains all the global (module) level types.
  4. Module Info: This block contains the types of the global variables and functions in the module as well as the constant initializers for the global variables
  5. Constants: This block contains all the global constants except function arguments, global values and constant strings.
  6. Functions: One function block is written for each function in the module.
  7. Symbol Table: The module level symbol table that provides names for the various other entries in the file is the final block written.
Signature Block

The signature occurs in every LLVM bytecode file and is always first. It simply provides a few bytes of data to identify the file as being an LLVM bytecode file. This block is always four bytes in length and differs from the other blocks because there is no identifier and no block length at the start of the block. Essentially, this block is just the "magic number" for the file.

Byte(s) Bit(s) Align? Type Field Description
00-Nochar Constant "l" (0x6C)
01-Nochar Constant "l" (0x6C)
02-Nochar Constant "v" (0x76)
03-Nochar Constant "m" (0x6D)
Module Block

The module block contains a small pre-amble and all the other blocks in the file. Of particular note, the bytecode format number is simply a 28-bit monotonically increase integer that identifiers the version of the bytecode format (which is not directly related to the LLVM release number). The bytecode versions defined so far are (note that this document only describes the latest version):

The table below shows the format of the module block header. It is defined by blocks described in other sections.

Byte(s) Bit(s) Align? Type Field Description
04-07-Nounsigned Module Identifier (0x01)
08-11-Nounsigned Size of the module block in bytes
12-1500Yesuint32_vbr Format Information
''0-bit Big Endian?
''1-bit Pointers Are 64-bit?
''2-bit Has No Endianess?
''3-bit Has No Pointer Size?
''4-31-bit Bytecode Format Version
16-end--blocks The remaining bytes in the block consist solely of other block types in sequence.

Note that we plan to eventually expand the target description capabilities of bytecode files to target triples.

Global Type Pool

The global type pool consists of type definitions. Their order of appearance in the file determines their slot number (0 based). Slot numbers are used to replace pointers in the intermediate representation. Each slot number uniquely identifies one entry in a type plane (a collection of values of the same type). Since all values have types and are associated with the order in which the type pool is written, the global type pool must be written as the first block of a module. If it is not, attempts to read the file will fail because both forward and backward type resolution will not be possible.

The type pool is simply a list of types definitions, as shown in the table below.

Byte(s) Bit(s) Align? Type Field Description
00-03-Nounsigned Type Pool Identifier (0x13)
04-07-Nounsigned Size in bytes of the symbol table block.
08-111-Nouint32_vbr Number of entries in type plane
12-151-Nouint32_vbr Type plane index for following entries
16-end1,2-Notype Each of the type definitions.
1Maximum length shown, may be smaller
2Repeated field.
Module Info

To be determined.

Constants

To be determined.

Functions

To be determined.

Symbol Table

A symbol table can be put out in conjunction with a module or a function. A symbol table is a list of type planes. Each type plane starts with the number of entries in the plane and the type plane's slot number (so the type can be looked up in the global type pool). For each entry in a type plane, the slot number of the value and the name associated with that value are written. The format is given in the table below.

Byte(s) Bit(s) Align? Type Field Description
00-03-Nounsigned Symbol Table Identifier (0x13)
04-07-Nounsigned Size in bytes of the symbol table block.
08-111-Nouint32_vbr Number of entries in type plane
12-151-Nouint32_vbr Type plane index for following entries
16-191,2-Nouint32_vbr Slot number of a value.
variable1,2-Nostring Name of the value in the symbol table.
1Maximum length shown, may be smaller
2Repeated field.

Valid CSS! Valid HTML 4.01! Reid Spencer and Chris Lattner
The LLVM Compiler Infrastructure
Last modified: $Date$