Sub topic 10
Operand forwarding: removing most data stalls
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), pipeline registers, and RAW hazards. It also assumes the usual GATE convention: the register file is written in the first half of a cycle and read in the second half.
The problem forwarding solves
Take a RAW (read after write) dependency between two adjacent instructions:
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in ID: RAW hazard on r1Without forwarding, sub cannot read r1 until add has written it back. add is in WB in cycle 5, so sub must be in ID in cycle 5. Normally sub would be in ID in cycle 3, so it stalls 2 cycles.
The stall is unnecessary. add computes r1 at the end of its EX stage, in cycle 3. The value already sits in a pipeline register, but nothing connects it back to the ALU input. Operand forwarding (also called bypassing) adds those connections.
How forwarding works
Forwarding adds wires and multiplexers (muxes). A mux in front of each ALU input chooses between three sources:
- the normal operand read from the register file in ID (carried in the ID/EX register)
- the ALU result held in the EX/MEM register (from the instruction one ahead)
- the value held in the MEM/WB register (from the instruction two ahead)
"Distance" is how many instructions separate the producer from the consumer. The table shows what happens at each distance for an ALU producer.
| Distance | Where the value is when the consumer is in EX | Mechanism |
|---|---|---|
| 1 (adjacent) | EX/MEM register | Forward from EX/MEM |
| 2 | MEM/WB register | Forward from MEM/WB |
| 3 | Already written to the register file | Write in the first half, read in the second half |
| 4 or more | In the register file | Ordinary read |
The control logic is a comparison of register numbers. The EX/MEM forward fires when all of these hold:
- The instruction in EX/MEM writes a register.
- Its destination is not register 0, if the ISA has a hardwired zero register.
- Its destination equals the source register of the instruction in EX.
The MEM/WB forward uses the same test. If both match, the EX/MEM path wins, because it holds the more recent write.
Timeline with forwarding
For add r1,r2,r3 followed by sub r4,r1,r5:
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| add | IF | ID | EX | MEM | WB | |
| sub | IF | ID | EX | MEM | WB |
add produces r1 at the end of cycle 3. sub needs it at the start of cycle 4. The EX/MEM register supplies it, so there is no stall. The rule is that a consumer needs a value at the start of its EX, and the producer must have it by the end of the previous cycle.
Counting stalls: a small simulator
The rules above reduce to one formula per source operand. Let ID_j be the cycle in which the producer is in ID.
- Without forwarding, the consumer's ID cycle must be at least
ID_j + 3, the producer's WB cycle. - With forwarding from an ALU producer, the consumer's ID must be at least
ID_j + 1. This is always met in an in-order pipeline. - With forwarding from a load, the consumer's ID must be at least
ID_j + 2.
def count_stalls(prog, forwarding):
# prog: list of (opcode, dest, [sources]); in-order five-stage pipeline.
# Instruction i is ideally in ID at cycle i + 2.
id_cycle = []
for i, (_, _, srcs) in enumerate(prog):
t = id_cycle[-1] + 1 if id_cycle else 2
for s in srcs:
# find the most recent earlier writer of s
for j in range(i - 1, -1, -1):
pop, pdest, _ = prog[j]
if pdest == s:
if forwarding:
need = id_cycle[j] + (2 if pop == "lw" else 1)
else:
need = id_cycle[j] + 3
t = max(t, need)
break
id_cycle.append(t)
return id_cycle[-1] - (len(prog) + 1)
prog = [
("lw", "r1", ["r2"]),
("add", "r3", ["r1", "r4"]),
("sub", "r5", ["r3", "r6"]),
("or", "r7", ["r3", "r1"]),
]
print(count_stalls(prog, False), count_stalls(prog, True)) # 4 1Without forwarding, add stalls 2 cycles waiting for the load and sub stalls 2 more waiting for add. With forwarding, only the load-use stall between lw and add remains.
What forwarding cannot fix
A load produces its value at the end of MEM, not EX. The consumer needs it at the start of the next EX. If the consumer is adjacent, that moment is the same cycle in which the load is still in 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 must wait 1 cycle. The pipeline inserts a bubble, then forwards from MEM/WB. The next chapter covers this load-use hazard in detail. The takeaway here is that forwarding removes stalls only when the value already exists somewhere in the pipeline.
Gotchas
- Branches resolved in ID. If the branch compares registers in ID, the operands are needed one stage earlier. An ALU instruction followed by a dependent branch then stalls 1 cycle even with forwarding. A load followed by a dependent branch stalls 2 cycles.
- Stores. A store needs its data register only in MEM. Forwarding a loaded value from MEM/WB into the store's memory input lets
lwfollowed by a dependentswrun with no stall. Its address register is needed in EX, so it follows the normal rules. - WAR and WAW. Forwarding addresses RAW only. In the in-order five-stage pipeline, WAR and WAW cannot occur, because reads happen in ID and writes happen in order in WB.
- Question wording. If a GATE question says "with operand forwarding" and does not mention a register file with a split-cycle write and read, check the stated assumptions. Without the split-cycle behavior, a distance-3 dependency stalls 1 cycle when forwarding is absent.
- Cost. Forwarding adds muxes and comparators on the critical path. It can lengthen the clock period slightly, which matters in questions that compare designs.
Total cycles with stalls
The cycle count for n instructions in a k-stage pipeline with stalls is k + n - 1 + stalls. For the four-instruction program above, k = 5 and n = 4:
- Without forwarding: 5 + 4 - 1 + 4 = 12 cycles.
- With forwarding: 5 + 4 - 1 + 1 = 9 cycles.
Exercise
A five-stage pipeline (IF, ID, EX, MEM, WB) has full forwarding and a register file that is written in the first half and read in the second half of a cycle. How many cycles does this sequence take? lw r1, 0(r2) add r3, r1, r4 sub r5, r3, r1
Show answer
Only the load-use pair (lw, add) stalls, for 1 cycle. The sub depends on add at distance 1 (ALU producer, no stall) and on lw at distance 2 (forwarded from MEM/WB or read from the register file in time). Total = 5 + 3 - 1 + 1 = 8 cycles.
Exercise
The same pipeline has no forwarding. How many cycles does this sequence take? add r1, r2, r3 sub r4, r1, r5 and r6, r1, r7
Show answer
add is in ID in cycle 2 and WB in cycle 5. The sub must be in ID in cycle 5, so it stalls 2 cycles. The and is then in ID in cycle 6, after the write in cycle 5, so it needs no extra stall. Total = 5 + 3 - 1 + 2 = 9 cycles.