Pipelining & Hazards
The classic 5-stage pipeline, structural, data and control hazards, forwarding, stalls, branch prediction — with an interactive pipeline simulator.
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
| Stage | Name | Work |
|---|---|---|
| IF | Instruction fetch | Read the instruction at PC; PC += 4 |
| ID | Decode / register read | Decode; read source registers |
| EX | Execute | ALU operation or address calculation |
| MEM | Memory | Load or store (if any) |
| WB | Write back | Write the result to the register file |
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.
| instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| lw x1, 0(x2) | IF | ID | EX | MEM | WB | |||||
| add x3, x1, x4 | IF | stall | ID | EX | MEM | WB | ||||
| sub x5, x3, x6 | IF | IF | ID | EX | MEM | WB | ||||
| and x7, x8, x9 | IF | ID | EX | MEM | WB | |||||
| or x10, x5, x7 | IF | ID | EX | MEM | WB |
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:
add x1, x2, x3 # x1 written in WB (cycle 5)
sub x4, x1, x5 # needs x1 in ID (cycle 3)!| Fix | How | Cost |
|---|---|---|
| Stall (bubble) | Hold the dependent instruction until the value is written | Lost cycles |
| Forwarding (bypassing) | Route the result from the EX/MEM pipeline registers straight back to the ALU input | Extra MUXes; removes most stalls |
| Compiler scheduling | Reorder independent instructions into the gap | Free 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).
| Strategy | Idea |
|---|---|
| Stall | Wait until the branch resolves — simple, slow |
| Predict not-taken | Keep fetching sequentially; flush if wrong |
| Dynamic prediction | 2-bit saturating counters, branch history, TAGE predictors: over 95% accuracy |
| Branch target buffer | Cache 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.
Real CPI
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).
- 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.
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.