LLVM Crash Course
LLVM Crash Course
Time/Space
Improvements
Instruction Selection
Register Allocation
Instruction Scheduling
C, C++, Obj-C
Source Code
Frontend
Language-Specific
AST
Optimizer
Backend
Machine
Code
Fortran
Source Code
Frontend
Language-Specific
AST
Optimizer
Backend
Machine
Code
LLVM Approach
Your Code
(C, C++,
Obj-C)
Frontend
(Clang)
Your Code
(Python)
Frontend
(Python)
Your Code
(Fortran)
Frontend
(Fortran)
Common
Intermediate
Representation
LLVM
Optimizer
LLVM
Backend
(ARM)
Machine
Code
(ARM)
LLVM
Backend
(x86)
Machine
Code
(x86)
LLVM
Backend
(PowerPC)
Machine
Code
(PowerPC)
C:
IR:
int main() {
int a = 10;
int b = a + 5;
return 0;
}
C:
IR:
int main() {
int a = 10;
int b = a + 5;
return 0;
}
C:
IR:
#include <stdio.h>
int main() {
printf(Hello
world!\n);
return 42;
}
C:
IR:
#include <stdio.h>
int main() {
printf(Hello
world!\n);
return 42;
}
Passes
Common
Intermediate
Representation
Your Code
(C, C++,
Obj-C)
Front End
(Clang)
LLVM
Optimizer
Back End
(LLVM)
Machine Code
(x86, ARM)
Passes
An LLVM pass is an operation on a piece of IR
Mutate/transform IR
Compute higher-order information about IR
Used for optimizations and analysis
Can depend on other passes
Transformation Passes
Analysis Passes
#include "llvm/Pass.h
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Hello : public FunctionPass {
static char ID;
Hello() : FunctionPass (ID) {}
bool runOnFunction (Function &F) override
{
errs() << "Hello: " ;
errs().write_escaped (F.getName()) <<
'\n';
return false;
}
};
}
char Hello::ID = 0;
static RegisterPass <Hello> X("hello", "Hello
World Pass" , false, false );
You may encounter a strange error: no member named gets in global namespace. Not all versions of GCCs std lib
implementation are compatible with all versions of Clang.