Problemcore
Dependence testing
Also called array dependence analysis.
Decide whether two array references in a loop nest can touch the same element, an integer programming problem over the iteration space.
In plain words
Two statements in a loop nest touch an array, and at least one of them writes it. Can some run of the first statement and some run of the second touch the same element? If never, the compiler may reorder, run in parallel or vectorize them freely. If yes, it must keep their order. Each loop index takes integer values between its bounds, so the question is whether a small system of integer equations and inequalities has a solution. That is integer programming, which is hard in general. Compilers therefore run a mix of fast tests that are exact on the common cases and careful on the rest: when a test is not sure, it must answer "maybe", never "independent". Tests also report the distances or the directions of the dependence, because the transformations need them.
Definition
Input. Two references to the same array of dimensions, with affine subscripts:
- a reference in statement with iteration domain ,
- a reference in statement with iteration domain ,
where are integer matrices and integer vectors. The two statements have common loops, the first entries of both vectors. Optionally, a direction vector .
Question. Is there a pair with
The equations are the dependence equations, one per dimension. The relation is , , or no constraint, in the notation of direction vectors used here (Wolfe's; means that the second reference runs in a later iteration of loop ). The set of solutions is a set of integer points of a polyhedron, in the isl notation { S[i] -> T[j] : F i + f = G j + g and ... }.
Outputs that tests give.
- Decision: "independent" if there is no solution, else "dependent". A conservative test may answer "maybe" in place of either; it must never answer "independent" when a solution exists.
- Directions: the set of plain direction vectors for which a solution exists.
- Distances: the set over the solutions on the common loops, or a summary of it.
From solutions to dependences. A solution with on the common loops is a dependence from to ; a solution with is a dependence from to ; a solution with is a loop-independent dependence in the direction of the order of and in the body (none if and the two references are the same access). Its kind (flow, anti, output) follows from which access writes. See data dependence.
Variants
- Constant bounds. All loops are rectangular with known integer bounds. This is the setting of the GCD test, the Banerjee test and the practical tests.
- Affine bounds. Bounds of inner loops depend on outer indices (triangular loops). The question is still integer feasibility of a polyhedron. Banerjee's bounds were extended to trapezoidal domains; the omega test handles any affine constraints.
- Symbolic bounds. A bound is a parameter not known at compile time. The question becomes "for some " or "for all ", a formula of Presburger arithmetic.
- Memory-based against value-based. The question above asks whether two accesses touch the same element at all (memory-based). The value-based question asks whether the value read was last written by the given write; it removes dependences through overwritten values. Feautrier's array dataflow analysis answers it exactly with parametric integer programming.
- Nonaffine subscripts. Subscripts such as
A[i*j]orA[B[i]]are outside the affine model. Compilers assume a dependence, or test at run time.
Examples
A subscript that the GCD decides
for i = 1 to 100:
A[2*i] = A[2*i + 1] + 1
The equation has no integer solution, since its left side is even and its right side is odd. The GCD test proves independence without the bounds.
A real solution but no integer solution in the bounds
for i = 1 to 3:
A[2*i] = A[3*i + 2] + 1
The equation is . The gcd of and is , which divides , so the GCD test says "maybe". Over the box the value of runs over the real interval , which holds , so the Banerjee test also says "maybe". But the loop writes and reads : there is no dependence. The integer solutions need for and for . Only an exact test, such as the exact SIV test of the practical tests, finds this.
A coupled pair
for i = 1 to 8:
A[i][i] = A[i][i+1] + 1
Each dimension alone has solutions: for the first, and for the second. Together they have none. A test that treats each dimension on its own says "maybe"; a test that joins the constraints, such as the delta test, says "independent".
A dependence with a known distance
for i = 1 to 4:
for j = 1 to 4:
A[i+1][j] = A[i][j] + 1
The equations are and . Every solution has the distance , so there is one flow dependence with direction vector , carried by the outer loop. The inner loop is parallel, and interchange is legal.
Known algorithms
| test | handles | exact? | cost |
|---|---|---|---|
| GCD test | one equation, no bounds | exact for one equation without bounds | per equation |
| Banerjee test | one equation, bounds, directions | exact over the reals, not over the integers | per equation and direction vector |
| practical tests (ZIV, SIV, MIV, delta) | all subscripts, constant bounds | exact when every coupled group reduces to single-index subscripts | near linear in the number of subscripts in practice |
| Fourier-Motzkin elimination | any affine constraints | exact over the reals | can grow doubly exponentially |
| omega test | any affine constraints, symbolic bounds | exact over the integers | exponential in the worst case, fast in practice |
| Feautrier's array dataflow | value-based, parametric | exact | parametric integer programming |
All of them except the omega test and Feautrier's method are conservative. Their answer "independent" is a proof. Their answer "maybe" says only that they could not find a proof. Other tests in the literature, such as the lambda test of Li, Yew and Zhu, the I test of Kong, Klappholz and Psarris, and the power test of Wolfe and Tseng, sit between Banerjee and the omega test.
Most compilers use a cascade: cheap exact tests on simple subscripts first, a general test only on the rest. Goff, Kennedy and Tseng measured that most subscript pairs in scientific Fortran code are ZIV or SIV, where cheap exact tests exist.
Directions. Any of the tests can take a direction vector as extra constraints. To find the set of direction vectors, test ; if it is not independent, split the first into , and and test each, and go on down. A branch that is proved independent is cut. The search tests at most vectors, and far fewer when most branches are cut early.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Banerjee test | O(m k) worst | O(1) extra | deterministiciterative | 1 language |
| GCD test | O(m k log C) worst | O(1) extra | deterministiciterative | 1 language |
| Subscript-wise dependence tests | O(m^2 k + m log C + 3^k m k) worst | O(m k) extra | deterministiciterative | 1 language |
Bounds
Theorem. Dependence testing with affine subscripts and affine bounds is NP-complete. It is NP-hard already for one subscript and loops that run from to .
Membership. The question is the feasibility of an integer program with the variables and : the equations, the domain inequalities and the direction constraints (a strict is over the integers). Integer feasibility is in NP: if a system has an integer solution, it has one whose size in bits is polynomial in the size of the system (Papadimitriou 1981). That solution is the certificate.
Hardness. Reduce from subset sum. Take positive integers and a target . Build the nest
for i1 = 0 to 1:
...
for ik = 0 to 1:
S: A[w1*i1 + ... + wk*ik] = 0
R: x = A[T]
writes for each , and runs after the nest. There is a flow dependence from to if and only if some 0-1 vector has , that is, if and only if some subset of the sums to . The nest has size polynomial in the input. So a polynomial algorithm for dependence testing would decide subset sum. For and , the iteration writes ; for no iteration does.
Subset sum is solvable in pseudo-polynomial time, so this shows only weak NP-hardness. An array with many dimensions gives strong NP-hardness from exact cover: one 0-1 loop per set, one dimension per element of the universe with subscript , and a read of . All coefficients are or .
Fixed depth. For a fixed number of loops, the integer program has a fixed number of variables, and integer programming in fixed dimension is polynomial (Lenstra 1983). Real loop nests are shallow, which is one reason why exact tests are fast in practice.
Beyond affine. With polynomial subscripts and unbounded loop indices, dependence testing is undecidable. A write A[p(i1, ..., ik)] in a nest whose indices range over all natural numbers, and a read of A[0], are dependent if and only if the polynomial has a root in natural numbers, which is Hilbert's tenth problem, undecidable by Matiyasevich's theorem.
Reductions
- To integer feasibility. The definition is an instance of integer feasibility with variables and equations. Every exact test solves this integer program, and every conservative test solves a relaxation of it: the GCD test drops the bounds, the Banerjee test drops integrality.
- To linear Diophantine equations. Without bounds, each equation is a linear Diophantine equation, and a system of them is solved exactly with the Hermite normal form. This is the generalized GCD test.
- To Presburger arithmetic. With symbolic bounds, the question with its quantifiers over parameters is a sentence of Presburger arithmetic, which the omega test decides.
- From subset sum and exact cover, as in the bounds section.
Applications
- Legality. Every reordering transformation needs the dependences of the nest; the legality theorem checks them against the transformation.
- Parallelization and vectorization. A loop is parallel when it carries no dependence; vectorization and the Allen-Kennedy algorithm need the level of each dependence.
- Interchange, skewing, tiling. These need direction or distance vectors: loop interchange, loop skewing, loop tiling, unimodular transformations.
- Polyhedral compilation. The polyhedral model keeps the solution set itself as a relation and schedules with it.
References
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 3, Dependence Testing (subscript classification, the GCD and Banerjee tests, the SIV and MIV tests and the delta test)
- Utpal Banerjee, Dependence Analysis for Supercomputing, Kluwer Academic Publishers, 1988, the dependence equations of a pair of affine references and the GCD and bounds tests
- Gina Goff, Ken Kennedy and Chau-Wen Tseng, Practical dependence testing, Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 1991, the empirical study of subscripts in real programs, the ZIV, SIV and MIV classes and the delta test
- William Pugh, A practical algorithm for exact array dependence analysis, Communications of the ACM 35(8), 1992, pages 102 to 114, exact dependence testing with the omega test and its cost in practice
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Section 11.6 (array data-dependence analysis as integer linear programming, the GCD test and the heuristics for the general case)
- Christos H. Papadimitriou, On the complexity of integer programming, Journal of the ACM 28(4), 1981, the bound on the size of an integer solution, which puts integer feasibility in NP
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W0 stub |
|
Tag N2BH62 Edit this page History Report an error Markdown