Techniquecore
Loop fusion
Also called loop jamming, loop merging.
Merges adjacent loops with the same bounds into one loop, which improves reuse of data and lowers loop overhead.
Loop fusion merges two adjacent loops with the same iteration range into one loop whose body is the body of the first loop followed by the body of the second. The work is the same, but the order changes: before, every iteration of the first loop ran before any iteration of the second; after, they alternate. Fusion is legal exactly when no dependence goes from iteration of the first loop to an earlier iteration of the second loop. Such a dependence is called fusion-preventing: it is a loop-independent dependence between the two loops that would become a backward carried dependence in the fused loop. The test is about the dependences between the loops, so two loops that are each fully parallel can still be impossible to fuse. When fusion is legal, a value that the first body writes is often read by the second body in the same iteration, while it is still in a register or in cache. Fusion is the inverse of loop distribution.
In plain words
Two loops that walk the same range one after the other can often walk it together: at each step, do the work of the first loop and then the work of the second. Each piece of data is then used twice while it is close at hand, instead of being fetched again on a second pass. The danger is in what the second loop reads. Before the merge, the first loop had finished completely when the second loop began, so the second loop could read any result of the first. After the merge, the second loop at step 5 runs before the first loop reaches step 6. If it needs a value that step 6 of the first loop writes, it reads an old value. The merge is safe when the second loop at each step only needs results of the first loop from the same step or earlier steps.
When it applies
Take two loops
for i = L to U: S1(i)
for i = L to U: S2(i)
where S1 and S2 stand for the bodies. The conditions are:
- (F1) Same range. Both loops run the same iterations in the same order. Loops with ranges that differ by a few iterations can be made to match by peeling those iterations off.
- (F2) Adjacent. No statement runs between the two loops, or the statements between them have no dependence with the second loop and can be moved before the first.
- (F3) No fusion-preventing dependence. No dependence from S1 at iteration to S2 at iteration has .
Dependences inside S1 or inside S2 do not matter for legality. A dependence from S2 to S1 cannot exist in the original program, because every instance of S1 runs before every instance of S2.
The template
FUSE(for i = L to U: S1(i), for i = L to U: S2(i))- 1
if the two loops do not have the same range then - 2
return "not conformable: peel or align first" - 3
for each dependence from S1(i) to S2(j) do - 4
if j < i then - 5
return "illegal: fusion-preventing dependence" - 6
return for i = L to U: S1(i); S2(i)
The holes are the two bodies and their common range. For dependences with a constant distance, the test of line L4 reads: S1 writes an element at iteration that S2 reads or writes at with , or the other way round. With the array subscripts A[i + a] in S1 and A[i + b] in S2, iteration of S1 and iteration of S2 touch the same element when , so the pair is fusion-preventing exactly when .
Theorem. Under (F1) and (F2), fusion keeps every dependence pair in order if and only if (F3) holds.
Proof. Write an instance as or . The fused loop runs them in the order: before , and every instance of iteration before every instance of iteration . Take a dependence pair.
- Both ends in S1. The fused order restricted to the instances of S1 is , the same as in the first loop. The pair keeps its order. The same holds for S2.
- Source , sink . In the fused order, runs first if : in the same iteration S1 comes first, and in a later iteration everything comes later. If , iteration ends before iteration starts, so runs first.
- Source in S2, sink in S1. This is not possible, as said above.
So all pairs keep their order exactly when no pair has source and sink with . By Theorem 1 of the legality page, the fused loop then gives the same result.
Profit. The cost model is the memory traffic, or the number of loads. A value written in S1 and read in S2 at the same iteration needs no second load, and if the array is a temporary that nothing else reads, it can become a scalar (array contraction), which removes the array from memory. The loop control runs once instead of twice. The costs are more registers for the larger body, and possibly lost parallelism: if the fused loop carries a dependence between S1 and S2, it is sequential even when both loops were parallel.
Classic examples
Two parallel loops that cannot be fused.
for i = 1 to n: A[i] = B[i] + 1
for i = 1 to n: C[i] = A[i+1] * 2
Each loop is a DOALL loop: no iteration of either loop depends on another iteration of the same loop. Between the loops, S2 at iteration reads A[i+1], which S1 writes at iteration : a dependence from to , with . In the fused loop, reads A[i+1] one iteration before writes it. With , B = [1, 2, 3, 4] and A all zero at the start, the original code gives C = [6, 8, 10, 0] and the fused loop gives C = [0, 0, 0, 0]. The fix is alignment: shift S2 by one iteration, so that runs with , and peel the ends:
A[1] = B[1] + 1
for i = 2 to n:
A[i] = B[i] + 1
C[i-1] = A[i] * 2
C[n] = A[n+1] * 2
Now the read of A[i] follows its write in the same iteration, and the fused loop is still parallel.
Fusion that is legal but costs parallelism. If S2 is C[i] = A[i-1] * 2, the dependence goes from to , forward, and fusion is legal. The fused loop carries this dependence with distance 1, so it is no longer a DOALL loop. A compiler that wants parallel loops treats such a dependence as fusion-preventing too, as Kennedy and McKinley do when they fuse only loops of the same type.
Fusion for reuse. For A[i] = B[i] + 1 followed by C[i] = A[i] * 2, the dependence has distance 0, fusion is legal, and the fused loop is parallel. Before fusion, the two loops move elements through memory: read B, write A, read A again, write C. After fusion, A[i] is still in a register when S2 reads it: . If A is dead after the loop, it becomes a scalar, and the traffic is . Compilers for array languages fuse the loops of consecutive array statements for this reason, and GPU compilers fuse kernels so that the second kernel does not read the temporary back from global memory.
Variations
- Fusion of many loops. A program has a sequence of loops and a choice of which ones to fuse. Kennedy and McKinley build a fusion graph with one node per loop, dependence edges and fusion-preventing edges, and they show that the choice that maximizes reuse, with weights on the edges, is NP-hard. Their typed fusion report gives an algorithm for loops, edges and types of loops ranked by priority, and shows that the problem is NP-hard when types have no priority order.
- Fusion with alignment and peeling. Shift one loop by a constant, as in the example, to turn a fusion-preventing dependence into a loop-independent one. The polyhedral model chooses such shifts as part of a schedule.
- Fusion of nests. Two nests with the same bounds on several levels fuse level by level. The test uses the full distance vector between the two bodies: it must be lexicographically nonnegative.
- Unroll and jam. Unrolling an outer loop and fusing the copies of the inner loop, on the loop unrolling page, is fusion inside one nest.
Pitfalls
- Testing each loop alone. That both loops are parallel says nothing about fusion, as the first example shows. Only the dependences between the two bodies decide it.
- Forgetting the anti-dependences. A read in S1 followed by a write in S2 is a dependence too. If S1 reads
A[i-1]and S2 writesA[i], the read in and the write in form a pair with . After fusion the write runs first and S1 reads the new value: fusion-preventing. If S1 readsA[i+1]instead, the pair is and , and fusion is legal. - Losing parallelism by accident. A forward carried dependence keeps fusion legal but makes the fused loop sequential. Check what the fused loop is for.
- Different trip counts. Fusing loops whose ranges differ without peeling runs extra iterations or skips some.
- Register pressure. A large fused body can need more registers than the machine has, and the spills cost more than the saved loads.
Exercises
- Can
for i: A[i] = B[i] * 2andfor i: B[i] = C[i] + 1be fused? Answer. Yes. The pair is the read ofB[i]in and the write ofB[i]in , an anti-dependence with . In the fused body the read still comes first. - Can
for i: A[i] = B[i] * 2andfor i: B[i] = C[i] + A[i-1]be fused, and is the fused loop parallel? Answer. The flow dependence goes from to , forward, and the anti-dependence onB[i]has , so fusion is legal. The fused loop carries the flow dependence with distance 1 and is not parallel. - S1 writes
A[2i]and S2 readsA[i]. Is fusion legal? Answer. reads the element that wrote when . For , , so every dependence is forward and fusion is legal on the range . - Give the alignment for
A[i] = B[i] + 1followed byC[i] = A[i+2] - 1. Answer. Shift S2 by 2: in iteration run S1 for and S2 for . Peel two iterations of S1 at the start and two of S2 at the end.
References
- Ken Kennedy and Kathryn S. McKinley, Maximizing loop parallelism and improving data locality via loop fusion and distribution, Languages and Compilers for Parallel Computing (LCPC 1993), Lecture Notes in Computer Science 768, Springer, 1994, 301-320, the fusion graph with fusion-preventing edges, fusion of parallel loops without loss of parallelism, and the proof that fusion for the most data reuse with weighted edges is NP-hard
- Ken Kennedy and Kathryn S. McKinley, Typed fusion with applications to parallel and sequential code generation, Technical Report TR93-208, Rice University, 1993, Section 2 (fusion is illegal when a loop-independent dependence becomes a backward carried dependence), the O((N + E)T) ordered typed fusion algorithm, and the NP-hardness of typed fusion with more than two types
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 6 (Creating Coarse-Grained Parallelism), loop fusion and fusion-preventing dependences; Section 8.6 (Loop Fusion for Register Reuse)
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 | W2 basic |
|
Tag 0RJ20A Edit this page History Report an error Markdown