4. Pipeline Hazards
Data dependence
Example: lw $1, 200($2)
add $3, $4, $1
add can’t do ID (i.e., read register $1) until lw updates $1
Control dependence
Example: bne $1, $2, target
add $3, $4, $5
next IF can’t start until bne completes the comparison
Instruction order
instr #1 IF ID EXE MEM WB
Fact: Register access VERY fast. Takes half the time of ALU stage or less
always Write to registers during 1st half of each clock cycle
always Read from Registers during 2nd half of each clock cycle
Register file supports Write and Read during same clock cycle (in this
order)
lw $s0, 20($t1)
sub $t2, $s0, $t3
13 cycles 11 cycles
In MIPS pipeline
Need to compare registers and compute target early in the
pipeline
Add hardware to do it in ID stage
Prediction
correct
Prediction
incorrect
Subject to hazards
Structure, data, control
EX hazard
if (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRs))
ForwardA = 10
if (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRt))
ForwardB = 10
MEM hazard
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and (MEM/WB.RegisterRd = ID/EX.RegisterRs))
ForwardA = 01
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and (MEM/WB.RegisterRd = ID/EX.RegisterRt))
ForwardB = 01
MEM hazard
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRs))
and (MEM/WB.RegisterRd = ID/EX.RegisterRs))
ForwardA = 01
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRt))
and (MEM/WB.RegisterRd = ID/EX.RegisterRt))
ForwardB = 01
Need to stall
for one cycle
Stall inserted
here
Or, more
accurately…
Zero input
to create a
nop
operation
Flush these
instructions
(Set control
values to 0)
PC
Target address
calculator and
Register
comparator
IF.Flush flushes
the “and”
instruction.
… IF ID EX MEM WB
beq stalled IF ID
beq stalled IF ID
beq stalled ID