Computer architecture
Pipeline hazards and forwarding
Also called: data hazard · RAW hazard · stall · bubble · forwarding · load-use
Definition
A hazard is a situation where the next instruction cannot proceed on schedule. Data hazards happen when an instruction needs a result an earlier one has not finished producing.
Picture it
If add writes r1 and the very next instruction reads r1, the value is not in the register file yet. The pipeline can stall (insert bubbles) or forward the result straight from the ALU output to the next instruction's input. A load's value only exists after the memory stage, so a load followed immediately by a use still costs one bubble.
The maths
classic 5-stage, no forwarding: RAW on adjacent instructions → 2 bubbles with forwarding: ALU→ALU → 0 bubbles; load-use → 1 bubble
| Quantity | Unit | Symbol |
|---|---|---|
| clock cycles | cycle (count) | 1 |
Worked example
add r1, r2, r3 is followed by sub r4, r1, r5. How many total cycles do the two take on a 5-stage pipeline without forwarding, and with forwarding?
- No hazard: 5 + 1 = 6 cycles
- Without forwarding: 2 bubbles → 8 cycles
- With forwarding: 0 bubbles → 6 cycles
Answer8 cycles without forwarding, 6 with.
✓ The 2 numbers in this answer are recomputed from scratch by an automated test.
laundry line
Pipeline diagram
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| add r1, r2, r3 | IF | ID | EX | MEM | WB | |||||||
| sub r4, r1, r5 | IF | ID | ·· | ·· | EX | MEM | WB | |||||
| and r6, r4, r1 | IF | ·· | ·· | ID | ·· | ·· | EX | MEM | WB | |||
| or r7, r2, r3 | IF | ·· | ·· | ID | EX | MEM | WB |
Classic 5-stage in-order pipeline: register file written in the first half of WB and read in the second half of ID. A teaching model, not any specific processor.
Common misconceptions
- Misconception: Forwarding does not remove every stall; load-use still needs one.
- Misconception: Compilers can often reorder independent instructions to fill bubbles.