Sub topic 11
The Load-Use Hazard: The Stall Forwarding Cannot Remove
Assume you already know the five pipeline stages: instruction fetch (IF), instruction decode and register read (ID), execute (EX), memory access (MEM), and write-back (WB). Also assume a five-stage pipeline with one instruction entering each cycle and operand forwarding between stages.
A load-use hazard occurs when an instruction immediately uses the value loaded by the preceding instruction. Forwarding removes many data hazards, but it cannot remove this one completely.
Why a load-use hazard occurs
Consider this sequence:
lw r1, 0(r2) # Load memory[r2 + 0] into r1
add r3, r1, r4 # Use r1 in the next instructionThe add instruction needs r1 when it begins its EX stage. However, the lw instruction produces the loaded value only at the end of its MEM stage.
The timing is:
| Cycle | `lw` | `add` |
|---|---|---|
| 1 | IF | |
| 2 | ID | IF |
| 3 | EX | ID |
| 4 | MEM | EX |
| 5 | WB | MEM |
During cycle 4, the add instruction is already entering EX. The load's memory operation also finishes during cycle 4. Therefore, the value is not available early enough for the add instruction's EX operation.
The processor inserts one stall cycle:
| Cycle | `lw` | `add` | Next instruction |
|---|---|---|---|
| 1 | IF | ||
| 2 | ID | IF | |
| 3 | EX | ID | IF |
| 4 | MEM | Stall in ID | |
| 5 | WB | EX | IF |
| 6 | MEM | ID |
The stall keeps add in ID for one extra cycle and inserts a bubble, which is an empty pipeline slot, into EX.
Why forwarding is still useful
Without forwarding, the dependent instruction might need to wait until the load writes r1 in WB. With forwarding, the value can travel directly from the load's later pipeline stage to the dependent instruction's EX stage.
That path saves several cycles, but the value still becomes available only after memory access. For an immediately following instruction, one cycle is too soon. This is why the usual five-stage pipeline requires one stall for a load-use pair.
Detecting and solving the hazard
A typical hazard unit checks whether:
- the instruction in EX is a load,
- its destination register is the same as a source register of the instruction in ID.
If both conditions hold, the processor usually:
- Prevents the PC from advancing.
- Prevents the IF/ID pipeline register from changing.
- Inserts a bubble into the ID/EX pipeline register.
The dependent instruction then reaches EX one cycle later, when the loaded value can be forwarded.
A compiler can sometimes avoid the stall by placing an independent instruction between the load and its user:
lw r1, 0(r2)
add r6, r7, r8 # Independent instruction
add r3, r1, r4 # r1 is now availableThis technique is called instruction scheduling. It works only when a suitable independent instruction exists.
Recognizing the pattern
The important distinction is the distance between the load and its user:
| Sequence | Typical result with forwarding |
|---|---|
| `lw r1, 0(r2)` followed immediately by `add r3, r1, r4` | One stall |
| `lw r1, 0(r2)` followed by one independent instruction, then use of `r1` | No stall |
| An arithmetic instruction producing `r1`, followed immediately by its user | Usually no stall |
The last case works because an arithmetic result is produced at the end of EX, early enough for forwarding to the next instruction's EX stage.
Exercise
In a five-stage pipeline with forwarding, how many stall cycles are normally required for lw r1, 0(r2) followed immediately by sub r4, r1, r5?
Show answer
One stall cycle. The load value is available only after the MEM stage, while sub needs it at the start of EX.