Run-Time Requirements for Compilers Compilers Spring 2001 Robert Dewar.
Advanced Restructuring Compilers
Transcript of Advanced Restructuring Compilers
AdvancedRestructuring
Compilers
Advanced Topics Spring 2008
Prof. Robert van Engelen
HPC II Spring 2008 22/19/08
Overview
Data dependence classification
Data dependences in loops
Dependence direction and distance Loop-independent dependences
K-level loop-carried dependences
Testing dependence Dependence equations
Dependence solvers
Two example loop transformations Fission
Interchange
HPC II Spring 2008 32/19/08
Data Dependence
Definition
There is a data dependence from statement S1 tostatement S2 iff
1) both statements S1 and S2 access the same memorylocation and at least one of them stores into it, and
2) there is a feasible run-time execution path fromstatement S1 to S2
HPC II Spring 2008 42/19/08
Data Dependence Classification
Definition
A dependence relation δ is
A true dependence (or flowdependence), denoted S1 δ S2
An anti dependence, denotedS1 δ-1 S2
An output dependence,denoted S1 δo S2
S1 X = …S2 … = X
S1 … = XS2 X = …
S1 X = …S2 X = …
S1 δ S2
S1 δ-1 S2
S1 δo S2
HPC II Spring 2008 52/19/08
Compiling for Parallelism
Theorem
Any instruction statement reordering transformationthat preserves every dependence in a programpreserves the meaning of that program
Bernstein’s conditions for loop parallelism: Iteration I1 does not write into a location that is read by iteration
I2
Iteration I2 does not write into a location that is read by iterationI1
Iteration I1 does not write into a location that is written into byiteration I2
HPC II Spring 2008 62/19/08
Dependence in Loops
The statement instances S1[i]for iterations i = 1,…,Nrepresent the loop execution
We have the following flowdependences:
S1[1] δ S1[2]S1[2] δ S1[3]…S1[N-1] δ S1[N]
DO I = 1, NS1 A(I+1) = A(I) + B(I) ENDDO
S1[1] A(2) = A(1) + B(1)S1[2] A(3) = A(2) + B(2)S1[3] A(4) = A(3) + B(3)S1[4] A(5) = A(4) + B(4) …
Statement instances with flowdependences
HPC II Spring 2008 72/19/08
Representing Dependenceswith Data Dependence Graphs
It is generally infeasible torepresent all datadependences that arise in aprogram
Usually only static datadependences are recorded S1 δ(=) S2 means S1[i] δ S2[i]
for all i = 1, …, 10000 S2 δ(<) S2 means S1[i] δ S2[j]
for all i,j = 1, …, 10000 with i < j
A data dependence graphcompactly represent datadependences in a loop nest
DO I = 1, 10000S1 A(I) = B(I) * 5S2 C(I+1) = C(I) + A(I) ENDDO
Static data dependences foraccesses to A and C:S1 δ(=) S2 and S2 δ(<) S2
Datadependencegraph
S1
S2
(=)
(<)
HPC II Spring 2008 82/19/08
Iteration Vector
Definition
Given a nest of n loops, the iteration vector i of aparticular iteration of the innermost loop is a vector ofintegers
i = (i1, i2, …, in)
where ik represents the iteration number for the loop atnesting level k
The set of all possible iteration vectors spans an iterationspace over loop statement instances Sj[i]
HPC II Spring 2008 92/19/08
Iteration Vector Example
The iteration space of thestatement at S1 is the set ofiteration vectors {(1,1), (2,1),(2,2), (3,1), (3,2), (3,3)}
Example: at iteration i = (2,1)statement instance S1[i]assigns the value of A(1,2) toA(2,1)
DO I = 1, 3 DO J = 1, IS1 A(I,J) = A(J,I) ENDDO ENDDO
(1,1)
(2,1) (2,2)
(3,2) (3,3)(3,1)
I
J
HPC II Spring 2008 102/19/08
Iteration Vector Ordering
The iteration vectors are naturally ordered according to alexicographical order For example, iteration (1,2) precedes (2,1) and (2,2)
Definition
Iteration i precedes iteration j, denoted i < j, iff
1) i[1:n-1] < j[1:n-1], or
2) i[1:n-1] = j[1:n-1] and in < jn
HPC II Spring 2008 112/19/08
Cross-Iteration Dependence
Definition
There exist a dependence from S1 to S2 in a loop nest iffthere exist two iteration vectors i and j such that
1) i < j and there is a path from S1 to S2
2) S1 accesses memory location M on iteration i and S2
accesses memory location M on iteration j
3) one of these accesses is a write
HPC II Spring 2008 122/19/08
Dependence Example
Show that the loop has nocross-iteration dependence
Answer: there are no iterationvectors i and j in {(1,1), (2,1),(2,2), (3,1), (3,2), (3,3)} suchthat i < j and S1 in i writes to the same
element of A that is read at S1
in iteration j
or S1 in iteration i reads anelement A that is written at S1
in iteration j
DO I = 1, 3 DO J = 1, IS1 A(I,J) = A(J,I) ENDDO ENDDO
(1,1)
(2,1) (2,2)
(3,2) (3,3)(3,1)
I
J
HPC II Spring 2008 132/19/08
Dependence Distance andDirection Vectors
Definition
Given a dependence from S1 on iteration i to S2 oniteration j, the dependence distance vector d(i, j) isdefined as d(i, j) = j - i
Given a dependence from S1 on iteration i and S2 oniteration j, the dependence direction vector D(i, j) isdefined for the kth component as
D(i, j)k = “<” if d(i, j)k > 0“=” if d(i, j)k = 0“>” if d(i, j)k < 0
HPC II Spring 2008 142/19/08
Example 1
DO I = 1, 3 DO J = 1, IS1 A(I+1,J) = A(I,J) ENDDO ENDDO
Flow dependence between S1 and itself on:i = (1,1) and j = (2,1): d(i, j) = (1,0), D(i, j) = (<, =)i = (2,1) and j = (3,1): d(i, j) = (1,0), D(i, j) = (<, =)i = (2,2) and j = (3,2): d(i, j) = (1,0), D(i, j) = (<, =)
S1(<,=)
1 2 31
2
3
J
I
HPC II Spring 2008 152/19/08
Example 2
DO I = 1, 4 DO J = 1, 4S1 A(I,J+1) = A(I-1,J) ENDDO ENDDO
J
I1 2 3
1
2
3
4
4
Distance vector is (1,1)S1 δ(<,<) S1
S1(<,<)
HPC II Spring 2008 162/19/08
Example 2
I1 2 3
1
2
3
4
4
Distance vector is (1,-1) S1 δ(<,>) S1
DO I = 1, 4 DO J = 1, 5-IS1 A(I+1,J+I-1) = A(I,J+I-1) ENDDO ENDDO
J
S1(<,>)
HPC II Spring 2008 172/19/08
Example 3
I1 2 3
1
2
3
4
4
Distance vectors are (0,1) and (1,0)S1 δ(=,<) S1
S2 δ(<,=) S1
S2 δ(<,=) S2
DO I = 1, 4 DO J = 1, 4S1 B(I) = B(I) + A(I,J)S2 A(I+1,J) = A(I,J) ENDDO ENDDO
J
S1(=,<)
S2
(<,=)
(<,=)
HPC II Spring 2008 182/19/08
Loop-Independent Dependences
Definition
Statement S1 has a loop-independent dependence on S2
iff there exist two iteration vectors i and j such that
1) S1 refers to memory location M on iteration i, S2 refersto M on iteration j, and i = j
2) there is a control flow path from S1 to S2 within theiteration
HPC II Spring 2008 192/19/08
K-Level Loop-CarriedDependences
DefinitionStatement S1 has a loop-carried dependence on S2 iff1) there exist two iteration vectors i and j such that S1refers to memory location M on iteration i and S2 refers toM on iteration j2) d(i, j) > 0, that is, D(i, j) contains a “<” as its leftmostnon-“=” component
A loop-carried dependence from S1 to S2 is1) lexically forward if S2 appears after S1 in the loop body2) lexically backward if S2 appears before S1 (or if S1=S2)
The level of a loop-carried dependence is the index ofthe leftmost non-“=” of D(i, j) for the dependence
HPC II Spring 2008 202/19/08
Example
DO I = 1, 10 DO J = 1, 10 DO K = 1, 10S1 A(I,J,K+1) = A(I,J,K) ENDDO ENDDO ENDDO
S1 δ(=,=,<) S1
All loop-carried dependencesare of level 3, D(i, j) = (=, =, <)
Level-k dependences aresometimes denoted by Sx δk Sy
S1 δ3 S1Alternative notation for alevel-3 dependence
HPC II Spring 2008 212/19/08
Combining Direction Vectors
A loop nest can have multipledifferent directions at the sameloop level k
We abbreviate a level-kdirection vector componentwith “*” to denote any direction
DO I = 1, 10 DO J = 1, 10S1 A(J) = A(J)+1 ENDDO ENDDO
DO I = 1, 10S1 S = S + A(I) ENDDO
DO I = 1, 9S1 A(I) = …S2 … = A(10-I) ENDDO
S1 δ(*) S2≡
S1 δ(<) S2
S1 δ(=) S2
S1 δ(>) S2
S1 δ(*) S1
S1 δ(*,=) S1
HPC II Spring 2008 222/19/08
How to DetermineDependences?
A system of Diophantine dependence equations is set upand solved to test direction of dependence (flow/anti) Proving there is (no) solution to the equations means there is (no)
dependence Most dependence solvers require affine array index expressions
of the forma1 i1 + a2 i2 + … + an in + a0
where ij are loop index variables and ak are integer
Non-linear subscripts are problematic Symbolic terms Function values Indirect indexing etc
HPC II Spring 2008 232/19/08
Dependence Equation:Testing Flow Dependence
DO I = 1, NS1 A(I+1) = A(I) ENDDO
DO I = 1, NS1 A(f(I)) = A(g(I)) ENDDO
DO I = 1, NS1 A(2*I+1) = A(2*I) ENDDO
2α+1 = 2β has no solution
α+1 = β has solution α=β-1
write read
To determine flow dependence:prove the dependence equation
f(α) = g(β)has a solution for iteration
vectors α and β, such that α < β
HPC II Spring 2008 242/19/08
Dependence Equation:Testing Anti Dependence
DO I = 1, NS1 A(I+1) = A(I) ENDDO
DO I = 1, NS1 A(f(I)) = A(g(I)) ENDDO
DO I = 1, NS1 A(2*I) = A(2*I+2) ENDDO
2α = 2β+2 has solution α=β+1
α+1 = β has no solution
To determine anti dependence:prove the dependence equation
f(α) = g(β)has a solution for iteration
vectors α and β, such that β < αwrite read
HPC II Spring 2008 252/19/08
Dependence EquationExamples
DO I = 1, NS1 A(5) = A(I) ENDDO
DO I = 1, NS1 A(5) = A(6) ENDDO
To determine flow dependence:prove there are iteration vectors α < β such that f(α) = g(β)
5 = 6 has no solution
5 = β is solution if 5 < N
DO I = 1, N DO J = 1, NS1 A(I-1) = A(J) ENDDO ENDDO
α = (αI, αJ) and β = (βI, βJ)αI-1 = βJ has solution
HPC II Spring 2008 262/19/08
Dependence System:Loop Normalization
DO I = 2, 100 DO J = 1, I-1S1 A(I,J) = A(J,I) ENDDO ENDDO
Consider normalized loopiteration spaces Set lower bound to zero by
adjusting limits andoccurrence of loop variable
A dependence system consistsof a dependence equationalong with a set of constraints: Solution must lie within loop
bounds
Solution must be integer
Need dependence distance ordirection vector (flow/anti)
DO I = 0, 98 DO J = 0, I+2-2S1 A(I+2,J+1) = A(J+1,I+2) ENDDO ENDDO
HPC II Spring 2008 272/19/08
Dependence System:Formulating Flow Dependence
Consider normalized loopiteration spaces Set lower bound to zero by
adjusting limits andoccurrence of loop variable
A dependence systemconsists of a dependenceequation along with a set ofconstraints: Solution must lie within loop
bounds
Solution must be integer
Need dependence distance ordirection vector (flow/anti)
DO I = 0, 98 DO J = 0, I+2-2S1 A(I+2,J+1) = A(J+1,I+2) ENDDO ENDDO
αI+2 = βJ+1αJ+1 = βI+2
0 < αI , βI < 980 < αJ , βJ < αI0 < αJ , βJ < βI
αI < βI
Dependenceequations
Loopconstraints
Constraint for (<,*) dep. direction
α < β such that f(α) = g(β)where α = (αI, αJ) and β = (βI, βJ)
HPC II Spring 2008 282/19/08
Dependence System:Matrix Notation
Consider normalized loopiteration spaces Set lower bound to zero by
adjusting limits andoccurrence of loop variable
A dependence system consistsof a dependence equationalong with a set of constraints: Solution must lie within loop
bounds
Solution must be integer
Need dependence distance ordirection vector (flow/anti)
1
-1
βJ
αJ
βI
αI
0
-1
1
0
-1
0
0
1=
00-11
10-10
-1000
010-1
0-100
0010
00-10
0
0
0
0
0
0
1
-1
βJ
αJ
βI
αI
<
-1
0
0
0
0
98
0
98
0
HPC II Spring 2008 292/19/08
Fourier-Motzkin Projection
-1-2
-11
11
10
x2
x1
-7
5
9
6
<
x1
x2
Projections on x1 and x2
System of linearinequalities
Ax < b
x2 < 6
x1 + x
2 < 9
x 1 -
x 2 <
5-2x
1 - x2 < -7
HPC II Spring 2008 302/19/08
Fourier-Motzkin VariableElimination (FMVE)
FMVE procedure:
1. Select an unknown xj
2. L = {i | aij < 0}
3. U = {i | aij > 0}4. if L=∅ or U=∅ then xj is
unconstrained (delete it)5. for i ∈ L ∪ U
A[i] := A[i] / |aij| bi := bi / |aij|
6. for i ∈ L for k ∈ U add new inequality A[i] + A[k] < bi + bk
7. Delete old rows L and U
-1-2
-11
11
10
x2
x1
-7
5
9
6
<
0-1
0-2
02
01
x2
x1
2
-1
14
11
<
Select x2: L = {3,4}, U = {1,2} new system:
max(1/2,-2) < x1 < min(11,7)
HPC II Spring 2008 312/19/08
GCD Test
Requires that f(α) and g(β)are affine:f(α)= a0+a1 α1+…+an αn
g(β)= b0+b1 β1+…+bn βn
Reordering gives linearDiophantine equation:a1α1-b1β1+…+anαn-bnβn = b0-a0
which has a solution ifgcd(a1,…,an,b1,…,bn) dividesb0-a0
DO I = 1, NS1 A(2*I+1) = A(2*I) ENDDO
DO I = 1, NS1 A(4*I+1) = A(2*I+3) ENDDO
DO I = 1, 10 DO J = 1, 10S1 A(1+2*I+20*J) = A(2+20*I+2*J) ENDDO ENDDO
Which of these loopshave dependences?
HPC II Spring 2008 322/19/08
Loop Fission
Compute the acyclic condensationof the dependence graph to find alegal order of the loops
S1 DO I = 1, 10S2 A(I) = A(I) + B(I-1)S3 B(I) = C(I-1)*X + ZS4 C(I) = 1/B(I)S5 D(I) = sqrt(C(I))S6 ENDDO
S2
S3
S4
S5
0
01
1
Dependence graph
S2 S5
S3 S4
Acyclic condensation
S1 DO I = 1, 10S3 B(I) = C(I-1)*X + ZS4 C(I) = 1/B(I)Sx ENDDOSy DO I = 1, 10S2 A(I) = A(I) + B(I-1)Sz ENDDOSu DO I = 1, 10S5 D(I) = sqrt(C(I))Sv ENDDO
S3 δ(<) S2
S4 δ(<) S3
S3 δ(=) S4
S4 δ(=) S5
HPC II Spring 2008 332/19/08
Loop Interchange
Compute the direction matrix andfind which columns can bepermuted without violatingdependence relations in originalloop nest
S1 DO I = 1, NS2 DO J = 1, MS3 DO K = 1, LS4 A(I+1,J+1,K) = A(I,J,K) + A(I,J+1,K+1)S5 ENDDOS6 ENDDOS7 ENDDO
S4 δ(<,<,=) S4
S4 δ(<,=,>) S4
< < =< = >
Direction matrix
< = <= > <
< < =< = >
Invalid
< < == < >
< < =< = >
Valid
HPC II Spring 2008 342/19/08
Further Reading
[High] Chapters 5, 7-9 (parts)