Chains of Recurrence based analysis in SSA form
Download
Report
Transcript Chains of Recurrence based analysis in SSA form
Toward Efficient Flow-Sensitive
Induction Variable Analysis and
Dependence Testing for Loop
Optimization
Yixin Shou, Robert A. van Engelen, Johnnie Birch, Kyle A. Gallivan
Restructuring Compilers Research Lab
Florida State University
Outline
Motivation
The CR# Algebra
Flow-Sensitive Induction Variable Analysis
Algorithms with CRs on SSA forms
Application: Data Dependence Testing
Conclusions
Motivation
Induction Variable Recognition: a Classic Compiler
Problem
Most Loop Optimizations Rely on Accurate IV
Recognition
Loop strength reduction
IV elimination
IV substitution
Array recovery
Array data dependence testing
Problem
nonlinear IV
conditionally updated IV
Pointer arithmetic in loops
Motivating Examples (cont.)
do l = 1, latt(1)
k = 0
if (...) then
do j = 1, n
nred = nred+1
a[k] = ...
lisred(nred) = ...
if (...) then
else
k = k+1
...
else
endif
k = j
...
endif
end do
...
end do
k = 0
do j = 1, n
a[j-1] = ...
...
end do
(a) Loop from QCD
Benchmark
(c) Optimized
Loop
(b) Original Loop
Conditional updated IV and partial wrap around variables in loops
k = 1
Do j = 1, n
a[k]= ...
if (...) then
k = k+2
else if (...) then
k = j
else
...
endif
enddo
Example Loop
Value Sequence of Function
Motivating Examples
35
g(i)
f(i)
30
25
20
15
10
5
0
Index i
0
1
2
3
4
Bounding Functions
IVs which do not have closed forms
5
More Powerful IV Recognition
Methods
IV recognition can quickly gets more complicated
with deps and flow
Induction variable recognition with chains of
recurrences by van Engelen
Linear, nonlinear and conditional IV
Further improve other compiler optimizations
Extend IV analysis algorithm
based on CR# algebra
on SSA form
Outline
Motivation
The CR# Algebra
Flow-Sensitive Induction Variable Analysis Algorithms
on SSA forms
Application: Data Dependence Testing
Conclusions
Preliminaries
A chain of recurrences (CR) represents a polynomial or exponential
function or mix evaluated over a unit-distance grid
Basic form:
Фi = {ϕ0, ⊙1, f1}i, where ⊙1 = + or *
A value sequence with initial value ϕ0 updated by adding or
multiplying the current value by step function f1
When f1 is another CR form, this produces a chain of
recurrences
Фi = {ϕ0, ⊙1, {ϕ1, ⊙2, {ϕ2,..., ⊙k, {ϕk}i}i}i}i
which is usually written in flattened form
Фi = {ϕ0, ⊙1, ϕ1, ⊙2, ϕ2,..., ⊙ k, ϕ k}i
Multi-variate CR forms
CR coefficients are nested CR forms of a different grid variable
Chains of Recurrences: Algebra
CR simplification rules produces an equivalent CR
expression that can be evaluated more efficiently.
CR Inverse rules (CR-1) derive a closed form
functions corresponding to CR expressions.
Some selected rules
{x, +, y} + c
{x+c, +, y}
c{x, +, y}
{c·x, +, c·y}
{x, +, y} + {u, +, v}
{x+u, +, y+v}
{x, +, y} * {u, +, v}
{x·u, +, y{u, +, v}+v{x, +, y}+y·v}
{0, +, f1}i
i * f1
How to Obtain CR Forms using
CR Algebra
Algorithm to compute the CR form of a
symbolic function f(i):
1.
2.
Replace index i with its CR form {0,+,1} in the symbolic form of f
Compute CR form using the CR algebra rewrite rules
Example:
f(i) = c·(i+a) = c·({0, +, 1}+a) = c{a, +, 1} = {c·a, +, c}
CR# Alignment and Bounds
CR# Algebra: a new extension of CR algebra
The Delay Operator #
Define recurrences with out of sequence values
CR form with Delayed operator can be used to define any sequence of
values followed by a polynomial, geometric CR form, or another delayed
form.
wrap-around variables can be accurately represented using delayed
forms
Aligned CR forms
CR Alignment Rules
Same index variable
Same length
Operators pairwise match
Lemma 1, 2, 3
Compute bounds for two CR form after CR alignment
Close form function of CRs
f(i) = {1, #, 1, +, 2}i =
1
if i = 0
2*i -1 if i > 0
g(i) = {1, *, 2}i = 2i for i = 0…n
After CR alignment
Value Sequence of Function
Example
35
f(i)
30
g(i)
25
20
15
10
5
0
Index i
f(i) = {1, #, 1, +, 2}i = {1, #, 1, +, 2, *, 1}i
g(i) = {1, *, 2}i = {1, #, 2, *, 2}i = {1, #, 2, +, 2, *, 2}i
0
1
2
After CR Bounding
Min({1, #, 1, +, 2, *, 1}i , {1, #, 2, +, 2, *, 2}i) = {1, #, 1, +, 2, *, 1}I
Max({1, #, 1, +, 2, *, 1}i , {1, #, 2, +, 2, *, 2}i) = {1, #, 2, +, 2, *, 2}I
3
4
5
Outline
Motivation
The CR# Algebra
Flow-Sensitive Induction Variable Analysis Algorithms
on SSA forms
Application: Data Dependence Testing
Conclusions
SSA Form
Static Single Assignment (SSA) Form: a internal compiler
representation
each use refers to a single definition
each definition has a unique name by attaching indices to variables
The compiler inserts special join functions (called -functions) at points
where different control flow paths meet.
Advantage of SSA
explicit def-use pairs to help compiler data flow optimizations
no anti and output data dependences
Compiler dataflow optimizations algorithms are strongly enhanced by
using SSA forms.
Integrate CR Approach on SSA Form
Static Single Assignment (SSA) form is used in
many modern compilers to facilitate a wide range
of powerful compiler dataflow optimizations.
CR representation is a very effective form for
induction expression in loops and facilitate the
loop analysis, optimization and dependence
testing.
Integrate the CR approach in SSA forms
improve many data flow analysis and dependence testing.
Make our unified CR framework adapt to a much wider range of
compilers
Flow-Sensitive Induction Variable
Analysis Algorithms on SSA forms
The algorithm for IV recognition with CRs is based on A
major observation from Gerlek, Stolz, and Wolfe
A strongly connected region (SCR) of SSA chain graph represent a
induction variable in the loop.
SCR: a set of node in the graph where there exist a path for any
two nodes in the set.
Collect initial value and induction sequence information in the SCR
cycle.
Determine recurrence relations and then CR form of the induction
variable
Example 1: Linear recurrences pattern
Original loop nest:
a := 1
while (a<10) {
x := a + 2
a := a+1
}
SSA form:
a0 := 1
if (a0>=10) goto L2
L1:
a1 := (a0, a2)
x0 :=a1 + 2
a2 := a1+1
if (a2<10) goto L1
L2:
Recurrence Relations Set:
<a1, (1, a1+1)>
CR form for variable a1
{1, +, 1}
1
a0
2
a1
1
+
x0
a2
+
Example 2: Non-linear recurrences pattern
x := 0
i := 1
while (i<10) {
x := x+i
i := i+1
}
x0 := 0
i0 := 1
if (i0>=10) goto L2
L1:
x1:= (x0, x2)
i1 := (i0, i2)
x2 := x1+i1
i2 := i1+1
if (i2<10) goto L1
L2:
Recurrence Relations Set:
<i1, (1, i1+1)>
<x1, (0, x1+i1)>
CR form:
i1: {1,+, 1}
x1: {0, +, 1, +, 1}
1
0
i0
x0
i1
1
x1
x2
+
i2
+
Example 3: Conditional recurrences pattern
j := 0
i := 1
while (i<10) {
if (p) then
j := j+2
else
j := j+3
i = i + 1
}
j0 := 0 i0 := 1
if (i0>=10) goto L2
L1: i1 := (i0, i2)
j1:= (j0, j4)
if (p)
j2 := j1+2
goto L3
j3 := j1+3
L3: j4:= (j2, j3)
i2 := i1+1
if (i2<10) goto L1
L2:
Recurrence Relations Set:
<i1, (1, i1+1)>
<j1, (0, j1+2)>, <j1, (0, j1+3)>
0
j0
2
j1
3
+
+
j4
j3
j2
Example 4:
Conditional and Wrap Around Recurrences
Outline
Motivation
The CR# Algebra
Flow-Sensitive Induction Variable Analysis Algorithms
on SSA forms
Application: Data Dependence Testing
Conclusions
Data Dependence testing Based on
CR# algebra
Accurate data dependence testing is crucial for
parallelizing compilers
Benefits of Array & Pointer Dependence Testing
with CRs
Directly build the dependence equation based on CR forms
Eliminates IV substitution phase
Eliminates the need for pointer-to-array conversion for dependence testing
on pointer-based C code
Able to solve dependence with linear, nonlinear, conditional
updated variables and pointers arithmetic.
CR Dependence Equations
Compute dependence equations in CR form for
pointer and array accesses in loop nests directly
without IV substitution or pointer-to-array
conversion
Solve the equations by computing value ranges of
the CR forms to determine solution intervals
If the solution space is empty, there is no
dependence
Determining the Value Range of
a CR Form on a Domain
If a function is monotonic on its domain, then it is
trivial to find its exact value range
The monotonic properties of a CR form are
determined by the stepping functions
Suppose x(i) = {x0, +, s(i-1)} for i = 0, …, n
If s(i-1) > 0 then x(i) is monotonically increasing
If s(i-1) < 0 then x(i) is monotonically decreasing
Example:
Loop Nest
CR Solution
p = A
q = A+n
for (i=0 to n-1)
*p++ = *q++
if … then
q = q+1
endif
P1:
{A, +, 1}
endfor
q1:
Min CR:
{A+n, +, 1}
Max CR:
{A+n, +, 2}
Lower Bound of
Dependence
Equation
Low({A+n, +, 1} –
{A, +, 1})
= n > n -1 >= 0
Conclusions
The CR-based compiler analysis framework supports:
IV recognition
Array dependence testing with affine, nonlinear, and symbolic
dependence equations
Dependence testing on pointer arithmetic
Induction variable substitution
Flow-sensitive generalized induction variables (GIVs)
analysis algorithm based on CR# algebra are able to
analyze
nonlinear index expression
Non-closed index expression
Enhance the monotonic analysis, data dependence testing and other
compiler optimizations
Thanks