The LLVM Compiler Framework and Infrastructure
The LLVM Compiler Framework and Infrastructure
Chris Lattner
Vikram Adve
http://llvm.cs.uiuc.edu/
LCPC Tutorial: September 22, 2004
Acknowledgements
UIUC Contributors:
External Contributors:
Tanya Brethour
Misha Brukman
Henrik Bach
Nate Begeman
Cameron Buschardt
John Criswell
Jeff Cohen
Paolo Invernizzi
Alkis Evlogimenos
Brian Gaeke
Brad Jones
Vladimir Merzliakov
Ruchira Sasanka
Anand Shukla
Vladimir Prus
Reid Spencer
Bill Wendling
Funding:
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 in part
by the NSF Operating Systems and Compilers program (grant #CCR-9988482), the
NSF Embedded Systems program (grant #CCR-0209202), the MARCO/DARPA
Gigascale Systems Research Center (GSRC), IBM through the DARPA-funded
PERCS project, and the Motorola University Partnerships in Research program.
Chris Lattner
Tutorial Overview
We want:
int callee(int X) {
return X+1;
}
int caller() {
return callee(4);
}
compiles to
Tutorial Overview
llvmgcc
.o file
C++ file
llvmg++
.o file
Compile Time
llvm linker
executable
Link Time
Distinguishing features:
Uses LLVM optimizers, not GCC optimizers
.o files contain LLVM IR/bytecode, not machine code
Executable can be bytecode (JITd) or machine code
Chris Lattner
llvmgcc
.o file
C++ file
llvmg++
.o file
C to LLVM
Frontend
Compile-time
Optimizer
C++ to LLVM
Frontend
Compile-time
Optimizer
cc1
gccas
cc1plus
gccas
Modified
of
GCC
LLVMversion
IR
LLVM
Emits
LLVM IR asVerifier
text file
Parser
Lowers C AST to LLVM
Modified
version
of G++
40 LLVM Analysis
&
LLVM .bc
Emits LLVM
as text file
Optimization Passes
FileIRWriter
Lowers C++ AST to LLVM
.o file
.o file
LLVM
Linker
llvm linker
executable
Link-time
Optimizer
Native executable
C Compiler
gcc
Native
executable
Tutorial Overview
Goals of LLVM IR
loop:
%i.1 = phi int [ 0, %bb0 ], [ %i.2, %loop ]
%AiAddr = getelementptr float* %A, int %i.1
for (i = 0; i < N;
call void %Sum(float %AiAddr, %pair* %P)
++i)
%i.2 = add int %i.1, 1
Sum(&A[i], &P); %tmp.4 = setlt int %i.1, %N
br bool %tmp.4, label %loop,
label %outloop
Chris Lattner
loop:
%i.1 = phi int [ 0, %bb0 ], [ %i.2, %loop ]
%AiAddr = getelementptr float* %A, int %i.1
for (i = 0; i < N;
call void %Sum(float %AiAddr, %pair* %P)
++i)
%i.2 = add int %i.1, 1
Sum(&A[i], &P); %tmp.4 = setlt int %i.1, %N
br bool %tmp.4, label %loop,
label %outloop
Chris Lattner
Examples of lowering:
References turn into pointers: T& T*
Complex numbers: complex float { float, float }
Bitfields: struct X { int Y:4; int Z:2; } { int }
Inheritance: class T : S { int X; } { S, int }
Methods: class T { void foo(); } void foo(T*)
Chris Lattner
Linker
All loads/stores
internalizes
are
Stack allocation is
most
explicit
functions
in the in
LLVM
most
explicit in LLVM
representation
cases
Chris Lattner
Other transformation
Update
all
sites of
Insert
Change
load
thecall
instructions
prototype
(-mem2reg) cleans up
callee
for
intothe
allfunction
callers
the rest
Tutorial Overview
const {
aliases
data layout
CallGraph
Chris Lattner
bool SimpleArgPromotion::
runOnSCC(const std::vector<CallGraphNode*> &SCC) {
bool Changed = false, LocalChange;
do {
// Iterate until we stop promoting from this SCC.
LocalChange = false;
// Attempt to promote arguments from all functions in this SCC.
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
LocalChange |= PromoteArguments(SCC[i]);
Changed |= LocalChange; // Remember that we changed something.
} while (LocalChange);
return Changed;
// Passes return true if something changed.
}
Chris Lattner
Chris Lattner
load P
Modifie
s *P?
Modifie
s *P?
Modifie
s *P?
Entry
load P
load P
175: for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);
PI != E; ++PI)
// Loop over predecessors of BB.
// Check each block from BB to entry (DF search on inverse graph).
for (idf_iterator<BasicBlock*> I = idf_begin(*PI);
I != idf_end(*PI); ++I)
// Might *P be modified in this basic block?
if (AA.canBasicBlockModify(**I, Arg, LoadSize))
return false;
Chris Lattner
Chris Lattner
Chris Lattner
Chris Lattner
Chris Lattner
Tutorial Overview
Chris Lattner
(default)
Chris Lattner
Chris Lattner
Chris Lattner
Target independent:
Driven by an algorithm independent target description
LLVM
Machine
SSA Opts
Target
Specific (byare
All passes
hand for now)
Register
Allocator
Instr
Sched
.s file
replaceable
Target Independent
Code
Emission
Target Specific
(generated)
4 algorithms
register
availableallocators
today
llc -regalloc=foo
Scheduling,
Peephole, ? passes
Targets can
add custom
Exposes
all target-specific
e.g.
X86 has special
details about a function
(calling conventions, etc)
Chris Lattner
Optimizer/Codegen crashes:
Throw portion of test case away, check for crash
Debugging Miscompilations
Optimizer miscompilation:
Split testcase in two, optimize one. Still broken?
Keep shrinking the portion being optimized
Codegen miscompilation:
Split testcase in two, compile one with CBE, broken?
Shrink portion being compiled with non CBE codegen
Extremely effective:
Can often reduce a 100K LOC program and 60 passes
Limitations:
Program must be deterministic
or modified to be so
Tutorial Overview
Chris Lattner
http://llvm.cs.uiuc.edu/docs/GettingStarted.ht
ml
Walks you through install and setup
Lots of other docs available in docs directory
Join us on mailing lists and IRC
Happy hacking!
Chris Lattner