Skip to content
Tactiverge

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
SI units used in this entry
QuantityUnitSymbol
clock cyclescycle (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?

  1. No hazard: 5 + 1 = 6 cycles
  2. Without forwarding: 2 bubbles → 8 cycles
  3. 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

Stage occupied by each instruction in each clock cycle
Instruction123456789101112
add r1, r2, r3IFIDEXMEMWB
sub r4, r1, r5IFID····EXMEMWB
and r6, r4, r1IF····ID····EXMEMWB
or r7, r2, r3IF····IDEXMEMWB
12 cycles for 4 instructions, with 4 stall bubbles (shown as ··).

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.