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 copies of it, for the iterations , and steps the index by . A short remainder loop runs the last iterations when the trip count is not a multiple of . 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 iterations, and the 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 in this order, or none if . Unrolling by a factor needs three facts:
- (U1) Fixed bounds. and are evaluated once, before the loop, and BODY does not assign . This is the meaning of a counted
forloop. A loop written as awhileloop must first be found as a natural loop and recognized as a counted loop, with induction variable analysis. - (U2) No overflow. The test is computed without overflow. With machine integers and near the largest value, write it as 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, areturn, 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
UNROLL(for i = L to U: BODY(i), factor k)- 1
i <- L - 2
while i + k - 1 <= U do ▷ main loop: one test for k iterations - 3
BODY(i); BODY(i + 1); ... ; BODY(i + k - 1) - 4
i <- i + k - 5
while i <= U do ▷ remainder loop: at most k - 1 iterations - 6
BODY(i) - 7
i <- i + 1
The holes are the body BODY, the bounds and , and the factor . Inside line L3 each copy uses in place of , 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 , in this order, and nothing else except the updates of . 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 in order, and . It holds at the start with . If the test of L2 is true, then , so line L3 runs the next instances, all of them in the range, and line L4 restores the invariant with . Each pass raises by , so the main loop ends. When it ends, , so at most instances are left. Lines L5 to L7 run them one by one, in order, with the invariant "the instances run so far are ", and stop when . If , both tests fail at once and nothing runs, as in the original loop. In both cases ends with the value the original loop leaves, or .
Profit. The cost model is the number of instructions executed. In the toy IR below, the original loop executes instructions for iterations. The loop unrolled by 4 executes , where and : 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 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 :
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 and . The counts of executed instructions, with each instruction and each terminator counted as 1:
| 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 the main loop runs twice ( and ) and the remainder loop runs twice ( and ). For 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 reads the value that the instance for 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 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 now run before . It is a reordering, and Allen and Kennedy give its legality test. A dependence with distance , and , has its source and sink in one group of outer iterations, and the jammed order runs the sink first. Every other dependence keeps its order. So unroll and jam by is legal if no dependence has direction with an outer distance less than , which holds in this example: the only dependences are on C[i], with outer distance 0.
Variations
- Remainder first. Run the leftover iterations before the main loop instead of after it. Duff's device in C writes the leftover part as a
switchthat 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
whileloop 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 iterations are left. Testing instead of runs copies past the end of the range.
- Overflow of the test. can overflow when is near the largest machine integer, and a wrapped value makes the test true. Compute once, with a guard.
- Floating-point partial sums. Several accumulators change the result of a floating-point sum. For the values the sum in order is , because rounds to . With one accumulator for the even positions and one for the odd positions it is . 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
- A loop with and is unrolled by 4. How many times do the main loop and the remainder loop run? Answer. The main loop runs for , so three times, and stops at , because . The remainder loop runs once, for .
- In the toy IR example, for which is the unrolled loop slower than the original? Answer. It is slower when . With this is , so exactly when , that is, for .
- 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.
- Why is unroll and jam of
for i: for j: A[i][j] = A[i-1][j+1] + 1by 2 illegal? Answer. The dependence has distance , direction , and outer distance . The instance reads , which writes. After the jam, runs before when is the first row of a group, so it reads the old value.
References
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 3XKRGT Edit this page History Report an error Markdown