Course contentsShow
← Computer Science
Lesson 1167 of 2,87227. Pipelining and PerformanceFree lesson

Data Hazards: Write-After-Write (WAW)

Understanding WAW hazards where instructions write to the same register out of order, corrupting final value.

Data Hazards: Write-After-Write (WAW)

You'll learn how a Write-After-Write hazard occurs when two instructions try to write the same register in the wrong order, potentially corrupting your data.

What Is a WAW Hazard?

A Write-After-Write (WAW) hazard happens when two instructions in the pipeline both write to the same destination register, and due to pipeline overlap, they might complete out of order. If the later instruction finishes before the earlier one, the register ends up with the wrong final value.

Unlike RAW hazards (where you need to read before the write happens), WAW hazards are about which write wins. The program expects the second instruction's result to be final, but if stages complete out of sequence, the first instruction might overwrite it.

When Does This Happen?

Consider this sequence:

I1: ADD R1, R2, R3    # R1 = R2 + R3
I2: SUB R1, R4, R5    # R1 = R4 - R5

Both instructions write to R1. In a typical five-stage pipeline with uniform timing, these finish in order naturally. But with multi-cycle instructions or out-of-order execution, I1 might take longer (maybe a slow multiply) and finish after I2. Now R1 holds the ADD result instead of the SUB result—wrong!

The Real-World Analogy

Imagine two chefs updating the same recipe card. Chef A starts writing first (the old version), Chef B writes second (the new version). If Chef A is slow and finishes writing after Chef B, the card ends up with the old recipe—not what you wanted!

Prevention

WAW hazards are resolved by:

  • Tracking instruction completion order
  • Stalling or buffering writes until earlier instructions finish
  • Using register renaming (future topic)

Key Takeaway: WAW hazards corrupt data when a later write completes before an earlier write to the same register, leaving the wrong value behind.