1 / 47

Preliminary Transformations

Chapter 4 of Allen and Kennedy. Preliminary Transformations. Harel Paz. Introduction. 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:

halona
Download Presentation

Preliminary Transformations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 of Allen and Kennedy Preliminary Transformations Harel Paz

  2. Introduction • 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

  3. Programmers optimized code 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 U(KI) cannot be tested • Preliminary transformations!

  4. An Example- cont’ KI is an auxiliary induction variable 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

  5. Induction-variable substitution Replaces references to auxiliary induction variable with direct functions of loop index. An Example- cont’ KI contains a loop- invariant value 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 KI is an auxiliary induction variable of the outer loop

  6. Second application of induction-variable substitution- remove all references to KI 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

  7. An Example- cont’ INC and K are constant values 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

  8. Applying Constant Propagation Substitutes the constants 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

  9. Applying Dead Code Elimination Removes all unused code An Example- cont’ 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. 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 Information Requirements

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

  13. 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 Direction vector of(<,>) J+I-1=J’+I’-1 I=I’-1

  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 graphis a graph that contains an edge from each definition point in the program to every possible use of the variable at run time. Definition-use Graph

  17. Switch Case 1 Case 2 Case 3 B Blocks • 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. 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. Definition-use Graph- Sets

  20. Computing reaches for one block b may immediately change all other reaches including b’sitself 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 Definition-use Graph:Reaches Set

  21. Definition-use Graph – Calculating reaches

  22. Definition-use Graph – Calculating reaches

  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. X=t*2+j Y=X X=5 Dead Code Elimination –Main Idea Output X and Y values

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

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

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

  28. Constant Propagation - Algorithm

  29. Constant Propagation - Algorithm y

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

  31. Static Single-Assignment Form • SSA- a variation on the definition-use graph with the following properties: • Each assignment creates a different variable name. • 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. X= X= X= =X =X =X SSA Example

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

  34. 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. Forward Expression Substitution 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 A(K) = A(K) + 5 ELSE K = I + 1 A(K) = A(K) + 6 END ENDDO DO I = 1, 100 IF (I%2==0) THEN K = I + 2 END A(K) = A(K) + 5 ENDDO  

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

  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. Second Part of the Lecture Preliminary Transformations Harel Paz

  39. Goal: high dependence test accuracy Preliminaries transformations: Loop normalization Dead code elimination Constant propagation Induction variables substitution Forward expression substitution Last Week Data flow analysis: definition-use graph & SSA

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

  41. 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. Induction Variable Substitution

  42. Induction Variable Recognition-Main Idea DO I = 1, N A(I) = B(K) + 1 K = K + 4 … D(K) = D(K) + A(I) ENDDO • 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.

  43. S Φ K=K+4 Induction Variable Substitution

  44. Φ A(I)=B(K)+1 S K=K+4 D(K)=D(K)+A(I) Induction Variable Substitution DO I = 1, N A(I) = B(K)+1 K = K + 4 … D(K) = D(K)+A(I) ENDDO

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

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

More Related