A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. –...
Transcript of A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. –...
![Page 2: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/2.jpg)
What is LLVM?
● A compiler? (clang)
![Page 3: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/3.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.
![Page 4: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/4.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.– A simple, typed IR (bitcode)– Program analysis / optimization libraries– Machine code generation libraries– Tools that compose the libraries to perform tasks
![Page 5: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/5.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.– A simple, typed IR (bitcode)– Program analysis / optimization libraries– Machine code generation libraries– Tools that compose the libraries to perform tasks
![Page 6: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/6.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.– A simple, typed IR (bitcode)– Program analysis / optimization libraries– Machine code generation libraries– Tools that compose the libraries to perform tasks
![Page 7: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/7.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.– A simple, typed IR (bitcode)– Program analysis / optimization libraries– Machine code generation libraries– Tools that compose the libraries to perform tasks
![Page 8: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/8.jpg)
What is LLVM?
● A compiler? (clang)
● A set of formats, libraries, and tools.– A simple, typed IR (bitcode)– Program analysis / optimization libraries– Machine code generation libraries– Tools that compose the libraries to perform tasks
● Easy to add / remove / change functionality
![Page 9: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/9.jpg)
How will you be using it?
● Compiling programs to bitcode:clang -g -c -emit-llvm <sourcefile> -o <bitcode>.bc
![Page 10: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/10.jpg)
How will you be using it?
● Compiling programs to bitcode:clang -g -c -emit-llvm <sourcefile> -o <bitcode>.bc
● Analyzing the bitcode:opt -load <plugin>.so --<plugin> -analyze <bitcode>.bc
![Page 11: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/11.jpg)
How will you be using it?
● Compiling programs to bitcode:clang -g -c -emit-llvm <sourcefile> -o <bitcode>.bc
● Analyzing the bitcode:opt -load <plugin>.so --<plugin> -analyze <bitcode>.bc
● Writing your own tools:./callcounter -static test.bc
![Page 12: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/12.jpg)
How will you be using it?
● Compiling programs to bitcode:clang -g -c -emit-llvm <sourcefile> -o <bitcode>.bc
● Analyzing the bitcode:opt -load <plugin>.so --<plugin> -analyze <bitcode>.bc
● Writing your own tools:./callcounter -static test.bc
● Reporting properties of the program:Function Counts===============b : 2a : 1printf : 3
![Page 13: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/13.jpg)
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
![Page 14: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/14.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
Code
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
IR
clang -c -S -emit-llvm -O1 -g0
![Page 15: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/15.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
![Page 16: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/16.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
Functions
![Page 17: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/17.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
Basic Blocks
![Page 18: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/18.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
Basic Blocks
labels & predecessors
![Page 19: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/19.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
Basic Blocks branches & successors
![Page 20: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/20.jpg)
#include<stdio.h>
voidfoo(unsigned e) { for (unsigned i = 0; i < e; ++i) { printf("Hello\n"); }}
intmain(int argc, char **argv) { foo(argc); return 0;}
What is LLVM Bitcode?● A (relatively) simple
intermediate representation (IR)– It captures the
program dependence graph
@str = private constant [6 x i8] c"Hello\00"
define void @foo(i32) { %2 = icmp eq i32 %0, 0 br i1 %2, label %3, label %4
; <label>:3: ; preds = %4, %1 ret void
; <label>:4: ; preds = %1, %4 %5 = phi i32 [ %7, %4 ], [ 0, %1 ] %6 = tail call i32 @puts(i8* getelementptr ([6 x i8], [6 x i8]* @str, i64 0, i64 0)) %7 = add nuw i32 %5, 1 %8 = icmp eq i32 %7, %0 br i1 %8, label %3, label %4}
define i32 @main(i32, i8** nocapture readnone) { tail call void @foo(i32 %0) ret i32 0}
clang -c -S -emit-llvm -O1 -g0
Instructions
![Page 21: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/21.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
![Page 22: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/22.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) {
...
Iterate over the:● Functions in a Module● BasicBlocks in a Function● Instructions in a BasicBlock
![Page 23: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/23.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; }
...
dyn_cast() efficiently checksthe runtime types of LLVM IR components.
![Page 24: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/24.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; }
...
dyn_cast() efficiently checksthe runtime types of LLVM IR components.
CallBase provides a common interfacefor different type of function calls
![Page 25: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/25.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; } outs() << "Found a function call: " << i << "\n";
...
outs() and other printing functionsmake inspecting components easy
![Page 26: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/26.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; } outs() << "Found a function call: " << i << "\n"; Value* called = cb->getCalledOperand()->stripPointerCasts(); if (Function* f = dyn_cast<Function>(called)) { outs() << "Direct call to function: " << f->getName() << "\n";...
Working within the API allows youto ask questions about code.
![Page 27: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/27.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; } outs() << "Found a function call: " << i << "\n"; Value* called = cb->getCalledOperand()->stripPointerCasts(); if (Function* f = dyn_cast<Function>(called)) { outs() << "Direct call to function: " << f->getName() << "\n";...
Working within the API allows youto ask questions about code.
![Page 28: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/28.jpg)
Inspecting Bitcode
● LLVM libraries help examine the bitcode– Easy to examine and/or manipulate– Many helpers (e.g. CallBase, outs(), dyn_cast)
Module& module = ...;for (Function& fun : module) { for (BasicBlock& bb : fun) { for (Instruction& i : bb) { CallBase* cb = dyn_cast<CallBase>(&i); if (!cb) { continue; } outs() << "Found a function call: " << i << "\n"; Value* called = cb->getCalledOperand()->stripPointerCasts(); if (Function* f = dyn_cast<Function>(called)) { outs() << "Direct call to function: " << f->getName() << "\n";...
Working within the API allows youto ask questions about code.
![Page 29: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/29.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
![Page 30: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/30.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
![Page 31: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/31.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
void foo() unsigned i = 0; while (i < 10) { i = i + 1; }}
![Page 32: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/32.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
void foo() unsigned i = 0; while (i < 10) { i = i + 1; }}
What is the single definitionof i at this point?
![Page 33: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/33.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
● Phi instructions select which incoming value to use among options– Phi nodes must occur at the beginning of a basic block
![Page 34: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/34.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
● Phi instructions select which incoming value to use among options– Phi nodes must occur at the beginning of a basic block
void foo() { unsigned i = 0; while (i < 10) { i = i + 1; }}
define void @foo() { br label %1
; <label>:1 ; preds = %1, %0 %i.phi = phi i32 [ 0, %0 ], [ %2, %1 ] %2 = add i32 %i.phi, 1 %exitcond = icmp eq i32 %2, 10 br i1 %exitcond, label %3, label %1
; <label>:3 ; preds = %1 ret void}
![Page 35: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/35.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
● Phi instructions select which incoming value to use among options– Phi nodes must occur at the beginning of a basic block
void foo() { unsigned i = 0; while (i < 10) { i = i + 1; }}
define void @foo() { br label %1
; <label>:1 ; preds = %1, %0 %i.phi = phi i32 [ 0, %0 ], [ %2, %1 ] %2 = add i32 %i.phi, 1 %exitcond = icmp eq i32 %2, 10 br i1 %exitcond, label %3, label %1
; <label>:3 ; preds = %1 ret void}
![Page 36: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/36.jpg)
Static Single Assignment (SSA)
● Program dependence graphs help answer questions like:– Where was a variable defined?– Where is a particular value used?
● Compilers today help provide this using SSA form– Each variable has a single definition,
so resolving dependencies is easier
● Phi instructions select which incoming value to use among options– Phi nodes must occur at the beginning of a basic block
void foo() { unsigned i = 0; while (i < 10) { i = i + 1; }}
define void @foo() { br label %1
; <label>:1 ; preds = %1, %0 %i.phi = phi i32 [ 0, %0 ], [ %2, %1 ] %2 = add i32 %i.phi, 1 %exitcond = icmp eq i32 %2, 10 br i1 %exitcond, label %3, label %1
; <label>:3 ; preds = %1 ret void}
![Page 37: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/37.jpg)
Dependencies in General
● You can loop over the values an instruction uses
for (Use& u : inst->operands()) { // inst uses the Value* u}
![Page 38: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/38.jpg)
Dependencies in General
● You can loop over the values an instruction uses
for (Use& u : inst->operands()) { // inst uses the Value* u}
Given %a = %b + %c: [%b, %c]
![Page 39: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/39.jpg)
Dependencies in General
● You can loop over the values an instruction uses
● You can loop over the instructions that use a particular value
for (Use& u : inst->operands()) { // inst uses the Value* u}
Given %a = %b + %c: [%b, %c]
Instruction* inst = ...;for (User* user : inst->users()) if (auto* i = dyn_cast<Instruction>(user)) { // inst is used by Instruction i }
![Page 40: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/40.jpg)
Dealing with Types
● LLVM IR is strongly typed– Every value has a type → getType()
● A value must be explicitly cast to a new type
define i64 @trunc(i16 zeroext %a) { %1 = zext i16 %a to i64 ret i64 %1}
![Page 41: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/41.jpg)
Dealing with Types
● LLVM IR is strongly typed– Every value has a type → getType()
● A value must be explicitly cast to a new type
define i64 @trunc(i16 zeroext %a) { %1 = zext i16 %a to i64 ret i64 %1}
![Page 42: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/42.jpg)
Dealing with Types
● LLVM IR is strongly typed– Every value has a type → getType()
● A value must be explicitly cast to a new type
● Also types for pointers, arrays, structs, etc.– Strong typing means they take a bit more work
define i64 @trunc(i16 zeroext %a) { %1 = zext i16 %a to i64 ret i64 %1}
![Page 43: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/43.jpg)
Dealing with Types: GEP
● We sometimes need to extract elements/fields from arrays/structs– Pointer arithmetic– Done using GetElementPointer (GEP)
%struct.rec = type { i32, i32 }
@buf = global %struct.rec* null
define void @foo() { %1 = load %struct.rec*, %struct.rec** @buf %2 = getelementptr %struct.rec, %struct.rec* %1, i64 5, i32 1 store i32 7, i32* %2 ret void}
struct rec { int x; int y;};
struct rec *buf;
void foo() { buf[5].y = 7;}
![Page 44: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/44.jpg)
Dealing with Types: GEP
● We sometimes need to extract elements/fields from arrays/structs– Pointer arithmetic– Done using GetElementPointer (GEP)
%struct.rec = type { i32, i32 }
@buf = global %struct.rec* null
define void @foo() { %1 = load %struct.rec*, %struct.rec** @buf %2 = getelementptr %struct.rec, %struct.rec* %1, i64 5, i32 1 store i32 7, i32* %2 ret void}
struct rec { int x; int y;};
struct rec *buf;
void foo() { buf[5].y = 7;}
![Page 45: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/45.jpg)
Dealing with Types: GEP
● We sometimes need to extract elements/fields from arrays/structs– Pointer arithmetic– Done using GetElementPointer (GEP)
%struct.rec = type { i32, i32 }
@buf = global %struct.rec* null
define void @foo() { %1 = load %struct.rec*, %struct.rec** @buf %2 = getelementptr %struct.rec, %struct.rec* %1, i64 5, i32 1 store i32 7, i32* %2 ret void}
struct rec { int x; int y;};
struct rec *buf;
void foo() { buf[5].y = 7;}
![Page 46: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/46.jpg)
Dealing with Types: GEP
● We sometimes need to extract elements/fields from arrays/structs– Pointer arithmetic– Done using GetElementPointer (GEP)
%struct.rec = type { i32, i32 }
@buf = global %struct.rec* null
define void @foo() { %1 = load %struct.rec*, %struct.rec** @buf %2 = getelementptr %struct.rec, %struct.rec* %1, i64 5, i32 1 store i32 7, i32* %2 ret void}
struct rec { int x; int y;};
struct rec *buf;
void foo() { buf[5].y = 7;}
![Page 47: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/47.jpg)
Where can you get more information?
● The online documentation is extensive:– LLVM Programmer’s Manual– LLVM Language Reference Manual
![Page 48: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/48.jpg)
Where can you get more information?
● The online documentation is extensive:– LLVM Programmer’s Manual– LLVM Language Reference Manual
● The header files!– All in llvm-12.x.src/include/llvm/
InstrTypes.hIRBuilder.hSupport/InstVisitor.hType.h
BasicBlock.hDerivedTypes.hFunction.hInstructions.h
![Page 49: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/49.jpg)
Creating aStatic Analysis
![Page 50: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/50.jpg)
Making a new analysis
● Analyses are organized into individual passes– ModulePass– FunctionPass– LoopPass– …
Derive from the appropriate base class to make a Pass
![Page 51: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/51.jpg)
Making a new analysis
● Analyses are organized into individual passes– ModulePass– FunctionPass– LoopPass– …
3 Steps1) Declare your pass2) Register your pass3) Define your pass
Derive from the appropriate base class to make a Pass
![Page 52: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/52.jpg)
Making a new analysis
● Analyses are organized into individual passes– ModulePass– FunctionPass– LoopPass– …
3 Steps1) Declare your pass2) Register your pass3) Define your pass
Let's count the number of static direct calls to each
function.
Derive from the appropriate base class to make a Pass
![Page 53: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/53.jpg)
Making a ModulePass (1)
● Declare your ModulePass
struct StaticCallCounter : public llvm::ModulePass {
static char ID;
DenseMap<Function*, uint64_t> counts;
StaticCallCounter() : ModulePass(ID) { }
bool runOnModule(Module& m) override;
void print(raw_ostream& out, const Module* m) const override;
void handleInstruction(CallBase& cb);};
![Page 54: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/54.jpg)
Making a ModulePass (1)
● Declare your ModulePass
struct StaticCallCounter : public llvm::ModulePass {
static char ID;
DenseMap<Function*, uint64_t> counts;
StaticCallCounter() : ModulePass(ID) { }
bool runOnModule(Module& m) override;
void print(raw_ostream& out, const Module* m) const override;
void handleInstruction(CallBase& cb);};
![Page 55: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/55.jpg)
Making a ModulePass (1)
● Declare your ModulePass
struct StaticCallCounter : public llvm::ModulePass {
static char ID;
DenseMap<Function*, uint64_t> counts;
StaticCallCounter() : ModulePass(ID) { }
bool runOnModule(Module& m) override;
void print(raw_ostream& out, const Module* m) const override;
void handleInstruction(CallBase& cb);};
![Page 56: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/56.jpg)
Making a ModulePass (2)
● Register your ModulePass– This allows it to even be dynamically loaded as a plugin– Depending on your use cases, it may not be necessary
char StaticCallCounter::ID = 0;
RegisterPass<StaticCallCounter> SCCReg("callcounter", "Print the static count of direct calls");
![Page 57: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/57.jpg)
Making a ModulePass (3)
● Define your ModulePass– Need to override runOnModule() and print()
boolStaticCallCounter::runOnModule(Module& m) { for (auto& f : m) for (auto& bb : f) for (auto& i : bb) if (CallBase *cb = dyn_cast<CallBase>(&i)) { handleInstruction(CallSite{&i}); } return false; // False because we didn't change the Module}
![Page 58: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/58.jpg)
Making a ModulePass (3)
● Define your ModulePass– Need to override runOnModule() and print()
boolStaticCallCounter::runOnModule(Module& m) { for (auto& f : m) for (auto& bb : f) for (auto& i : bb) if (CallBase *cb = dyn_cast<CallBase>(&i)) { handleInstruction(CallSite{&i}); } return false; // False because we didn't change the Module}
![Page 59: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/59.jpg)
Making a ModulePass (3)
● Analysis continued...void StaticCallCounter::handleInstruction(CallBase* cb) { // Check whether the called function is directly invoked auto called = cb.getCalledOperand()->stripPointerCasts(); auto fun = dyn_cast<Function>(called); if (!fun) { return; }
// Update the count for the particular call auto count = counts.find(fun); if (counts.end() == count) { count = counts.insert(std::make_pair(fun, 0)).first; } ++count->second;}
![Page 60: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/60.jpg)
Making a ModulePass (3)
● Analysis continued...void StaticCallCounter::handleInstruction(CallBase* cb) { // Check whether the called function is directly invoked auto called = cb.getCalledOperand()->stripPointerCasts(); auto fun = dyn_cast<Function>(called); if (!fun) { return; }
// Update the count for the particular call auto count = counts.find(fun); if (counts.end() == count) { count = counts.insert(std::make_pair(fun, 0)).first; } ++count->second;}
![Page 61: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/61.jpg)
Making a ModulePass (3)
● Analysis continued...void StaticCallCounter::handleInstruction(CallBase* cb) { // Check whether the called function is directly invoked auto called = cb.getCalledOperand()->stripPointerCasts(); auto fun = dyn_cast<Function>(called); if (!fun) { return; }
// Update the count for the particular call auto count = counts.find(fun); if (counts.end() == count) { count = counts.insert(std::make_pair(fun, 0)).first; } ++count->second;}
![Page 62: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/62.jpg)
Making a ModulePass (3)
● Printing out the results
voidCallCounterPass::print(raw_ostream& out, const Module* m) const { out << "Function Counts\n" << "===============\n"; for (auto& kvPair : counts) { auto* function = kvPair.first; uint64_t count = kvPair.second; out << function->getName() << " : " << count << "\n"; }}
![Page 63: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/63.jpg)
Creating aDynamic Analysis
![Page 64: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/64.jpg)
Making a Dynamic Analysis
● We have counted the static direct calls to each function.
● How might we count all dynamic calls to each function?
![Page 65: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/65.jpg)
Making a Dynamic Analysis
● We have counted the static direct calls to each function.
● How might we count all dynamic calls to each function?
● Need to modify the original program!
![Page 66: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/66.jpg)
Making a Dynamic Analysis
● We have counted the static direct calls to each function.
● How might we count all dynamic calls to each function?
● Need to modify the original program!
● Steps:1) Modify the program using passes2) Compile the modified version3) Run the new program
![Page 67: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/67.jpg)
Modifying the Original Program
● Goal: Count the dynamic calls to each function in an execution.– So how do we want to modify the program?
Keep a counter for each function!
2 Choices:1) increment count for each function as it starts2) increment count for each function at its call site
void foo() bar();} ?
Does that even matter? Are there trade offs?
![Page 68: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/68.jpg)
Modifying the Original Program
● Goal: Count the dynamic calls to each function in an execution.– So how do we want to modify the program?
● We'll increment at the function entry.(the demo code shows both options)
void foo() bar();}
void foo() count[foo]++; bar();}
![Page 69: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/69.jpg)
Modifying the Original Program
● Goal: Count the dynamic calls to each function in an execution.– So how do we want to modify the program?
● We'll increment at the function entry.– Using numeric IDs for functions is sometimes easier
void foo() bar();}
void foo() count[1]++; bar();}
0 ↦ main1 ↦ foo2 ↦ bar …
![Page 70: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/70.jpg)
Modifying the Original Program
● Goal: Count the dynamic calls to each function in an execution.– So how do we want to modify the program?
● We'll increment at the function entry.– Using numeric IDs for functions is sometimes easier– Inserting function calls is easier than adding raw instructions
void foo() bar();}
void foo() countCall(1); bar();}
voidcountCall(id) count[id]++;}
![Page 71: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/71.jpg)
Modifying the Original Program
● Goal: Count the dynamic calls to each function in an execution.– So how do we want to modify the program?
● We'll increment at the function entry.– Using numeric IDs for functions is sometimes easier– Inserting function calls is easier than adding raw instructions
● Add new definitions to the original code● Link against an instrumentation library
void foo() bar();}
void foo() countCall(1); bar();}
voidcountCall(id) count[id]++;}
![Page 72: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/72.jpg)
Modifying the Original Program
● What might adding this call look like?
voidDynamicCallCounter::handleInstruction(CallBase& cb, Value* counter) { // Check whether the called function is directly invoked auto calledValue = cb.getCalledOperation()->stripPointerCasts(); auto calledFunction = dyn_cast<Function>(calledValue); if (!calledFunction) { return; }
// Insert a call to the counting function. IRBuilder<> builder(&cb); builder.CreateCall(counter, builder.getInt64(ids[calledFunction]));}
![Page 73: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/73.jpg)
Modifying the Original Program
● What might adding this call look like?
voidDynamicCallCounter::handleInstruction(CallBase& cb, Value* counter) { // Check whether the called function is directly invoked auto calledValue = cb.getCalledOperation()->stripPointerCasts(); auto calledFunction = dyn_cast<Function>(calledValue); if (!calledFunction) { return; }
// Insert a call to the counting function. IRBuilder<> builder(&cb); builder.CreateCall(counter, builder.getInt64(ids[calledFunction]));}
![Page 74: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/74.jpg)
Modifying the Original Program
● What might adding this call look like?
voidDynamicCallCounter::handleInstruction(CallBase& cb, Value* counter) { // Check whether the called function is directly invoked auto calledValue = cb.getCalledOperation()->stripPointerCasts(); auto calledFunction = dyn_cast<Function>(calledValue); if (!calledFunction) { return; }
// Insert a call to the counting function. IRBuilder<> builder(&cb); builder.CreateCall(counter, builder.getInt64(ids[calledFunction]));}
In practice, it is more complex.You can find details in the demo code.
![Page 75: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/75.jpg)
Using a Runtime Library
● Recall that the definition of countCall() needs to live somewhere1) Add directly to the modified code2) Implemented separately & linked in via a library
What trade offs do you see?
![Page 76: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/76.jpg)
Using a Runtime Library
● Recall that the definition of countCall() needs to live somewhere1) Add directly to the modified code2) Implemented separately & linked in via a library
● In practice, linking against a library is common, easy, & powerful– Regardless of the language being analyzed
voidcountCalled(uint64_t id) { ++functionInfo[id];}
![Page 77: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/77.jpg)
Revisiting the Big Picture of Dynamic Analysis
Program/Module
Analysis Tool
Instrumentation Pass
Compilation
Modified Program
RuntimeLibrary
Input Results!
![Page 78: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/78.jpg)
Revisiting the Big Picture of Dynamic Analysis
Program/Module
Analysis Tool
Instrumentation Pass
Compilation
Modified Program
RuntimeLibrary
Input Results!
Insert useful callsto a runtime library
Step 1:
![Page 79: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/79.jpg)
Revisiting the Big Picture of Dynamic Analysis
Program/Module
Analysis Tool
Instrumentation Pass
Compilation
Modified Program
RuntimeLibrary
Input Results!
Compile & linkagainst the runtimelibrary.
Step 2:
![Page 80: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/80.jpg)
Revisiting the Big Picture of Dynamic Analysis
Program/Module
Analysis Tool
Instrumentation Pass
Compilation
Modified Program
RuntimeLibrary
Input Results!
Run the new program toproduce your results
Step 3:
![Page 81: A Brief Introduction to LLVMwsumner/teaching/886/llvm.pdfA set of formats, libraries, and tools. – A simple, typed IR (bitcode) – Program analysis / optimization libraries –](https://reader035.fdocuments.net/reader035/viewer/2022070216/611bd2fc92c0721c24214dcf/html5/thumbnails/81.jpg)
Summary
● LLVM organizes groups of passes and tools into projects
● Easiest way to start is by using the demo on the course page
● For the most part, you can follow the directions online& in the project description