Preliminary Transformations

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Optimizing Compilers for Modern Architectures Copyright, 1996 © Dale Carnegie & Associates, Inc. Dependence Testing Allen and Kennedy, Chapter 3 thru Section.
Optimizing Compilers for Modern Architectures Syllabus Allen and Kennedy, Preface Optimizing Compilers for Modern Architectures.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Optimizing Compilers for Modern Architectures Allen and Kennedy, Chapter 13 Compiling Array Assignments.
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Optimizing Compilers for Modern Architectures Preliminary Transformations Chapter 4 of Allen and Kennedy.
Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Compiler Challenges, Introduction to Data Dependences Allen and Kennedy, Chapter 1, 2.
CS 536 Spring Global Optimizations Lecture 23.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Idiom Recognition in the Polaris Parallelizing Compiler Bill Pottenger and Rudolf Eigenmann Presented by Vincent Yau.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Dependence Testing Optimizing Compilers for Modern Architectures, Chapter 3 Allen and Kennedy Presented by Rachel Tzoref and Rotem Oshman.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Optimizing Compilers for Modern Architectures Dependence: Theory and Practice Allen and Kennedy, Chapter 2 pp
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Optimizing Compilers for Modern Architectures Dependence: Theory and Practice Allen and Kennedy, Chapter 2.
1 CS 201 Compiler Construction Data Flow Analysis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Array Dependence Analysis COMP 621 Special Topics By Nurudeen Lameed
Toward Efficient Flow-Sensitive Induction Variable Analysis and Dependence Testing for Loop Optimization Yixin Shou, Robert A. van Engelen, Johnnie Birch,
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 Theory and Practice of Dependence Testing Data and control dependences Scalar data dependences  True-, anti-, and output-dependences Loop dependences.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
PLC '06 Experience in Testing Compiler Optimizers Using Comparison Checking Masataka Sassa and Daijiro Sudo Dept. of Mathematical and Computing Sciences.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Code Optimization Overview and Examples
Introduction to Optimization
High-level optimization Jakub Yaghob
Code Optimization.
Data Flow Analysis Suman Jana
CS314 – Section 5 Recitation 13
Static Single Assignment
Data Dependence, Parallelization, and Locality Enhancement (courtesy of Tarek Abdelrahman, University of Toronto)
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Introduction to Optimization
University Of Virginia
Code Optimization Chapter 10
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Optimizations using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
Interval Partitioning of a Flow Graph
Data Flow Analysis Compiler Design
Introduction to Optimization
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Preliminary Transformations Chapter 4 of Allen and Kennedy We talked about dependence testing in the last two class

Overview Why do we need this? Requirements of dependence testing Stride 1 Normalized loop Linear subscripts Subscripts composed of functions of loop induction variables Higher dependence test accuracy Easier implementation of dependence tests

An Example Programmers optimized code INC = 2 KI = 0 DO I = 1, 100 Confusing to smart compilers INC = 2 KI = 0 DO I = 1, 100 DO J = 1, 100 KI = KI + INC U(KI) = U(KI) + W(J) ENDDO S(I) = U(KI)

An Example Applying induction-variable substitution Replace references to AIV with functions of loop index INC = 2 KI = 0 DO I = 1, 100 DO J = 1, 100 ! Deleted: KI = KI + INC U(KI + J*INC) = U(KI + J*INC) + W(J) ENDDO KI = KI + 100 * INC S(I) = U(KI)

An Example Second application of IVS Remove all references to KI INC = 2 KI = 0 DO I = 1, 100 DO J = 1, 100 U(KI + (I-1)*100*INC + J*INC) = U(KI + (I-1)*100*INC + J*INC) + W(J) ENDDO ! Deleted: KI = KI + 100 * INC S(I) = U(KI + I * (100*INC)) KI = KI + 100 * 100 * INC

An Example Applying Constant Propagation Substitute the constants INC = 2 ! Deleted: KI = 0 DO I = 1, 100 DO J = 1, 100 U(I*200 + J*2 - 200) = U(I*200 + J*2 -200) + W(J) ENDDO S(I) = U(I*200) KI = 20000

An Example Applying Dead Code Elimination DO I = 1, 100 DO J = 1, 100 Removes all unused code DO I = 1, 100 DO J = 1, 100 U(I*200 + J*2 - 200) = U(I*200 + J*2 - 200) + W(J) ENDDO S(I) = U(I*200)

Information Requirements Transformations need knowledge Loop Stride Loop-invariant quantities Constant-values assignment Usage of variables

Loop Normalization Lower Bound 1 with Stride 1 To make dependence testing as simple as possible Serves as information gathering phase

Loop Normalization Algorithm Procedure normalizeLoop(L0); i = a unique compiler-generated LIV S1: replace the loop header for L0 DO I = L, U, S with the adjusted loop header DO i = 1, (U – L + S) / S; S2: replace each reference to I within the loop by i * S – S + L; S3: insert a finalization assignment I = i * S – S + L; immediately after the end of the loop; end normalizeLoop;

Loop Normalization Caveat Un-normalized: DO I = 1, M DO J = I, N A(J, I) = A(J, I - 1) + 5 ENDDO Has a direction vector of (<,=) Normalized: DO J = 1, N – I + 1 A(J + I – 1, I) = A(J + I – 1, I – 1) + 5 Has a direction vector of (<,>)

Loop Normalization Caveat Handled by another transformation Consider interchanging loops (<,=) becomes (=,>) OK (<,>) becomes (>,<) Problem Handled by another transformation What if the step size is symbolic? Prohibits dependence testing Workaround: use step size 1 Less precise, but allow dependence testing

Definition-use Graph Traditionally called Definition-use Chains Provides the map of variables usage Heavily used by the transformations

Definition-use Graph Definition-use graph is a graph that contains an edge from each definition point in the program to every possible use of the variable at run time uses(b): the set of all variables used within the block b that have no prior definitions within the block defsout(b): the set of all definitions within block b that are not killed within the block killed(b): the set of all definitions that define variables killed by other definitions within block b

Definition-use Graph Computing reaches for one block b may immediately change all other reaches including b itself since reaches(b) is an input into other reaches equations Archiving correct solutions requires simultaneously solving all individual equations There is a workaround this

Definition-use Graph

Definition-use Graph

Dead Code Elimination Removes all dead code What is Dead Code ? Code whose results are never used in any ‘Useful statements’ What are Useful statements ? Are they simply output statements ? Output statements, input statements, control flow statements, and their required statements Makes code cleaner

Dead Code Elimination

Constant Propagation Replace all variables that have constant values at runtime with those constant values

Constant Propagation

Constant Propagation

Static Single-Assignment Reduces the number of definition-use edges Improves performance of algorithms

Static Single-Assignment Example

Forward Expression Substitution Example DO I = 1, 100 K = I + 2 A(K) = A(K) + 5 ENDDO DO I = 1, 100 A(I+2) = A(I+2) + 5 ENDDO

Forward Expression Substitution Need definition-use edges and control flow analysis Need to guarantee that the definition is always executed on a loop iteration before the statement into which it is substituted Data structure to find out if a statement S is in loop L Test whether level-K loop containing S is equal to L

Forward Expression Substitution

Forward Expression Substitution

Forward Expression Substitution

Forward Expression Substitution

Induction Variable Substitution Definition: an auxiliary induction variable in a DO loop headed by DO I = LB, UB, S is any variable that can be correctly expressed as cexpr * I + iexprL at every location L where it is used in the loop, where cexpr and iexprL are expressions that do not vary in the loop, although different locations in the loop may require substitution of different values of iexprL

Induction Variable Substitution Example: DO I = 1, N A(I) = B(K) + 1 K = K + 4 … D(K) = D(K) + A(I) ENDDO

Induction Variable Recognition

Induction Variable Substitution Induction Variable Recognition

Induction Variable Substitution

Induction Variable Substitution

Induction Variable Substitution More complex example DO I = 1, N, 2 K = K + 1 A(K) = A(K) + 1 ENDDO Alternative strategy is to recognize region invariance A(K+1) = A(K+1) + 1 K = K+1 + 1

Induction Variable Substitution Driver

IVSub without loop normalization DO I = L, U, S K = K + N … = A(K) ENDDO … = A(K + (I – L + S) / S * N) K = K + (U – L + S) / S * N

IVSub without loop normalization Problem: Inefficient code Nonlinear subscript

IVSub with Loop Normalization DO i = 1, (U-L+S)/S, 1 K = K + N … = A (K) I = I + 1 ENDDO

IVSub with Loop Normalization DO i = 1, (U – L + S) / S, 1 … = A (K + i * N) ENDDO K = K + (U – L + S) / S * N I = I + (U – L + S) / S

Summary Transformations to put more subscripts into standard form Loop Normalization Constant Propagation Induction Variable Substitution Do loop normalization before induction-variable substitution Leave optimizations to compilers