CPU PIPELINE

Five stages, one clock each, and the cycles hazards steal

L216 MINSYSTEMS & LOW-LEVEL
PREDICT

A 5-stage pipeline (fetch IF, decode ID, execute EX, memory MEM, write back WB) has full forwarding: a result can go straight from one instruction to the next without waiting for WB. It runs lw r1, 0(r0) followed immediately by add r2, r1, r1. How many stall cycles does the add pay?

A pipelined core splits each instruction into five stages: fetch (IF), decode and read registers (ID), execute (EX), memory (MEM), write back (WB). Five instructions overlap, so up to one finishes per cycle.

The overlap breaks when an instruction needs a register that an older instruction has not written yet. A core with no hazard detection reads the old value. A safe core stalls the younger instruction until the value is ready. Forwarding hands an ALU result straight to the next instruction, so most stalls disappear. A load is different: its data exists only after MEM, so the next instruction still waits one cycle. A taken branch that was predicted not-taken flushes the two instructions fetched behind it.

▶ SIMULATION
FRONT END
IF bubble
ID bubble
interlock + forwarding
REGISTER FILE
r1 = 0 · r2 = 0 · r3 = 0 · r4 = 0
stale reads: 0
BACK END
EX bubble
MEM bubble
WB bubble
diagramF D X M W = IF ID EX MEM WB · a repeated letter is a stall · x = flushed
clockcycle 0 · retired 0 · CPI —
lost cycles0 stall + 0 flush (0 mispredicts)
eventno program yet
✓ no instruction reads a stale register
SCHEDULE
01[CONFIG]core: no hazard detection, no forwarding
02[CONFIG]load program (4 instructions)
03[CPU]cycle 1: pipeline filling
04[CPU]cycle 2: pipeline filling
05[CPU]cycle 3: pipeline filling
06[CPU]cycle 4: STALE READ: add (#2) read r1 = 0, sequential order says 5
07[CPU]cycle 5: STALE READ: sub (#3) read r1 = 0, sequential order says 5
08[CPU]cycle 6: STALE READ: add (#4) read r2 = 0, sequential order says 10
09[CPU]cycle 7: #3 retires
10[CPU]cycle 8: #4 retires
Start of no hazard detection scenario. 10 steps.
Classic 5-stage RISC pipeline

IF   fetch instruction
ID   decode, read registers (second half of the cycle)
EX   ALU op, address calc, branch resolves here (this model)
MEM  load/store data
WB   write register (first half of the cycle)

No hazards: one instruction retires per cycle once full.
  cycles = n + 4, CPI -> 1 as n grows. Latency per instruction stays 5.

RAW hazard  (add r2, r1, r1 right after something that writes r1)
  no forwarding: wait until the producer is in WB   -> 2 stall cycles
  forwarding:    take the ALU result from EX/MEM    -> 0 stall cycles
  load-use:      load data exists only after MEM    -> 1 stall cycle

Hazard unit (forwarding on):
  if (EX.op == lw && EX.dst != r0 && EX.dst in ID.srcs) stall

Control hazard (predict not-taken, resolve in EX):
  taken branch -> flush IF and ID -> 2 lost cycles

Watch the invariant strip and the diagram rows. NO HAZARD DETECTION finishes in 8 cycles with 6 stale reads, so r4 ends at 0 instead of 15. STALL is correct but takes 14 cycles. FORWARDING is correct in 9: one load-use bubble. MISPREDICTED BRANCH stays correct and pays 2 flush cycles.

BREAK IT NOT YET

You are the compiler for the forwarding core. Chain A loads r1 and doubles it; chain B loads r3 and doubles it. Each emit reruns the program. Emit all four instructions so the core loses 2 cycles to load-use bubbles. Then CLEAR and find the order that loses none.

GOAL: all four emitted and 2 load-use stall cycles

FIX IT NOT YET

Write the hazard unit. next is the instruction in ID; prev[0] is in EX and prev[1] is in MEM (either may be null, a bubble). Each is \{ op, dst, srcs \}; dst is null for branches and r0 is hard-wired to 0. Return true if next must wait one cycle. The grader runs real programs through the pipeline with your unit, with and without forwarding: any stale read fails, and so does any stall the hardware did not need.

Mental model:

a pipeline is an assembly line that assumes each instruction is independent of the one in front. Every dependency is a place where that assumption fails, and the hardware either waits (a stall), shortcuts (forwarding), or guesses and throws work away (a flush). CPI above 1 is the bill for those three.

IN PRODUCTION

Hot loops in numeric code, parsers and interpreters. Symptoms show up as low IPC in perf stat, branch-miss counters, and code that speeds up when you unroll it, reorder loads, or make a branch predictable (sorting data before a branchy loop is the classic example).

IN INTERVIEWS

“Walk me through the 5-stage pipeline and its hazards.” Name data, control and structural hazards, explain forwarding and why a load-use hazard still costs a bubble, and say what a mispredict costs and how prediction and compiler scheduling hide it.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: MEMORY ORDERING →