---
title: Pipelining limits and optimal depth
heading: Latch overhead, hazard penalties that grow with depth, the optimal pipeline and power-aware depth
eyebrow: S4 · Lesson 6 · Pipelining limits and optimal depth
xp: 50
---

::: hook
In the early 2000s one processor family stretched its pipeline past 30 stages to chase clock rate, and its successor went back to about 14. The reason is that a deeper pipeline wins clock rate but loses in two ways. Every stage adds latch overhead that no amount of splitting removes, and every hazard that empties the pipeline (a mispredicted branch, above all) costs more cycles the deeper the pipeline is. Somewhere between too shallow and too deep is an optimum, and once power is counted, the optimum moves shallower still. This lesson finds it.
:::

::: play
- Time per instruction, in FO4 delays, against pipeline depth k: $\text{TPI} = \left(\frac{t}{k} + o\right)(1 + bk)$, with t FO4 of logic per instruction, o FO4 of overhead per stage, and b stall cycles per instruction per stage of depth.
- The marker is the depth that minimizes TPI, $k^* = \sqrt{t/(ob)}$. The curve is flat near it, so a few stages either way cost little.
- Dashed: the power-aware metric BIPS³/W (performance cubed over power, with each stage's latches adding power), scaled to its own peak. Its optimum is shallower.
:::
:::

```sim plot
view: graph
x: k = 2..60
y: T = 0..50
slider: t = 50..200 = 100 FO4
slider: o = 2..8 = 4 FO4
slider: b = 0.01..0.2 = 0.05
slider: p = 0.01..0.2 = 0.05
plot: (t/k + o)*(1 + b*k) | samples: 300 | label: time per instruction, FO4
plot: 40*((t/sqrt(t/(o*b)) + o)*(1 + b*sqrt(t/(o*b)))/((t/k + o)*(1 + b*k)))^3*(1 + p*sqrt(t/(o*b)))/(1 + p*k) | samples: 300 | dashed | color: ch2 | label: BIPS³/W, relative (40 = value at k*)
marker: x = sqrt(t/(o*b)) | label: k*
readout: optimal depth k* = sqrt(t/(o*b))
readout: FO4 per stage at k* = t/sqrt(t/(o*b)) + o
```

::: predict
## A core has 100 FO4 of logic per instruction, 4 FO4 of overhead per stage, and loses 0.05 cycles per instruction per stage to hazards. Which depth gives the shortest time per instruction?

- [ ] 5 stages
- [ ] 10 stages
- [x] About 22 stages
- [ ] 40 stages

::: reveal
$k^* = \sqrt{100/(4 \times 0.05)}$ = 22.4 stages, about 8.5 FO4 per stage and a CPI of 2.1, for 17.9 FO4 per instruction. Five stages give 30 FO4 and 40 stages 19.5. But latches cost power: with each stage adding 5% to the core's power, the depth that maximizes BIPS³/W is about 16, and for plain BIPS/W about 11. Product lines that weigh power heavily build shallower pipelines.
:::
:::

::: nameit
## Why a pipeline can't be split forever

- **Overhead per stage:** latch delay, setup, clock skew and jitter, together o, about 2 to 5 FO4. The period is $t/k + o$, never below o.
- **Imbalance:** stages can't be cut at arbitrary points; the slowest one sets the clock.
- **Hazards that grow with depth:** a branch resolved at the end of a deep front end flushes more instructions, and load-use distances grow in cycles. A simple model charges $b$ stall cycles per instruction per stage, so CPI = 1 + bk.

## The optimum

$$\text{TPI}(k) = \left(\frac{t}{k} + o\right)(1 + bk), \qquad k^* = \sqrt{\frac{t}{ob}}$$

At the optimum the overhead's share and the hazards' share balance. Studies of real workloads found the performance optimum near 6 to 8 FO4 of logic per stage.

## Counting power

Each stage's latches and clocking add power, roughly $P(k) = P_0(1 + pk)$. Metrics that weigh energy, such as BIPS/W or BIPS³/W (whose ranking doesn't change when voltage scaling trades speed for power), have shallower optima than raw performance.

## The FO4 yardstick

Expressing delays in FO4 (S3.18) makes these results independent of the process: a 100 FO4 instruction path in 22 stages runs at about 8.5 FO4 per cycle in any technology.
:::

::: example
## With t = 100 FO4, o = 4 FO4 and b = 0.05, compare 5, 10, 22 and 40 stages.

::: solution
1. **5 stages.**
   $$\begin{aligned} \text{TPI}(5) &= (20 + 4)(1.25) \\ &= 30\ \text{FO4} \end{aligned}$$
2. **10 stages.**
   $$\begin{aligned} \text{TPI}(10) &= (10 + 4)(1.5) \\ &= 21\ \text{FO4} \end{aligned}$$
3. **22.4 stages, the optimum.**
   $$\begin{aligned} \text{TPI}(22.4) &= 8.472 \times 2.118 \\ &= 17.9\ \text{FO4} \end{aligned}$$
4. **40 stages.**
   $$\begin{aligned} \text{TPI}(40) &= (2.5 + 4)(3) \\ &= 19.5\ \text{FO4} \end{aligned}$$
5. **What the extra depth bought.** The last 18 stages beyond the optimum bought a clock 1.3 times faster and lost it all to the CPI.

**Answer:** 30, 21, 17.9 and 19.5 FO4 per instruction for 5, 10, 22 and 40 stages, so about 22 stages is best.
:::
:::

::: example
## A mispredicted branch in a 14-stage pipeline resolves at stage 11. With 20% branches and a 5% misprediction rate, what CPI do mispredictions add? What if a 31-stage pipeline resolves at stage 25?

::: solution
1. **14 stages, resolving at stage 11.** The penalty is 10 cycles:
   $$\begin{aligned} \Delta\text{CPI} &= 0.2 \times 0.05 \times 10 \\ &= 0.10 \end{aligned}$$
2. **31 stages, resolving at stage 25.** The penalty is 24 cycles:
   $$\begin{aligned} \Delta\text{CPI} &= 0.2 \times 0.05 \times 24 \\ &= 0.24 \end{aligned}$$
3. **What the deeper pipeline must overcome.** Its faster clock has to outrun 0.14 more CPI from branches alone, before counting longer load-use and cache-hit latencies in cycles.

**Answer:** mispredictions add 0.10 CPI to the 14-stage pipeline and 0.24 CPI to the 31-stage one.
:::
:::

::: example
## An instruction's path is 120 FO4 of logic, each stage adds 3 FO4 of overhead, and hazards cost 0.1 cycles per instruction per stage. What depth is best, and how much logic does each stage hold there?

::: solution
1. **Best depth.**
   $$\begin{aligned} k^* &= \sqrt{t/(ob)} \\ &= \sqrt{120/0.3} \\ &= 20\ \text{stages} \end{aligned}$$
   each with 120/20 = 6 FO4 of logic.
2. **Time per instruction.**
   $$\begin{aligned} \text{TPI} &= (6 + 3)(1 + 0.1 \times 20) \\ &= 27\ \text{FO4} \end{aligned}$$
   which also equals $(\sqrt{tb} + \sqrt o)^2$.
3. **Compare with real designs.** That 6 FO4 per stage agrees with the 6 to 8 FO4 that studies of real workloads found.

**Answer:** 20 stages is best, each holding 6 FO4 of logic.
:::
:::

::: check
given: t = 80 | 120 | 180; o = 3 | 5; b = 0.03 | 0.08
answer: sqrt(t/(o*b))
tolerance: 1%

## A core has {t} FO4 of logic per instruction, {o} FO4 of overhead per stage and loses {b} cycles per instruction per stage of depth. What depth minimizes time per instruction?

::: right
Right. $\sqrt{t/(ob)}$ = {answer} stages.
:::

::: misses
- [t/o] That only balances logic against overhead; include the hazards.
- [any] Not yet. Minimize $(t/k + o)(1 + bk)$.
:::
:::

::: check
given: t = 80 | 150; o = 3 | 5; b = 0.04 | 0.1; k = 8 | 15 | 30
answer: (t/k + o)*(1 + b*k)
tolerance: 0.5%

## With {t} FO4 of logic, {o} FO4 of overhead and {b} stalls per instruction per stage, what's the time per instruction at {k} stages, in FO4?

::: right
Right. $(t/k + o)(1 + bk)$ = {answer} FO4.
:::

::: misses
- [t/k + o] Include the hazard stalls.
- [any] Not yet. Cycle time times CPI.
:::
:::

::: check
given: t = 60 | 100 | 160; o = 3 | 4; k = 10 | 20
answer: 100*o/(t/k + o)
unit: %
tolerance: 0.5%

## A pipeline of {k} stages splits {t} FO4 of logic evenly, with {o} FO4 of overhead per stage. What fraction of each cycle is overhead?

::: right
Right. $\frac{o}{t/k + o}$ = {answer}%.
:::

::: misses
- [100*o/(t/k)] The cycle includes the overhead itself.
- [any] Not yet. Overhead over the whole cycle.
:::
:::

::: check
given: f = 0.15 | 0.22; m = 0.03 | 0.08; s = 8 | 12 | 25
answer: f*m*(s - 1)
tolerance: 0.5%

## Branches are {f} of instructions, {m} of them mispredicted, and they resolve at stage {s}. How much CPI do mispredictions add?

::: right
Right. $f_bm(s - 1)$ = {answer}.
:::

::: misses
- [f*m*s] The branch's own fetch cycle isn't lost.
- [any] Not yet. Frequency × rate × penalty.
:::
:::

::: check
given: t = 100 | 140; o = 3 | 4; k = 12 | 25
answer: t/k + o
tolerance: 0.5%

## How many FO4 is each cycle when {t} FO4 of logic is split over {k} stages with {o} FO4 of overhead?

::: right
Right. $t/k + o$ = {answer} FO4.
:::

::: misses
- [t/k] Add the overhead.
- [any] Not yet. Logic per stage plus overhead.
:::
:::

::: check
given: t = 100 | 160; o = 3 | 5; b = 0.04 | 0.08
answer: (t/sqrt(t/(o*b)) + o)*(1 + b*sqrt(t/(o*b)))
tolerance: 1%

## At the optimal depth, what's the time per instruction for t = {t} FO4, o = {o} FO4 and b = {b}?

::: right
Right. {answer} FO4, which simplifies to $(\sqrt{o} + \sqrt{tb})^2$.
:::

::: misses
- [any] Not yet. Put $k^*$ into $(t/k + o)(1 + bk)$.
:::
:::

::: check
kind: parts
given: t = 80 | 120 | 160; o = 2 | 4; b = 0.03 | 0.08; k = 10 | 20

## An instruction's path is {t} FO4; each stage adds {o} FO4 of overhead, and hazards cost {b} cycles per instruction per stage.

::: part
answer: (t/k + o)*(1 + b*k)
unit: FO4
tolerance: 0.5%

### What is the time per instruction with {k} stages?

::: misses
- [t/k + o] Include the hazard cycles: $(1 + bk)$.
- [any] $(t/k + o)(1 + bk)$.
:::
:::

::: part
answer: sqrt(t/(o*b))
tolerance: 0.5%

### What depth minimizes it?

::: misses
- [any] $\sqrt{t/(ob)}$.
:::
:::
:::

::: check
kind: derivation
vars: t = 20..300, o = 1..6, b = 0.01..0.2

## A pipeline's time per instruction is $(t/k + o)(1 + bk)$.

::: step
answer: sqrt(t/(o*b))

### What depth k minimizes it?
:::

::: step
answer: (sqrt(t*b) + sqrt(o))^2

### What is the least time per instruction?
:::

::: step
answer: sqrt(t*o*b)

### How much logic does each stage hold at the optimum?
:::

::: right
Right. At the optimum the overhead's cost and the hazards' cost balance.
:::
:::

::: check
kind: python

## Write `best_depth(t, o, b, p=None)`: search depths k = 1 to 100 and return (best k, its time per instruction $(t/k + o)(1 + bk)$). With p = None, minimize the time; with a number p, maximize $\text{BIPS}^3/\text{W}$, $(1/\text{TPI})^3/(1 + pk)$, for power that grows as $1 + pk$.

```python starter
def best_depth(t, o, b, p=None):
    return (1, (t + o) * (1 + b))
```

```python solution
def best_depth(t, o, b, p=None):
    best = None
    for k in range(1, 101):
        tpi = (t / k + o) * (1 + b * k)
        m = -tpi if p is None else (1 / tpi) ** 3 / (1 + p * k)
        if best is None or m > best[0]:
            best = (m, k, tpi)
    return (best[1], best[2])
```

```python tests
k, tpi = best_depth(120, 3, 0.1)
assert k == 20 and abs(tpi - 27) < 1e-9, "The worked example: 20 stages, 27 FO4."
k2, _ = best_depth(120, 3, 0.1, 0.05)
k3, _ = best_depth(120, 3, 0.1, 0.2)
assert k3 < k2 < k, "Counting power favors shallower pipelines, more so as each stage costs more."
```

::: right
Right. A metric that weighs energy moves the optimum toward fewer, longer stages.
:::
:::

::: explain
## Why does counting power push the best pipeline depth shallower than the depth that maximizes performance?

::: model
Near the performance optimum, the time per instruction is almost flat: adding or removing a few stages changes performance only slightly. Each extra stage, though, adds a full row of latches, more clock load and more speculative work thrown away on every misprediction, so power keeps rising steadily with depth. A metric that weighs power, such as performance per watt or BIPS³/W, therefore gains more from removing stages than it loses in speed, until the speed loss becomes steep. That moves its optimum to a shallower depth than raw performance alone would choose, which is why power-conscious designs settled on moderate pipelines.
:::
:::

::: practice

::: check
given: t = 90 | 140; o = 3.5 | 4.5; b = 0.05 | 0.12
answer: sqrt(t/(o*b))
tolerance: 1%

## Optimal depth: t = {t}, o = {o}, b = {b}?

::: misses
- [any] $\sqrt{t/(ob)}$.
:::
:::

::: check
given: t = 100 | 180; o = 3 | 4; b = 0.03 | 0.06; k = 10 | 35
answer: (t/k + o)*(1 + b*k)
tolerance: 0.5%

## TPI at {k} stages: t = {t}, o = {o}, b = {b}?

::: misses
- [any] $(t/k + o)(1 + bk)$.
:::
:::

::: check
given: f = 0.12 | 0.2; m = 0.02 | 0.06; s = 10 | 20
answer: f*m*(s - 1)
tolerance: 0.5%

## Misprediction CPI: {f} branches, {m} mispredicted, resolved at stage {s}?

::: misses
- [any] $f_bm(s - 1)$.
:::
:::
:::
