Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology Liveness analysis for register allocation Concepts Next Time Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1
How do we automate the process? Represent programs in intermediate representations (e.g. LLVM) a = b + c // info about types of a, b, and c d = load *a Typed, 3-address code Program analyses needed to find data dependences between computations Data-flow analyses (dependencies between scalar variables) Data-dependence analysis (dependencies between iterations in loops) Specifying loop transformations and parallelization in the polyhedral framework Represent the loops within an integer tuple space Represent transformations as transformations in the integer tuple space CS553 Lecture 2 Optimizations and LLVM 2
LLVM Intermediate Representation and Compiler Infrastructure Typed, 3-address code Control-flow graphs with basic blocks Static Single Assignment (SSA) Compiler Infrastructure: Tools, Analysis and Transformation Passes clang frontend that translateds C/C++ into LLVM bit code opt analyze and transform LLVM bitcode llc code generator from LLVM bitcode to native code CS553 Lecture 2 Optimizations and LLVM 3 Figure from The Architecture of Open Source Applications: LLVM by Chris Lattner.
LLVM Pass Infrastructure Analyses and Transformations are Passes Dependencies between passes When registered passes indicate which other passes they depend upon by overriding the getanalysisusage method The PassManager figures out the dependency Graph Each Pass returns a boolean True indicates the pass has made a changed to the program. False indicates that no change was made. The PassManager runs until everyone stops. Pass classes to derive from FunctionPass, used in PA1 LoopPass, visits loops BasicBlockPass, visits basic blocks some others CS553 Lecture 2 Optimizations and LLVM 4
A Low-Level IR: 3-address code We want to do analysis on an Intermediate Representation: 3-address code Linear representation: assignments, labels, (conditional) jumps Typically language-independent and nearly corresponds to machine instructions, difference: no stack code, but (temporary) variables There are named variables (parameters, variables) and temporaries (expressions). We can name the temporaries assuming unbounded number of (symbolic) registers. Example operations Unary / binary op x = op z, x = v op z, t1 = t2 op t3 Address of p = & v Dereference x = *p, *p = x Pass param param t0 Call t1 = call f, 1 (conditinal) Branch goto L1, if t1 goto L1 (Indexed) copy x = y[i], y[i] = x, x = z, t1 = t2 CS553 Lecture 2 Optimizations and LLVM 5
LLVM (a typed 3-address code) Example operations Unary / binary op x = op z, x = v op z, t1 = t2 op t3 IN LLVM: %cmp = icmp sgt i32 %a, %b Address of p = & v IN LLVM: does not appear to have this Dereference x = *p, *p = x IN LLVM: %tmp = load i32* %X Pass param param t0 IN LLVM: see function calls Call t1 = call f, 1 IN LLVM: call void %foo(i8 97 signext) (conditinal) Branch goto L1, if t1 goto L1 IN LLVM: br i1 %cmp, label %if.then, label %if.else (Indexed) copy x = y[i], y[i] = x, x = z, t1 = t2 IN LLVM: %1 = getelementptr i32, i32* $a, i32 0, i32 1 CS553 Lecture 2 Optimizations and LLVM 6
LLVM Program Organization Program Representation Organization A Module is a compilation unit that contains functions and global variables and constants. A Function represents a function definition and contains basic blocks BasicBlocks contain Instructions Instructions contain operands, each of which is a Value. All of the above are Values except for Modules Control-Flow Graph Each basic block starts with a label Each branch instruction targets those labels llvm-as < t.ll opt -analyze -view-cfg llvm-as < t.ll opt dot-cfg CS553 Lecture 2 Optimizations and LLVM 7
C Code for Review Example int main() { int x,y,z; x = 0; y=1, z = 2; while (x<42) { if (x>0) { y++; x++; z++; continue; } y = z*y; } return 0; } CS553 Lecture Introduction to Data-flow Analysis 8
Control Flow Graph Review while.cond: %0 = load i32* %x, align 4 %cmp = icmp slt i32 %0, 42 br i1 %cmp, label %while.body, label %while.end if.end:... store i32 %mul, i32* %y, align 4 br label %while.cond while.end: ret i32 0 while.body: %1 = load i32* %x, align 4 %cmp1 = icmp sgt i32 %1, 0 br i1 %cmp1, label %if.then, label %if.end if.then:... br label %while.cond CS553 Lecture Introduction to Data-flow Analysis 9
Representing Computations with Iteration Spaces Idea Explicitly represent the iterations of a loop nest Example Iteration Space do i = 1,6 do j = 1,5 A(i,j) = A(i-1,j-1)+1 enddo enddo j Iteration Space i A set of integer tuples that represents the iterations of a loop Can visualize the dependences in an iteration space CS553 Lecture 2 Optimizations and LLVM 10
Loops as Polyhedra, Transformations as Affine Functions Original code do i = 1,6 do j = 1,5 A(i,j) = A(i-1,j+1)+1 enddo enddo Loop Skewing Transformation j i Bounds Transformed code do i = 1,6 do j = 1+i,5+i A(i,j -i ) = A(i -1,j -i +1)+1 enddo enddo CS553 Lecture 2 Optimizations and LLVM 11 j i
Concepts up to this point Optimal in name only Phase ordering problem Approach for Program Analysis and Transformation Represent code in typed, 3-address code and perform data-flow analysis and transformation. Represent loops with iteration spaces and transform those. Example Program Optimizations Loop-invariant code motion Induction variable elimination Loop unrolling, permutation, and parallelization Intermediate Representations : LLVM Typed, 3-address code Basic blocks and control flow graphs: optimization scope Pass architecture used in compiler infrastructures and LLVM CS553 Lecture 2 Optimizations and LLVM 12
Data-flow Analysis Idea Data-flow analysis derives information about the dynamic behavior of a program by only examining the static code Example How many registers do we need for the program on the right? Easy bound: the number of variables used (3) Better answer is found by considering the dynamic requirements of the program 1 a := 0 2 L1: b := a + 1 3 c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c CS553 Lecture Introduction to Data-flow Analysis 13
Liveness Analysis Definition A variable is live at a particular point in the program if its value at that point will be used in the future (dead, otherwise). \ To compute liveness at a given point, we need to look into the future Motivation: Register Allocation A program contains an unbounded number of variables Must execute on a machine with a bounded number of registers Two variables can use the same register if they are never in use at the same time (i.e, never simultaneously live). \ Register allocation uses liveness information CS553 Lecture Introduction to Data-flow Analysis 14
Control Flow Graphs (CFGs) Definition A CFG is a graph whose nodes represent program statements and whose directed edges represent control flow Example 1 a := 0 2 L1: b := a + 1 3 c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c 1 2 3 a = 0 b = a + 1 c = c + b 4 a = b * 2 5 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 15
Terminology Flow Graph Terms A CFG node has out-edges that lead to successor nodes and in-edges that come from predecessor nodes pred[n] is the set of all predecessors of node n succ[n] is the set of all successors of node n 1 a = 0 Examples Out-edges of node 5: succ[5] = {2,6} pred[5] = {4} pred[2] = {1,5} (5 6) and (5 2) 2 3 5 b = a + 1 c = c + b 4 a = b * 2 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 16
Liveness by Example What is the live range of b? Variable b is read in statement 4, so b is live on the (3 4) edge Since statement 3 does not assign into b, b is also live on the (2 3) edge Statement 2 assigns b, so any value of b on the (1 2) and (5 2) edges are not needed, so b is dead along these edges 1 2 3 a = 0 b = a + 1 c = c + b 4 a = b * 2 5 a<9 b s live range is (2 3 4) 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 17
Liveness by Example (cont) Live range of a a is live from (1 2) and again from (4 5 2) a is dead from (2 3 4) 1 2 a = 0 b = a + 1 Live range of b b is live from (2 3 4) Live range of c c is live from (entry 1 2 3 4 5 2, 5 6) 6 return c 3 5 c = c + b 4 a = b * 2 a<9 Variables a and b are never simultaneously live, so they can share a register No Yes CS553 Lecture Introduction to Data-flow Analysis 18
Uses and Defs Def (or definition) An assignment of a value to a variable def_node[v] = set of CFG nodes that define variable v def[n] = set of variables that are defined at node n a = 0 Use A read of a variable s value use_node[v] = set of CFG nodes that use variable v use[n] = set of variables that are used at node n a < 9? v live Ï def_node[v] More precise definition of liveness A variable v is live on a CFG edge if Î use_node[v] (1) $ a directed path from that edge to a use of v (node in use_node[v]), and (2) that path does not go through any def of v (no nodes in def_node[v]) CS553 Lecture Introduction to Data-flow Analysis 19
The Flow of Liveness Data-flow Liveness of variables is a property that flows through the edges of the CFG Direction of Flow Liveness flows backwards through the CFG, because the behavior at future nodes determines liveness at a given node Consider a Consider b Later, we ll see other properties that flow forward 1 a := 0 2 3 4 5 b := a + 1 c := c + b a := b * 2 No 6 return c a < 9? Yes CS553 Lecture Introduction to Data-flow Analysis 20
Liveness at Nodes We have liveness on edges How do we talk about liveness at nodes? a = 0 edges program points just before computation just after computation Two More Definitions A variable is live-out at a node if it is live on any of that node s out-edges n live-out out-edges A variable is live-in at a node if it is live on any of that node s in-edges n live-in in-edges CS553 Lecture Introduction to Data-flow Analysis 21
Computing Liveness Rules for computing liveness (1) Generate liveness: If a variable is in use[n], it is live-in at node n n live-in use (2) Push liveness across edges: If a variable is live-in at a node n then it is live-out at all nodes in pred[n] (3) Push liveness across nodes: If a variable is live-out at node n and not in def[n] then the variable is also live-in at n Data-flow equations (1) in[n] = use[n] È (out[n] def[n]) (3) out[n] = È in[s] (2) s Î succ[n] live-out live-out n live-in live-out live-in n live-out pred[n] CS553 Lecture Introduction to Data-flow Analysis 22
Solving the Data-flow Equations Algorithm for each node n in CFG in[n] = Æ; out[n] = Æ repeat for each node n in CFG in [n] = in[n] out [n] = out[n] in[n] = use[n] È (out[n] def[n]) out[n] = È in[s] until in [n]=in[n] and out [n]=out[n] for all n s Î succ[n] initialize solutions save current results solve data-flow equations test for convergence This is iterative data-flow analysis (for liveness analysis) CS553 Lecture Introduction to Data-flow Analysis 23
Example node # 1 a 2 a b 3 bc c 5 a 1st 2nd 3rd 4th 5th 6th 7th use def in out in out in out in out in out in out in out 4 b a 6 c a bc b a a c c a a bc bc b b a a ac c a ac bc bc b b a ac ac c ac ac bc bc b b ac ac ac c ac ac bc bc b bc ac ac ac c c c ac ac bc bc bc bc ac ac ac c c ac ac bc bc bc bc ac ac ac 1 a := 0 2 b := a + 1 3 c := c + b 4 a := b * 2 Data-flow Equations for Liveness in[n] = use[n] È (out[n] def[n]) out[n] = È in[s] s Î succ[n] No 6 return c 5 a < 9? Yes CS553 Lecture Introduction to Data-flow Analysis 24
Do Liveness Example from Old Midterm CS553 Lecture Introduction to Data-flow Analysis 25
Concepts Intermediate Representations : LLVM Typed, 3-address code Basic blocks and control flow graphs: optimization scope Pass architecture used in compiler infrastructures and LLVM Liveness Used in register allocation Generating liveness Flow and direction Data-flow equations and analysis Control flow graph terminology CS553 Lecture 2 Optimizations and LLVM 26
Next Time Reading Assignments For Monday, DataFlow Analysis Notes by Susan Horwitz Wednesday reading assignment will be posted soon Homework Set up demo for this week if haven t already. CS553 Lecture 2 Optimizations and LLVM 27