Description

A simple pipeline issues instructions in order; one stalled instruction blocks everything behind it. Dynamic scheduling lets hardware execute instructions out of order as soon as their operands and a functional unit are ready, while still producing the right results.

Dependences recap (see Topic 9):

  • RAW (true): real data flow, must be respected.
  • WAR (anti) and WAW (output): name dependences caused by reusing register names. Register renaming removes them.

Tomasulo's algorithm (Robert Tomasulo, IBM 360/91, 1967):

  • Reservation stations (RS): buffers in front of each functional unit that hold an instruction and its operands (or the tags of the units that will produce them).
  • Register renaming via tags: an operand not yet computed is replaced by the tag of the RS that will produce it, which removes WAR and WAW hazards.
  • Common Data Bus (CDB): broadcasts each result with its tag to every waiting RS and register at once.
  • Three steps per instruction: Issue (in order, allocate an RS, read ready operands or record tags) → Execute (when both operands are available) → Write result (broadcast on CDB).

Register renaming (modern form): a rename table maps the architectural registers to a larger pool of physical registers; each new destination gets a fresh physical register, so only true RAW dependences remain.

Re-Order Buffer (ROB): instructions complete out of order but commit in program order. The ROB holds results until it is safe to write architectural state, giving precise exceptions and enabling speculation.

Branch prediction keeps the front end fed past unresolved branches:

PredictorIdea
StaticAlways taken / not taken, or by direction (backward taken)
1-bitRemember last outcome; mispredicts twice per loop exit
2-bit saturating counterNeeds two wrong outcomes to flip; mispredicts once per loop exit
Correlating / two-levelUse global or local history of recent branches (e.g. gshare: PC XOR history)
Tournament / TAGECombine predictors; pick the best per branch (used in modern cores)
BTB (branch target buffer)Cache of predicted target addresses so fetch can redirect immediately
Return address stackPredicts function-return targets

Speculative execution: execute instructions beyond a predicted branch; commit them only if the prediction was right, otherwise flush (roll back) and restart on the correct path. Used in Intel and ARM cores. (The same mechanism is the root of the Spectre-class security attacks.)

Key Formulas

CPI = CPI_base + (branch frequency) × (misprediction rate) × (misprediction penalty)

2-bit counter:  states 00,01 = predict not taken;  10,11 = predict taken
                taken → increment (saturate at 11);  not taken → decrement (saturate at 00)

gshare index = (PC bits) XOR (global history register)

Tomasulo reservation-station fields: Op | Vj | Vk | Qj | Qk | A | Busy
   V = operand value, Q = tag of producing station (0 or blank = value is ready)

Steps

Tomasulo trace: (1) Issue in order: free RS available? Copy ready operand values; for operands still pending store the producing RS tag in Qj/Qk; set the destination register's status to this RS, (2) Execute when Qj = Qk = 0, (3) Write: broadcast result and tag on CDB; waiting stations capture it; the register file updates only if its status still points to this RS.

With a ROB (4 steps): Issue → Execute → Write result (into ROB entry) → Commit (when the entry reaches the ROB head; writes register/memory, handles exceptions).

Mispredicted branch: (1) branch resolves, (2) flush all younger instructions from RS and ROB, (3) restore rename map, (4) restart fetch at the correct target.

Worked Example

A. Dependence analysis (H&P-style sequence).

1  fdiv.d f0,  f2,  f4
2  fadd.d f6,  f0,  f8      # RAW on f0 (from 1)
3  fsd    f6,  0(x1)        # RAW on f6 (from 2)
4  fsub.d f8,  f10, f14     # WAR with 2 (2 reads f8, 4 writes f8)
5  fmul.d f6,  f10, f8      # RAW on f8 (from 4); WAW with 2 (both write f6)
  • RAW: 1→2 (f0), 2→3 (f6), 4→5 (f8). Must be respected.
  • WAR: 2 reads f8, 4 writes f8. WAW: 2 and 5 both write f6.
  • Renaming removes WAR and WAW, so instructions 4 and 5 can run while the long fdiv.d is still executing.

B. Tomasulo at issue time.

InstrStationOperands capturedRegister status after issue
1 fdivDiv1Vj = f2, Vk = f4 (both ready)f0 → Div1
2 faddAdd1Qj = Div1, Vk = value of f8 (captured now)f6 → Add1
3 fsdStore1waits for Add1 (value of f6)none
4 fsubAdd2Vj = f10, Vk = f14 (ready)f8 → Add2
5 fmulMul1Vj = f10, Qk = Add2f6 → Mul1
  • Instruction 2 already copied the old f8, so instruction 4 overwriting f8 is harmless (WAR gone).
  • f6's status now points to Mul1, so when Add1 finishes, its result feeds Store1 through the CDB but does not overwrite f6 in the register file (WAW gone).

C. Loop predictor. A loop branch is taken 9 times then not taken once, repeated.

  • 1-bit: mispredicts the exit and the first iteration of the next pass → 2 per 10 → 80% accuracy.
  • 2-bit: the exit only moves the counter from strongly taken to weakly taken, so the next pass is still predicted taken → 1 per 10 → 90%.
  • Simulation over 20 passes starting cold: 1-bit = 0.80, 2-bit = 0.89 (approaches 0.90 in steady state).

D. Misprediction cost. Base CPI 1.0, 20% branches, 15-cycle penalty:

  • 5% mispredicted: CPI = 1 + 0.2 × 0.05 × 15 = 1.15.
  • 10% mispredicted: CPI = 1 + 0.2 × 0.10 × 15 = 1.30.

Doubling the miss rate doubles the penalty, and deeper pipelines make it worse.

E. ROB and precise exceptions. Instructions 1-5 in the ROB; instruction 3 faults. Instructions 1-2 commit, 3 raises the exception at the ROB head, 4-5 (which may already have executed) are discarded. Software sees a state exactly as if the program had run in order up to instruction 3.

Python / Assembly

def run_1bit(outcomes):
    state, correct = 0, 0
    for o in outcomes:
        correct += (state == o); state = o
    return correct / len(outcomes)

def run_2bit(outcomes, s=0):
    correct = 0
    for o in outcomes:
        pred = 1 if s >= 2 else 0
        correct += (pred == o)
        s = min(3, s + 1) if o else max(0, s - 1)
    return correct / len(outcomes)

loop = ([1] * 9 + [0]) * 20            # 1 = taken
print(run_1bit(loop), run_2bit(loop))  # 0.8  0.89
print(run_2bit(([1]*9 + [0]) * 100, s=3))   # 0.9 (warm start)

def gshare_index(pc, ghr, bits=10):
    return ((pc >> 2) ^ ghr) & ((1 << bits) - 1)

The branch-heavy code that predictors love or hate:

# Counted loop: taken N-1 times, not taken once (easy for a 2-bit predictor)
        li   t0, 100
loop:   addi t0, t0, -1
        bnez t0, loop

# Data-dependent branch: hard to predict if the data are random
        lw   t1, 0(a0)
        bgez t1, skip        # ~50% taken on random data -> many mispredictions
        addi t2, t2, 1
skip:

Notes

  • Tomasulo's original design (IBM 360/91) handled floating point; modern cores use the same ideas with physical register files plus a ROB.
  • The ROB is what makes speculation safe; Tomasulo alone does not give precise exceptions.
  • Wide out-of-order engines are power-hungry; this is one reason ILP stopped scaling (Topic 11).
  • Branch-free code (conditional moves, predication, SIMD masks) can beat unpredictable branches.
  • Quick facts: Tomasulo uses reservation stations and register renaming; the CDB broadcasts results to all waiting instructions; the ROB executes out of order but commits in program order; a BTB caches predicted targets; a two-level predictor uses local or global history; register renaming solves WAR and WAW.

References: [HP6] Ch. 3 (3.3-3.9); [PH-RV] Ch. 4 (4.10); Tomasulo, "An Efficient Algorithm for Exploiting Multiple Arithmetic Units," IBM J. Res. Dev., 1967; Smith, "A Study of Branch Prediction Strategies," ISCA 1981; Kocher et al., "Spectre Attacks: Exploiting Speculative Execution," IEEE S&P 2019;