---
title: Pipelining and retiming
heading: Trading latency for throughput, balancing stages, and the limit loops impose
eyebrow: S3 · Lesson 23 · Pipelining and retiming
xp: 50
---

::: hook
If a block of logic is too slow for the clock, cut it in pieces with registers between them. Each piece now has a whole clock period, so the clock can run faster, and a new input can enter every cycle while earlier ones are still in flight. That's pipelining, the reason processors, DSP chains and network switches reach gigahertz rates. It isn't free: every register adds its clock-to-Q and setup time, so the gains shrink as stages get thinner, each result takes more cycles to come out, and a loop that feeds a result back into its own input can't be pipelined at all without changing the algorithm.
:::

::: play
- A block with total logic delay T split into k equal pipeline stages, each adding a register overhead r. The solid curve is throughput (results per nanosecond, so GHz); the dashed one is latency in nanoseconds.
- Throughput rises nearly in proportion to k at first, then flattens toward 1/r as the register overhead dominates.
- Latency, T + kr, only grows. Increase r and the useful number of stages drops.
:::
:::

```sim plot
view: graph
x: k = 1..20
y: v = 0..10
slider: T = 1..8 = 4 ns
slider: r = 0.03..0.3 = 0.1 ns
plot: 1/(T/k + r) | samples: 300 | label: throughput, GHz
plot: T + k*r | samples: 300 | dashed | color: ch2 | label: latency, ns
readout: throughput limit 1/r, GHz = 1/r
```

::: predict
## A 4 ns block with 0.1 ns of register overhead per stage is pipelined into 4 stages. What happens to its throughput and its latency?

- [ ] Throughput ×4, latency the same
- [x] Throughput ×3.7, latency up 7%
- [ ] Throughput ×4, latency ×4
- [ ] Throughput the same, latency ÷4

::: reveal
The period drops from 4.1 ns to 4/4 + 0.1 = 1.1 ns, so throughput rises 4.1/1.1 = 3.7 times, a little short of 4 because of the extra registers. Each result now takes 4 × 1.1 = 4.4 ns to come out, 7% longer than before. So the pipeline delivers results 3.7 times as often, and each one takes slightly longer to arrive.
:::
:::

::: nameit
## Throughput and latency

Splitting logic of delay T into k balanced stages, each with register overhead $r = t_{clk\text{-}q} + t_\text{setup}$ (plus skew and jitter margin):

$$T_\text{clk} = \frac{T}{k} + r, \qquad \text{throughput} = \frac{1}{T_\text{clk}}, \qquad \text{latency} = kT_\text{clk} = T + kr$$

Throughput approaches 1/r as k grows, and power and area grow with every register. Unbalanced stages are worse: the period is set by the slowest stage, $\max_i t_i + r$.

## Retiming

Registers can be moved backward or forward across logic without changing what the circuit computes (as long as every path's register count between input and output is kept), to balance stage delays or shorten the critical path. Synthesis tools do it automatically when allowed.

## Loops limit pipelining

A result that feeds back into the next computation (an accumulator, an IIR filter, a processor's branch decision) can't use a result that's still in the pipeline. For a loop with total logic delay $L$ and R registers around it, the period can't be less than

$$T_\text{clk} \geq \frac{L}{R}$$

(the **iteration bound**). Adding registers to the loop changes the algorithm, so loops are fixed by transforming the algorithm (look-ahead, interleaving several independent streams) or by forwarding, as in processors (lesson 42).

## Pipelining for power

At a fixed throughput, a deeper pipeline gives each stage more slack, so the supply can drop: with $P \propto V^2$, that often saves more than the extra registers cost.
:::

::: example
## A 4 ns block with 0.1 ns of register overhead: compare 1, 4 and 10 stages.

::: solution
1. **One stage.** The period is $4 + 0.1 = 4.1$ ns, 244 MHz, and the latency is 4.1 ns.
2. **Four stages.** Each holds 1 ns of logic: the period is 1.1 ns, 909 MHz, and the latency is
   $$4 \times 1.1 = 4.4\ \text{ns}$$
3. **Ten stages.** Each holds 0.4 ns: the period is 0.5 ns, 2.0 GHz, and the latency is
   $$4 + 10 \times 0.1 = 5.0\ \text{ns}$$
4. **Compare four and ten.** Going from 4 to 10 stages buys $1.1/0.5 = 2.2$ times the throughput for 2.5 times the registers. Beyond that the 0.1 ns overhead dominates and the returns fade.

**Answer:** 244 MHz with one stage, 909 MHz with four and 2.0 GHz with ten, at latencies of 4.1, 4.4 and 5.0 ns.
:::
:::

::: example
## A four-stage pipeline has stage delays of 1.2, 0.8, 1.0 and 1.0 ns with 0.1 ns of overhead. What's its clock, and what does retiming to balance the stages give?

::: solution
1. **Unbalanced.** The slowest stage sets the clock:
   $$\begin{aligned} T &= 1.2 + 0.1 \\ &= 1.3\ \text{ns} \end{aligned}$$
   which is 769 MHz.
2. **Retimed.** Moving a register so 0.2 ns of logic shifts from the first stage to the second gives four 1.0 ns stages:
   $$\begin{aligned} T &= 1.0 + 0.1 \\ &= 1.1\ \text{ns} \end{aligned}$$
   which is 909 MHz.
3. **Compare.** $1.3/1.1 = 1.18$, so 18% faster, with the same logic and the same register count.

**Answer:** 769 MHz as it stands, and 909 MHz (18% faster) after retiming.
:::
:::

::: example
## An accumulator's loop has 1.6 ns of adder and multiplexer logic and one register, with 0.1 ns of register overhead. What clock can it run at, and what does interleaving four independent streams with four registers in the loop give?

::: solution
1. **One register.**
   $$1.6 + 0.1 = 1.7\ \text{ns}$$
2. **Four registers in the loop.** Retimed around the loop, the iteration bound is
   $$\frac{1.6}{4} = 0.4\ \text{ns per stage}$$
   so the clock is $0.4 + 0.1 = 0.5$ ns.
3. **Throughput.**
   $$\frac{1.7}{0.5} = 3.4$$
   times as much, provided four independent accumulations take turns (each one still gets a new input only every fourth cycle).

**Answer:** a 1.7 ns clock with one register; interleaving four streams allows a 0.5 ns clock, 3.4 times the throughput.
:::
:::

::: check
given: T = 2 | 5 | 8; k = 2 | 5 | 10; r = 0.05 | 0.15
answer: T/k + r
unit: ns
tolerance: 0.5%

## A {T} ns block is split into {k} balanced stages with {r} ns of register overhead each. What's the clock period?

::: right
Right. $T/k + r$ = {answer} ns.
:::

::: misses
- [T/k] Add the register overhead.
- [any] Not yet. Each stage's logic plus its register.
:::
:::

::: check
given: T = 2 | 5 | 8; k = 3 | 6; r = 0.05 | 0.15
answer: T + k*r
unit: ns
tolerance: 0.5%

## The same kind of pipeline: {T} ns of logic in {k} stages with {r} ns overhead. What's its latency?

::: right
Right. $T + kr$ = {answer} ns.
:::

::: misses
- [T] Each stage's register adds its overhead.
- [any] Not yet. k periods.
:::
:::

::: check
given: T = 3 | 6; k = 4 | 8; r = 0.08 | 0.2
answer: (T + r)/(T/k + r)
tolerance: 0.5%

## How many times higher is the throughput of a {k}-stage pipeline than the unpipelined block ({T} ns of logic, {r} ns overhead per register)?

::: right
Right. $\frac{T + r}{T/k + r}$ = {answer}.
:::

::: misses
- [k] Register overhead keeps the speedup below k.
- [any] Not yet. Compare the two periods.
:::
:::

::: check
given: a = 1 | 1.5; b = 0.6 | 0.9; c = 1.1 | 1.3; r = 0.1
answer: 1/(((a + b + abs(a - b))/2 + c + abs((a + b + abs(a - b))/2 - c))/2 + r)*1000
unit: MHz
tolerance: 0.5%

## A pipeline's stages take {a}, {b} and {c} ns, with {r} ns of register overhead. What's its maximum clock?

::: right
Right. The slowest stage plus overhead sets the period: {answer} MHz.
:::

::: misses
- [3/(a + b + c + 3*r)*1000] The stages don't average: the slowest one sets the clock.
- [any] Not yet. Find the largest stage delay.
:::
:::

::: check
given: L = 2 | 3 | 5; R = 2 | 3 | 4
answer: L/R
unit: ns
tolerance: 0.5%

## A feedback loop has {L} ns of logic and {R} register(s) around it. What's the shortest possible clock period?

::: right
Right. The iteration bound L/R = {answer} ns.
:::

::: misses
- [L] With R registers in the loop, each period only has to cover L/R.
- [any] Not yet. The loop's delay is shared by its registers.
:::
:::

::: check
given: T = 4 | 6; r = 0.1 | 0.25
answer: 1/r
unit: GHz
tolerance: 0.5%

## What throughput does a {T} ns block approach as it's pipelined ever deeper, with {r} ns of register overhead?

::: right
Right. 1/r = {answer} GHz: the register overhead is a floor on the period.
:::

::: misses
- [any] Not yet. As T/k → 0, the period → r.
:::
:::

::: check
kind: parts
given: T = 2 | 4 | 8; k = 2 | 4 | 8; r = 0.05 | 0.1 | 0.2

## A block of {T} ns of logic is split into {k} balanced stages, each with {r} ns of register overhead.

::: part
answer: T/k + r
unit: ns
tolerance: 0.5%

### What is the clock period?

::: misses
- [T/k] Add the register overhead.
- [any] $T/k + r$.
:::
:::

::: part
answer: 1000/(T/k + r)
unit: MHz
tolerance: 0.5%

### What throughput does it reach?

::: misses
- [any] One result per period.
:::
:::

::: part
answer: T + k*r
unit: ns
tolerance: 0.5%

### What is the latency?

::: misses
- [T] Each stage adds its overhead.
- [any] $T + kr$.
:::
:::
:::

::: check
kind: derivation
vars: L = 0.5..10, R = 1..16, r = 0.02..0.3

## A feedback loop has total logic delay L and R registers around it, each with overhead r.

::: step
answer: L/R

### What is the iteration bound?
:::

::: step
answer: L/R + r

### What is the shortest clock period?
:::

::: step
answer: (L + r)/(L/R + r)

### By what factor does throughput rise over a single register (with R interleaved streams)?
:::

::: right
Right. Registers in a loop only help if the algorithm is reorganized, by interleaving or look-ahead, to fill them.
:::
:::

::: explain
## Why does pipelining increase throughput but never reduce latency?

::: model
Pipelining doesn't make any piece of logic faster: a result still has to pass through all of the original logic, and now it also passes through extra registers, each adding a little delay and holding the result until its stage's clock edge. So the time for one result, from input to output, gets slightly longer. What changes is that each stage now holds a different result, so the block works on several at once. A new input can enter every clock, and the clock can be much faster because each stage contains only a fraction of the logic. The block produces results more often, even though each one takes as long or longer.
:::
:::

::: practice

::: check
given: T = 3 | 7; k = 4 | 8; r = 0.06 | 0.12
answer: T/k + r
unit: ns
tolerance: 0.5%

## Period: {T} ns in {k} stages, {r} ns overhead?

::: misses
- [any] $T/k + r$.
:::
:::

::: check
given: T = 2.5 | 5; k = 3 | 7; r = 0.1 | 0.2
answer: T + k*r
unit: ns
tolerance: 0.5%

## Latency: {T} ns in {k} stages, {r} ns overhead?

::: misses
- [any] $T + kr$.
:::
:::

::: check
given: L = 2.5 | 4; R = 2 | 4
answer: L/R
unit: ns
tolerance: 0.5%

## Iteration bound: {L} ns, {R} registers?

::: misses
- [any] L/R.
:::
:::
:::
