llvm compiler (2 of 3) jason dangel. lectures high-level overview of llvm (katie) walkthrough of...
Post on 29-Dec-2015
223 Views
Preview:
TRANSCRIPT
LLVM Compiler (2 of 3)
Jason Dangel
Lectures
• High-level overview of LLVM (Katie)
• Walkthrough of LLVM in context of our project (Jason)
– Input requirements
– Configuration needs
• Additional configuration and global context (Dave)
– Optimizations
– Other file formats
2
Overview• High level overview
• LLVM Applied to Project
• Optimizer Inputs Explained
• Variable types
• Instruction Overview
• Code Examples
• Dissection of Clang
• How to use LLVM
• Command Line Examples
3
LLVM Compiler Design
4
LLVM Inputs
• RISC like instruction set
• Strongly typed– Simple types: i32 and i32**
• Some details are abstracted away– Example ‘call’ and ‘ret’ with explicit arguments
• Does not use fixed set of registers– Uses an infinite set of temporaries starting with %
5
Language Reference
• http://llvm.org/docs/LangRef.html– Details on all variable types – Details and examples of instructions
• Memory, Binary, Terminator(flow control)
– Very detailed and should probably be treated as a dictionary not a users manual.
6
Variable Tyes
• Integer (i1,i8, i32)– Any bit width from 1 bit to 223-1)
• Floats (half, float, double)
• Pointers
• Array – < <# elements> x <elementtype> >
7
Declaring and Assign Values
• Identifiers:– ‘[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*’
• Global declaration:– @X = global i32 17
– @Y = global i32 42
– @Z = global [2 x i32*] [ i32* @X, i32* @Y ]
– Local declaration– %X = i32 17
– %Y = i32 42
– %1 =
8
Declaring Functions
• define [linkage] [visibility] [DLLStorageClass] [cconv] [ret attrs] <ResultType> @<FunctionName> ([argument list]) [unnamed_addr] [fn Attrs] [section "name"] [comdat [($name)]] [align N] [gc] [prefix Constant] [prologue Constant] { ... }
• *Don’t worry I will explain later with an example and this will make more sense.
9
Instructions• Flow Control
– br, ret, switch, resume,…
• Binary Operational• Add, fadd, sub, mul, fmul,….
• Bitwise Binary Operational• shl, shr, and, xor, ….
• Memory, Call, Compare and Conversion
10
*Only a small subset, see reference manual for all
Provided Hello World Example
11
Code Examples
12
unsigned add1(unsigned a, unsigned b) {
return a+b;
}
// Perhaps not the most efficient way to add two numbers.
unsigned add2(unsigned a, unsigned b) {
if (a == 0) return b;
return add2(a-1, b+1);
}
define i32 @add1(i32 %a, i32 %b) {
entry:
%tmp1 = add i32 %a, %b
ret i32 %tmp1
}
define i32 @add2(i32 %a, i32 %b) {
entry:
%tmp1 = icmp eq i32 %a, 0
br i1 %tmp1, label %done, label %recurse
recurse:
%tmp2 = sub i32 %a, 1
%tmp3 = add i32 %b, 1
%tmp4 = call i32 @add2(i32 %tmp2, i32 %tmp3)
ret i32 %tmp4
done:
ret i32 %b
}
Clang
• Three block process performed automatically
• Clang has optimized the ‘add2()’ instruction through the LLVM optimizer– Now looks exactly like ‘add1()’
• Needed to disassemble the bitcode to read LLVM IR
13
Clang ‘hello world’ C-Code Example
14
Clang Output Results After Disassembling
15
How to Use LLVM
• Use clang with similar ‘C’ code to help– clang -O3 -emit-llvm hello.c -c -o hello.bc
• To Assemble use llvm-as to create bitcode– llvm-as hello.o –o hello.bc
• To compile for use on local computer use llc– llc hello.bc –o hello.exe
• To run on local computer with interpreter use lli– lli hello.bc
16
Command Line Example• ‘llvm-as’ will convert RISC-like
IR to bit code– Compressed to fit on disk and run
with interpreter
• ‘lli’ is the interpreter – Like java interpreter
• Bitcode can be analyzed with disassembler
17
Questions?
18
top related