5-Stage RISC Instruction Pipelining and Hazards
How overlapping IF, ID, EX, MEM, and WB increases instruction throughput, and how forwarding and branch prediction resolve data and control hazards.
1. Throughput vs. Latency in an Instruction Pipeline
In a non-pipelined processor, an instruction must finish all five steps — fetching, decoding, executing, accessing memory, and writing back its result — before the next instruction starts. If each step takes 1 ns, each instruction takes 5 ns.
Pipelining splits the datapath into five stages separated by pipeline registers so that five different instructions execute in parallel (one in each stage):
- IF (Instruction Fetch): Read the instruction at the Program Counter (
PC) from instruction memory and incrementPC. - ID (Instruction Decode & Register Read): Decode the opcode and read source registers (e.g.,
R1, R2) from the register file. - EX (Execute): The ALU performs arithmetic/logic operations, calculates memory addresses, or evaluates branch conditions.
- MEM (Memory Access):
LOADreads from data memory;STOREwrites to data memory. Other instructions pass through idle. - WB (Write Back): Write the result back into the destination register in the register file.
Click BASIC in the simulator and step through: pipelining does not reduce the latency of a single instruction (still 5 cycles), but once the pipeline is full, one instruction completes every cycle, increasing throughput up to 5×.
For N instructions on a 5-stage pipeline with zero stalls:
Total Cycles = 5 + (N − 1)
CPI (Cycles Per Instruction) = Total Cycles / N (approaches 1.0 as N grows)
2. Pipeline Hazards
A hazard is any situation that prevents the next instruction in the stream from executing in its designated clock cycle. There are three classes:
- Structural Hazard: Two instructions need the same hardware resource in the same cycle (solved in RISC CPUs by using separate Instruction and Data caches and dual-ported register files).
- Data Hazard (RAW — Read After Write): An instruction needs to read a register that a previous instruction in the pipeline has not yet written back in
WB. - Control Hazard (Branch Hazard): The CPU must fetch the next instruction in
IFbefore a branch instruction (BEQ,JMP) has resolved its condition and target inEX.
3. Resolving Data Hazards: Stalls vs. Forwarding (Bypassing)
Load the DATA HAZARD preset:
LOAD R1, [R2] ; Loads R1 in MEM (end of cycle 4), writes R1 in WB (cycle 5)
ADD R3, R1, R4 ; Needs R1 at the start of EX (cycle 3)
SUB R5, R3, R1 ; Needs R3 from ADD
Without Forwarding (Stalling): Set DATA FORWARDING to Disabled and click RUN ALL. Because ADD cannot proceed into EX until LOAD reaches WB, the hazard unit freezes IF and ID and injects pipeline bubbles (NOPs) into EX. Every stall cycle increases CPI.
With Data Forwarding (Bypassing): Notice that an ALU instruction like ADD R3, R1, R4 finishes computing R3 at the end of EX — two cycles before WB. Forwarding adds multiplexer bypass paths from the EX/MEM and MEM/WB pipeline registers directly back to the ALU inputs. That eliminates stalls between back-to-back ALU instructions.
Why the Load-Use Hazard still requires 1 stall: Switch DATA FORWARDING to Enabled and click RUN ALL. The hazard between ADD and SUB is forwarded with zero stalls, but one bubble remains between LOAD and ADD. Why? Because LOAD R1, [R2] only receives its value from memory at the end of MEM (cycle 4), whereas ADD needed R1 at the beginning of EX (cycle 3). Forwarding cannot send data backward in time, so a load-use hazard always costs 1 stall cycle (unless the compiler reorders an independent instruction in between).
4. Resolving Control Hazards: Branch Prediction & Flushing
Load the CONTROL HAZARD preset:
MOV R1, 1
MOV R2, 1
BEQ R1, R2, TARGET ; Branch if R1 == R2 (always taken here)
ADD R3, R1, R2 ; Fall-through instruction (should be skipped!)
TARGET:
SUB R4, R1, R2
BEQ evaluates whether R1 == R2 in the EX stage (2 cycles after it was fetched). In the meantime, the processor predicts the outcome:
- Predict Not Taken: Keeps fetching sequential instructions (
ADD R3, R1, R2). WhenBEQresolves as Taken inEX, the prediction was wrong — the CPU flushes the instructions inIFandID(turning them into bubbles) and setsPC = TARGET. - Predict Taken: Immediately redirects
PCtoTARGETduring fetch. Because this branch is taken, zero cycles are wasted on flushes.
Exercises
1. Load DATA HAZARD and run it with Data Forwarding Disabled vs. Enabled. How many total cycles and stalls occur in each mode, and why does 1 stall remain when Forwarding is Enabled?
Show solution
ADD (waiting on R1) and SUB (waiting on R3) stall for 2 cycles each (4 stalls total, 11 cycles). With Forwarding Enabled, only the load-use hazard between LOAD R1 and ADD stalls for 1 cycle (LOAD produces data at the end of MEM, which is forwarded to ADD's EX on the next cycle), while ADD → SUB forwards directly from EX/MEM with 0 stalls (8 cycles total).2. How many clock cycles does an ideal 5-stage pipeline with zero hazards take to execute 20 instructions, and what is its CPI?
Show solution
Cycles = 5 + (20 − 1) = 24 cycles. CPI = 24 / 20 = 1.20 (compared to 20 × 5 = 100 cycles on a non-pipelined processor).Quiz
How many cycles does a 5-stage pipeline require to execute 10 instructions if no hazards occur?
Total cycles = k + (N − 1) = 5 + (10 − 1) = 14 cycles.
Why does a LOAD immediately followed by an ADD that uses the loaded register still require a 1-cycle stall even when Data Forwarding is enabled?
This is the classic load-use data hazard: MEM/WB can forward to EX only after a 1-cycle bubble delays the dependent instruction.
In which pipeline stage are source registers read from the register file?
Source registers are read in ID; destination registers are written in WB.
What action does the pipeline take when a branch in the EX stage was mispredicted?
Flushing turns the speculatively fetched instructions in IF and ID into no-op bubbles before they can modify architectural state.