Sub topic 8
Data hazards: RAW, WAR, and WAW
A data hazard appears when two instructions in the pipeline use the same register or memory location, and overlapping them would change the result the program defines. This chapter classifies the three kinds, shows which ones occur in the classic five-stage pipeline, and counts the stalls they cause.
It assumes you know the five stages (IF, ID, EX, MEM, WB) and what a pipeline stall is. It also assumes you can read simple register-style assembly.
Dependences and hazards
A dependence is a property of the program. Two instructions use the same storage, so their order matters. A hazard is what happens when the pipeline's timing would let that order be violated. A dependence only becomes a hazard if the two instructions are close enough together to overlap in the pipeline.
Take two instructions, I1 followed by I2 in program order. There are three ways they can conflict over a register.
| Hazard | Meaning | Dependence name | Pattern |
|---|---|---|---|
| RAW (read after write) | I2 reads a value that I1 writes | True (flow) dependence | I1 writes R, I2 reads R |
| WAR (write after read) | I2 writes a register that I1 reads | Anti-dependence | I1 reads R, I2 writes R |
| WAW (write after write) | I2 writes a register that I1 also writes | Output dependence | I1 writes R, I2 writes R |
Read after read (RAR) is not a hazard. Two instructions that only read the same register never disturb each other.
RAW is a true dependence because a real value flows from I1 to I2. WAR and WAW are name dependences. The instructions share only a register name, and no value passes between them. Giving one of them a different register removes the conflict.
RAW hazards in the five-stage pipeline
RAW is the hazard that matters in a simple in-order pipeline. Operands are read in ID, but results are written to the register file in WB, three stages later.
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in ID: RAW hazard on r1Assume the register file is written in the first half of a clock cycle and read in the second half. Then a register can be written and read correctly in the same cycle. Without forwarding, sub must wait in ID until add reaches WB.
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| add | IF | ID | EX | MEM | WB | |||
| sub | IF | ID stall | ID stall | ID | EX | MEM | WB |
sub stalls for 2 cycles. If the register file cannot write and read in the same cycle, sub must read in the cycle after WB, and the stall becomes 3 cycles. GATE questions usually state which assumption applies. If they do not, check whether the question mentions split-cycle register access.
The stall count depends on the distance between the two instructions. If other independent instructions sit between producer and consumer, they fill the gap.
- Adjacent instructions: 2 stalls (3 without split-cycle access).
- One instruction in between: 1 stall.
- Two instructions in between: no stall.
def raw_stalls(distance, forwarding, producer_is_load=False, split_cycle=True):
"""Stalls for a RAW hazard in a classic 5-stage pipeline.
distance: 1 means the consumer immediately follows the producer.
"""
if forwarding:
# Only a load followed closely by its consumer still stalls.
return max(0, 2 - distance) if producer_is_load else 0
base = 3 if split_cycle else 4
return max(0, base - distance)
for d in (1, 2, 3):
print(d,
raw_stalls(d, forwarding=False),
raw_stalls(d, forwarding=False, split_cycle=False),
raw_stalls(d, forwarding=True),
raw_stalls(d, forwarding=True, producer_is_load=True))
# 1 2 3 0 1
# 2 1 2 0 0
# 3 0 1 0 0The columns are: no forwarding with split-cycle access, no forwarding without it, forwarding with an ALU producer, and forwarding with a load producer.
Forwarding and the load-use case
With forwarding (bypassing), the ALU result of add is passed from the EX/MEM pipeline register straight to the EX stage of sub. No stall is needed for the example above. Later chapters cover forwarding in detail. The point here is that forwarding cures RAW hazards only when the value already exists at the time it is needed.
A load is the exception, because its value appears only at the end of MEM.
lw r1, 0(r2) # r1 is available only after MEM
add r3, r1, r4 # needs r1 at the start of EX: load-use hazardEven with forwarding, add stalls 1 cycle.
Why WAR and WAW rarely occur in the basic pipeline
Both hazards need a later instruction to write a register before an earlier instruction has finished with it. The classic five-stage in-order pipeline prevents this by its structure.
- All register reads happen early, in ID.
- All register writes happen late, in WB.
- Instructions pass through the stages in program order, and each stage handles one instruction per cycle.
For WAR, I1 reads in ID and I2 cannot write until its own WB, which is later than I1's ID. For WAW, I1 and I2 reach WB in program order, so the final value is the one I2 wrote.
When WAR and WAW do appear
They become real in three situations.
- Different execution latencies. If a floating-point divide takes many cycles and a later integer add takes one, the add can reach its write step before the divide does.
- Out-of-order execution. The hardware issues or completes instructions in an order different from program order.
- Early register writes. An instruction such as an auto-increment load writes its address register in an early stage. A following instruction could then write a register that an earlier instruction has not yet read.
Here is a WAW example:
div f1, f2, f3 # long latency, writes f1 late
add f1, f4, f5 # short latency, would write f1 firstIf add finishes first, the stale div result overwrites it later. A later instruction that reads f1 would then see the wrong value. A processor must either stall add's write until div has written, or discard div's write.
Because WAR and WAW are name dependences, register renaming removes them. The hardware maps each write to a fresh physical register, so the two instructions no longer share storage. The last chapter touches on this in the context of out-of-order execution.
Classifying hazards in a GATE problem
Take a sequence and compare each pair of instructions. For each pair (earlier I1, later I2), write down the destination register and source registers of each instruction. Then apply the flowchart.
Consider these three instructions:
I1: add r1, r2, r3
I2: sub r4, r1, r5
I3: mul r1, r6, r7- I1 and I2: I2 reads r1, which I1 writes. This is RAW.
- I2 and I3: I2 reads r1 and I3 writes r1. This is WAR. It cannot occur in the five-stage pipeline, because I2 reads r1 in ID before I3 reaches WB.
- I1 and I3: both write r1. This is WAW. It also cannot occur in the five-stage pipeline, because I1 writes before I3.
Two cautions for exam questions:
- Check the wording. "Which hazards can occur in this pipeline?" depends on the pipeline described, not only on the code.
- For stall counts, look at both the distance between the instructions and whether forwarding exists. A RAW pair three or more instructions apart causes no stall in the standard model.
Common errors
- Calling any shared register a RAW hazard. The direction matters: RAW needs the later instruction to read what the earlier one writes.
- Reporting stalls for WAR or WAW in a simple in-order five-stage pipeline. They occur there only if the question changes the stage in which registers are read or written.
- Forgetting the split-cycle assumption. It changes 3 stalls into 2.
- Assuming forwarding removes load-use stalls. It does not. One stall remains for an adjacent dependent instruction.
Exercise
Consider the instruction sequence I1: R1 = R2 + R3, I2: R4 = R1 * R5, I3: R1 = R6 + R7. Classify the hazard for the pairs (I1, I2), (I2, I3), and (I1, I3). Then say which can cause a stall in a five-stage in-order pipeline that reads registers in ID and writes them in WB.
Show answer
(I1, I2) is RAW, because I2 reads R1, which I1 writes. (I2, I3) is WAR, because I3 writes R1, which I2 reads. (I1, I3) is WAW, because both write R1. Only the RAW pair can cause a stall. I2 reads R1 in ID before I3 reaches WB, so the WAR pair is safe. I1 writes R1 before I3 does, so the WAW pair is safe.
Exercise
In a five-stage pipeline with no forwarding, the register file is written in the first half of a cycle and read in the second half. Instruction B uses a result produced by instruction A. How many stall cycles occur if B immediately follows A, and how many if one independent instruction sits between them?
Show answer
With B immediately after A, there are 2 stall cycles. With one independent instruction in between, there is 1 stall cycle. In general the stall count is 3 minus the distance between the instructions, with a minimum of 0, where adjacent instructions have distance 1.