Pipelining & Hazards

The classic 5-stage pipeline, structural, data and control hazards, forwarding, stalls, branch prediction — with an interactive pipeline simulator.

Advanced 4 min readLesson 9 of 15 Interview Q&A

Pipelining overlaps instructions like an assembly line: while one instruction is being executed, the next is being decoded and the one after that fetched. Each instruction still takes the same time (latency), but one finishes every cycle (throughput).

The classic 5-stage pipeline

StageNameWork
IFInstruction fetchRead the instruction at PC; PC += 4
IDDecode / register readDecode; read source registers
EXExecuteALU operation or address calculation
MEMMemoryLoad or store (if any)
WBWrite backWrite the result to the register file
1234567i1i2i3IFIDEXMEMWBIFIDEXMEMWBIFIDEXMEMWB
Three instructions finish in 7 cycles instead of 15.

With k stages and n instructions, time ≈ (k + n − 1) cycles versus n × k unpipelined. The ideal speed-up approaches k, but pipeline registers add overhead and stages are never perfectly balanced.

Try it

Toggle forwarding and watch how many cycles the program needs and where the stalls (bubbles) appear.

Interactive5-stage RISC pipeline
instruction12345678910
lw x1, 0(x2)IFIDEXMEMWB
add x3, x1, x4IFstallIDEXMEMWB
sub x5, x3, x6IFIFIDEXMEMWB
and x7, x8, x9IFIDEXMEMWB
or x10, x5, x7IFIDEXMEMWB
IFIDEXMEMWBstall
Cycle 1. Total 10 cycles (ideal 9) → 1 stall cycle. With forwarding only the load-use hazard (lw → add) costs a bubble.
1/10

Hazards

A hazard is anything that prevents the next instruction from starting in the next cycle.

1. Structural hazards

Two instructions need the same hardware in the same cycle, for example a single memory used for both instruction fetch and data access. The fix is to duplicate the resource: separate L1 instruction and data caches, multi-ported register files.

2. Data hazards

An instruction needs a result that hasn't been written back yet:

Assembly
add  x1, x2, x3     # x1 written in WB (cycle 5)
sub  x4, x1, x5     # needs x1 in ID (cycle 3)!
FixHowCost
Stall (bubble)Hold the dependent instruction until the value is writtenLost cycles
Forwarding (bypassing)Route the result from the EX/MEM pipeline registers straight back to the ALU inputExtra MUXes; removes most stalls
Compiler schedulingReorder independent instructions into the gapFree at runtime

3. Control hazards

After a branch, which instruction should be fetched next? The answer isn't known until the branch is resolved in EX (or ID with extra hardware).

StrategyIdea
StallWait until the branch resolves — simple, slow
Predict not-takenKeep fetching sequentially; flush if wrong
Dynamic prediction2-bit saturating counters, branch history, TAGE predictors: over 95% accuracy
Branch target bufferCache of branch address → target so the jump starts in IF
Delayed branch (old MIPS)The instruction after a branch always executes; obsolete

2-bit counter: states strongly/weakly taken/not-taken. It takes two wrong guesses in a row to flip the prediction, so a loop branch mispredicts only once per loop exit instead of twice.

takentakentakenstrong NTweak NTweak Tstrong T
Taken moves right, not-taken moves left.

Real CPI

CPI=1+stall cycles per instruction (data)+branch frequency×mispredict rate×penalty\text{CPI} = 1 + \text{stall cycles per instruction (data)} + \text{branch frequency} \times \text{mispredict rate} \times \text{penalty}

Example: 20% branches, 5% mispredicted, 15-cycle penalty → 0.2 × 0.05 × 15 = 0.15 extra CPI just from branches.

Deeper and wider

  • Deeper pipelines (Pentium 4 had 20–31 stages) allow a higher clock but make mispredictions and hazards more expensive. Modern cores settle around 14–20 stages.
  • Superscalar pipelines issue several instructions per cycle (CPI below 1, so we talk about IPC above 1).
  • Out-of-order execution finds independent work further ahead in the instruction stream to fill stalls (CPU architecture).
Key takeaways
  • Pipelining improves throughput, not single-instruction latency.
  • 5 stages: IF, ID, EX, MEM, WB; one instruction completes per cycle ideally.
  • Hazards: structural (resource conflicts), data (dependencies), control (branches).
  • Forwarding removes most data stalls; load-use still costs one cycle.
  • Branch prediction (2-bit counters and better) keeps the pipeline full.
Interview questions
QWhy doesn't a 10-stage pipeline give a 10× speed-up?medium

Stages aren't perfectly balanced (the clock fits the slowest stage), pipeline registers add delay, and hazards cause stalls and flushes. Filling and draining the pipeline also costs k − 1 cycles.

QWhat is forwarding?easy

Sending an ALU result directly from a later pipeline register to the ALU input of a dependent instruction, instead of waiting for it to be written to and read from the register file.