LLVM Compiler (2 of 3) Jason Dangel. Lectures High-level overview of LLVM (Katie) Walkthrough of...

18
LLVM Compiler (2 of 3) Jason Dangel

Transcript of LLVM Compiler (2 of 3) Jason Dangel. Lectures High-level overview of LLVM (Katie) Walkthrough of...

Page 1: 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.

LLVM Compiler (2 of 3)

Jason Dangel

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

LLVM Compiler Design

4

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

Provided Hello World Example

11

Page 12: 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.

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

}

Page 13: 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.

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

Page 14: 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.

Clang ‘hello world’ C-Code Example

14

Page 15: 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.

Clang Output Results After Disassembling

15

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

Questions?

18