Sub topic 10
Operand Forwarding: Removing Most Data Stalls
Prerequisites and the forwarding idea
This chapter assumes that you know the five classic pipeline stages: instruction fetch (IF), instruction decode and register read (ID), execute (EX), memory access (MEM), and write-back (WB). It also assumes that you can read simple assembly instructions and draw a cycle-by-cycle pipeline diagram.
A data hazard occurs when one instruction needs a value produced by an earlier instruction. The most common case is a read-after-write (RAW) hazard. The later instruction reads a register before the earlier instruction has written the new value into that register.
Consider this sequence:
add r1, r2, r3 # r1 = r2 + r3
sub r4, r1, r5 # r4 = r1 - r5The sub needs the new value of r1. Waiting until the add reaches WB would create unnecessary delay. The result of the add is already available at the end of its EX stage.
Operand forwarding, also called bypassing, sends a computed value directly from an internal pipeline register to the input of a later execution stage. The value does not need to travel through the register file first.
How forwarding works
A forwarding unit compares the source registers of the instruction in EX with the destination registers of older instructions. If a match exists, a multiplexer selects the newer result instead of the value read earlier from the register file.
For an ALU instruction, the result can usually be forwarded from the EX/MEM pipeline register to the next instruction's ALU input. A result farther along in the pipeline can also be forwarded from MEM/WB.
With forwarding, the timing can be represented as follows:
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| `add` | IF | ID | EX | MEM | WB | |
| `sub` | IF | ID | EX with forwarded r1 | MEM | WB |
The sub does not stall. Its EX stage receives the add result through the bypass path.
What forwarding cannot solve
Forwarding cannot provide a value before the producing instruction has generated it. A load instruction produces its value only after the MEM stage:
lw r1, 0(r2) # r1 becomes available after MEM
add r3, r1, r4 # needs r1 at the start of EXIn a basic five-stage pipeline, this load-use hazard requires one stall cycle. After the stall, the loaded value can be forwarded to the add.
| Dependency | Forwarding result |
|---|---|
| ALU result followed by dependent ALU instruction | Usually zero stalls |
| Load followed immediately by dependent instruction | Usually one stall |
| Result needed before it is produced | Forwarding cannot remove the delay |
Forwarding also does not resolve structural hazards, such as two instructions requiring one memory port at the same time. It does not resolve control hazards caused by branches.
A systematic method
For each dependency:
- Mark the producer's result-availability stage.
- Mark the consumer's required-input stage.
- Check the available forwarding paths.
- Insert a stall only when the value arrives too late.
- Recheck later instructions because one stall shifts their stages.
Exercise
In a five-stage pipeline with EX-to-EX forwarding, how many stall cycles are needed for add r1, r2, r3 followed immediately by sub r4, r1, r5?
Show answer
Zero stall cycles. The add result is forwarded from the EX/MEM pipeline register to the sub instruction's EX input.