Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Similar documents
Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

LLVM Tutorial. John Criswell University of Rochester

Compilation 2013 Liveness Analysis

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Introduction to LLVM compiler framework

Lecture 21 CIS 341: COMPILERS

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks

Lecture 2 Overview of the LLVM Compiler

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

Lecture 6 More on the LLVM Compiler

Introduction to LLVM compiler framework

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall

A Brief Introduction to Using LLVM. Nick Sumner

Lecture 20 CIS 341: COMPILERS

Lecture Compiler Middle-End

Lecture 4 Overview of the LLVM Compiler

Lecture 3 Overview of the LLVM Compiler

Lecture 3 Overview of the LLVM Compiler

Lecture 23 CIS 341: COMPILERS

Lecture 4 More on the LLVM Compiler

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

LLVM and IR Construction

Advanced Compiler Construction

Targeting LLVM IR. LLVM IR, code emission, assignment 4

CIS 341 Final Examination 4 May 2017

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Liveness Analysis and Register Allocation

Visualizing code structure in LLVM

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Introduction to LLVM. UG3 Compiling Techniques Autumn 2018

Loop Transformations, Dependences, and Parallelization

Calvin Lin The University of Texas at Austin

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Induction Variable Identification (cont)

More Dataflow Analysis

Compiler Construction: LLVMlite

Compiler Optimization and Code Generation

Intermediate representation

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

CS553 Lecture Generalizing Data-flow Analysis 3

Programming Language Processor Theory

Control flow and loop detection. TDT4205 Lecture 29

Using Static Single Assignment Form

Lecture 3 Local Optimizations, Intro to SSA

Lecture 4 Introduction to Data Flow Analysis

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Tour of common optimizations

Tiling: A Data Locality Optimizing Algorithm

CIS 341 Final Examination 30 April 2013

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Universidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888

CIS 341 Final Examination 3 May 2011

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

LLVM & LLVM Bitcode Introduction

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Principles of Compiler Design

CS553 Lecture Introduction to Data-flow Analysis 1

Compiler Construction 2016/2017 Loop Optimizations

Gil Rapaport and Ayal Zaks. Intel Corporation, Israel Development Center. March 27-28, 2017 European LLVM Developers Meeting

The LLVM Compiler Framework and Infrastructure

Lecture 25 CIS 341: COMPILERS

CS 406/534 Compiler Construction Putting It All Together

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Languages and Compiler Design II IR Code Optimization

Intermediate Code Generation

CS202 Compiler Construction

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

A Framework for Automatic OpenMP Code Generation

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Compiler Construction 2010/2011 Loop Optimizations

Simone Campanoni Loop transformations

Languages and Compiler Design II IR Code Generation I

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

Lecture 7 CIS 341: COMPILERS

Homework Assignment #1 Sample Solutions

CS553 Lecture Dynamic Optimizations 2

Compiler Optimisation

Topic I (d): Static Single Assignment Form (SSA)

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

6. Intermediate Representation

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Intermediate Representations & Symbol Tables

CS202 Compiler Construction

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

Baggy bounds with LLVM

Transcription:

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