CPU PIPELINE
Five stages, one clock each, and the cycles hazards steal
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.
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.
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
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.
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).
“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.