Theoremcore
Legality of loop reordering
Also called lexicographically positive dependence theorem.
A reordering of loop iterations keeps the meaning if every dependence distance vector remains lexicographically positive after it.
Statement
A program runs a finite set of statement instances in a total order (see data dependence). Assume:
- each instance reads a fixed set of memory locations and writes a fixed set, which do not depend on the order of the run;
- the values that writes are a function of the values that reads.
A pair with is a dependence if and access a common location and at least one of them writes it. It is a flow, anti or output dependence when the write is in only, in only, or in both. A reordering of is a program that runs the same instances in another total order .
Theorem 1 (reordering). If for every dependence of , then from every initial memory state, every instance reads the same values in as in , and ends in the same memory state as .
Now take a perfect loop nest of depth with iteration domain . The body runs once per iteration vector in lexicographic order , and the statements of one iteration run in the order of the text. A dependence from to has the distance vector , and . A zero distance means a loop-independent dependence inside one iteration; a nonzero one is carried by the loop of its first nonzero entry.
Theorem 2 (unimodular transformation). Let be a integer matrix with . Let run the iteration at the new iteration vector : the new nest scans in lexicographic order and runs the unchanged body at for each new point . If for every nonzero distance vector of a dependence of the nest, then is a reordering of with the same meaning, in the sense of Theorem 1.
With direction vectors, Theorem 2 needs for every distance that the direction vector allows. This page uses Wolfe's notation: the entry at level means , that is ; means ; means ; and means any. A direction vector is lexicographically positive if its first entry that is not is .
In plain words
A compiler may run the parts of a program in another order, as long as it keeps the order of every pair of parts that touch the same memory and where at least one of them writes. Then every read still sees the value from the same write, so every part computes the same thing, and the last write to each location is still the same. For loops, the order of two iterations is given by the difference of their loop counters. A change of the loops keeps the order of a dependent pair exactly when that difference, after the change, still points forward: its first nonzero entry is positive. So a compiler checks each dependence once, and if all of them still point forward, the new loops compute the same result.
Proof
Proof of Theorem 1
For a location and an instance that reads , let be the last instance before in that writes , or "initial" if there is none. Define in the same way for . The value that reads from is the value written by , or the initial value of .
Lemma 1. If the dependences are kept, then for every read.
Proof. Suppose first that is an instance. The pair is a flow dependence, so . Let be any other writer of with ; we show . In , the instance is not between and , because is the last writer before . If , then is an output dependence, so . If , then is an anti dependence, so , against the choice of . So every writer of before in comes before , and .
If is "initial", then every writer of has , so is an anti dependence and . No writer comes before in , so is "initial" too.
Proof of the theorem. Use induction on the position of in . Assume that every instance before in reads the same values in as in ; then, by the second assumption, it also writes the same values. For each location that reads, Lemma 1 gives the same writer in both runs (or the initial value), and , so wrote the same value in both runs. So reads the same values. This proves the claim for all instances.
For the final state, take a location . Any two writers of form an output dependence, so they run in the same order in and , and the last writer of is the same instance in both runs. It writes the same value, by the first part. A location that no instance writes keeps its initial value in both runs.
Proof of Theorem 2
The new nest runs the same instances. Since , the inverse is an integer matrix (see unimodular matrix). So maps onto one to one, and it maps one to one onto . Each new point runs the body at the integer point , and each is reached from exactly one . So runs each instance exactly once: it is a reordering of .
The new order. In , the instance runs before if , or if (then ) and comes before in the body. Since is linear, holds exactly when .
The dependences are kept. Take a dependence from to with distance .
- If , both instances are in one iteration, with before in the body (or with the read before the write of one statement run, which is not changed). The body is unchanged, so their order is kept.
- If , then by the hypothesis, so runs before in .
So all dependences are kept, and Theorem 1 applies.
The proof does not use for the order, only for the claim that the new nest is easy to write: with , the image has no holes, and the new loops have unit steps. For any one to one map of the iterations, Theorem 1 gives the same condition: the map must keep the order of each dependent pair.
Consequences
Worked example
The nest for i: for j: A[i][j] = f(A[i-1][j+1]) on a domain has one flow dependence with distance , direction .
| transformation | legal? | ||
|---|---|---|---|
| interchange | no | ||
| skew by | yes | ||
| skew, then interchange | yes |
A run with symbolic values (each a term) confirms it: the interchanged nest computes different terms, and the two skewed nests compute the same terms as the original. The last one runs the anti-diagonals one after the other, and inside each anti-diagonal the inner loop carries no dependence, so its iterations can run in parallel, as in the hyperplane method.
Parallel loops
The loop at level can run its iterations in any order, with the outer loops fixed, if no dependence is carried at level . Proof: such a run keeps the order of the outer loops, so it keeps every dependence carried at a level below . A dependence carried at a level above , or loop-independent, has , so both instances are in one iteration of level and their order inside it is unchanged. Theorem 1 gives the same result for each order of the level- iterations, which is the condition for a DOALL loop. Allen and Kennedy state a general form of this as their Theorem 2.4.
Permutation and reversal
- Loop interchange and any permutation of the loops is legal if each permuted direction vector is lexicographically positive. A dependence with direction blocks the interchange of the two loops, and or does not.
- Reversal of the loop at level is the unimodular matrix that negates entry . It is legal if no dependence is carried at level : such a dependence has and earlier entries 0, so its new vector has at its first nonzero entry.
Fully permutable bands and tiling
Call the loops at levels fully permutable if every dependence that is not carried outside this band has for . Then every permutation of the band is legal. A permutation of the band does not change the entries before or after the band. A dependence carried in the band stays nonnegative and nonzero on the band, so it stays lexicographically positive; any other dependence keeps its first nonzero entry.
Loop tiling of a fully permutable band is also legal, although it is not linear. Take the band to be the whole nest, for short. With tile size , tiling runs the iteration at the new vector , compared lexicographically. For a dependence with entrywise, is also entrywise. If it is nonzero, it is lexicographically positive; if it is zero, the two instances are in one tile and the second part, , is lexicographically positive. So the dependence is kept, and Theorem 1 applies. The nest of the worked example has and cannot be tiled as it is: a run with tiles changes the result. After the skew the distance is , and the tiled skewed nest computes the original result. This is why loop skewing comes before tiling in many compilers.
Zero distances
A zero distance vector stands for a loop-independent dependence. for every , so Theorem 2 cannot use for it, and does not need to: the body is not changed, so the order inside an iteration stays. A transformation that also changes the body, such as loop distribution or statement reordering, must keep the loop-independent dependences by other means.
Tightness
The condition is sufficient, not necessary. Three examples show reorderings that break a dependence and still keep the meaning.
- Overwritten values.
X = 1; X = 2; X = 3has an output dependence from the first write to the second. Swapping them gives the same final state, because the third write hides both, and no read lies between them. - Reductions.
for i: s = s + A[i]has flow, anti and output dependences onsbetween every pair of iterations. Running the loop backwards breaks all of them. On integers the sum is the same, for example for in both orders, because integer addition is associative and commutative. On floating-point numbers it can differ: , while . Compilers handle reductions as a separate case. - Equal values. If both statements of a dependence write the same constant, any order gives the same state.
Each example uses the meaning of the operations or the fact that a value is never read. The theorem uses neither: it holds for every function in the second assumption. So it is the best rule that looks only at which locations each instance reads and writes, not at what the operations do.
Memory-based dependences are more than needed. The proof of Lemma 1 only needs three kinds of pairs: a write and a read that sees its value, a read and the next write of the location, and two consecutive writes of the location. The other dependences follow from these by transitivity. Value-based dependence analysis keeps only the pairs from a write to the reads that see its value (see array privatization); after privatization or renaming of storage, fewer orders need to be kept.
Unimodular matrices are not all transformations. Theorem 2 covers the reorderings by one linear map of the whole nest. Tiling, as shown above, and the affine schedules of the polyhedral model, which give each statement its own map, need Theorem 1 directly.
Direction vectors lose precision. A direction vector stands for a set of distances, and Theorem 2 needs for all of them. A test with must assume that can be negative, so it can reject a transformation that the real distances allow.
History
Bernstein (1966) gave conditions on the read and write sets of two program parts under which they can run in either order or in parallel. Lamport (1974) reordered the iterations of DO loops by linear maps that keep all dependences, the hyperplane method. The theorem that a reordering transformation keeps the meaning of a program if it keeps every dependence is Theorem 2.2 of Allen and Kennedy, who call it the fundamental theorem of dependence, with a proof by the order of the instances. Banerjee, and Wolf and Lam (1991), stated the condition for unimodular transformations, , which unifies interchange, reversal and skewing, and Wolf and Lam used fully permutable nests for tiling. The dragon book states the same conditions for affine transforms and blocking.
References
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 2 (reordering transformations, Theorem 2.2, the fundamental theorem of dependence, and the direction vector condition for loop interchange)
- Michael E. Wolf and Monica S. Lam, A loop transformation theory and an algorithm to maximize parallelism, IEEE Transactions on Parallel and Distributed Systems 2(4), 1991, the legality condition for unimodular transformations on distance and direction vectors, and fully permutable loop nests
- Leslie Lamport, The parallel execution of DO loops, Communications of the ACM 17(2), 1974, the hyperplane method, which reorders the iterations of a nest by a linear map that keeps all dependences
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Chapter 11 (the legality of affine transforms and of blocking, and fully permutable loops)
- Arthur J. Bernstein, Analysis of programs for parallel processing, IEEE Transactions on Electronic Computers EC-15(5), 1966, the conditions on read and write sets under which two program parts can run in either order or in parallel
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 9QN3G8 Edit this page History Report an error Markdown