Data Hazards: Write-After-Read (WAR)
What You'll Learn
You'll discover how Write-After-Read (WAR) hazards occur when an instruction tries to write to a register before an earlier instruction finishes reading from it.
Understanding WAR Hazards
A Write-After-Read (WAR) hazard happens when a later instruction wants to write to a register that an earlier instruction still needs to read from. This becomes problematic in processors that execute instructions out of order to improve performance.
The Problem Scenario
Consider this sequence:
Instruction 1: ADD R3, R1, R2 // Reads from R1 and R2
Instruction 2: MOV R1, #10 // Writes to R1
In a simple in-order pipeline (like the classic five-stage pipeline you've learned), Instruction 1 always completes its read from R1 before Instruction 2 writes to it. No problem!
But in out-of-order execution, if Instruction 2 somehow completes its write to R1 before Instruction 1 reads it, Instruction 1 gets the wrong value. The new value overwrites the old one too early.
Why It's Called "Anti-Dependency"
WAR hazards are also called anti-dependencies because they reverse the normal flow: a later instruction constrains an earlier one. It's like you need to read a library book (Instruction 1), but someone returns a different edition with the same title (Instruction 2) before you finish reading the original.
When Does This Matter?
WAR hazards primarily concern advanced processors that:
- Execute instructions out of their original order
- Use techniques like register renaming to boost parallelism
In the classic five-stage pipeline you've studied, WAR hazards don't actually occur because stages proceed in strict order.
Key Takeaway: Write-After-Read (WAR) hazards occur when out-of-order execution allows a later write to overwrite a register before an earlier instruction reads it, breaking program correctness.