---
title: Pipelining
heading: Cutting a long combinational path into stages separated by registers, the higher clock rate and throughput that buys, the latency it costs, the register overhead that limits how far it helps, filling and draining the pipeline, and retiming registers to balance the stages
eyebrow: D10 · Lesson 9 · Pipelining
xp: 40
---

::: hook
A laundry with a washer and a dryer finishes the first load no sooner when you pipeline it, but once the washer passes a load to the dryer and starts the next, a load comes out every hour instead of every two. Digital pipelines work the same way: break a long chain of logic into stages with registers between them, and each stage can start on new data every cycle. The clock gets faster in proportion to the number of stages, until the registers' own delay starts to dominate, and every result takes a few cycles longer to emerge.
:::

::: play
- A block of logic taking 10 ns, split into N equal stages, each followed by a register with 0.5 ns of overhead (clock-to-output plus setup).
- The clock rate (solid) climbs almost in proportion to N at first, then levels off as the fixed overhead dominates. The latency (dashed, in ns) grows.
- Throughput is one result per clock, so it follows the clock rate.
:::

```sim plot
view: graph
x: N = 1..20
y: f = 0..2000
slider: k = 1..20 step 1 = 4
plot: 1000/(10/N + 0.5) | samples: 300 | label: clock rate, MHz
plot: N*(10/N + 0.5)*50 | samples: 300 | dashed | color: ch3 | label: latency, ns × 50
marker: x = k | label: your stages
readout: clock rate, MHz = 1000/(10/k + 0.5) | digits: 4
readout: latency, ns = k*(10/k + 0.5) | digits: 3
readout: speedup over one stage = (10.5)/(10/k + 0.5) | digits: 3
```

::: predict
## The 10 ns block is split into 4 stages, each with 0.5 ns of register overhead. How much faster does data flow through?

- [ ] 4 times
- [x] 3.5 times
- [ ] 2 times
- [ ] It's slower: the registers add delay

::: reveal
Unpipelined, the period is 10 + 0.5 = 10.5 ns. With 4 stages it's 10/4 + 0.5 = 3 ns: 333 MHz against 95 MHz, a speedup of 10.5/3 = 3.5. Each result now takes 4 × 3 = 12 ns from input to output, longer than before, but a new one finishes every 3 ns.
:::
:::

::: nameit
## The gain

Splitting logic of delay $T_L$ into N balanced stages with register overhead $t_r$ each gives a clock period

$$T = \frac{T_L}{N} + t_r$$

so the throughput (one result per cycle) rises until the overhead dominates: it can never exceed $1/t_r$.

## The costs

- **Latency** grows to $NT = T_L + Nt_r$: the first result appears N cycles after its input.
- Processing M independent items takes $N + M - 1$ cycles: N − 1 to fill the pipeline, then one per cycle.
- Registers cost area and power, and **dependencies**, when one result is needed to compute the next, force stalls or forwarding (C8, S4).

## Balancing

The clock must accommodate the slowest stage, so unbalanced stages waste the others' time. **Retiming** moves registers across logic to even out the stages without changing the function, and synthesis tools can do it automatically.
:::

::: example
## With 10 stages instead of 4, what are the clock rate and latency? How long do 1000 items take with 4 stages?

::: solution
1. **Clock period with 10 stages.** Each stage holds 1 ns of logic plus 0.5 ns of overhead:
   $$\begin{aligned} T &= 1 + 0.5 \\ &= 1.5\ \text{ns} \end{aligned}$$
2. **Clock rate.**
   $$\frac{1}{1.5\ \text{ns}} = 667\ \text{MHz}$$
3. **Latency and speedup.** Ten stages of 1.5 ns make a latency of 15 ns, and against the 10.5 ns unpipelined path the speedup is
   $$\frac{10.5}{1.5} = 7$$
4. **1000 items with 4 stages.** At 3 ns per cycle, the first item takes 4 cycles and each of the other 999 one more:
   $$\begin{aligned} t &= (4 + 999) \times 3\ \text{ns} \\ &= 1003 \times 3\ \text{ns} \\ &= 3.01\ \mu\text{s} \end{aligned}$$
5. **Without the pipeline.**
   $$1000 \times 10.5\ \text{ns} = 10.5\ \mu\text{s}$$

**Answer:** with 10 stages, 667 MHz and a latency of 15 ns; with 4 stages, 1000 items take 3.01 µs, against 10.5 µs unpipelined.
:::
:::

::: example
## Stages of 2, 3 and 5 ns plus 0.4 ns of overhead each. What's the clock period, and what would balancing achieve?

::: solution
1. **The slowest stage sets the period.**
   $$5 + 0.4 = 5.4\ \text{ns}$$
2. **Balance the stages.** Rebalanced to 10/3 ns each:
   $$\begin{aligned} T &= 3.33 + 0.4 \\ &= 3.73\ \text{ns} \end{aligned}$$
3. **Compare.**
   $$\frac{5.4}{3.73} = 1.45$$
   so balancing makes it 45% faster.

**Answer:** a 5.4 ns period, and 3.73 ns once balanced, 45% faster.
:::
:::

::: example
## In a pipelined processor, 20% of instructions are loads whose result the next instruction needs, costing one stall each. What is the CPI?

::: solution
1. **Add the stalls.** Each of the 20% of instructions that stall costs one extra cycle:
   $$\begin{aligned} \text{CPI} &= 1 + 0.2 \times 1 \\ &= 1.2 \end{aligned}$$
2. **Throughput.**
   $$\frac{1}{1.2} = 83\%$$
   of the ideal.
3. **Recovering it.** Compilers reorder instructions to fill those slots and bring the CPI back toward 1.

**Answer:** a CPI of 1.2, so the pipeline runs at 83% of its ideal throughput.
:::
:::

::: check
given: L = 6 | 10 | 20; N = 2 | 5 | 8; r = 0.3 | 0.6
answer: 1000/(L/N + r)
unit: MHz
tolerance: 0.5%

## Logic taking {L} ns is split into {N} balanced stages with {r} ns of register overhead each. What clock rate does it allow?

::: right
Right. $1/(T_L/N + t_r)$ = {answer} MHz.
:::

::: misses
- [1000/(L/N)] Include the register overhead in every stage.
- [any] Not yet. One over the stage time.
:::
:::

::: check
given: L = 6 | 10 | 20; N = 2 | 5 | 8; r = 0.3 | 0.6
answer: L + N*r
unit: ns
tolerance: 0.5%

## For that pipeline ({L} ns of logic, {N} stages, {r} ns overhead each), how long does one result take from input to output?

::: right
Right. $T_L + Nt_r$ = {answer} ns.
:::

::: misses
- [L] The registers add their overhead at every stage.
- [any] Not yet. N stages of $T_L/N + t_r$.
:::
:::

::: check
given: N = 3 | 5 | 12; M = 10 | 100 | 1000
answer: N + M - 1
tolerance: 0.4

## How many cycles does an {N}-stage pipeline take to process {M} independent items?

::: right
Right. $N + M - 1$ = {answer} cycles.
:::

::: misses
- [N*M] The items overlap: once full, one finishes every cycle.
- [any] Not yet. Fill, then one per cycle.
:::
:::

::: check
given: a = 2 | 3; b = 4 | 6; c = 3 | 5; r = 0.2 | 0.5
answer: ((a + b + abs(a - b))/2 + c + abs((a + b + abs(a - b))/2 - c))/2 + r
unit: ns
tolerance: 0.5%

## A pipeline's three stages take {a}, {b} and {c} ns of logic, each plus {r} ns of overhead. What clock period does it need?

::: right
Right. The slowest stage plus overhead: {answer} ns.
:::

::: misses
- [(a + b + c)/3 + r] That's after balancing; as built, the slowest stage sets the clock.
- [any] Not yet. The slowest stage.
:::
:::

::: check
kind: parts
given: L = 8 | 10 | 20; k = 4 | 8 | 10; o = 0.2 | 0.5

## A combinational job of {L} ns is split into {k} equal pipeline stages, each adding {o} ns of register overhead.

::: part
answer: 1000/(L/k + o)
unit: MHz
tolerance: 0.5%

### What clock rate results?

::: misses
- [any] 1 ÷ (L/k + overhead).
:::
:::

::: part
answer: k*(L/k + o)
unit: ns
tolerance: 0.5%

### What is the latency?

::: misses
- [any] Stages × period.
:::
:::
:::

::: check
kind: parts
given: p = 10 | 20 | 30; s = 1 | 2

## In a pipelined processor, {p}% of instructions stall for {s} cycle(s) each.

::: part
answer: 1 + p/100*s
tolerance: 0.5%

### What is the CPI?

::: misses
- [any] 1 + fraction × stall.
:::
:::

::: part
answer: 100/(1 + p/100*s)
unit: %
tolerance: 0.5%

### What share of the ideal throughput does it get?

::: misses
- [any] 1/CPI.
:::
:::
:::

::: explain
## Why does pipelining raise throughput but not reduce latency?

::: model
Pipelining inserts registers into a long chain of logic so that each stage only has to finish its part within a clock cycle, which allows a much faster clock, and with a new item entering every cycle, results come out at that faster rate. But every item still has to pass through all of the logic, and now also through every register, each of which adds its own setup and clock-to-output delay. So the time from an item's arrival to its result is the original logic delay plus the registers' overhead, a little longer than before. Pipelining pays off when there's a steady stream of independent work.
:::
:::

::: practice

::: check
given: L = 1 | 50; N = 1 | 2 | 4 | 8 | 16 | 32; r = 0.1 | 1
answer: 1000/(L/N + r)
unit: MHz
tolerance: 0.5%

## $f_\max$: {L} ns in {N} stages, {r} ns overhead?

::: misses
- [any] $1/(T_L/N + t_r)$.
:::
:::

::: check
given: N = 2 | 3 | 5 | 8 | 20; M = 1 | 10 | 1000 | 1000000
answer: N + M - 1
tolerance: 0.4

## Cycles for {M} items through {N} stages?

::: misses
- [any] $N + M - 1$.
:::
:::
:::
