Algorithm Search⌘K

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 IIII 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 IIII 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 IIII. 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: IIII is at least the resource bound ResMII\mathrm{ResMII} and at least the recurrence bound RecMII\mathrm{RecMII}.

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 (u,v)(u, v) has a delay δ(u,v)\delta(u, v), usually the latency of uu, and a distance d(u,v)≥0d(u, v) \ge 0: operation vv of iteration i+di + d depends on operation uu of iteration ii. Iteration ii starts at cycle i⋅IIi \cdot II, and operation vv of iteration ii runs at cycle i⋅II+t(v)i \cdot II + t(v).

  1. SOFTWARE-PIPELINE(loop, machine)
  2. 1G <- DEPENDENCES(loop body) ▷ hole: edges with a delay and a distance
  3. 2II <- max(ResMII, RecMII)
  4. 3repeat
  5. 4t <- PLACE(G, II, machine) ▷ hole: a modulo scheduler, may fail
  6. 5if PLACE failed then II <- II + 1
  7. 6until PLACE succeeded
  8. 7stage(v) <- floor(t(v) / II); SC <- 1 + max over v of stage(v)
  9. 8NAMES <- RENAME(t, II) ▷ hole: modulo variable expansion or rotating registers
  10. 9emit the prologue (SC - 1 passes), the kernel, and the epilogue (SC - 1 passes)
  11. 10emit GUARD(SC) ▷ hole: run the plain loop when the trip count is below SC - 1

The bounds (line L2). If a resource class rr has crc_r units and the body uses it uru_r times, then in the steady state IIII cycles must hold uru_r uses, so II≥ResMII=max⁡r⌈ur/cr⌉II \ge \mathrm{ResMII} = \max_r \lceil u_r / c_r \rceil. For a cycle CC of the dependence graph, adding the constraints t(v)+II⋅d(u,v)≥t(u)+δ(u,v)t(v) + II \cdot d(u, v) \ge t(u) + \delta(u, v) around it gives II⋅d(C)≥δ(C)II \cdot d(C) \ge \delta(C), so II≥RecMII=max⁡C⌈δ(C)/d(C)⌉II \ge \mathrm{RecMII} = \max_C \lceil \delta(C) / d(C) \rceil. A cycle with d(C)=0d(C) = 0 is an error in the input: it would ask an operation to wait for itself.

Placement (line L4). A schedule tt is legal for IIII when every edge has t(v)+II⋅d(u,v)≥t(u)+δ(u,v)t(v) + II \cdot d(u, v) \ge t(u) + \delta(u, v), and, for each class rr and each slot kk from 0 to II−1II - 1, at most crc_r operations of class rr have t(v) mod II=kt(v) \bmod II = k. 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 vv of iteration i+di + d runs at (i+d)II+t(v)≥i⋅II+t(u)+δ(i + d) II + t(v) \ge i \cdot II + t(u) + \delta. For a resource, the operations of class rr that run in cycle cc come from different operations vv with t(v)≡c(modII)t(v) \equiv c \pmod{II}, at most one iteration each. The search for such a tt with a small IIII is NP-hard in general, so line L4 is a heuristic, and line L5 tries the next IIII when it fails.

Stages and code (lines L7 to L9). Operation vv belongs to stage stage(v)\mathrm{stage}(v). In pass pp of the kernel, slot kk runs each vv with t(v) mod II=kt(v) \bmod II = k for iteration p−stage(v)p - \mathrm{stage}(v). The prologue is the first SC−1SC - 1 passes, where some of these iterations do not exist yet, and the epilogue is the last SC−1SC - 1, where some are already past the end. With NN iterations the whole run takes (N+SC−1)(N + SC - 1) passes, about N⋅IIN \cdot II cycles for large NN instead of NN times the length of one iteration.

Registers (line L8). A value written once per iteration is written again every IIII 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 ResMII=2\mathrm{ResMII} = 2. No dependence crosses an iteration, so RecMII\mathrm{RecMII} does not limit IIII, and II=2II = 2. A legal schedule and its table:

operation tt slot t mod 2t \bmod 2 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 SC=4SC = 4. The kernel runs, in slot 0, the load of iteration pp and the multiply of iteration p−1p - 1, and in slot 1, the add of iteration p−2p - 2 and the store of iteration p−3p - 3. For N=100N = 100 the run has 103 passes, 6 cycles of prologue, 97 kernel passes and an epilogue, and takes 2⋅103=2062 \cdot 103 = 206 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 kk times and scheduling the larger body overlaps kk iterations, but the overlap stops at each back edge. Software pipelining keeps the overlap across all iterations with a kernel of IIII cycles. Unrolling is still used inside it, for modulo variable expansion and when the best IIII 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 t(v)−t(u)≥δ−II⋅dt(v) - t(u) \ge \delta - II \cdot d 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 RecMII\mathrm{RecMII} 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 IIII 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 5/25/2 cycles per iteration, unroll the body twice and pipeline it with II=5II = 5.

Pitfalls

  • Missing a loop-carried dependence. If line L1 keeps only the dependences inside one iteration, a sum s = s + y can read s before the previous iteration writes it. Every edge needs its distance, also through memory, as in a[i] = a[i - 1] * c.
  • Values that live longer than IIII. If line L8 keeps one register per value, the next iteration overwrites the value before its last read, as with x in the example. Expand the variable or use rotating registers.
  • Short trip counts. The prologue and the epilogue assume at least SC−1SC - 1 iterations. Without line L10, a loop with fewer iterations runs operations of iterations that do not exist.
  • The wrong rounding in the bounds. ResMII\mathrm{ResMII} and RecMII\mathrm{RecMII} round up. Rounding down gives an IIII that no schedule can meet, and with a heuristic that does not check, a schedule with conflicts.
  • Register pressure. A small IIII 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 IIII, or a scheduler that keeps lifetimes short, can be faster.

Exercises

  1. A loop computes x = a[i] (memory, latency 2) and s = s * x (multiply, latency 3), with one memory unit and one multiplier. Find ResMII\mathrm{ResMII}, RecMII\mathrm{RecMII} and the least IIII. (Answer: 1, 3 and 3.)
  2. Prove that II≥⌈δ(C)/d(C)⌉II \ge \lceil \delta(C) / d(C) \rceil for every cycle CC of the dependence graph, from the edge constraints of line L4.
  3. A value is written at cycle ww of its iteration and read last at cycle l≥wl \ge w. A write is seen by reads in the same cycle. Show that it needs ⌊(l−w)/II⌋+1\lfloor (l - w) / II \rfloor + 1 names in rotation, and check the count for x, y and z in the example.
  4. In the example, show that no legal schedule with II=2II = 2 and the load at cycle 0 puts the store before cycle 7.

References

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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.

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