1 of 40

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

2 of 40

Terminology (Open in a second browser if you like)

  • IR - Intermediate representation (Human-readable, 3 address, assembly like representation)
  • Bitcode (.bc) - LLVM binary format of the IR
  • JIT - Just-In-Time Compiler
  • SSA - Single Static Analysis

3 of 40

Introduction to LLVM

Michael Shah

@MichaelShah

9/30/16

30-60 Minutes

4 of 40

Demo Time! Right from the start!

  • So you know what to pay attention to!
  • (In case you walked into the wrong room by accident!)
  • While I set up, pair up with someone next to you who has a laptop with LLVM 3.9 ready to go
    • By ready to go, I mean you compiled it and got 0 errors-- that’s it!
    • You can work on one laptop or your machines independently
    • Oh, and say hello to your partner and state your name.

5 of 40

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

http://michaeldshah.net/

​

​

6 of 40

Goals for Today

  • Learn a little about LLVM
  • Use LLVM Tools to Play with IR
  • Write a Function Pass

7 of 40

Goals for Tomorrow

Because you’ll be ready to think about more solutions

  • Know some resources available to continue growing
  • Perform Static Analysis, generate call graphs
  • Manipulate IR, Perform Dynamic Analysis
  • Achieve your LLVM Dreams!

​

8 of 40

Learn a little about LLVM

9 of 40

LLVM (Formerly known as Low Level Virtual Machine--but it’s more!)

  • Started at The University of Illinois in 2000.
  • Chris Lattner is the lead architect
  • IR uses Single Static Assignment (SSA) form.
    • Registers used only once
    • Infinite registers
    • Aides in program analysis and compiler optimizations
      • Constant Propagation
      • Dead Code Elimination
      • etc.

10 of 40

Why is LLVM Popular

  • Modular Frontend and Backend
  • Backed by companies like Google, Microsoft, Apple, Intel, and more!

11 of 40

12 of 40

LLVM’s IR - Well defined and aides in many analysis

13 of 40

LLVM Tools

14 of 40

Core Design Ideas

  • LLVM is an umbrella of neat projects. Here are some:
    • llvm-as - Takes LLVM IR in assembly form and converts it to bitcode format.
    • llvm-dis - Converts bitcode to text readable llvm assembly
    • llvm-link - Links two or more llvm bitcode files into one file.
    • lli - Directly executes programs bit-code using JIT
    • llc - Static compiler that takes llvm input (assembly or bitcode) and generates assembly code
    • opt - LLVM analyzer and optimizer which runs certain optimizations and analysis on files
  • More

​

15 of 40

Some Examples

  • Here’s a piece of source code
  • Please enter it or download from the link.
  • Then compile (note that I am referencing the llvm bin directory that I built)

​

#include <iostream>

​

int main(){

int x = 0

​

std::cout << “Hello LLVM” << std::endl;

return 0;

}

​

​

​

​

16 of 40

Let us now output the IR of our program

  • Using flags “-S” and “-emit-llvm” and then supplying the .cpp file.
  • (Note ubuntu users: may have to add -fno-use-cxa-atexit tag if you are on 16.04 - link)
  • This now generates a “.ll” file, that is the textural form of LLVM’s IR

17 of 40

Pause -- Really take a second to look at the IR

Share with the group what you notice

18 of 40

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?

19 of 40

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!

20 of 40

The full circle -- compile our IR to assembly (.s file)

See a full list of available targets

21 of 40

Opt - An Important Tool

  • LLVM is a collection of libraries
    • llvm-as - Takes LLVM IR in assembly form and converts it to bitcode format.
    • llvm-dis - Converts bitcode to text readable llvm assembly
    • llvm-link - Links two or more llvm bitcode files into one file.
    • lli - Directly executes programs bit-code using JIT
    • llc - Static compiler that takes llvm input (assembly or bitcode) and generates assembly code
    • opt - LLVM analyzer and optimizer which runs certain optimizations and analysis on files
  • More

​

​

22 of 40

Lets run opt -

./../llvm_build/bin/opt hello.ll --time-passes

​

23 of 40

Different Types of Passes in LLVM

  • Levels of Granularity
    • Module Pass - Can think of this as a single source file
    • Call Graph Pass
    • Function Pass - What we just wrote
    • Basic Block Pass
  • Analysis Passes versus ....

24 of 40

Writing Our First Function Pass

25 of 40

Goal - Find all of the Functions in a program

  • What do we need?
  • We’re going to write a ‘Function Pass’

26 of 40

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)

27 of 40

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)

28 of 40

Let us write a new pass -- This time to collect some data

  • It will count basic blocks and instruction counts.
  • It will print the function name and these statistics
  • We’ll use this new sample source code -- or even better use one of your own!

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;

}

29 of 40

  1. Compile program to IR
    1. ./../llvm_build/bin/clang++ -S -emit-llvm loops.cpp
    2. Test opt with our old pass (note we can just use the .ll version for this sample)
    3. ./../llvm_build/bin/opt -load ./../llvm_build/lib/LLVMHello.so -hello < hello.bc > /dev/null

30 of 40

The Stats Pass

31 of 40

Results

​

​

Let’s add more!

What can we do with instruction information?

32 of 40

Find Direct Calls

Added new header: #include "llvm/IR/CallSite.h"

33 of 40

Going Further (Challenges/Project Ideas)

Time permitting:

  • Easy
    • Print out function arguments
    • Recover and print metdata and/or Profile Guided Optimization Data with functions
  • Medium
    • Build both a control flow graph and call graph and output to .dot
    • Find Program attributes
      • Add an attribute for any function < 10 instructions, and force it to inline
  • Hard/Interesting?
    • Autovectorizing (Find patterns and Insert SIMD instructions)

​

34 of 40

(Bonus: On outputting .dot files)

  • sudo apt install xdot
  • ./../llvm_build/bin/opt -dot-cfg-only loops.ll
  • xdot cfg._Z7oneloopv.dot

​

35 of 40

Resources

36 of 40

Resources

37 of 40

More Guidance - Your LLVM Syllabus

38 of 40

Thank You!

@MichaelShah

39 of 40

Some Gotcha’s

  • Having trouble with llvm-config?
    • Make sure your PATH variable is updated
    • export PATH=/home/mike/Desktop/llvm/llvm_build/bin/:$PATH

​

40 of 40

Sneak Preview of Future Talk

  • Profile-Guided Optimizations
    • Date TBD--But I like this area of work in LLVM!