Presentation is loading. Please wait.

Presentation is loading. Please wait.

Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz.

Similar presentations


Presentation on theme: "Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz."— Presentation transcript:

1 Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz

2 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

3 An Example 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) ENDDO Programmers optimized code  Preliminary transformations! U(KI) cannot be tested

4 An Example- cont ’ 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) ENDDO INC is invariant in the inner loop KI is an auxiliary induction variable

5 An Example- cont ’ 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) 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

6 An Example- cont ’ 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)) ENDDO KI = KI + 100 * 100 * INC Second application of induction-variable substitution- remove all references to KI

7 An Example- cont ’ 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 S(I) = U(KI + I * (100*INC)) ENDDO KI = KI + 100 * 100 * INC INC and K are constant values

8 An Example- cont ’ 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) ENDDO KI = 20000 Applying Constant Propagation Substitutes the constants

9 An Example- cont ’ Applying Dead Code Elimination Removes all unused code INC = 2 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) ENDDO KI = 20000

10 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

11 Loop Normalization Lower Bound 1, with Stride 1 Makes dependence testing as simple as possible. Makes transformations like induction- variable substitution easier to perform.

12 Loop Normalization - Algorithm Procedure normalizeLoop(L 0 ); i = a unique compiler-generated LIV S1: replace the loop header for L 0 ( 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;

13 Loop Normalization - Caveat Un-normalized: DO I = 1, M DO J = I, N A(J, I) = A(J, I - 1) + 5 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 Direction vector of (<,=) J=J ’ I=I ’ -1 J+I-1=J ’ +I ’ -1 I=I ’ -1 Direction vector of ( )

14 Loop Normalization - Caveat Caveat Consider interchanging loops (<,=) becomes (=,<) OK ( ) becomes (>,<) Problem Handled by another transformation

15 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.

16 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.

17 Blocks Switch Case 1Case 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.

18 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.

19 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.

20 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 1Case 2 Case 3 B

21 Definition-use Graph – Calculating reaches

22

23 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

24 Dead Code Elimination – Main Idea Output X and Y values Y=X X=t*2+j X=5

25 Dead Code Elimination dead code should be eliminated output z z=k+5 y x

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

27 Constant Propagation – Main Idea X=5 Y=X+ZY=X+15 xx Values can only move down in the lattice

28 Constant Propagation - Algorithm

29 y

30 Complexity Issue Number of definition-use edges can grow very large in presence of control flow. X= =X 9 definition-use edges

31 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.

32 SSA Example X= =X

33 Another Example DO I = 1, N..... ENDDO I = 1 IF ( I > N ) GO TO E …… I = I + 1 GO TO L L E I1 = 1 I3= Φ(I1,I2) IF ( I3 > N ) GO TO E …… I2 = I3 + 1 GO TO L L E I1 = 1 IF ( I > N ) GO TO E …… I2 = I + 1 GO TO L L E Φ

34 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) + 5 ENDDO DO I = 1, 100 A(I+2) = A(I+2) + 5 ENDDO

35 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) + 5 ENDDO 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 END ENDDO 

36 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 = 1 I3= Φ(I1,I2) IF ( I3 > N ) GO TO E K=I3+2 … I2 = I3 + 1 GO TO L L E Φ

37 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.

38 Preliminary Transformations Second Part of the Lecture Harel Paz

39 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

40 SSA - Loop Example DO I = 1, N..... ENDDO I = 1 IF ( I > N ) GO TO E …… I = I + 1 GO TO L L E I1 = 1 I3= Φ(I1,I2) IF ( I3 > N ) GO TO E …… I2 = I3 + 1 GO TO L L E I1 = 1 IF ( I > N ) GO TO E …… I2 = I + 1 GO TO L L E Φ

41 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 + iexpr L at every location L where it is used in the loop, where cexpr and iexpr L are expressions that do not vary in the loop, although different locations in the loop may require substitution of different values of iexpr L. We ’ ll only deal with auxiliary induction variables defined by a statement like: K = K ± cexpr.

42 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

43 Induction Variable Substitution K=K+4 Φ S

44 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) S Induction Variable Substitution

45 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) ENDDO K = K + (U – L + S) / S * N Problem: Inefficient code Nonlinear subscript  IVsub for such loop is fruitless!

46 IVSub on a Normalized Loop DO I = L, U, S K = K + N … = A(K) ENDDO I = 1 DO i = 1, (U-L+S)/S, 1 K = K + N … = A (K) I = I + 1 ENDDO Advantages: Efficient code. Appropriate for dependence testing.  IVsub for such loop is beneficial! I = 1 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 Loop normalization IVSub

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


Download ppt "Preliminary Transformations Chapter 4 of Allen and Kennedy Harel Paz."

Similar presentations


Ads by Google