---
title: Speculation and the reorder buffer
heading: Executing past unresolved branches, committing in order, precise exceptions and memory disambiguation
eyebrow: S4 · Lesson 10 · Speculation and the reorder buffer
xp: 50
---

::: hook
Tomasulo's algorithm lets instructions execute out of order, but it can't look past a branch it hasn't resolved, and branches come every five or six instructions. Speculation removes that limit: the core follows the predicted path, executes everything there as if the prediction were right, and keeps the results in a reorder buffer until it knows. Instructions leave the buffer (commit) strictly in program order, so a wrong guess or an exception can be undone by discarding everything after it. How much of that speculative work turns out useful depends steeply on prediction accuracy and on how far ahead the window reaches.
:::

::: play
- The fraction of a full speculative window that's on the correct path, against the window size W, when one instruction in 1/f is a branch and each is predicted correctly with probability a.
- An instruction k branches ahead of the oldest unresolved one is useful with probability $a^k$, so the far end of a big window is mostly wasted work unless prediction is very accurate.
- At 95% accuracy and a branch every 5 instructions, a 200-entry window is only 44% useful; at 99%, 83%.
:::
:::

```sim plot
view: graph
x: W = 8..512 log
y: u = 0..1
slider: a = 0.85..0.999 = 0.95
slider: f = 0.1..0.3 = 0.2
plot: (1 - a^(f*W))/(f*W*(1 - a)) | samples: 300 | label: fraction of window on the correct path
plot: a^(f*W) | dashed | color: ch2 | label: chance the youngest instruction is on it
readout: useful instructions in a 200-entry window = (1 - a^(f*200))/(f*(1 - a))
```

::: predict
## A core keeps 200 instructions in flight, with a branch every 5 instructions predicted 95% correctly. About how many of the 200 are on the right path, on average?

- [ ] 190
- [ ] 150
- [x] About 90
- [ ] About 10

::: reveal
About 87. The window spans 40 branches, and the youngest instruction is on the right path only if all 40 predictions are right: $0.95^{40}$ = 13%. Summing over the window gives 87 useful instructions. At 99% accuracy it's 166. A bigger window pays off only with a better predictor, which is why the two grew together.
:::
:::

::: nameit
## The reorder buffer

Each instruction gets a reorder buffer (ROB) entry at issue, in program order. Results go to the ROB (or to a renamed physical register) when execution finishes; **commit** takes completed instructions from the head, in order, and updates the architectural state.

- A **mispredicted branch** reaching the head (or detected earlier) squashes every younger entry and redirects fetch.
- An **exception** is taken only when the faulting instruction reaches the head, so all older instructions have finished and no younger one has changed state: **precise exceptions**.
- Stores write memory only at commit, from a store buffer.

## How far speculation reaches

With a branch every 1/f instructions predicted with accuracy a, a window of W instructions spans B = fW branches. The k-th branch ahead is on the correct path with probability $a^k$, and the expected number of correct-path instructions is

$$\frac{1}{f}\cdot\frac{1 - a^{B}}{1 - a}$$

## Memory disambiguation

A load may execute before older stores whose addresses aren't known yet. The **load-store queue** checks each store's address, when it becomes known, against younger loads that already ran; a match means the load got stale data and must be squashed and replayed. Predictors learn which loads tend to conflict.

## Limits

Commit width caps IPC; the ROB size caps the window (Little's law, lesson 9); and every squashed instruction cost energy for nothing.
:::

::: example
## Find the useful part of a 200-instruction window at 95% and 99% accuracy, with a branch every 5 instructions, and of a 64-instruction window at 95%.

::: solution
1. **200 entries at 95%.** The window spans B = 40 branches:
   $$\begin{aligned} U &= \frac{1 - 0.95^{40}}{0.2 \times 0.05} \\ &= 87 \end{aligned}$$
   which is 44% of the window.
2. **200 entries at 99%.**
   $$\begin{aligned} U &= \frac{1 - 0.99^{40}}{0.2 \times 0.01} \\ &= 166 \end{aligned}$$
   which is 83%.
3. **64 entries at 95%.** The window spans B = 12.8 branches:
   $$\begin{aligned} U &= \frac{1 - 0.95^{12.8}}{0.2 \times 0.05} \\ &= 48 \end{aligned}$$
   which is 75%.
4. **Compare.** Tripling the window at 95% gained only 39 useful instructions, while the better predictor on the big window doubled them.

**Answer:** 87 useful instructions (44%) and 166 (83%) in the 200-entry window at 95% and 99%, and 48 (75%) in the 64-entry window at 95%.
:::
:::

::: example
## A load executes speculatively and reads address 0x1000. Later, an older store's address resolves to 0x1000. What must happen, and what if it resolves to 0x2000?

::: solution
1. **Same address, 0x1000.** The load read a value the store hasn't written yet, so the load and everything after it that used its value are squashed and replayed. The store's data is then forwarded from the store queue.
2. **Different address, 0x2000.** There's no conflict, and the speculation saved the cycles the load would have spent waiting.
3. **Why speculate.** Conflicts are rare for most loads, so speculating and occasionally replaying beats always waiting.

**Answer:** at 0x1000 the load and its dependents are squashed and replayed with the store's forwarded data; at 0x2000 nothing happens, and the load's early execution saved time.
:::
:::

::: example
## A 128-instruction window sees a branch every 5 instructions, predicted with 97% accuracy. How much of the window is on the correct path?

::: solution
1. **Branches in the window.**
   $$128/5 = 25.6\ \text{branches}$$
2. **Correct-path instructions.**
   $$\begin{aligned} U &= 5 \times \frac{1 - 0.97^{25.6}}{0.03} \\ &= 90 \end{aligned}$$
   which is 71% of the window.
3. **Why it matters.** Each misprediction near the head throws away everything behind it, so accuracy and window size have to grow together.

**Answer:** about 90 instructions, 71% of the window, are on the correct path.
:::
:::

::: check
given: a = 0.9 | 0.95 | 0.98; k = 10 | 20 | 30
answer: a^k
tolerance: 0.5%

## Each branch is predicted correctly with probability {a}. What's the probability that an instruction {k} branches past the oldest unresolved one is on the correct path?

::: right
Right. $a^k$ = {answer}.
:::

::: misses
- [1 - k*(1 - a)] Probabilities of independent events multiply.
- [any] Not yet. All k predictions must be right.
:::
:::

::: check
given: a = 0.93 | 0.97 | 0.995; f = 0.15 | 0.25; W = 64 | 192 | 512
answer: (1 - a^(f*W))/(f*(1 - a))
tolerance: 0.5%

## A {W}-instruction window has a branch every 1/{f} instructions, each predicted with accuracy {a}. How many instructions are on the correct path, on average?

::: right
Right. $\frac{1}{f}\frac{1 - a^{fW}}{1 - a}$ = {answer}.
:::

::: misses
- [W*a] Errors compound along the window.
- [W*a^(f*W)] That's the youngest instruction's chance times the window.
- [any] Not yet. Sum $a^k$ over the branches, 1/f instructions each.
:::
:::

::: check
given: c = 2 | 4 | 6; w = 4 | 8; u = 0.7 | 0.9
answer: ((c + w*u) - abs(c - w*u))/2
tolerance: 0.5%

## A core fetches and executes {w} instructions per cycle, but only {u} of them turn out to be on the correct path, and it commits at most {c} per cycle. What's its best IPC?

::: right
Right. The smaller of the commit width and the useful issue rate: {answer}.
:::

::: misses
- [w] Squashed instructions don't count, and commit caps it too.
- [any] Not yet. Useful work is limited by both.
:::
:::

::: check
given: W = 96 | 224 | 352; w = 4 | 6; L = 20 | 50
answer: (w + W/L - abs(w - W/L))/2
tolerance: 0.5%

## A {w}-wide core with a {W}-entry ROB averages {L} cycles from dispatch to commit. What IPC can it sustain at best?

::: right
Right. min(w, W/L) = {answer}.
:::

::: misses
- [any] Not yet. Little's law, capped by the width.
:::
:::

::: check
given: l = 0.25 | 0.3; r = 0.01 | 0.03; p = 10 | 20
answer: l*r*p
tolerance: 0.5%

## Loads are {l} of instructions, {r} of them are caught reading before a conflicting older store and replayed, and each replay costs {p} cycles. What CPI does that add?

::: right
Right. $f_\text{load} \times r \times p$ = {answer}.
:::

::: misses
- [r*p] Weight by how often loads occur.
- [any] Not yet. Frequency × replay rate × cost.
:::
:::

::: check
given: W = 128 | 256; a = 0.95 | 0.98; f = 0.2
answer: W - (1 - a^(f*W))/(f*(1 - a))
tolerance: 0.5%

## A full {W}-entry window has a branch every 5 instructions predicted with accuracy {a}. On average, how many of its instructions are wasted work?

::: right
Right. {answer} instructions will be squashed.
:::

::: misses
- [W*(1 - a)] Errors compound along the window.
- [any] Not yet. The window minus the expected useful part.
:::
:::

::: check
kind: parts
given: k = 4 | 5 | 8; a = 0.9 | 0.95 | 0.99; W = 64 | 256

## A window of {W} instructions sees a branch every {k} instructions, predicted with accuracy {a}.

::: part
answer: W/k
tolerance: 0.5%

### How many branches does it span?

::: misses
- [any] W ÷ k.
:::
:::

::: part
answer: k*(1 - a^(W/k))/(1 - a)
tolerance: 0.5%

### How many of its instructions are on the correct path, on average?

::: misses
- [any] $\frac1f\cdot\frac{1 - a^B}{1 - a}$.
:::
:::

::: part
answer: k/(1 - a)
tolerance: 0.5%

### What is the most any window could have on the correct path?

::: misses
- [any] The limit as B grows: $1/(f(1 - a))$.
:::
:::
:::

::: check
kind: derivation
vars: k = 2..10, a = 0.8..0.999, W = 8..1024

## A branch comes every k instructions, predicted with accuracy a; the window holds W instructions.

::: step
answer: W/k

### How many branches does the window span?
:::

::: step
answer: k*(1 - a^(W/k))/(1 - a)

### How many instructions are on the correct path on average?
:::

::: step
answer: k*(1 - a^(W/k))/((1 - a)*W)

### What fraction of the window is useful?
:::

::: right
Right. The useful part saturates at $k/(1 - a)$: past that, a bigger window only holds wrong-path work.
:::
:::

::: explain
## Why do out-of-order cores commit instructions in program order, when they've already executed them out of order?

::: model
Executing out of order is safe as long as nobody can observe the order, and committing in order is what guarantees that. Until an instruction commits, its result lives in a temporary place, a reorder buffer entry or a renamed register that the architectural state doesn't yet point to. If an older branch turns out mispredicted or an older instruction faults, everything younger can simply be thrown away, and the program's visible state is exactly what it would have been on an in-order machine. That gives precise exceptions, lets the operating system restart a faulting instruction cleanly, and makes speculation past branches recoverable. The cost is buffering every in-flight result until its turn.
:::
:::

::: practice

::: check
given: a = 0.92 | 0.97; k = 8 | 25
answer: a^k
tolerance: 0.5%

## Chance an instruction {k} branches ahead is on the right path, accuracy {a}?

::: misses
- [any] $a^k$.
:::
:::

::: check
given: a = 0.95 | 0.99; f = 0.18 | 0.22; W = 128 | 400
answer: (1 - a^(f*W))/(f*(1 - a))
tolerance: 0.5%

## Useful instructions: window {W}, branch rate {f}, accuracy {a}?

::: misses
- [any] $\frac{1}{f}\frac{1 - a^{fW}}{1 - a}$.
:::
:::

::: check
given: l = 0.2 | 0.35; r = 0.005 | 0.02; p = 12 | 25
answer: l*r*p
tolerance: 0.5%

## Replay CPI: {l} loads, {r} replayed, {p} cycles each?

::: misses
- [any] $f_\text{load}rp$.
:::
:::
:::
