Sub topic 11
The load-use hazard: the stall forwarding cannot remove
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), the meaning of RAW hazards, and how operand forwarding moves a result from a pipeline register to the ALU input. It covers why one case of RAW still costs a cycle after forwarding, how to count the stall, and how to compute its effect on CPI.
Why forwarding falls short for loads
Forwarding works by routing a value from a pipeline register to the input of a later stage. It only helps if the value already exists when that later stage needs it.
For an ALU instruction, the result exists at the end of EX. A dependent instruction directly behind it needs the value at the start of its own EX, which is one cycle later. The EX/MEM register holds the value at that moment, so the forward arrives in time.
For a load, the value comes from the data memory. It exists only at the end of MEM, and it is held in the MEM/WB register after that. A dependent instruction directly behind the load needs the value at the start of its EX. That EX would happen in the same cycle as the load's MEM. The data is not yet available, and no wire can deliver a value that has not been read.
lw r1, 0(r2) # r1 is available only after MEM
add r3, r1, r4 # needs r1 at the start of EX: load-use hazardA load-use hazard is a RAW hazard where the producer is a load and the consumer is the instruction that needs the loaded value for its EX stage. It is the case that forwarding cannot fully remove.
Timeline of the stall
Label cycles 1 to 8. The load enters IF in cycle 1.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| lw r1, 0(r2) | IF | ID | EX | MEM | WB | |||
| add r3, r1, r4 | IF | ID | stall | EX | MEM | WB |
The add reaches ID in cycle 3. Without a stall it would execute in cycle 4, the same cycle in which the load is in MEM. The hardware holds the add in ID for one cycle (cycle 4). In cycle 5 the add enters EX. The load has finished MEM at the end of cycle 4, so its value sits in the MEM/WB register, and a forwarding path from MEM/WB to the ALU input delivers it.
In the stall cycle, a bubble (a no-operation that does nothing) travels down the pipeline in place of the add. The instruction behind the add is also held in IF, so the program order is preserved.
Stall counts: with and without forwarding
The stall count depends on the hardware, so always read the problem's assumptions first.
| Producer / consumer | No forwarding (write first half, read second half) | Forwarding |
|---|---|---|
| ALU followed by dependent ALU, adjacent | 2 stalls | 0 stalls |
| Load followed by dependent ALU, adjacent | 2 stalls | 1 stall |
| Load, one unrelated instruction, then dependent ALU | 1 stall | 0 stalls |
| Load, two unrelated instructions, then dependent ALU | 0 stalls | 0 stalls |
Without forwarding, the consumer must read the register file after the producer's WB. With the register file written in the first half of a cycle and read in the second half, the consumer's ID can share a cycle with the producer's WB. That gives 2 stalls for an adjacent pair, whether the producer is a load or an ALU instruction.
With forwarding, each instruction placed between the load and its consumer hides one cycle of the delay. The general rule for a load and a dependent consumer with d instructions in between is:
stalls = max(0, 1 − d)
So the penalty is 1 for an adjacent consumer and 0 once at least one other instruction separates them.
Variations you will meet
- Store as consumer. In
lw r1, 0(r2)followed bysw r1, 4(r3), the store needs r1 as data to write in MEM, not at EX. The load's value is in MEM/WB at the start of the store's MEM, so a forward from MEM/WB to the memory data input works with no stall. This holds only when r1 is the stored data. If r1 were the base address register of the store, the address is needed in EX, and the stall returns. - Branch as consumer. If a branch compares in ID (an early-branch design), a load result needed by the branch causes a longer stall, because the value arrives later than ID. A branch that compares in EX behaves like an ALU consumer, with the 1-cycle load-use stall.
- Consumer two instructions after a load. The value is in MEM/WB at the start of the consumer's EX, and the forward from MEM/WB delivers it. No stall.
- Dependent load pair, such as
lw r1, 0(r2)followed bylw r3, 0(r1). The second needs r1 as an address in EX, so it stalls 1 cycle.
Detection and the compiler's role
The pipeline detects the hazard in the ID stage. The condition is: the instruction in EX is a load (a MemRead signal is set), and its destination register equals a source register of the instruction in ID. When true, the control logic does three things:
- Zeroes the control signals going into ID/EX, which creates the bubble.
- Keeps the PC unchanged, so the same fetch repeats.
- Keeps the IF/ID register unchanged, so the dependent instruction stays in ID.
A compiler can avoid the stall by instruction scheduling. It moves an independent instruction into the slot after the load.
# Before: 1 stall
lw r1, 0(r2)
add r3, r1, r4
sub r5, r6, r7
# After: 0 stalls (sub is independent of the load)
lw r1, 0(r2)
sub r5, r6, r7
add r3, r1, r4Some older designs expose a load delay slot: the instruction after a load must not use the loaded register, and the compiler fills the slot or inserts a nop. Questions that say "load delay slot" are asking about this software-managed variant.
Effect on CPI
Each stall adds to the average cycles per instruction. If a fraction f of instructions are loads followed immediately by a dependent instruction, the effective CPI is 1 + f × 1.
def effective_cpi(base_cpi, stall_events):
# stall_events: list of (frequency per instruction, stall cycles per event)
return base_cpi + sum(freq * penalty for freq, penalty in stall_events)
# 25% of instructions are loads, and 40% of those are followed
# immediately by a dependent instruction: 0.25 * 0.40 = 0.10
cpi = effective_cpi(1.0, [(0.25 * 0.40, 1)])
print(round(cpi, 2)) # 1.1
print(round(5 / cpi, 2)) # 4.55 speedup over an unpipelined 5-cycle machineThe key step is to multiply the fraction of loads by the fraction of those loads that trigger the stall. Many students forget the second factor and overcount.
Common errors
- Saying forwarding removes all RAW stalls. It removes ALU-to-ALU stalls only. A load-use pair keeps 1 stall.
- Using 2 stalls for the load-use case with forwarding. Two is the no-forwarding count.
- Counting a stall when a load and its consumer are separated by an unrelated instruction. With forwarding that costs nothing.
- Forgetting that the stall also delays everything behind the consumer by one cycle. Total time rises by the number of stall cycles, not more.
- Ignoring which operand is dependent. A store's data operand is forwarded without a stall, while its address operand is not.
Worked problem
A five-stage pipeline has full forwarding. The sequence is:
lw r1, 0(r2)
add r3, r1, r4
lw r5, 8(r3)
sub r6, r5, r1Instruction 2 depends on the load in instruction 1 and is adjacent, so 1 stall. Instruction 3 depends on instruction 2 (an ALU result), so forwarding from EX/MEM gives 0 stalls. Instruction 4 depends on the load in instruction 3 and is adjacent, so 1 stall. Total stalls = 2.
Total cycles for 4 instructions in 5 stages with no stalls would be 5 + 4 − 1 = 8. Adding 2 stalls gives 10 cycles.
Exercise
A five-stage pipeline (IF, ID, EX, MEM, WB) has full forwarding. Of all instructions, 30% are loads, and 50% of loads are immediately followed by an instruction that uses the loaded value. Branches and structural hazards add no stalls. What is the effective CPI, and what is the speedup over an unpipelined machine with CPI 5 at the same clock?
Show answer
The stall frequency per instruction is 0.30 × 0.50 = 0.15, each costing 1 cycle. Effective CPI = 1 + 0.15 = 1.15. Speedup = 5 / 1.15 ≈ 4.35.
Exercise
With forwarding, how many stall cycles occur in this sequence? lw r1, 0(r2); sw r1, 4(r3); lw r4, 0(r1); add r5, r4, r1
Show answer
The sw uses r1 as store data, which is needed in MEM and is forwarded from MEM/WB with no stall. The second lw uses r1 as its address in EX, but it is two instructions after the first lw, so MEM/WB forwarding delivers it with no stall. The add uses r4, which comes from the adjacent lw, so it stalls 1 cycle. The add also reads r1, which is long since available. Total = 1 stall.