---
title: Dynamic scheduling with Tomasulo's algorithm
heading: Data hazards, register renaming, reservation stations, the common data bus, and the window Little's law demands
eyebrow: S4 · Lesson 9 · Dynamic scheduling
xp: 50
---

::: hook
An in-order pipeline stops at the first instruction whose operands aren't ready, even when the next ten instructions are independent and could run. Dynamic scheduling lets those instructions go ahead: they wait in reservation stations until their operands arrive, then execute in whatever order their data allows. Robert Tomasulo's algorithm for the IBM 360/91 floating-point unit (1967) did this with three ideas that every out-of-order core still uses: renaming registers to tags so only true dependences remain, reservation stations that capture operands as they're produced, and a common data bus that broadcasts each result to everyone waiting for it.
:::

::: play
- The IPC an out-of-order core can sustain against the size of its instruction window, W, when instructions take L cycles on average from entering the window to completing (long-latency loads included) and the core can issue w per cycle.
- By Little's law, the window holds $\text{IPC} \times L$ instructions, so IPC can't exceed W/L. It also can't exceed the width w.
- The knee is at $W = wL$. Raise the average latency, as cache misses do, and the window needed to stay at full width grows in proportion.
:::
:::

```sim plot
view: graph
x: W = 8..512 log
y: I = 0..8
slider: w = 1..8 step 1 = 4
slider: L = 5..100 = 30 cycles log
plot: (w + W/L - abs(w - W/L))/2 | samples: 400 | label: sustainable IPC
marker: x = w*L | label: window needed for full width
readout: window needed = w*L
```

::: predict
## A 4-wide out-of-order core sees an average of 30 cycles from dispatch to completion per instruction, counting cache misses. Its window holds 64 instructions. What IPC can it sustain at best?

- [ ] 4
- [ ] 3
- [x] About 2.1
- [ ] 64

::: reveal
2.13. Little's law says the number of instructions in flight equals throughput times latency, so 64 = IPC × 30 gives IPC = 2.13. To sustain all 4 per cycle it would need a 120-entry window. That's why windows grew from tens to hundreds of entries as memory latency grew in cycles.
:::
:::

::: nameit
## Dependences and hazards

- **RAW** (read after write): a true dependence; the consumer needs the producer's value.
- **WAR** (write after read) and **WAW** (write after write): name dependences, caused only by reusing a register name. They disappear if each new value gets a fresh name.

## Tomasulo's algorithm

1. **Issue:** take the next instruction in order; if a reservation station is free, put it there. For each source register, copy the value if it's ready, or else the **tag** of the station that will produce it. Record this station as the register's future producer (renaming: later readers wait for this tag, and WAR and WAW vanish).
2. **Execute:** when all operands are present, start on a functional unit, oldest first.
3. **Write result:** broadcast the value and its tag on the **common data bus (CDB)**. Every station and register waiting for that tag captures it.

Loads and stores use address buffers and must respect memory dependences: a load can't pass an earlier store to the same address.

## Sizing the machine

- **Little's law:** instructions in flight = IPC × average latency. A window of W entries sustains at most $W/L$ IPC.
- Reservation stations per unit: about (issue rate to that unit) × (its latency + waiting time).
- Physical registers with renaming: the architectural ones plus one per in-flight instruction that writes a register.
- The CDB carries one result per bus per cycle: with c buses and a fraction $f_w$ of instructions producing a result, IPC ≤ $c/f_w$.
:::

::: example
## A 6-wide core averages 40 cycles of latency per instruction. How big must its window be for full width, and what IPC does a 224-entry window allow? How many physical registers does it need if 75% of instructions write one of 32 architectural registers?

::: solution
1. **Window for full width.**
   $$6 \times 40 = 240\ \text{entries}$$
2. **IPC with 224 entries.**
   $$\text{IPC} \le 224/40 = 5.6$$
3. **Physical registers.**
   $$32 + 0.75 \times 224 = 200$$
   Machines add a few more so renaming never stalls.

**Answer:** full width needs a 240-entry window; 224 entries allow an IPC of 5.6 and need about 200 physical registers.
:::
:::

::: example
## Two iterations of `fld f0, 0(x1)`, `fmul f4, f0, f2`, `fsd f4, 0(x1)`, `addi x1, x1, 8`: which hazards does Tomasulo remove?

::: solution
1. **Within an iteration.** fmul waits for fld (RAW on f0) and fsd waits for fmul (RAW on f4). Those are real dependences.
2. **Between iterations.** The second fld writes f0 while the first fmul may still need the old f0 (WAR), and the second fmul writes f4 like the first (WAW).
3. **Renaming.** Tomasulo gives each f0 and f4 its own station tag, so the second load can issue and execute while the first multiply is still running.
4. **The result.** The loop overlaps itself, which in-order issue couldn't do.

**Answer:** Tomasulo removes the WAR hazard on f0 and the WAW hazard on f4 between iterations; the RAW hazards on f0 and f4 within each iteration remain.
:::
:::

::: example
## A core has 2 common data buses, and 70% of its instructions produce a register result. Its floating-point multiplier takes 4 cycles, receives one instruction a cycle, and each waits about 2 cycles for operands. What IPC can the buses carry, and how many reservation stations does the multiplier need?

::: solution
1. **Bus limit.** Each bus carries one result a cycle, and 70% of instructions need one:
   $$\text{IPC} \le 2/0.7 = 2.86$$
2. **Multiplier stations.** Each station holds an instruction for its wait plus its latency, so by Little's law:
   $$1 \times (4 + 2) = 6\ \text{stations}$$
3. **Sizing.** Too few stations stall issue; too many waste area and wake-up power.

**Answer:** the buses carry an IPC of at most 2.86, and the multiplier needs about 6 reservation stations.
:::
:::

::: check
given: w = 2 | 4 | 6; L = 15 | 40 | 80
answer: w*L
tolerance: 0.5%

## A {w}-wide core averages {L} cycles from dispatch to completion. What window does it need to sustain full width?

::: right
Right. Little's law: $wL$ = {answer} instructions.
:::

::: misses
- [L] The window must hold w instructions for every cycle of latency.
- [any] Not yet. In flight = throughput × latency.
:::
:::

::: check
given: W = 48 | 128 | 320; L = 20 | 60; w = 4 | 8
answer: (w + W/L - abs(w - W/L))/2
tolerance: 0.5%

## A {w}-wide core has a {W}-entry window and averages {L} cycles of latency per instruction. What IPC can it sustain at most?

::: right
Right. The smaller of w and W/L: {answer}.
:::

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

::: check
given: A = 32 | 64; W = 96 | 192 | 512; f = 0.7 | 0.8
answer: A + W*f
tolerance: 0.5%

## A core with {A} architectural registers keeps {W} instructions in flight, {f} of which write a register. How many physical registers does renaming need?

::: right
Right. $A + Wf$ = {answer}.
:::

::: misses
- [W*f] The committed architectural state needs registers too.
- [A + W] Only instructions that write a register need a new one.
- [any] Not yet. Committed values plus one per in-flight result.
:::
:::

::: check
given: c = 1 | 2 | 3; f = 0.6 | 0.75 | 0.9
answer: c/f
tolerance: 0.5%

## A Tomasulo machine has {c} common data bus(es), each carrying one result per cycle, and {f} of instructions produce a result. What IPC does the CDB allow at most?

::: right
Right. $c/f_w$ = {answer}.
:::

::: misses
- [c] Instructions without a result don't use the bus.
- [any] Not yet. Results per cycle over results per instruction.
:::
:::

::: check
given: r = 0.5 | 1 | 1.5; l = 3 | 6 | 12; q = 2 | 4
answer: r*(l + q)
tolerance: 0.5%

## A multiplier receives {r} instructions per cycle on average. Each spends {q} cycles waiting for operands and {l} cycles executing. How many reservation stations does it need on average?

::: right
Right. Little's law: $r(l + q)$ = {answer}.
:::

::: misses
- [r*l] Count the waiting cycles too.
- [any] Not yet. Arrival rate × time each spends in a station.
:::
:::

::: check
given: a = 3 | 4 | 6; n = 3 | 5 | 8
answer: n*(a + 1)
tolerance: 0

## A chain of {n} dependent multiplies each executes in {a} cycles and then takes 1 cycle to broadcast its result on the CDB before the next can start. How many cycles from the first starting to execute to the last result's broadcast?

::: right
Right. $n(a + 1)$ = {answer}: dependent instructions can't overlap, however wide the core.
:::

::: misses
- [n*a] Each result also spends a cycle on the bus before its consumer can start.
- [a + 1] Each multiply must wait for the one before it.
- [any] Not yet. Execute plus broadcast, once per link of the chain.
:::
:::

::: check
kind: parts
given: w = 4 | 6 | 8; L = 20 | 40; f = 0.6 | 0.8; c = 2 | 3

## A {w}-wide out-of-order core averages {L} cycles of latency per instruction; {f} of instructions write one of 32 architectural registers, and it has {c} common data buses.

::: part
answer: w*L
tolerance: 0
unit: entries

### How big must its window be to sustain full width?

::: misses
- [any] Little's law: IPC × latency.
:::
:::

::: part
answer: 32 + w*L*f
unit: registers
tolerance: 0.5%

### How many physical registers does that window need?

::: misses
- [w*L*f] Add the architectural registers.
- [any] 32 + window × writing fraction.
:::
:::

::: part
answer: c/f
tolerance: 0.5%

### What IPC can its result buses carry?

::: misses
- [any] $c/f_w$.
:::
:::
:::

::: check
kind: derivation
vars: W = 16..512, L = 5..100, i = 0.5..8

## An out-of-order window holds W instructions, each staying L cycles on average.

::: step
answer: W/L

### What is the highest IPC it can sustain?
:::

::: step
answer: i*L

### How big a window does an IPC of i need?
:::

::: right
Right. Long-latency misses inflate L, which is why windows grew into the hundreds.
:::
:::

::: explain
## Why does register renaming remove WAR and WAW hazards but not RAW hazards?

::: model
A RAW hazard is a real flow of data: the second instruction needs the value the first one computes, so it can't execute until that value exists, whatever the value is called. WAR and WAW hazards exist only because two different values happen to be assigned to the same register name: a later instruction wants to write the name while an earlier one still needs to read the old value, or two writes must land in order so the right one remains. If every new value gets its own storage, a reservation-station tag or a physical register, the two values no longer share a location, so neither can disturb the other, and only the true data dependences are left to order execution.
:::
:::

::: practice

::: check
given: w = 3 | 8; L = 25 | 60
answer: w*L
tolerance: 0.5%

## Window for full width: {w}-wide, {L} cycles latency?

::: misses
- [any] $wL$.
:::
:::

::: check
given: W = 64 | 256; L = 30 | 80; w = 4 | 6
answer: (w + W/L - abs(w - W/L))/2
tolerance: 0.5%

## Sustainable IPC: window {W}, latency {L}, width {w}?

::: misses
- [any] min(w, W/L).
:::
:::

::: check
given: A = 32 | 64; W = 128 | 400; f = 0.65 | 0.85
answer: A + W*f
tolerance: 0.5%

## Physical registers: {A} architectural, {W} in flight, {f} writing?

::: misses
- [any] $A + Wf$.
:::
:::
:::
