COL729 Lab 1 : LLVM IR and x86 Assembly
Due date - 23:55, 4 February 2019
In this assignment, we will examine the effect of different clang optimisation
levels on the generated LLVM IR and x86 Assembly code
Instructions
- Familiarise yourself with LLVM
IR and x86 Assembly instuction set
- Compile the following programs using clang to LLVM IR (bitcode) and 32-bit x86
assembly. The provided Makefile already contains some commands to compile to LLVM IR and 32-bit x86; you may want to modify/adapt it for your needs. Also to standardize the results, we will be using clang-3.6 version, 32-bit x86 mode and Haswell chip. The recommended procedure to generate the code is:
- You can compile a C program to LLVM bitcode using the following command
clang -S -emit-llvm <compiler-options> foo.c
- Run at different optimisation levels - unoptimised, O1, O2, O3, Ofast, any other
flags, etc.
- Please check the clang documentation on your own for instructions to run bitcode and flags.
- You can link multiple bitcode files into a single bitcode file using the
following command
llvm-link foo.bc bar.bc
- You can disassemble a bitcode file using the
following command
llvm-dis < foo.bc > foo.bc.s
The output disassembly will be available in foo.bc.s
.
- Generate code from LLVM bitcode to the target assembly (32-bit or 64-bit
x86):
llc foo.bc
- Generate executable from target assembly using gcc assembler:
gcc foo.s -o foo.out
- You can disassemble x86 executable using objdump:
objdump -d < foo.out > foo.out.dis
The output disassembly will be available in foo.out.dis
.
You have been given six programs: gcd
, loops
, print_arg
, emptyloop
, fibo_iter
, fib
. Some of these programs have multiple functions inside them -- you are required to describe all functions in these cases.
For LLVM IR bitcode, you are required to test O0 and O2 optimization levels (see Makefile).
For 32-bit x86, you are required to test O0, O2, and O3 optimization levels (see Makefile).
Prepare a report summarizing your findings: Your report should be succinct
yet insightful. It should have the following information:
- What is the performance difference you observe across different optimizaion levels at bitcode/assembly level?
- For LLVM IR bitcode: Describe the bitcode of each program individually, to explain how the bitcode is achieving the desired logic of the program. Do this for each optimization level. Finally, summarize your impressions about the optimizations performed across O0 and O2 in the IR/bitcode level.
- For x86 assembly: Describe the x86 assembly of each program individually, to explain how the assembly code is achieving the desired logic of the program. Do this for O0 and O2 levels (do not need to do this for O3). Finally, summarize your impressions about the optimizations performed across O0 and O2 at the assembly level.