Techniquecore
Software pipelining
Also called loop pipelining.
Overlaps iterations of a loop so that a new iteration starts before the previous ends, with a prologue, steady kernel and epilogue.
Software pipelining is a technique for scheduling loops. It overlaps the iterations of a loop: a new iteration starts every cycles, the initiation interval, before the earlier ones end, like the stages of a hardware pipeline. The code that results has three parts: a prologue that starts the first iterations, a kernel of cycles that repeats and holds pieces of several iterations at once, and an epilogue that finishes the last ones. This page is about the technique and what any software pipeliner must do. Iterative modulo scheduling is one algorithm for its central step, the choice of a start time for each operation at a given . Other algorithms for that step are SDC-based modulo scheduling, swing modulo scheduling and integer programming. Two lower bounds hold for every one of them: is at least the resource bound and at least the recurrence bound .
In plain words
A loop body often has a chain of steps that wait for each other: load a number, multiply it, add, store. One pass alone keeps most of the machine idle. A laundry does not wait for one load to dry before it washes the next. In the same way, the compiler starts the next pass of the loop a few cycles after the previous one, while that one is still busy, so that the passes overlap and the units stay busy. In the middle of the run, each short repeated stretch of code does one step of several different passes. The start and the end of the run need extra code, to fill the overlap and to empty it.
When it applies
- An inner loop runs many times, and its body is straight-line code, or can be made straight-line by if-conversion.
- One iteration alone is long compared to what the machine could issue: its dependence chain is long, or it uses few of the units.
- The machine issues several operations per cycle and does not reorder them by itself, as in VLIW, EPIC and DSP processors, or the target is hardware, as in high-level synthesis.
- The dependences that cross iterations, the loop-carried dependences, are known, each with its distance in iterations, and their cycles are short compared to the body.
- There are enough registers for the values of the overlapped iterations, or the machine has rotating registers.
The template
The body is a set of operations, each with a resource class and a latency. A dependence edge has a delay , usually the latency of , and a distance : operation of iteration depends on operation of iteration . Iteration starts at cycle , and operation of iteration runs at cycle .
SOFTWARE-PIPELINE(loop, machine)- 1
G <- DEPENDENCES(loop body) ▷ hole: edges with a delay and a distance - 2
II <- max(ResMII, RecMII) - 3
repeat - 4
t <- PLACE(G, II, machine) ▷ hole: a modulo scheduler, may fail - 5
if PLACE failed then II <- II + 1 - 6
until PLACE succeeded - 7
stage(v) <- floor(t(v) / II); SC <- 1 + max over v of stage(v) - 8
NAMES <- RENAME(t, II) ▷ hole: modulo variable expansion or rotating registers - 9
emit the prologue (SC - 1 passes), the kernel, and the epilogue (SC - 1 passes) - 10
emit GUARD(SC) ▷ hole: run the plain loop when the trip count is below SC - 1
The bounds (line L2). If a resource class has units and the body uses it times, then in the steady state cycles must hold uses, so . For a cycle of the dependence graph, adding the constraints around it gives , so . A cycle with is an error in the input: it would ask an operation to wait for itself.
Placement (line L4). A schedule is legal for when every edge has , and, for each class and each slot from 0 to , at most operations of class have . The table of these slots is the modulo reservation table. These two conditions are exactly what makes the overlapped run legal. For an edge, operation of iteration runs at . For a resource, the operations of class that run in cycle come from different operations with , at most one iteration each. The search for such a with a small is NP-hard in general, so line L4 is a heuristic, and line L5 tries the next when it fails.
Stages and code (lines L7 to L9). Operation belongs to stage . In pass of the kernel, slot runs each with for iteration . The prologue is the first passes, where some of these iterations do not exist yet, and the epilogue is the last , where some are already past the end. With iterations the whole run takes passes, about cycles for large instead of times the length of one iteration.
Registers (line L8). A value written once per iteration is written again every cycles. If it must live longer than that, one register is not enough. Modulo variable expansion (Lam 1988) unrolls the kernel and gives each copy its own name. Rotating registers do the renaming in hardware.
Classic examples
A small loop, worked. The body computes x = a[i] (memory, latency 2), y = x * c (multiply, latency 3), z = y + x (ALU, latency 1) and b[i] = z (memory, latency 1), on a machine with one unit of each class. The loop control is left out. Two memory operations on one memory unit give . No dependence crosses an iteration, so does not limit , and . A legal schedule and its table:
| operation | slot | stage | |
|---|---|---|---|
x = a[i] |
0 | 0 | 0 |
y = x * c |
2 | 0 | 1 |
z = y + x |
5 | 1 | 2 |
b[i] = z |
7 | 1 | 3 |
The store cannot go at cycle 6, its earliest start, because slot 0 of the memory unit holds the load. So . The kernel runs, in slot 0, the load of iteration and the multiply of iteration , and in slot 1, the add of iteration and the store of iteration . For the run has 103 passes, 6 cycles of prologue, 97 kernel passes and an epilogue, and takes cycles. Without overlap one iteration takes 7 cycles, so 700. The value x is written at cycle 2 of its iteration and read last at cycle 5, but the next load writes it again at cycle 4. So x needs two names, and the kernel is unrolled twice. The values y and z are read before the next write and need one name each.
Loop unrolling. Unrolling times and scheduling the larger body overlaps iterations, but the overlap stops at each back edge. Software pipelining keeps the overlap across all iterations with a kernel of cycles. Unrolling is still used inside it, for modulo variable expansion and when the best is a fraction.
Iterative modulo scheduling. Rau's algorithm fills line L4: it places operations by height, one at a time, into the modulo reservation table, and moves earlier ones out when there is no free slot, within a budget of steps.
SDC-based modulo scheduling. In high-level synthesis, loop pipelining is the same technique for hardware: the dependence rows form a system of difference constraints, and the modulo slots are added one operation at a time.
Iteration bound, retiming and unfolding. For a digital filter on unlimited hardware, the iteration bound is without the ceiling. Retiming moves the registers of a circuit, which is the same as changing the stages of its operations. Unfolding runs several iterations as one, which reaches a bound that is a fraction.
Variations
- Modulo scheduling algorithms. Lam (1988) schedules strongly connected components first and reduces conditionals to single nodes. Iterative modulo scheduling (Rau 1994) uses heights and displacement. Swing modulo scheduling (Llosa et al. 1996) orders operations to keep lifetimes short and is the pipeliner of GCC. Integer programming gives optimal schedules for small loops.
- Kernel recognition. Instead of fixing first, unroll the loop, schedule it greedily, and wait for a pattern that repeats. Allan et al. (1995) survey these methods.
- Hardware support. Rotating registers rename each value once per kernel pass, and stage predicates turn the stages of the prologue and the epilogue off, so the kernel alone runs the loop. The Cydra 5 and the IA-64 processors had both.
- Loops with branches. If-conversion turns the body into predicated straight-line code. Hierarchical reduction (Lam 1988) schedules each branch arm first and treats the result as one operation with a union of resources.
- Fractional rates. If the best rate is cycles per iteration, unroll the body twice and pipeline it with .
Pitfalls
- Missing a loop-carried dependence. If line L1 keeps only the dependences inside one iteration, a sum
s = s + ycan readsbefore the previous iteration writes it. Every edge needs its distance, also through memory, as ina[i] = a[i - 1] * c. - Values that live longer than . If line L8 keeps one register per value, the next iteration overwrites the value before its last read, as with
xin the example. Expand the variable or use rotating registers. - Short trip counts. The prologue and the epilogue assume at least iterations. Without line L10, a loop with fewer iterations runs operations of iterations that do not exist.
- The wrong rounding in the bounds. and round up. Rounding down gives an that no schedule can meet, and with a heuristic that does not check, a schedule with conflicts.
- Register pressure. A small with many stages keeps many values live at once. If the machine runs out of registers, the spill code can cost more than the overlap gains. A larger , or a scheduler that keeps lifetimes short, can be faster.
Exercises
- A loop computes
x = a[i](memory, latency 2) ands = s * x(multiply, latency 3), with one memory unit and one multiplier. Find , and the least . (Answer: 1, 3 and 3.) - Prove that for every cycle of the dependence graph, from the edge constraints of line L4.
- A value is written at cycle of its iteration and read last at cycle . A write is seen by reads in the same cycle. Show that it needs names in rotation, and check the count for
x,yandzin the example. - In the example, show that no legal schedule with and the load at cycle 0 puts the store before cycle 7.
References
- B. Ramakrishna Rau and Christopher D. Glaeser, Some scheduling techniques and an easily schedulable horizontal architecture for high performance scientific computing, Proceedings of the 14th Annual Microprogramming Workshop, 1981, 183-198, the schedule of one iteration that starts a new iteration at a fixed interval, with the resources reserved modulo that interval, and the machine designed to make such schedules easy to find
- Monica Lam, Software pipelining, an effective scheduling technique for VLIW machines, Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation, 1988, 318-328, modulo scheduling with lower bounds from resources and recurrences, modulo variable expansion, and hierarchical reduction of conditional statements, in the compiler for the Warp machine
- Vicki H. Allan, Reese B. Jones, Randall M. Lee and Stephen J. Allan, Software pipelining, ACM Computing Surveys 27(3), 1995, 367-432, the survey of modulo scheduling and kernel recognition methods, with the prologue, kernel and epilogue, the bounds on the initiation interval, and register needs
- B. Ramakrishna Rau, Iterative modulo scheduling, an algorithm for software pipelining loops, Proceedings of the 27th Annual International Symposium on Microarchitecture, 1994, 63-74, the definitions of ResMII and RecMII and the iterative search over initiation intervals
- Alan E. Charlesworth, An approach to scientific array processing, the architectural design of the AP-120B/FPS-164 family, Computer 14(9), 1981, 18-27, the overlapped loop code of the FPS array processors
- Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Section 17.4 (Software Pipelining)
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 3SECX7 Edit this page History Report an error Markdown