Algorithm Search⌘K

Techniqueintro

Loop unrolling

Also called loop unwinding.

Replicates the body of a loop k times and divides the trip count by k, which cuts branch overhead and exposes instruction parallelism.

Loop unrolling replaces the body of a loop by kk copies of it, for the iterations i,i+1,…,i+k−1i, i+1, \dots, i+k-1, and steps the index by kk. A short remainder loop runs the last n mod kn \bmod k iterations when the trip count nn is not a multiple of kk. The program runs the same body instances in the same order, so unrolling is always legal and it changes no data dependence. What it changes is the cost: one test and one increment now serve kk iterations, and the kk copies form one long block that the instruction scheduler can overlap. The price is code size and, often, more registers. Strip mining has the same shape with a loop around the copies instead of written-out copies.

In plain words

A loop does two kinds of work on each turn: the real work, and the bookkeeping of the loop itself, which is a test, a jump and an increment of the counter. If you write the real work four times in a row inside the loop, the bookkeeping happens once for every four pieces of work. The work is the same, and it happens in the same order, so the result cannot change. When the number of turns is not a multiple of four, a small second loop does the last few turns one at a time. The four copies side by side also let the processor start the next piece of work before the previous one ends. The cost is a longer program, and the short second loop.

When it applies

Take a counted loop for i = L to U: BODY(i) that runs the iterations L,L+1,…,UL, L+1, \dots, U in this order, or none if U<LU < L. Unrolling by a factor k≥2k \ge 2 needs three facts:

  • (U1) Fixed bounds. LL and UU are evaluated once, before the loop, and BODY does not assign ii. This is the meaning of a counted for loop. A loop written as a while loop must first be found as a natural loop and recognized as a counted loop, with induction variable analysis.
  • (U2) No overflow. The test i+k−1≤Ui + k - 1 \le U is computed without overflow. With machine integers and UU near the largest value, write it as i≤U−k+1i \le U - k + 1 and guard the case $U - k + 1 < $ the smallest value.
  • (U3) Single exit. The only exit is the loop test. If BODY can leave the loop (a break, a return, a trap), each copy keeps its exit, and the proof below still holds, because the copies run in the original order.

No dependence test is needed. That is the point of the technique: it is a pure change of the control code.

The template

  1. UNROLL(for i = L to U: BODY(i), factor k)
  2. 1i <- L
  3. 2while i + k - 1 <= U do ▷ main loop: one test for k iterations
  4. 3BODY(i); BODY(i + 1); ... ; BODY(i + k - 1)
  5. 4i <- i + k
  6. 5while i <= U do ▷ remainder loop: at most k - 1 iterations
  7. 6BODY(i)
  8. 7i <- i + 1

The holes are the body BODY, the bounds LL and UU, and the factor kk. Inside line L3 each copy uses i+ci + c in place of ii, and a compiler usually gives each copy fresh names for its temporaries, so that the copies do not reuse one register.

Legality. Under (U1) to (U3), the code above runs exactly the instances BODY(L),BODY(L+1),…,BODY(U)\mathrm{BODY}(L), \mathrm{BODY}(L+1), \dots, \mathrm{BODY}(U), in this order, and nothing else except the updates of ii. This is proved below. Two runs that execute the same sequence of statement instances from the same state pass through the same states, so the result, the traps and the final memory are the same. Since the order of all instances is unchanged, every dependence keeps its source before its sink, and the legality theorem is not even needed.

Proof. At each arrival at line L2, the invariant is: the instances run so far are BODY(L),…,BODY(i−1)\mathrm{BODY}(L), \dots, \mathrm{BODY}(i-1) in order, and L≤iL \le i. It holds at the start with i=Li = L. If the test of L2 is true, then i+k−1≤Ui + k - 1 \le U, so line L3 runs the next kk instances, all of them in the range, and line L4 restores the invariant with i+ki + k. Each pass raises ii by kk, so the main loop ends. When it ends, i+k−1>Ui + k - 1 > U, so at most U−i+1≤k−1U - i + 1 \le k - 1 instances are left. Lines L5 to L7 run them one by one, in order, with the invariant "the instances run so far are BODY(L),…,BODY(i−1)\mathrm{BODY}(L), \dots, \mathrm{BODY}(i-1)", and stop when i=U+1i = U + 1. If U<LU < L, both tests fail at once and nothing runs, as in the original loop. In both cases ii ends with the value the original loop leaves, U+1U + 1 or LL.

Profit. The cost model is the number of instructions executed. In the toy IR below, the original loop executes 5n+65n + 6 instructions for nn iterations. The loop unrolled by 4 executes 12q+5r+912q + 5r + 9, where q=⌊n/4⌋q = \lfloor n/4 \rfloor and r=n mod 4r = n \bmod 4: each main iteration does the work of four iterations in 12 instructions instead of 20. The second gain is not counted here: the four copies form one block with no branch, so a scheduler can overlap their loads and arithmetic, and a vectorizer or software pipelining can work on them.

Costs. The code grows by about kk times the body. A large body unrolled many times can stop fitting in the instruction cache. Fresh names for the temporaries of each copy raise the register pressure, and spills can cost more than the saved branches. A loop with a short trip count runs mostly in the remainder loop, and then unrolling makes it slower.

Classic examples

A sum in the toy IR. The parameter is n. The original loop adds 0+1+⋯+(n−1)0 + 1 + \dots + (n-1):

entry:
  i = 0
  s = 0
  jump head
head:
  t = i < n
  br t body exit
body:
  s = s + i
  i = i + 1
  jump head
exit:
  ret s

Unrolled by 4, with the remainder loop in head1 and body1:

entry:
  i = 0
  s = 0
  jump head4
head4:
  j = i + 3
  t = j < n
  br t body4 head1
body4:
  s = s + i
  i1 = i + 1
  s = s + i1
  i2 = i + 2
  s = s + i2
  i3 = i + 3
  s = s + i3
  i = i + 4
  jump head4
head1:
  t = i < n
  br t body1 exit
body1:
  s = s + i
  i = i + 1
  jump head1
exit:
  ret s

The test i + 3 < n is line L2 of the template for L=0L = 0 and U=n−1U = n - 1. The counts of executed instructions, with each instruction and each terminator counted as 1:

nn return value original unrolled by 4
0 0 6 9
3 3 21 24
4 6 26 21
10 45 56 43
100 4950 506 309

For n=10n = 10 the main loop runs twice (i=0i = 0 and i=4i = 4) and the remainder loop runs twice (i=8i = 8 and i=9i = 9). For n≤3n \le 3 the unrolled loop is slower: it pays for one failed main test and does all the work in the remainder loop.

A recurrence keeps its dependences. In for i = 1 to n: A[i] = A[i-1] + 1, the instance for ii reads the value that the instance for i−1i - 1 wrote: a loop-carried flow dependence with distance 1. Unrolled by 2, the body is A[i] = A[i-1] + 1; A[i+1] = A[i] + 1 and ii steps by 2. The pairs of dependent instances are the same. What changes is how they are labeled: the dependence from the first copy to the second copy is now loop independent, and the dependence from the second copy to the first copy of the next iteration is carried by the new loop with distance 1. The chain of dependences is as long as before, so unrolling cannot make this loop run in parallel.

Unroll and jam. For a nest for i: for j: C[i] = C[i] + A[i][j] * B[j], unroll the outer loop by 2 and fuse the two copies of the inner loop: for i step 2: for j: C[i] += A[i][j]*B[j]; C[i+1] += A[i+1][j]*B[j]. Each load of B[j] now serves two rows. This is not plain unrolling, because the instances (i+1,j)(i+1, j) now run before (i,j+1)(i, j+1). It is a reordering, and Allen and Kennedy give its legality test. A dependence with distance (d1,d2)(d_1, d_2), 0<d1<k0 < d_1 < k and d2<0d_2 < 0, has its source and sink in one group of kk outer iterations, and the jammed order runs the sink first. Every other dependence keeps its order. So unroll and jam by kk is legal if no dependence has direction (<,>)(<, >) with an outer distance less than kk, which holds in this example: the only dependences are on C[i], with outer distance 0.

Variations

  • Remainder first. Run the n mod kn \bmod k leftover iterations before the main loop instead of after it. Duff's device in C writes the leftover part as a switch that jumps into the middle of the unrolled body.
  • Complete unrolling. If the trip count is a small constant, replace the loop by straight-line code with no test at all. This often enables constant folding of the index expressions.
  • Unknown trip counts. A while loop whose trip count is not known can still be unrolled: keep the exit test after each copy. Only the jump back to the header is saved.
  • Unroll and jam. Unroll an outer loop and fuse the copies of the inner loop, as in the example. It improves register reuse, and its legality is a dependence condition.
  • Partial sums. For a reduction, some compilers give each copy its own accumulator and add the accumulators at the end. This is a separate transformation, not part of unrolling: it changes the order of the additions.

Pitfalls

  • Off-by-one in the main test. The main loop must stop when fewer than kk iterations are left. Testing i≤Ui \le U instead of i+k−1≤Ui + k - 1 \le U runs copies past the end of the range.
  • Overflow of the test. i+k−1i + k - 1 can overflow when UU is near the largest machine integer, and a wrapped value makes the test true. Compute U−k+1U - k + 1 once, with a guard.
  • Floating-point partial sums. Several accumulators change the result of a floating-point sum. For the values 1016,1,−1016,110^{16}, 1, -10^{16}, 1 the sum in order is 11, because 1016+110^{16} + 1 rounds to 101610^{16}. With one accumulator for the even positions and one for the odd positions it is 0+2=20 + 2 = 2. A compiler must not do this without permission to reassociate.
  • Expecting parallelism. Unrolling does not remove a loop-carried dependence. It only moves some dependences inside the body, where they are still dependences.
  • Too large a factor. A factor that makes the body larger than the instruction cache, or that needs more registers than the machine has, makes the loop slower. Typical factors are 2 to 8.

Exercises

  1. A loop with L=1L = 1 and U=13U = 13 is unrolled by 4. How many times do the main loop and the remainder loop run? Answer. The main loop runs for i=1,5,9i = 1, 5, 9, so three times, and stops at i=13i = 13, because 13+3>1313 + 3 > 13. The remainder loop runs once, for i=13i = 13.
  2. In the toy IR example, for which nn is the unrolled loop slower than the original? Answer. It is slower when 12q+5r+9>5n+612q + 5r + 9 > 5n + 6. With n=4q+rn = 4q + r this is 3>8q3 > 8q, so exactly when q=0q = 0, that is, for n≤3n \le 3.
  3. A loop has a flow dependence with distance 3. It is unrolled by 2. What are the distances, counted in iterations of the new loop? Answer. The source in the first copy (an even offset) and the sink are 3 old iterations apart, which is the second copy of the next new iteration: distance 1. A source in the second copy has its sink in the first copy two new iterations later: distance 2. The instance pairs are the same as before.
  4. Why is unroll and jam of for i: for j: A[i][j] = A[i-1][j+1] + 1 by 2 illegal? Answer. The dependence has distance (1,−1)(1, -1), direction (<,>)(<, >), and outer distance 1<21 < 2. The instance (i+1,j)(i+1, j) reads A[i][j+1]A[i][j+1], which (i,j+1)(i, j+1) writes. After the jam, (i+1,j)(i+1, j) runs before (i,j+1)(i, j+1) when ii is the first row of a group, so it reads the old value.

References

  1. Jack J. Dongarra and A. R. Hinds, Unrolling loops in FORTRAN, Software, Practice and Experience 9(3), 1979, 219-226, the unrolled loop with a cleanup loop for the leftover iterations, and measurements of the gain on several computers and compilers
  2. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Section 17.4.3 (Loop Unrolling), the unrolling factor and the rolled copy for the leftover iterations, as a step that gives the scheduler more freedom
  3. Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 8 (Improving Register Usage), Section 8.4 (Unroll-and-Jam) and its legality condition

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples