480 likes | 619 Views
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:
E N D
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: • Higher dependence test accuracy
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!
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
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
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
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
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
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
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
Loop Normalization • Lower Bound 1, with Stride 1 • Makes dependence testing as simple as possible. • Makes transformations like induction-variable substitution easier to perform.
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
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
Loop Normalization - Caveat • Caveat • Consider interchanging loops • (<,=) becomes (=,<) OK • (<,>) becomes (>,<) Problem • Handled by another transformation
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.
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
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.
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.
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
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
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
X=t*2+j Y=X X=5 Dead Code Elimination –Main Idea Output X and Y values
z=k+5 y x output z Dead Code Elimination dead code should be eliminated
Constant Propagation • Replace all variables that have constant values (at a certain point) at runtime with those constant values.
x x Y=X+Z Y=X+15 Values can only move down in the lattice Constant Propagation –Main Idea X=5
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
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.
X= X= X= =X =X =X SSA Example
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
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
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
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.
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.
Second Part of the Lecture Preliminary Transformations Harel Paz
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
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
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
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.
S Φ K=K+4 Induction Variable Substitution
Φ 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
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!
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
Summary • Transformations to put more subscripts into standard form • Loop Normalization • Induction Variable Substitution • Constant Propagation