Abstract (Last updated 9/16/16)
Abstract: In this talk, Michael Shah (“Mike”) will be presenting an introduction to the LLVM Compiler Infrastructure. A discussion of what LLVM is, who is using it, and why you might be interested in using LLVM will be presented during the first part of the talk. The second part of the talk will be an interactive lab, where we build our first function pass. We will build on top of our first function pass, to begin outputting some program metrics about programs. Mike will also be presenting some steps on how to proceed further and what resources are available for working with LLVM.
Materials: Please bring a laptop with LLVM 3.9 setup
Resources:
Downloading and setting up LLVM: http://llvm.org/docs/GettingStarted.html#checkout
A introduction guide: http://adriansampson.net/blog/llvm.html
Contact: mshah.475@gmail.com
Twitter: @MichaelShah
Terminology (Open in a second browser if you like)
Introduction to LLVM
Michael Shah
@MichaelShah
9/30/16
30-60 Minutes
Demo Time! Right from the start!
Who Am I?
Mike Shah
Ph.D. Candidate @Tufts -- Graduating in 2017.
I like teaching, guitar, running, hiking, and anything in computer science under the domain of graphics, visualization, concurrency, and parallelism.
I research in performance of Concurrent and Parallel Systems
Goals for Today
Goals for Tomorrow
Because you’ll be ready to think about more solutions
Learn a little about LLVM
LLVM (Formerly known as Low Level Virtual Machine--but it’s more!)
Why is LLVM Popular
Modularity [http://www.aosabook.org/en/llvm.html]
LLVM’s IR - Well defined and aides in many analysis
LLVM Tools
Core Design Ideas
Some Examples
#include <iostream>
int main(){
int x = 0
std::cout << “Hello LLVM” << std::endl;
return 0;
}
Let us now output the IR of our program
Pause -- Really take a second to look at the IR
Share with the group what you notice
The IR is very assembly like -- very readable!
In fact the machine can read it, and the machine can directly execute the IR using its Just-in-time (JIT) execution engine.
Let’s do it now using lli (“L L I”)
Wait, what happened?
lli executes bitcode (binary format of IR)
Ah, my mistake--lets convert to the .bc (bitcode) that our JIT engine can execute more efficiently (Why?).
Ah ha, now this unreadable mess is looking better--let us execute!
The full circle -- compile our IR to assembly (.s file)
See a full list of available targets
Opt - An Important Tool
Lets run opt -
./../llvm_build/bin/opt hello.ll --time-passes
Different Types of Passes in LLVM
Writing Our First Function Pass
Goal - Find all of the Functions in a program
We will be working in llvm/lib/Transforms/Hello/Hello.cpp
This is given to you when you download LLVM (You can learn how to add more passes here)
Run our pass with opt
./../llvm_build/bin/opt -load ./../llvm_build/lib/LLVMHello.so -hello < hello.bc > /dev/null
Neat--we see all of the functions!
(You’ll notice there is hello and hello2, run both of them)
Let us write a new pass -- This time to collect some data
void oneloop(){
int x = 0;
while(x<10){
x++;
}
}
void twoloop(){
for(int i =0; i < 2; ++i){
for(int j=0; j < 3; ++j){
}}}
int main(){
oneloop();
twoloop();
return 0;
}
The Stats Pass
Results
Let’s add more!
What can we do with instruction information?
Find Direct Calls
Added new header: #include "llvm/IR/CallSite.h"
Going Further (Challenges/Project Ideas)
Time permitting:
(Bonus: On outputting .dot files)
Resources
Resources
More Guidance - Your LLVM Syllabus
Thank You!
Some Gotcha’s
Sneak Preview of Future Talk