Converting IR to LLVM bitcode
In this recipe, you will learn to generate LLVM bit code from IR. The LLVM bit code file format (also known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format.
Getting Ready
The llvm-as
tool must be installed in the PATH.
How to do it...
Do the following steps:
- First create an IR code that will be used as input to
llvm-as
:$ cat test.ll define i32 @mult(i32 %a, i32 %b) #0 { %1 = mul nsw i32 %a, %b ret i32 %1 }
- To convert LLVM IR in
test.ll
to bitcode format, you need to use the following command:llvm-as test.ll –o test.bc
- The output is generated in the
test.bc
file, which is in bit stream format; so, when we want to have a look at output in text format, we get it as shown in the following screenshot:Since this is a bitcode file, the best way to view its content would be by using the
hexdump
tool. The following screenshot shows the output ofhexdump
:
How it works...
The llvm-as
is the LLVM assembler. It converts the LLVM assembly file that is the LLVM IR into LLVM bitcode. In the preceding command, it takes the test.ll
file as the input and outputs, and test.bc
as the bitcode file.
There's more...
To encode LLVM IR into bitcode, the concept of blocks and records is used. Blocks represent regions of bitstream, for example—a function body, symbol table, and so on. Each block has an ID specific to its content (for example, function bodies in LLVM IR are represented by ID 12). Records consist of a record code and an integer value, and they describe the entities within the file such as instructions, global variable descriptors, type descriptions, and so on.
Bitcode files for LLVM IR might be wrapped in a simple wrapper structure. This structure contains a simple header that indicates the offset and size of the embedded BC file.
See also
- To get a detailed understanding of the LLVM the bitstream file format, refer to http://llvm.org/docs/BitCodeFormat.html#abstract