+ All Categories
Home > Documents > The LLVM Compiler Framework and Infrastructure Vikram Adve [email protected] Chris Lattner...

The LLVM Compiler Framework and Infrastructure Vikram Adve [email protected] Chris Lattner...

Date post: 24-Dec-2015
Category:
Upload: candace-thompson
View: 229 times
Download: 0 times
Share this document with a friend
Popular Tags:
61
The LLVM Compiler Framework The LLVM Compiler Framework and Infrastructure and Infrastructure Vikram Adve Vikram Adve [email protected] [email protected] u u Chris Lattner Chris Lattner [email protected]. [email protected]. edu edu http://llvm.cs.uiuc.edu/ http://llvm.cs.uiuc.edu/ LCPC Tutorial: September 22, 2004 LCPC Tutorial: September 22, 2004
Transcript
Page 1: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

The LLVM Compiler The LLVM Compiler Framework and Framework and InfrastructureInfrastructure

Vikram AdveVikram [email protected]@cs.uiuc.e

dudu

Chris LattnerChris [email protected]@cs.uiuc.e

dudu

http://llvm.cs.uiuc.edu/http://llvm.cs.uiuc.edu/

LCPC Tutorial: September 22, 2004LCPC Tutorial: September 22, 2004

Page 2: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

AcknowledgementsAcknowledgementsUIUC Contributors:UIUC Contributors:

Tanya BrethourTanya Brethour Misha BrukmanMisha Brukman Cameron BuschardtCameron Buschardt John CriswellJohn Criswell Alkis EvlogimenosAlkis Evlogimenos Brian GaekeBrian Gaeke Ruchira SasankaRuchira Sasanka Anand ShuklaAnand Shukla Bill WendlingBill Wendling

External Contributors:External Contributors: Henrik BachHenrik Bach Nate BegemanNate Begeman Jeff CohenJeff Cohen Paolo InvernizziPaolo Invernizzi Brad JonesBrad Jones Vladimir MerzliakovVladimir Merzliakov Vladimir PrusVladimir Prus Reid SpencerReid Spencer

Funding: Funding:

This work is sponsored by the NSF Next Generation Software program through This work is sponsored by the NSF Next Generation Software program through grants EIA-0093426 (an NSF CAREER award) and EIA-0103756. It is also supported grants EIA-0093426 (an NSF CAREER award) and EIA-0103756. It is also supported in part by the NSF Operating Systems and Compilers program (grant #CCR-in part by the NSF Operating Systems and Compilers program (grant #CCR-9988482), the NSF Embedded Systems program (grant #CCR-0209202), the 9988482), the NSF Embedded Systems program (grant #CCR-0209202), the MARCO/DARPA Gigascale Systems Research Center (GSRC), IBM through the MARCO/DARPA Gigascale Systems Research Center (GSRC), IBM through the DARPA-funded PERCS project, and the Motorola University Partnerships in DARPA-funded PERCS project, and the Motorola University Partnerships in Research program.Research program.

Page 3: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Compiler SystemLLVM Compiler System

The LLVM Compiler InfrastructureThe LLVM Compiler Infrastructure Provides reusable components for building compilersProvides reusable components for building compilers Reduce the time/cost to build a new compilerReduce the time/cost to build a new compiler Build static compilers, JITs, trace-based optimizers, ...Build static compilers, JITs, trace-based optimizers, ...

The LLVM Compiler FrameworkThe LLVM Compiler Framework End-to-end compilers using the LLVM infrastructureEnd-to-end compilers using the LLVM infrastructure C and C++ are robust and aggressive:C and C++ are robust and aggressive:

Java, Scheme and others are in developmentJava, Scheme and others are in development Emit C code or native code for X86, Sparc, PowerPCEmit C code or native code for X86, Sparc, PowerPC

Page 4: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Three primary LLVM componentsThree primary LLVM components

The LLVM The LLVM Virtual Instruction SetVirtual Instruction Set The common language- and target-independent IRThe common language- and target-independent IR Internal (IR) and external (persistent) representationInternal (IR) and external (persistent) representation

A collection of well-integrated librariesA collection of well-integrated libraries Analyses, optimizations, code generators, JIT Analyses, optimizations, code generators, JIT

compiler, garbage collection support, profiling, …compiler, garbage collection support, profiling, …

A collection of tools built from the librariesA collection of tools built from the libraries Assemblers, automatic debugger, linker, code Assemblers, automatic debugger, linker, code

generator, compiler driver, modular optimizer, …generator, compiler driver, modular optimizer, …

Page 5: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 6: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Running example: arg promotionRunning example: arg promotion

Consider use of by-reference parameters:Consider use of by-reference parameters:int callee(const int &X) {int callee(const int &X) { return X+1;return X+1;}}int caller() {int caller() { return callee(4);return callee(4);}}

int callee(const int *X) {int callee(const int *X) { return *X+1; return *X+1; // memory load// memory load}}int caller() {int caller() { int tmp; int tmp; // stack object// stack object tmp = 4; tmp = 4; // memory store// memory store return callee(&tmp);return callee(&tmp);}}

compiles to

Eliminated load in callee

Eliminated store in caller

Eliminated stack slot for ‘tmp’

int callee(int X) {int callee(int X) { return X+1;return X+1;}}int caller() {int caller() { return callee(4);return callee(4);}}

We want:We want:

Page 7: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Why is this hard?Why is this hard?

Requires interprocedural analysis:Requires interprocedural analysis: Must change the prototype of the calleeMust change the prototype of the callee Must update all call sites Must update all call sites we must we must knowknow all callers all callers What about callers outside the translation unit?What about callers outside the translation unit?

Requires alias analysis:Requires alias analysis: Reference could alias other pointers in calleeReference could alias other pointers in callee Must know that loaded value doesn’t change from Must know that loaded value doesn’t change from

function entry to the loadfunction entry to the load Must know the pointer is not being stored throughMust know the pointer is not being stored through

Reference might not be to a stack object!Reference might not be to a stack object!

Page 8: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 9: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

The LLVM C/C++ CompilerThe LLVM C/C++ Compiler

From the high level, it is a standard compiler:From the high level, it is a standard compiler: Compatible with standard makefilesCompatible with standard makefiles Uses GCC 3.4 C and C++ parserUses GCC 3.4 C and C++ parser

Distinguishing features:Distinguishing features: Uses LLVM optimizers, not GCC optimizersUses LLVM optimizers, not GCC optimizers .o files contain LLVM IR/bytecode, not machine code.o files contain LLVM IR/bytecode, not machine code Executable can be bytecode (JIT’d) or machine codeExecutable can be bytecode (JIT’d) or machine code

llvmg++

llvmgccC file

C++ file

.o file

.o filellvm linker executable

Compile Time Link Time

Page 10: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Looking into events at compile-timeLooking into events at compile-time

llvmgccC file .o file llvmg++C++ file .o file

Modified version of G++Emits LLVM IR as text fileLowers C++ AST to LLVM

Modified version of GCCEmits LLVM IR as text fileLowers C AST to LLVM

LLVM IR Parser

LLVM Verifier

40 LLVM Analysis & Optimization Passes

LLVM .bc File Writer

C to LLVM Frontend

Compile-time Optimizer

C++ to LLVM Frontend

Compile-time Optimizer

“cc1” “cc1plus” “gccas”“gccas”

Dead Global Elimination, IP Constant Propagation, Dead Argument Elimination, Inlining, Reassociation, LICM, Loop

Opts, Memory Promotion, Dead Store Elimination, ADCE, …

Page 11: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Looking into events at link-timeLooking into events at link-time.o file

.o filellvm linker executable

Native Code Backend

Native executable

“llc”

C Code Backend

C CompilerNative

executable

“llc –march=c”“gcc”

Link in native .o files and libraries here

LLVM Linker

Link-time Optimizer

.bc file for LLVM JIT.o file

.o file

20 LLVM Analysis & Optimization Passes

Optionally “internalizes”: marks most functions as internal, to improve IPO

Perfect place for argument promotion optimization!

Page 12: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Goals of the compiler designGoals of the compiler design

Analyze and optimize as early as possible:Analyze and optimize as early as possible: Compile-time opts reduce modify-rebuild-execute cycleCompile-time opts reduce modify-rebuild-execute cycle Compile-time optimizations reduce work at link-time (by Compile-time optimizations reduce work at link-time (by

shrinking the program)shrinking the program)

All IPA/IPO make an open-world assumptionAll IPA/IPO make an open-world assumption Thus, they all work on libraries and at compile-timeThus, they all work on libraries and at compile-time ““Internalize” pass enables “whole program” optznInternalize” pass enables “whole program” optzn

One IR (without lowering) for analysis & optznOne IR (without lowering) for analysis & optzn Compile-time optzns can be run at link-time too!Compile-time optzns can be run at link-time too! The same IR is used as input to the JITThe same IR is used as input to the JIT

IR design is the key to these goals!IR design is the key to these goals!

Page 13: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 14: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Goals of LLVM IRGoals of LLVM IR

Easy to produce, understand, and define!Easy to produce, understand, and define! Language- and Target-IndependentLanguage- and Target-Independent

AST-level IR (e.g. ANDF, UNCOL) is not very feasibleAST-level IR (e.g. ANDF, UNCOL) is not very feasible Every analysis/xform must know about ‘all’ languagesEvery analysis/xform must know about ‘all’ languages

One IR for analysis and optimizationOne IR for analysis and optimization IR must be able to support aggressive IPO, loop opts, IR must be able to support aggressive IPO, loop opts,

scalar opts, … high- scalar opts, … high- andand low-level optimization! low-level optimization!

Optimize as much as early as possibleOptimize as much as early as possible Can’t postpone everything until link or runtimeCan’t postpone everything until link or runtime No lowering in the IR!No lowering in the IR!

Page 15: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Instruction Set Overview #1LLVM Instruction Set Overview #1

Low-level and target-independent semanticsLow-level and target-independent semantics RISC-like three address codeRISC-like three address code Infinite virtual register set in SSA formInfinite virtual register set in SSA form Simple, low-level control flow constructsSimple, low-level control flow constructs Load/store instructions with typed-pointersLoad/store instructions with typed-pointers

IR has text, binary, and in-memory formsIR has text, binary, and in-memory forms

for (i = 0; i < N;for (i = 0; i < N;

++i)++i)

Sum(&A[i], &P);Sum(&A[i], &P);

loop:loop:

%i.1 = %i.1 = phiphi intint [ 0, %bb0 ], [ %i.2, %loop ] [ 0, %bb0 ], [ %i.2, %loop ]

%AiAddr = %AiAddr = getelementptrgetelementptr float*float* %A, %A, intint %i.1 %i.1

callcall voidvoid %Sum( %Sum(floatfloat %AiAddr, %AiAddr, %pair*%pair* %P) %P)

%i.2 = %i.2 = addadd intint %i.1, 1 %i.1, 1

%tmp.4 = %tmp.4 = setltsetlt intint %i.1, %N %i.1, %N

brbr boolbool %tmp.4, %tmp.4, labellabel %loop, %loop, labellabel %outloop %outloop

Page 16: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Instruction Set Overview #2LLVM Instruction Set Overview #2

High-level information exposed in the codeHigh-level information exposed in the code Explicit dataflow through SSA formExplicit dataflow through SSA form Explicit control-flow graph (even for exceptions)Explicit control-flow graph (even for exceptions) Explicit language-independent type-informationExplicit language-independent type-information Explicit typed pointer arithmeticExplicit typed pointer arithmetic

Preserve array subscript and structure indexingPreserve array subscript and structure indexing

for (i = 0; i < N;for (i = 0; i < N;

++i)++i)

Sum(&A[i], &P);Sum(&A[i], &P);

loop:loop:

%i.1 = %i.1 = phiphi intint [ 0, %bb0 ], [ %i.2, %loop ] [ 0, %bb0 ], [ %i.2, %loop ]

%AiAddr = %AiAddr = getelementptrgetelementptr float*float* %A, %A, intint %i.1 %i.1

callcall voidvoid %Sum( %Sum(floatfloat %AiAddr, %AiAddr, %pair*%pair* %P) %P)

%i.2 = %i.2 = addadd intint %i.1, 1 %i.1, 1

%tmp.4 = %tmp.4 = setltsetlt intint %i.1, %N %i.1, %N

brbr boolbool %tmp.4, %tmp.4, labellabel %loop, %loop, labellabel %outloop %outloop

Page 17: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Type System DetailsLLVM Type System Details

The entire type system consists of:The entire type system consists of: Primitives: void, bool, float, ushort, opaque, …Primitives: void, bool, float, ushort, opaque, … Derived: pointer, array, structure, functionDerived: pointer, array, structure, function No high-level types: type-system is language neutral!No high-level types: type-system is language neutral!

Type system allows arbitrary casts:Type system allows arbitrary casts: Allows expressing weakly-typed languages, like CAllows expressing weakly-typed languages, like C Front-ends can Front-ends can implementimplement safe languages safe languages Also easy to define a type-safe subset of LLVMAlso easy to define a type-safe subset of LLVM

See also:See also: docs/LangRef.htmldocs/LangRef.html

Page 18: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Lowering source-level types to LLVMLowering source-level types to LLVM

Source language types are lowered:Source language types are lowered: Rich type systems expanded to simple type systemRich type systems expanded to simple type system Implicit & abstract types are made explicit & concreteImplicit & abstract types are made explicit & concrete

Examples of lowering:Examples of lowering: References turn into pointers: References turn into pointers: T& T& T*T* Complex numbers: Complex numbers: complex float complex float { float, float } { float, float }

Bitfields: Bitfields: struct X { int Y:4; int Z:2; } struct X { int Y:4; int Z:2; } { int }{ int }

Inheritance: Inheritance: class T : S { int X; } class T : S { int X; } { S, int }{ S, int }

Methods: Methods: class T { void foo(); } class T { void foo(); } void foo(T*)void foo(T*)

Same idea as lowering to machine codeSame idea as lowering to machine code

Page 19: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Program StructureLLVM Program Structure

Module contains Functions/GlobalVariablesModule contains Functions/GlobalVariables Module is unit of compilation/analysis/optimizationModule is unit of compilation/analysis/optimization

Function contains BasicBlocks/ArgumentsFunction contains BasicBlocks/Arguments Functions roughly correspond to functions in CFunctions roughly correspond to functions in C

BasicBlock contains list of instructionsBasicBlock contains list of instructions Each block ends in a control flow instructionEach block ends in a control flow instruction

Instruction is opcode + vector of operandsInstruction is opcode + vector of operands All operands have typesAll operands have types Instruction result is typedInstruction result is typed

Page 20: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Our example, compiled to LLVMOur example, compiled to LLVM

int callee(const int *X) {int callee(const int *X) { return *X+1; return *X+1; // load// load}}int caller() {int caller() { int T; int T; // on stack// on stack T = 4; T = 4; // store// store return callee(&T);return callee(&T);}}

internal int %callee(int* %X) {internal int %callee(int* %X) { %tmp.1 = load int* %X%tmp.1 = load int* %X %tmp.2 = add int %tmp.1, 1%tmp.2 = add int %tmp.1, 1 ret int %tmp.2ret int %tmp.2}}int %caller() {int %caller() { %T = alloca int%T = alloca int store int 4, int* %Tstore int 4, int* %T %tmp.3 = call int %callee(int* %T)%tmp.3 = call int %callee(int* %T) ret int %tmp.3ret int %tmp.3}}

Stack allocation is explicit in LLVM

All loads/stores are explicit in the LLVM

representation

Linker “internalizes” most functions in most

cases

Page 21: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Our example, desired transformationOur example, desired transformation

internal int %callee(int %X.val) {internal int %callee(int %X.val) { %tmp.2 = add int %X.val, 1%tmp.2 = add int %X.val, 1 ret int %tmp.2ret int %tmp.2}}int %caller() {int %caller() { %T = alloca int%T = alloca int store int 4, int* %Tstore int 4, int* %T %tmp.1 = load int* %T%tmp.1 = load int* %T %tmp.3 = call int %callee(%tmp.1)%tmp.3 = call int %callee(%tmp.1) ret int %tmp.3ret int %tmp.3}}

internal int %callee(int* %X) {internal int %callee(int* %X) { %tmp.1 = load int* %X%tmp.1 = load int* %X %tmp.2 = add int %tmp.1, 1%tmp.2 = add int %tmp.1, 1 ret int %tmp.2ret int %tmp.2}}int %caller() {int %caller() { %T = alloca int%T = alloca int store int 4, int* %Tstore int 4, int* %T %tmp.3 = call int %callee(int* %T)%tmp.3 = call int %callee(int* %T) ret int %tmp.3ret int %tmp.3}}

Change the prototype for the function

Insert load instructions into all callers

Update all call sites of ‘callee’

Other transformation(-mem2reg) cleans up

the rest

int %caller() {int %caller() { %tmp.3 = call int %callee(int 4)%tmp.3 = call int %callee(int 4) ret int %tmp.3ret int %tmp.3}}

Page 22: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 23: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Coding BasicsLLVM Coding Basics

Written in modern C++, uses the STL:Written in modern C++, uses the STL: Particularly the vector, set, and map classesParticularly the vector, set, and map classes

LLVM IR is almost all doubly-linked lists:LLVM IR is almost all doubly-linked lists: Module contains lists of Functions & GlobalVariablesModule contains lists of Functions & GlobalVariables Function contains lists of BasicBlocks & ArgumentsFunction contains lists of BasicBlocks & Arguments BasicBlock contains list of InstructionsBasicBlock contains list of Instructions

Linked lists are traversed with iterators:Linked lists are traversed with iterators:Function *M = …Function *M = …

for (Function::iterator I = M->begin(); I != M->end(); ++I) {for (Function::iterator I = M->begin(); I != M->end(); ++I) {

BasicBlock &BB = *I;BasicBlock &BB = *I;

......See also:See also: docs/ProgrammersManual.htmldocs/ProgrammersManual.html

Page 24: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Pass ManagerLLVM Pass Manager

Compiler is organized as a series of ‘passes’:Compiler is organized as a series of ‘passes’: Each pass is one analysis or transformationEach pass is one analysis or transformation

Four types of Pass:Four types of Pass: ModulePassModulePass: general interprocedural pass: general interprocedural pass CallGraphSCCPassCallGraphSCCPass: bottom-up on the call graph: bottom-up on the call graph FunctionPassFunctionPass: process a function at a time: process a function at a time BasicBlockPassBasicBlockPass: process a basic block at a time: process a basic block at a time

Constraints imposed (e.g. FunctionPass):Constraints imposed (e.g. FunctionPass): FunctionPass can only look at “current function”FunctionPass can only look at “current function” Cannot maintain state across functionsCannot maintain state across functions

See also:See also: docs/WritingAnLLVMPass.htmldocs/WritingAnLLVMPass.html

Page 25: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Services provided by PassManagerServices provided by PassManager

Optimization of pass execution:Optimization of pass execution: Process a function at a time instead of a pass at a timeProcess a function at a time instead of a pass at a time Example: If F, G, H are three functions in input pgm: Example: If F, G, H are three functions in input pgm:

“FFFFGGGGHHHH” not “FGHFGHFGHFGH”“FFFFGGGGHHHH” not “FGHFGHFGHFGH” Process functions in parallel on an SMP (future work)Process functions in parallel on an SMP (future work)

Declarative dependency management:Declarative dependency management: Automatically fulfill and manage analysis pass lifetimesAutomatically fulfill and manage analysis pass lifetimes Share analyses between passes when safe:Share analyses between passes when safe:

e.g. “DominatorSet live unless pass modifies CFG”e.g. “DominatorSet live unless pass modifies CFG”

Avoid boilerplate for traversal of programAvoid boilerplate for traversal of program

See also:See also: docs/WritingAnLLVMPass.htmldocs/WritingAnLLVMPass.html

Page 26: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Pass Manager + Arg Promotion #1/2Pass Manager + Arg Promotion #1/2

Arg Promotion is a CallGraphSCCPass:Arg Promotion is a CallGraphSCCPass: Naturally operates bottom-up on the CallGraphNaturally operates bottom-up on the CallGraph

Bubble pointers from callees out to callersBubble pointers from callees out to callers

24: #include "llvm/CallGraphSCCPass.h"47: struct SimpleArgPromotion : public CallGraphSCCPass {

Arg Promotion requires AliasAnalysis infoArg Promotion requires AliasAnalysis info To prove safety of transformationTo prove safety of transformation

Works with any alias analysis algorithm thoughWorks with any alias analysis algorithm though

48: 48: virtual void getAnalysisUsage(AnalysisUsage &AU) const {virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired<AliasAnalysis>(); // AU.addRequired<AliasAnalysis>(); // Get aliasesGet aliases AU.addRequired<TargetData>(); // AU.addRequired<TargetData>(); // Get data layoutGet data layout CallGraphSCCPass::getAnalysisUsage(AU); // CallGraphSCCPass::getAnalysisUsage(AU); // Get CallGraphGet CallGraph }}

Page 27: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Finally, implement Finally, implement runOnSCCrunOnSCC (line 65): (line 65):

bool SimpleArgPromotion::bool SimpleArgPromotion::runOnSCC(const std::vector<CallGraphNode*> &SCC) {runOnSCC(const std::vector<CallGraphNode*> &SCC) { bool Changed = false, LocalChange;bool Changed = false, LocalChange; do { // do { // Iterate until we stop promoting from this SCC.Iterate until we stop promoting from this SCC. LocalChange = false;LocalChange = false; // // Attempt to promote arguments from all functions in this SCC.Attempt to promote arguments from all functions in this SCC. for (unsigned i = 0, e = SCC.size(); i != e; ++i)for (unsigned i = 0, e = SCC.size(); i != e; ++i) LocalChange |= PromoteArguments(SCC[i]);LocalChange |= PromoteArguments(SCC[i]); Changed |= LocalChange; // Changed |= LocalChange; // Remember that we changed something.Remember that we changed something. } while (LocalChange);} while (LocalChange); return Changed; // return Changed; // Passes return true if something changed.Passes return true if something changed.}}

Pass Manager + Arg Promotion #2/2Pass Manager + Arg Promotion #2/2

static int foo(int ***P) {static int foo(int ***P) { return ***P;return ***P;}}

static int foo(int P_val_val_val) {static int foo(int P_val_val_val) { return P_val_val_val;return P_val_val_val;}}

Page 28: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM Dataflow AnalysisLLVM Dataflow Analysis

LLVM IR is in SSA form:LLVM IR is in SSA form: use-def and def-use chains are always availableuse-def and def-use chains are always available All objects have user/use info, even functionsAll objects have user/use info, even functions

Control Flow Graph is always available:Control Flow Graph is always available: Exposed as BasicBlock predecessor/successor listsExposed as BasicBlock predecessor/successor lists Many generic graph algorithms usable with the CFGMany generic graph algorithms usable with the CFG

Higher-level info implemented as passes:Higher-level info implemented as passes: Dominators, CallGraph, induction vars, aliasing, GVN, …Dominators, CallGraph, induction vars, aliasing, GVN, …

See also:See also: docs/ProgrammersManual.htmldocs/ProgrammersManual.html

Page 29: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: safety check #1/4Arg Promotion: safety check #1/4

#1: Function must be “internal” (aka “static”)#1: Function must be “internal” (aka “static”)

88:88: if (!F || !F->hasInternalLinkage()) return false; if (!F || !F->hasInternalLinkage()) return false;

#2: Make sure address of F is not taken#2: Make sure address of F is not taken In LLVM, check that there are only direct calls using FIn LLVM, check that there are only direct calls using F

99:99: for (Value::use_iterator UI = F->use_begin(); for (Value::use_iterator UI = F->use_begin(); UI != F->use_end(); ++UI) {UI != F->use_end(); ++UI) { CallSite CS = CallSite::get(*UI);CallSite CS = CallSite::get(*UI); if (!CS.getInstruction()) // if (!CS.getInstruction()) // "Taking the address" of F."Taking the address" of F. return false;return false;

#3: Check to see if any args are promotable:#3: Check to see if any args are promotable: 114:114: for (unsigned i = 0; i != PointerArgs.size(); ++i) for (unsigned i = 0; i != PointerArgs.size(); ++i) if (!isSafeToPromoteArgument(PointerArgs[i]))if (!isSafeToPromoteArgument(PointerArgs[i])) PointerArgs.erase(PointerArgs.begin()+i);PointerArgs.erase(PointerArgs.begin()+i); if (PointerArgs.empty()) return false; // if (PointerArgs.empty()) return false; // no args promotableno args promotable

Page 30: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: safety check #2/4Arg Promotion: safety check #2/4

#4: Argument pointer can only be loaded from:#4: Argument pointer can only be loaded from: No stores through argument pointer allowed!No stores through argument pointer allowed!

// // Loop over all uses of the argument (use-def chains).Loop over all uses of the argument (use-def chains).138:138: for (Value::use_iterator UI = Arg->use_begin(); for (Value::use_iterator UI = Arg->use_begin(); UI != Arg->use_end(); ++UI) {UI != Arg->use_end(); ++UI) {

// // If the user is a load:If the user is a load: if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {

// // Don't modify volatile loads.Don't modify volatile loads. if (LI->isVolatile()) return false;if (LI->isVolatile()) return false; Loads.push_back(LI);Loads.push_back(LI); } else {} else {

return false; // return false; // Not a load.Not a load. }} }}

Page 31: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

#5: Value of “*P” must not change in the BB#5: Value of “*P” must not change in the BB We move load out to the caller, value cannot change!We move load out to the caller, value cannot change!

// // Get AliasAnalysis implementation from the pass manager.Get AliasAnalysis implementation from the pass manager.156:156: AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); AliasAnalysis &AA = getAnalysis<AliasAnalysis>();

// // Ensure *P is not modified from start of block to loadEnsure *P is not modified from start of block to load169:169: if (AA.canInstructionRangeModify(BB->front(), *Load, if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))Arg, LoadSize)) return false; // return false; // Pointer is invalidated!Pointer is invalidated!

Arg Promotion: safety check #3/4Arg Promotion: safety check #3/4

See also:See also: docs/AliasAnalysis.htmldocs/AliasAnalysis.html

…………

load Pload P……

ModifieModifies “*P”?s “*P”?

Page 32: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

#6: “*P” cannot change from Fn entry to BB #6: “*P” cannot change from Fn entry to BB

175:175: for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) // PI != E; ++PI) // Loop over predecessors of BB.Loop over predecessors of BB. // // Check each block from BB to entry (DF search on inverse graph).Check each block from BB to entry (DF search on inverse graph). for (idf_iterator<BasicBlock*> I = idf_begin(*PI);for (idf_iterator<BasicBlock*> I = idf_begin(*PI); I != idf_end(*PI); ++I)I != idf_end(*PI); ++I) // // Might *P be modified in this basic block?Might *P be modified in this basic block? if (AA.canBasicBlockModify(**I, Arg, LoadSize))if (AA.canBasicBlockModify(**I, Arg, LoadSize)) return false;return false;

ModifieModifies “*P”?s “*P”?

ModifieModifies “*P”?s “*P”?

Arg Promotion: safety check #4/4Arg Promotion: safety check #4/4

EntryEntry

load Pload P

load Pload P

EntryEntry

Page 33: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: xform outline #1/4Arg Promotion: xform outline #1/4

#1: Make prototype with new arg types: #197#1: Make prototype with new arg types: #197 Basically just replaces ‘int*’ with ‘int’ in prototypeBasically just replaces ‘int*’ with ‘int’ in prototype

#2: Create function with new prototype:#2: Create function with new prototype:

214:214: Function *NF = new Function(NFTy, F->getLinkage(), Function *NF = new Function(NFTy, F->getLinkage(), F->getName());F->getName()); F->getParent()->getFunctionList().insert(F, NF);F->getParent()->getFunctionList().insert(F, NF);

#3: Change all callers of F to call NF:#3: Change all callers of F to call NF:

// // If there are uses of F, then calls to it remain.If there are uses of F, then calls to it remain.221:221: while (!F->use_empty()) { while (!F->use_empty()) { // // Get a caller of F.Get a caller of F. CallSite CS = CallSite::get(F->use_back());CallSite CS = CallSite::get(F->use_back());

Page 34: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: xform outline #2/4Arg Promotion: xform outline #2/4

#4: For each caller, add loads, determine args#4: For each caller, add loads, determine args Loop over the args, inserting the loads in the callerLoop over the args, inserting the loads in the caller

220:220: std::vector<Value*> Args; std::vector<Value*> Args;

226:226: CallSite::arg_iterator AI = CS.arg_begin(); CallSite::arg_iterator AI = CS.arg_begin(); for (Function::aiterator I = F->abegin(); I != F->aend();for (Function::aiterator I = F->abegin(); I != F->aend(); ++I, ++AI)++I, ++AI) if (!ArgsToPromote.count(I)) // if (!ArgsToPromote.count(I)) // Unmodified argument.Unmodified argument. Args.push_back(*AI);Args.push_back(*AI); else { // else { // Insert the load before the call.Insert the load before the call. LoadInst *LI = new LoadInst(*AI, (*AI)->getName()+".val",LoadInst *LI = new LoadInst(*AI, (*AI)->getName()+".val", Call); // Call); // Insertion pointInsertion point Args.push_back(LI);Args.push_back(LI); }}

Page 35: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: xform outline #3/4Arg Promotion: xform outline #3/4

#5: Replace the call site of F with call of NF#5: Replace the call site of F with call of NF

// // Create the call to NF with the adjusted arguments.Create the call to NF with the adjusted arguments.242:242: Instruction *New = new CallInst(NF, Args, "", Call); Instruction *New = new CallInst(NF, Args, "", Call);

// // If the return value of the old call was used, use the retval of the new call.If the return value of the old call was used, use the retval of the new call. if (!Call->use_empty())if (!Call->use_empty()) Call->replaceAllUsesWith(New);Call->replaceAllUsesWith(New);

// // Finally, remove the old call from the program, reducing the use-count of F.Finally, remove the old call from the program, reducing the use-count of F. Call->getParent()->getInstList().erase(Call);Call->getParent()->getInstList().erase(Call);

#6: Move code from old function to new Fn#6: Move code from old function to new Fn

259:259: NF->getBasicBlockList().splice(NF->begin(), NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());F->getBasicBlockList());

Page 36: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Arg Promotion: xform outline #4/4Arg Promotion: xform outline #4/4

#7: Change users of F’s arguments to use NF’s#7: Change users of F’s arguments to use NF’s

264: 264: for (Function::aiterator I = F->abegin(), I2 = NF->abegin();for (Function::aiterator I = F->abegin(), I2 = NF->abegin(); I != F->aend(); ++I, ++I2)I != F->aend(); ++I, ++I2) if (!ArgsToPromote.count(I)) { //if (!ArgsToPromote.count(I)) { // Not promoting this arg?Not promoting this arg? I->replaceAllUsesWith(I2); //I->replaceAllUsesWith(I2); // Use new arg, not old arg.Use new arg, not old arg. } else {} else { while (!I->use_empty()) { //while (!I->use_empty()) { // Only users can be loads.Only users can be loads. LoadInst *LI = cast<LoadInst>(I->use_back());LoadInst *LI = cast<LoadInst>(I->use_back()); LI->replaceAllUsesWith(I2);LI->replaceAllUsesWith(I2); LI->getParent()->getInstList().erase(LI);LI->getParent()->getInstList().erase(LI); }} }}

#8: Delete old function:#8: Delete old function:

286:286: F->getParent()->getFunctionList().erase(F); F->getParent()->getFunctionList().erase(F);

Page 37: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 38: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLVM tools: two flavorsLLVM tools: two flavors

““Primitive” tools: do a single jobPrimitive” tools: do a single job llvm-as: Convert from .ll (text) to .bc (binary)llvm-as: Convert from .ll (text) to .bc (binary) llvm-dis: Convert from .bc (binary) to .ll (text)llvm-dis: Convert from .bc (binary) to .ll (text) llvm-link: Link multiple .bc files togetherllvm-link: Link multiple .bc files together llvm-prof: Print profile output to human readersllvm-prof: Print profile output to human readers llvmc: Configurable compiler driverllvmc: Configurable compiler driver

Aggregate tools: pull in multiple featuresAggregate tools: pull in multiple features gccas/gccld: Compile/link-time optimizers for C/C++ FEgccas/gccld: Compile/link-time optimizers for C/C++ FE bugpoint: automatic compiler debuggerbugpoint: automatic compiler debugger llvm-gcc/llvm-g++: C/C++ compilersllvm-gcc/llvm-g++: C/C++ compilers

See also:See also: docs/CommandGuide/docs/CommandGuide/

Page 39: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

opt tool: LLVM modular optimizeropt tool: LLVM modular optimizer

Invoke arbitrary sequence of passes:Invoke arbitrary sequence of passes: Completely control PassManager from command lineCompletely control PassManager from command line Supports loading passes as plugins from .so filesSupports loading passes as plugins from .so files

opt -load foo.so -pass1 -pass2 -pass3 x.bc -o y.bcopt -load foo.so -pass1 -pass2 -pass3 x.bc -o y.bc

Passes “register” themselves:Passes “register” themselves:61: 61: RegisterOpt<SimpleArgPromotion> X("simpleargpromotion",RegisterOpt<SimpleArgPromotion> X("simpleargpromotion", "Promote 'by reference' arguments to 'by value'");"Promote 'by reference' arguments to 'by value'");

From this, they are exposed through opt:From this, they are exposed through opt:> opt -load libsimpleargpromote.so –help> opt -load libsimpleargpromote.so –help ...... -sccp - Sparse Conditional Constant Propagation-sccp - Sparse Conditional Constant Propagation -simpleargpromotion - Promote 'by reference' arguments to 'by-simpleargpromotion - Promote 'by reference' arguments to 'by -simplifycfg - Simplify the CFG-simplifycfg - Simplify the CFG ......

Page 40: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Running Arg Promotion with optRunning Arg Promotion with opt

Basic execution with ‘opt’:Basic execution with ‘opt’: opt -simpleargpromotion in.bc -o out.bcopt -simpleargpromotion in.bc -o out.bc

Load .bc file, run pass, write out resultsLoad .bc file, run pass, write out results Use “-load filename.so” if compiled into a libraryUse “-load filename.so” if compiled into a library PassManager resolves all dependenciesPassManager resolves all dependencies

Optionally choose an alias analysis to use:Optionally choose an alias analysis to use: opt –basicaa –simpleargpromotionopt –basicaa –simpleargpromotion (default) (default)

Alternatively, Alternatively, –steens-aa, –anders-aa, –ds-aa, …–steens-aa, –anders-aa, –ds-aa, …

Other useful options available:Other useful options available: -stats-stats: Print statistics collected from the passes: Print statistics collected from the passes -time-passes-time-passes: Time each pass being run, print output: Time each pass being run, print output

Page 41: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Example -stats output (gccas 176.gcc)Example -stats output (gccas 176.gcc)===-------------------------------------------------------------------------======-------------------------------------------------------------------------===

... Statistics Collected ...... Statistics Collected ...

===-------------------------------------------------------------------------======-------------------------------------------------------------------------===

23426 adce - Number of instructions removed23426 adce - Number of instructions removed

1663 adce - Number of basic blocks removed1663 adce - Number of basic blocks removed

5052592 bytecodewriter - Number of bytecode bytes written5052592 bytecodewriter - Number of bytecode bytes written

57489 cfgsimplify - Number of blocks simplified57489 cfgsimplify - Number of blocks simplified

4186 constmerge - Number of global constants merged4186 constmerge - Number of global constants merged

211 dse - Number of stores deleted211 dse - Number of stores deleted

15943 gcse - Number of loads removed15943 gcse - Number of loads removed

54245 gcse - Number of instructions removed54245 gcse - Number of instructions removed

253 inline - Number of functions deleted because all callers found253 inline - Number of functions deleted because all callers found

3952 inline - Number of functions inlined3952 inline - Number of functions inlined

9425 instcombine - Number of constant folds9425 instcombine - Number of constant folds

160469 instcombine - Number of insts combined160469 instcombine - Number of insts combined

208 licm - Number of load insts hoisted or sunk208 licm - Number of load insts hoisted or sunk

4982 licm - Number of instructions hoisted out of loop4982 licm - Number of instructions hoisted out of loop

350 loop-unroll - Number of loops completely unrolled350 loop-unroll - Number of loops completely unrolled

30156 mem2reg - Number of alloca's promoted30156 mem2reg - Number of alloca's promoted

2934 reassociate - Number of insts with operands swapped2934 reassociate - Number of insts with operands swapped

650 reassociate - Number of insts reassociated650 reassociate - Number of insts reassociated

67 scalarrepl - Number of allocas broken up67 scalarrepl - Number of allocas broken up

279 tailcallelim - Number of tail calls removed279 tailcallelim - Number of tail calls removed

25395 tailduplicate - Number of unconditional branches eliminated25395 tailduplicate - Number of unconditional branches eliminated

....................................................

Page 42: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Example -time-passes (gccas 176.gcc)Example -time-passes (gccas 176.gcc)===-------------------------------------------------------------------------======-------------------------------------------------------------------------===

... Pass execution timing report ...... Pass execution timing report ...

===-------------------------------------------------------------------------======-------------------------------------------------------------------------===

---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ------User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ---

16.2400 ( 23.0%) 0.0000 ( 0.0%) 16.2400 ( 22.9%) 16.2192 ( 22.9%) Global Common Subexpression Elimination16.2400 ( 23.0%) 0.0000 ( 0.0%) 16.2400 ( 22.9%) 16.2192 ( 22.9%) Global Common Subexpression Elimination

11.1200 ( 15.8%) 0.0499 ( 13.8%) 11.1700 ( 15.8%) 11.1028 ( 15.7%) Reassociate expressions11.1200 ( 15.8%) 0.0499 ( 13.8%) 11.1700 ( 15.8%) 11.1028 ( 15.7%) Reassociate expressions

6.5499 ( 9.3%) 0.0300 ( 8.3%) 6.5799 ( 9.3%) 6.5824 ( 9.3%) Bytecode Writer6.5499 ( 9.3%) 0.0300 ( 8.3%) 6.5799 ( 9.3%) 6.5824 ( 9.3%) Bytecode Writer

3.2499 ( 4.6%) 0.0100 ( 2.7%) 3.2599 ( 4.6%) 3.2140 ( 4.5%) Scalar Replacement of Aggregates3.2499 ( 4.6%) 0.0100 ( 2.7%) 3.2599 ( 4.6%) 3.2140 ( 4.5%) Scalar Replacement of Aggregates

3.0300 ( 4.3%) 0.0499 ( 13.8%) 3.0800 ( 4.3%) 3.0382 ( 4.2%) Combine redundant instructions3.0300 ( 4.3%) 0.0499 ( 13.8%) 3.0800 ( 4.3%) 3.0382 ( 4.2%) Combine redundant instructions

2.6599 ( 3.7%) 0.0100 ( 2.7%) 2.6699 ( 3.7%) 2.7339 ( 3.8%) Dead Store Elimination2.6599 ( 3.7%) 0.0100 ( 2.7%) 2.6699 ( 3.7%) 2.7339 ( 3.8%) Dead Store Elimination

2.1600 ( 3.0%) 0.0300 ( 8.3%) 2.1900 ( 3.0%) 2.1924 ( 3.1%) Function Integration/Inlining2.1600 ( 3.0%) 0.0300 ( 8.3%) 2.1900 ( 3.0%) 2.1924 ( 3.1%) Function Integration/Inlining

2.1600 ( 3.0%) 0.0100 ( 2.7%) 2.1700 ( 3.0%) 2.1125 ( 2.9%) Sparse Conditional Constant Propagation2.1600 ( 3.0%) 0.0100 ( 2.7%) 2.1700 ( 3.0%) 2.1125 ( 2.9%) Sparse Conditional Constant Propagation

1.6600 ( 2.3%) 0.0000 ( 0.0%) 1.6600 ( 2.3%) 1.6389 ( 2.3%) Aggressive Dead Code Elimination1.6600 ( 2.3%) 0.0000 ( 0.0%) 1.6600 ( 2.3%) 1.6389 ( 2.3%) Aggressive Dead Code Elimination

1.4999 ( 2.1%) 0.0100 ( 2.7%) 1.5099 ( 2.1%) 1.4462 ( 2.0%) Tail Duplication1.4999 ( 2.1%) 0.0100 ( 2.7%) 1.5099 ( 2.1%) 1.4462 ( 2.0%) Tail Duplication

1.5000 ( 2.1%) 0.0000 ( 0.0%) 1.5000 ( 2.1%) 1.4410 ( 2.0%) Post-Dominator Set Construction1.5000 ( 2.1%) 0.0000 ( 0.0%) 1.5000 ( 2.1%) 1.4410 ( 2.0%) Post-Dominator Set Construction

1.3200 ( 1.8%) 0.0000 ( 0.0%) 1.3200 ( 1.8%) 1.3722 ( 1.9%) Canonicalize natural loops1.3200 ( 1.8%) 0.0000 ( 0.0%) 1.3200 ( 1.8%) 1.3722 ( 1.9%) Canonicalize natural loops

1.2700 ( 1.8%) 0.0000 ( 0.0%) 1.2700 ( 1.7%) 1.2717 ( 1.7%) Merge Duplicate Global Constants1.2700 ( 1.8%) 0.0000 ( 0.0%) 1.2700 ( 1.7%) 1.2717 ( 1.7%) Merge Duplicate Global Constants

1.0300 ( 1.4%) 0.0000 ( 0.0%) 1.0300 ( 1.4%) 1.1418 ( 1.6%) Combine redundant instructions1.0300 ( 1.4%) 0.0000 ( 0.0%) 1.0300 ( 1.4%) 1.1418 ( 1.6%) Combine redundant instructions

0.9499 ( 1.3%) 0.0400 ( 11.1%) 0.9899 ( 1.4%) 0.9979 ( 1.4%) Raise Pointer References0.9499 ( 1.3%) 0.0400 ( 11.1%) 0.9899 ( 1.4%) 0.9979 ( 1.4%) Raise Pointer References

0.9399 ( 1.3%) 0.0100 ( 2.7%) 0.9499 ( 1.3%) 0.9688 ( 1.3%) Simplify the CFG0.9399 ( 1.3%) 0.0100 ( 2.7%) 0.9499 ( 1.3%) 0.9688 ( 1.3%) Simplify the CFG

0.9199 ( 1.3%) 0.0300 ( 8.3%) 0.9499 ( 1.3%) 0.8993 ( 1.2%) Promote Memory to Register0.9199 ( 1.3%) 0.0300 ( 8.3%) 0.9499 ( 1.3%) 0.8993 ( 1.2%) Promote Memory to Register

0.9600 ( 1.3%) 0.0000 ( 0.0%) 0.9600 ( 1.3%) 0.8742 ( 1.2%) Loop Invariant Code Motion0.9600 ( 1.3%) 0.0000 ( 0.0%) 0.9600 ( 1.3%) 0.8742 ( 1.2%) Loop Invariant Code Motion

0.5600 ( 0.7%) 0.0000 ( 0.0%) 0.5600 ( 0.7%) 0.6022 ( 0.8%) Module Verifier0.5600 ( 0.7%) 0.0000 ( 0.0%) 0.5600 ( 0.7%) 0.6022 ( 0.8%) Module Verifier

… …

Page 43: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Analyze tool: Visualize analysis resultsAnalyze tool: Visualize analysis results

Print most LLVM data structuresPrint most LLVM data structures Dominators, loops, alias sets, CFG, call graph, …Dominators, loops, alias sets, CFG, call graph, … Converts most LLVM data structures to ‘dot’ graphsConverts most LLVM data structures to ‘dot’ graphs

Function main

%struct.vert_st* array: SMR

%struct.vert_st array: HIMR

%struct.hash: HIMR

%struct.vert_st*: HMR

%struct.hash_entry* array: HIMR int (uint): GI%.hashfunc_4

%struct.hash_entry: HIMR

void: UI

%struct.vert_st*: GIMR%.MyVertexList_2

call

r f

Call Graph

External fn

Indirect call node

sqrt

printf

main

build_lateral Compute_Lateral

build_branch Compute_Branch

optimize_node

CFG for 'main' function

build_tree.entry:

T F

__main.entry:

endif.0.i:

no_exit.i10:

T F

Compute_Tree.entry26:

no_exit.i2:

T F

Compute_Tree.entry:

T F

shortcirc_next:

T F

else:loopexit:

CFG for 'optimize_node' function

entry:

loopentry.0:

T F

then.0:

endif.0:

T F

then.1:

T F

loopexit.2:

T F

make_orthogonal.entry73:

endif.i70:

make_orthogonal.entry:

T F

endif.i:

then.2:

T F

endif.2:

T F

then.3:

shortcirc_done.1:

T F

shortcirc_next.1:

T F

return:

shortcirc_next.2:

T F

Page 44: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLC Tool: Static code generatorLLC Tool: Static code generator

Compiles LLVM Compiles LLVM native assembly language native assembly language Currently for X86, Sparc, PowerPC (others in alpha)Currently for X86, Sparc, PowerPC (others in alpha) llc file.bc -o file.s -march=x86llc file.bc -o file.s -march=x86 as file.s –o file.oas file.s –o file.o

Compiles LLVM Compiles LLVM portable C code portable C code llc file.bc -o file.c -march=cllc file.bc -o file.c -march=c gcc –c file.c –o file.ogcc –c file.c –o file.o

Targets are modular & dynamically loadable:Targets are modular & dynamically loadable: llc –load libarm.so file.bc -march=armllc –load libarm.so file.bc -march=arm

Page 45: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

The LLVM Code GeneratorThe LLVM Code Generator

Target independent:Target independent: Driven by an algorithm independent target descriptionDriven by an algorithm independent target description

Data layout, Register, Instruction, Scheduling, …Data layout, Register, Instruction, Scheduling, …

Basic code generator layout:Basic code generator layout:

All passes are replaceableAll passes are replaceable e.g. Trivial to change and add register allocatorse.g. Trivial to change and add register allocators

Targets can add custom passesTargets can add custom passes e.g. X86 has special support for FP stacke.g. X86 has special support for FP stack

See also:See also: docs/CodeGenerator.htmldocs/CodeGenerator.html

Instruction Selection

LLVM .s fileMachine

SSA OptsRegister Allocator

Instr Sched

Code Emission

Exposes all target-specific details about a function

(calling conventions, etc)

Scheduling, Peephole, ?

Target IndependentTarget Specific

(generated)Target Specific

(by hand for now) 4 algorithms available todayllc -regalloc=foo

Page 46: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Porting LLVM to a new targetPorting LLVM to a new target

LLVM targets are very easy to write:LLVM targets are very easy to write: Anecdotal evidence suggests 1 week for a basic portAnecdotal evidence suggests 1 week for a basic port

… … for someone familiar with the target machine and for someone familiar with the target machine and compilers in general, but not with LLVMcompilers in general, but not with LLVM

LLVM targets are written with “tablegen” toolLLVM targets are written with “tablegen” tool Simple declarative syntaxSimple declarative syntax Designed to factor out redundancy in target descDesigned to factor out redundancy in target desc

Some C++ code is still requiredSome C++ code is still required Primarily in the instruction selectorPrimarily in the instruction selector Continuing work to improve thisContinuing work to improve this

See also:See also: docs/TableGenFundamentals.htmldocs/TableGenFundamentals.html and and WritingAnLLVMBackend.htmlWritingAnLLVMBackend.html

Page 47: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

LLI allows direct execution of .bc filesLLI allows direct execution of .bc files E.g.: E.g.: lli grep.bc -i foo *.clli grep.bc -i foo *.c

LLI uses a Just-In-Time compiler if available:LLI uses a Just-In-Time compiler if available: Uses same code generator as LLCUses same code generator as LLC

Optionally uses faster components than LLCOptionally uses faster components than LLC Emits machine code to memory instead of “.s” fileEmits machine code to memory instead of “.s” file JIT is a library that can be embedded in other toolsJIT is a library that can be embedded in other tools

Otherwise, it uses the LLVM interpreter:Otherwise, it uses the LLVM interpreter: Interpreter is extremely simple and very slowInterpreter is extremely simple and very slow Interpreter is portable though!Interpreter is portable though!

LLI Tool: LLVM Execution EngineLLI Tool: LLVM Execution Engine

Page 48: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

C and C++ Program Test SuiteC and C++ Program Test Suite

Large collection of programs and benchmarks:Large collection of programs and benchmarks: Standard suites (e.g. SPEC 95/2000, Olden, Ptrdist, Standard suites (e.g. SPEC 95/2000, Olden, Ptrdist,

McCat, Stanford, Freebench, Shootout…) McCat, Stanford, Freebench, Shootout…) Individual programs: sgefa, siod, sim, pi, povray, …Individual programs: sgefa, siod, sim, pi, povray, … Proprietary suites (e.g. SPEC) require suite sourceProprietary suites (e.g. SPEC) require suite source

Consistent build environment:Consistent build environment: Easy add hooks to build for profiling/instrumentationEasy add hooks to build for profiling/instrumentation Easy to get performance numbers from entire test suiteEasy to get performance numbers from entire test suite

Entire test suite is checked every night:Entire test suite is checked every night: Hosted on Linux,Solaris,FreeBSD on X86,Sparc & PPCHosted on Linux,Solaris,FreeBSD on X86,Sparc & PPC

See also:See also: docs/TestingGuide.htmldocs/TestingGuide.html

Page 49: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Integrated Debugging ToolsIntegrated Debugging Tools

Extensive assertions throughout codeExtensive assertions throughout code Find problems as early as possible (close to source)Find problems as early as possible (close to source)

LLVM IR Verifier: Checks modules for validityLLVM IR Verifier: Checks modules for validity Checks type properties, dominance properties, etc.Checks type properties, dominance properties, etc. Automatically run by optAutomatically run by opt Problem found?: print an error message and abortProblem found?: print an error message and abort

LLVM IR Leak DetectorLLVM IR Leak Detector Efficient and simple “garbage collector” for IR objectsEfficient and simple “garbage collector” for IR objects Ensure IR objects are deallocated appropriatelyEnsure IR objects are deallocated appropriately

Page 50: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

The Bugpoint automated bug finderThe Bugpoint automated bug finder

Simple idea: automate ‘binary’ search for bugSimple idea: automate ‘binary’ search for bug Bug isolation: which passes interact to produce bugBug isolation: which passes interact to produce bug Test case reductionTest case reduction: reduce input program: reduce input program

Optimizer/Codegen crashes:Optimizer/Codegen crashes: Throw portion of test case away, check for crashThrow portion of test case away, check for crash

If so, keep goingIf so, keep going Otherwise, revert and try something elseOtherwise, revert and try something else

Extremely effective in practiceExtremely effective in practice

Simple greedy algorithms for test reductionSimple greedy algorithms for test reduction Completely black-box approachCompletely black-box approach

See also:See also: docs/Bugpoint.htmldocs/Bugpoint.html

Page 51: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Debugging MiscompilationsDebugging Miscompilations

Optimizer miscompilation:Optimizer miscompilation: Split testcase in two, optimize one. Still broken?Split testcase in two, optimize one. Still broken? Keep shrinking the portion being optimizedKeep shrinking the portion being optimized

Codegen miscompilation:Codegen miscompilation: Split testcase in two, compile one with CBE, broken?Split testcase in two, compile one with CBE, broken? Shrink portion being compiled with non CBE codegenShrink portion being compiled with non CBE codegen

Code splitting granularities:Code splitting granularities: Take out whole functionsTake out whole functions Take out loop nestsTake out loop nests Take out individual basic blocksTake out individual basic blocks

Page 52: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

How well does this thing work?How well does this thing work?

Extremely effective:Extremely effective: Can often reduce a 100K LOC program and 60 Can often reduce a 100K LOC program and 60

passes to a few basic blocks and 1 pass in 5 minutespasses to a few basic blocks and 1 pass in 5 minutes Crashes are found much faster than miscompilationsCrashes are found much faster than miscompilations

no need to run the program to test a reductionno need to run the program to test a reduction

Interacts with integrated debugging toolsInteracts with integrated debugging tools Runtime errors are detected fasterRuntime errors are detected faster

Limitations:Limitations: Program must be deterministicProgram must be deterministic

… … or modified to be soor modified to be so Finds “a” bug, not “the” bugFinds “a” bug, not “the” bug

Page 53: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Tutorial OverviewTutorial Overview

Introduction to the running exampleIntroduction to the running example LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler Overview

High-level view of an example LLVM compilerHigh-level view of an example LLVM compiler

The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Set IR overview and type-systemIR overview and type-system

LLVM C++ IR and important API’sLLVM C++ IR and important API’s Basics, PassManager, dataflow, ArgPromotionBasics, PassManager, dataflow, ArgPromotion

Important LLVM ToolsImportant LLVM Tools opt, code generator, JIT, test suite, bugpointopt, code generator, JIT, test suite, bugpoint

Example applications of LLVMExample applications of LLVM

Page 54: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 1: Edge or Path ProfilingUse Case 1: Edge or Path Profiling

Goal: Goal: Profiling Research or PGOProfiling Research or PGO Implementation: Implementation:

FunctionPassFunctionPass: LLVM-to-LLVM transformation: LLVM-to-LLVM transformation Instrumentation: Use CFG, intervals, dominators Instrumentation: Use CFG, intervals, dominators Code generation: Use C or any native back endCode generation: Use C or any native back end Profile feedback: Use profile query interfaceProfile feedback: Use profile query interface

Core extensions needed: Core extensions needed: nonenone Major LLVM BenefitsMajor LLVM Benefits

Language-independence, CFG, very simple IRLanguage-independence, CFG, very simple IR

Page 55: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 2: Alias AnalysisUse Case 2: Alias Analysis

Goal: Goal: Research on new alias analysis algorithmsResearch on new alias analysis algorithms Implementation:Implementation:

ModulePassModulePass: Whole-program analysis pass on LLVM: Whole-program analysis pass on LLVM Use type information; SSA; heap/stack/globalsUse type information; SSA; heap/stack/globals Compare Compare SimpleAASimpleAA, , Steensgard’sSteensgard’s, , Andersen’sAndersen’s, , DSADSA Evaluate many clients via Evaluate many clients via AliasAnalysis AliasAnalysis interfaceinterface

Core extensions needed: Core extensions needed: nonenone Major LLVM BenefitsMajor LLVM Benefits

Language-independence, type info, SSA, DSA, IPOLanguage-independence, type info, SSA, DSA, IPO AliasAnalysis AliasAnalysis interface with many pre-existing clientsinterface with many pre-existing clients

Page 56: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 3: LDS PrefetchingUse Case 3: LDS Prefetching

Goal: Goal: Prefetching linked data structuresPrefetching linked data structures Implementation: Implementation:

ModulePassModulePass: Link-time LLVM-to-LLVM transformation: Link-time LLVM-to-LLVM transformation Code transformationsCode transformations: use type info, loop analysis, : use type info, loop analysis,

unrolling, prefetch insertionunrolling, prefetch insertion Data transformations Data transformations (e.g,. adding history pointers): (e.g,. adding history pointers):

use use strongstrong type info from DSA, IPOtype info from DSA, IPO Core extensions needed:Core extensions needed:

Prefetch operation: add as Prefetch operation: add as intrinsicintrinsic (in progress) (in progress) Major LLVM BenefitsMajor LLVM Benefits

Language-independence, type info, DSA, IPOLanguage-independence, type info, DSA, IPO

Page 57: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 4: Language Front endUse Case 4: Language Front end

Goal: Goal: Use LLVM to implement a new languageUse LLVM to implement a new language Implementation:Implementation:

Parser (say to AST), Semantic checkingParser (say to AST), Semantic checking AST-to-LLVM translatorAST-to-LLVM translator

Core extensions needed: Core extensions needed: dependsdepends High-level type system is High-level type system is omitted by designomitted by design

Major LLVM BenefitsMajor LLVM Benefits Low-level, but powerful type systemLow-level, but powerful type system Very simple IR to generate (e.g., compare GCC RTL)Very simple IR to generate (e.g., compare GCC RTL) Extensive global and IP optimization frameworkExtensive global and IP optimization framework JIT engine, native back-ends, C back-endJIT engine, native back-ends, C back-end

Page 58: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 5: JIT CompilerUse Case 5: JIT Compiler

Goal: Goal: Write JIT compiler for a bytecode languageWrite JIT compiler for a bytecode language Implementation: Implementation:

Extend the LLVM JIT frameworkExtend the LLVM JIT framework Simple JITSimple JIT: Fast translation from bytecode to LLVM : Fast translation from bytecode to LLVM

(then use LLVM JIT + GC)(then use LLVM JIT + GC) Optimizing JITOptimizing JIT: Language-specific optimizations + fast : Language-specific optimizations + fast

translation (then use LLVM optimizations, JIT, GC)translation (then use LLVM optimizations, JIT, GC) Core extensions needed: Core extensions needed: none in generalnone in general Major LLVM BenefitsMajor LLVM Benefits

Compact, typed, language-independent IRCompact, typed, language-independent IR Existing JIT framework and GCExisting JIT framework and GC

Page 59: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Use Case 6: Architecture ResearchUse Case 6: Architecture Research

Goal: Goal: Compiler support for new architecturesCompiler support for new architectures Implementation:Implementation:

Add new machine description (or modify one)Add new machine description (or modify one) Add any new LLVM-to-LLVM transformationsAdd any new LLVM-to-LLVM transformations

Core extensions needed: Core extensions needed: depends on goalsdepends on goals Imminent features:Imminent features: modulo sched; vector opsmodulo sched; vector ops

Major LLVM BenefitsMajor LLVM Benefits Low-level, typed, machine-independent IRLow-level, typed, machine-independent IR Explicit register/memory architectureExplicit register/memory architecture Aggressive mid-level and back-end compiler frameworkAggressive mid-level and back-end compiler framework Full-system evaluation: Full-system evaluation: applications, libraries, even OSapplications, libraries, even OS

Page 60: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Five point LLVM ReviewFive point LLVM Review Extremely simple IR to learn and useExtremely simple IR to learn and use

1-to-1 correspondence between .ll, .bc, and C++ IR1-to-1 correspondence between .ll, .bc, and C++ IR Very positive user reactionsVery positive user reactions

Powerful and modular optimizerPowerful and modular optimizer Easy to extend, or just use what is already thereEasy to extend, or just use what is already there

Clean and modular code generatorClean and modular code generator Easy to retarget, easy to replace/tweak componentsEasy to retarget, easy to replace/tweak components

Many “productivity tools” (bugpoint, verifier)Many “productivity tools” (bugpoint, verifier) Get more done, quicker!Get more done, quicker!

Active dev community, good documentationActive dev community, good documentation Mailing lists, IRC, doxygen, extensive docsMailing lists, IRC, doxygen, extensive docs

Page 61: The LLVM Compiler Framework and Infrastructure Vikram Adve vadve@cs.uiuc.edu Chris Lattner lattner@cs.uiuc.edu  LCPC Tutorial:

Chris Lattner – [email protected]

http://llvm.cs.uiuc.edu/

Get started with LLVM!Get started with LLVM!

Download latest release or CVS:Download latest release or CVS:

http://llvm.cs.uiuc.edu/releases/http://llvm.cs.uiuc.edu/releases/

Follow the “Getting Started Guide”:Follow the “Getting Started Guide”:

http://llvm.cs.uiuc.edu/docs/http://llvm.cs.uiuc.edu/docs/GettingStarted.htmlGettingStarted.html

Walks you through install and setupWalks you through install and setup Lots of other docs available in “docs” directoryLots of other docs available in “docs” directory Join us on mailing lists and IRCJoin us on mailing lists and IRC

Happy hacking!Happy hacking!


Recommended