preliminary transformations chapter 4 of allen and kennedy harel paz

47
Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Upload: melissa-ward

Post on 16-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Preliminary Transformations

Chapter 4 of Allen and Kennedy

Harel Paz

Page 2: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Most dependence tests require subscript expressions to be linear or affine functions of loop induction variables, with known constant coefficient and at most a symbolic additive constant. Affine functions:

Higher dependence test accuracy

Introduction

bxAxAxAxxxf nnn ...),...,,( 221121

Page 3: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example

INC = 2KI = 0DO I = 1, 100 DO J = 1, 100 KI = KI + INC U(KI) = U(KI) + W(J) ENDDO S(I) = U(KI)ENDDO

Programmers optimized code

Preliminary transformations!

U(KI) cannot be tested

Page 4: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

INC = 2KI = 0DO I = 1, 100 DO J = 1, 100 KI = KI + INC U(KI) = U(KI) + W(J) ENDDO S(I) = U(KI)ENDDO

INC is invariant in the inner loop

KI is an auxiliary induction variable

Page 5: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

INC = 2KI = 0DO 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)ENDDO

Induction-variable substitution Replaces references to auxiliary induction

variable with direct functions of loop index.

KI is an auxiliary induction variable of the outer loop

KI contains a loop- invariant value

Page 6: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

INC = 2KI = 0DO 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))ENDDOKI = KI + 100 * 100 * INC

Second application of induction-variable substitution- remove all references to KI

Page 7: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

INC = 2KI = 0DO 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 S(I) = U(KI + I * (100*INC))ENDDOKI = KI + 100 * 100 * INC

INC and K are constant values

Page 8: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

INC = 2! Deleted: KI = 0DO 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)ENDDOKI = 20000

Applying Constant Propagation Substitutes the constants

Page 9: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

An Example- cont’

Applying Dead Code Elimination Removes all unused code

INC = 2DO 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)ENDDOKI = 20000

Page 10: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Information Requirements Preliminaries transformations: induction

variables substitution, constant propagation, dead code elimination Loop normalization.

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

Data flow analysis

Page 11: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Loop Normalization

Lower Bound 1, with Stride 1 Makes dependence testing as simple

as possible. Makes transformations like induction-

variable substitution easier to perform.

Page 12: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Loop Normalization - Algorithm

Procedure normalizeLoop(L0);

i = a unique compiler-generated LIVS1: 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 L + (i -1)*S;S3: insert a finalization assignment I = L + (i -1)*S; immediately after the end of the loop;end normalizeLoop;

Page 13: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Loop Normalization - Caveat

Un-normalized: DO I = 1, M DO J = I, N A(J, I) = A(J, I - 1) + 5 ENDDO ENDDO

Normalized: DO I = 1, M DO J = 1, N – I + 1 A(J + I – 1, I) = A(J + I – 1, I – 1) + 5 ENDDO ENDDO

Direction vector of (<,=)

J=J’I=I’-1

J+I-1=J’+I’-1I=I’-1

Direction vector of (<,>)

Page 14: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Loop Normalization - Caveat

Caveat Consider interchanging loops

(<,=) becomes (=,<) OK (<,>) becomes (>,<) Problem

Handled by another transformation

Page 15: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Data Flow Analysis Goal: perform preliminaries

transformations. Need: Understand how data elements

are created and used in a program. Definition-use Graph. Static single assignment (SSA).

Data flow analysis are heavily used in other optimizing transformations that preserve the program’s meaning.

Page 16: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

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.

Page 17: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Blocks

Switch

Case 1 Case 2 Case 3

B

Basic block is a maximal group of statements such that one statements in the group is executed if and only if only every statements is executed.

Page 18: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Block’s Definition-Use Edges

Constructing definition-use edges for a basic block: Walk through each statement in order in the

block. For each statement, note the defined

variable, and the variables it uses. For each use, add an edge from the last

block definition. When a new definition is encountered for a

variable, it kills the existing definition.

Page 19: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Definition-use Graph- Sets Basic block computation produces the sets:

uses(b): the set of all variables used within 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.

Constructing the graph for the whole program: reaches(b): the set of all definitions from all blocks

(including b) that can possibly reach b.

Page 20: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Definition-use Graph:Reaches Set

Computing reaches for one block b may immediately change all other reaches including b’s itself since reaches(b) is an input into other reaches equations.

Achieving correct solutions requires simultaneously solving all equations There is a workaround

Switch

Case 1 Case 2 Case 3

B

)(

)))()(()(()(bPp

pkilledpreachespdefsoutbreaches

Page 21: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Definition-use Graph – Calculating reaches

Page 22: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Definition-use Graph – Calculating reaches

Page 23: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Dead Code Elimination Removes all dead code, thus making the

code cleaner Dead Code is code whose results are never

used in any ‘Useful statements’. What are Useful statements ?

Output statements, input statements, control flow statements, and their required statements

Page 24: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Dead Code Elimination –Main Idea

Output X and Y values

Y=X

X=t*2+j

X=5

Page 25: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Dead Code Elimination

dead code should be eliminated

output z

z=k+5y

x

Page 26: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Constant Propagation

Replace all variables that have constant values (at a certain point) at runtime with those constant values.

Page 27: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Constant Propagation –Main Idea

X=5

Y=X+Z Y=X+15

xx

otherwiseconstnon

jiconstconstconst

constnonanyconstnon

anyanyunknown

iji

Values can only move down in the lattice

Page 28: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Constant Propagation - Algorithm

Page 29: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Constant Propagation - Algorithm

y

Page 30: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Complexity Issue

Number of definition-use edges can grow very large in presence of control flow.

X= X= X=

=X =X =X

1S 2S 3S

4S

5S 6S 7S

9 definition-use edges

Page 31: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Static Single-Assignment Form

SSA- a variation on the definition-use graph with the following properties:

1. Each assignment creates a different variable name.

2. Where control flow joins, a special operation is inserted to merge different incarnations of the same variable.

Benefits: Reduces the number of definition-use edges. Improves performance of algorithms.

Page 32: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

SSA Example

X= X= X=

=X =X =X

1S 2S 3S

4S

5S 6S 7S

1S 2S 3S

4S

5S 6S 7S

1X 2X 3X

),,( 3214 XXXX

4X 4X 4X

Page 33: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Another Example

DO I = 1, N .....ENDDO

I = 1IF ( I > N ) GO TO E……I = I + 1GO TO L

L

E

I1 = 1I3= Φ(I1,I2)IF ( I3 > N ) GO TO E……I2 = I3 + 1GO TO L

L

E

I1 = 1IF ( I > N ) GO TO E……I2 = I + 1GO TO L

L

E

Φ

1I

2I

Page 34: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Forward Expression Substitution

Forward expression substitution we’ll deal with: substitution of statements whose right-hand side variables include only the loop induction variable or variables that are loop invariant.

DO I = 1, 100 K = I + 2 A(K) = A(K) + 5ENDDO

DO I = 1, 100 A(I+2) = A(I+2) + 5ENDDO

Page 35: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

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.

DO I = 1, 100 IF (I%2==0) THEN

K = I + 2 END A(K) = A(K) + 5ENDDO

DO I = 1, 100 IF (I%2==0) THEN

K = I + 2 A(K) = A(K) + 5 ELSE K = I + 1 A(K) = A(K) + 6 ENDENDDO

Page 36: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Forward Expression Substitution- Algorithm

In order to forward substitute expressions involving only loop invariant variables and the loop invariant variable: Examine each SSA edge into a

statement S, which is a candidate for forward substitution.

If the edge comes from the loop, it must be the Φ-node for the loop induction variable, at the loop beginning.

I1 = 1I3= Φ(I1,I2)IF ( I3 > N ) GO TO EK=I3+2…I2 = I3 + 1GO TO L

L

E

Φ

23 IK

S

Page 37: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Forward Expression Substitution- Algorithm

If a statement S can be forward substituted, examine each SSA edge whose source is S, and whose target is within the loop: If Φ-node, do nothing. Else substitute rhs(S), for every occurrence of lhs(S)

in the SSA sink edge. +Update SSA edges.

If all lhs(S) uses are removed S can be deleted. If all lhs(S) loop uses are removed (but there are

non-loop uses), S should be removed outside the loop.

If not all lhs(S) loop uses are removed, try IV substitution.

Page 38: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Preliminary Transformations

Second Part of the Lecture

Harel Paz

Page 39: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Last Week

Goal: high dependence test accuracy

Preliminaries transformations: Loop normalization Dead code elimination Constant propagation Induction variables substitution

Forward expression substitution

Data flow analysis:definition-use graph & SSA

Page 40: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

SSA - Loop Example

DO I = 1, N .....ENDDO

I = 1IF ( I > N ) GO TO E……I = I + 1GO TO L

L

E

I1 = 1I3= Φ(I1,I2)IF ( I3 > N ) GO TO E……I2 = I3 + 1GO TO L

L

E

I1 = 1IF ( I > N ) GO TO E……I2 = I + 1GO TO L

L

E

Φ

1I

2I

Page 41: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Induction Variable Substitution

Need to recognize auxiliary induction variables. 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.

We’ll only deal with auxiliary induction variables defined by a statement like: K = K ± cexpr.

Page 42: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Induction Variable Recognition-

Main Idea

A statement S may define an auxiliary induction variable for a loop L if S is contained in a simple cycle of SSA edges that involves only S and one another statement, a Φ-node, in the loop. Check that the form is: K = K ± cexpr. Check that ‘cexpr’ is loop invariant.

DO I = 1, N A(I) = B(K) + 1 K = K + 4 … D(K) = D(K) + A(I)ENDDO

Page 43: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Induction Variable Substitution

K=K+4

ΦhS

oS

S

Page 44: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

DO I = 1, N A(I) = B(K)+1 K = K + 4 … D(K) = D(K)+A(I)ENDDO

K=K+4

A(I)=B(K)+1

Φ

D(K)=D(K)+A(I)

hS

oS

S

Induction Variable Substitution

Page 45: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

IVSub Without Loop Normalization

DO I = L, U, S K = K + N … = A(K)ENDDO

DO I = L, U, S … = A(K + (I – L + S) / S * N)ENDDOK = K + (U – L + S) / S * N

Problem: Inefficient code Nonlinear subscript

IVsub for such loop is fruitless!

Page 46: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

IVSub on a Normalized Loop

DO I = L, U, S K = K + N … = A(K)ENDDO

I = 1DO i = 1, (U-L+S)/S,

1 K = K + N … = A (K) I = I + 1ENDDO

Advantages: Efficient code. Appropriate for

dependence testing. IVsub for such loop is

beneficial!I = 1DO i = 1, (U – L + S) / S, 1 … = A (K + i * N)ENDDOK = K + (U – L + S) / S * NI = I + (U – L + S) / S

Loop normalization

IVSub

Page 47: Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

Summary

Transformations to put more subscripts into standard form Loop Normalization Induction Variable Substitution Constant Propagation