Sub topic 8
Data Hazards: RAW, WAR, and WAW
This chapter assumes that you know registers, basic assembly instructions, and the five-stage pipeline: IF, ID, EX, MEM, and WB. We also assume a single-issue, in-order pipeline unless stated otherwise.
What is a data hazard?
A data hazard occurs when overlapping instructions use the same register or memory location, and the order of those uses matters. The pipeline must preserve the result that a sequential, non-pipelined processor would produce.
Consider these instructions:
add r1, r2, r3
sub r4, r1, r5The sub instruction needs the value produced by add. If both instructions overlap, sub might read r1 before add writes the new value. This is a data hazard.
The three names describe the required ordering:
| Hazard | Meaning | Required ordering |
|---|---|---|
| RAW | Read after write | A write must happen before a later read |
| WAR | Write after read | An earlier read must happen before a later write |
| WAW | Write after write | An earlier write must happen before a later write |
RAW: read after write
RAW is a true data dependency. The first instruction produces a value, and the second instruction consumes it.
add r1, r2, r3
sub r4, r1, r5The sub instruction must read the new value of r1, not the old value. In a classic five-stage pipeline, add writes r1 during WB. Without forwarding, sub may need to wait in ID until the value is available.
Forwarding, also called bypassing, sends a result directly from a later pipeline stage to an earlier execution stage. It often removes RAW stalls for arithmetic instructions.
WAR: write after read
WAR is an anti-dependency. An earlier instruction must read a value before a later instruction overwrites that value.
sub r4, r1, r5
add r1, r2, r3The first instruction reads r1; the second instruction writes r1. If the second instruction writes too early, the first instruction could read the wrong value.
A basic in-order pipeline normally avoids WAR hazards because instructions read operands in program order. WAR hazards become possible when instructions execute out of order, or when reads and writes use different timing paths.
WAW: write after write
WAW is an output dependency. Two instructions write the same destination, so their writes must complete in program order.
mul r1, r2, r3
add r1, r4, r5The final value of r1 should be the result of add. If add finishes first but mul writes later, the older result could incorrectly overwrite the newer one.
In-order pipelines usually avoid WAW hazards because write-back occurs in instruction order. Out-of-order processors prevent them with techniques such as register renaming, which gives different physical registers to different logical results.
A systematic classification method
For each pair of instructions:
- List every register or memory location read by each instruction.
- List every location written by each instruction.
- Check whether the instructions share a location.
- Identify the order of the two operations: read-read, read-write, write-read, or write-write.
- Name the hazard only when the ordering can affect the result.
Exercise
Classify the hazard in this sequence: lw r1, 0(r2) followed by add r3, r1, r4. Is it RAW, WAR, or WAW?
Show answer
RAW. The load writes r1, and the add reads r1. The add depends on the value produced by the load.