← 🚰 Instruction Pipelining & Hazards
Subtopic 4 of 8

2.4Data hazards

The idea

A data hazard is a clash between instructions that use the same register in an order that could give the wrong result if they overlapped.

Think of it like this: A shared notebook: reading a page before your colleague has written it (RAW), or writing over a page before they have read it (WAR).

Why it exists

The pipeline overlaps instructions but the program must behave as if they ran one after another.

How it works

  1. RAW (read after write): a later instruction reads what an earlier one writes. The only true dependency; the common one.
  2. WAR (write after read): later writes what earlier reads. Matters with out-of-order execution.
  3. WAW: both write the same register. Matters with out-of-order completion.

Worked example: I1: ADD R1,R2,R3 I2: SUB R4,R1,R5

I2 reads R1, which I1 writes: RAW.

Now play with it

  • →Pick I1 and I2: RAW. Pick I1 and I3: WAW. Pick I2 and I3: WAR. Pick I3 and I4: both RAW and WAR.
I1: ADD R1, R2, R3
I2: SUB R4, R1, R5
I3: MUL R1, R6, R7
I4: AND R6, R4, R1
RAW FOUND
later reads R1, which earlier writes (true dependency)
WAR none
later writes R4, which earlier reads (anti dependency)
WAW none
both write R1 (output dependency)

Check yourself

I1: ADD R1,R2,R3 I2: SUB R4,R1,R5. Which hazard exists between them?

Remember: RAW is the real one; WAR and WAW are name clashes.
Exam notes (the dense version)

RAW (true dependency, read after write), the common one. WAR (anti) and WAW (output) arise with out-of-order completion / different write stages.

Previous year questions: Instruction Pipelining & Hazards (15)

PYQs are grouped by topic, so this is the full set for Instruction Pipelining & Hazards; try the ones that match this subtopic. Open all 15 on the topic page.