Revisiting Hazards: Data Hazards Control Hazards Hardware
Revisiting Hazards: Data Hazards Control Hazards Hardware
ID/EX
0
M
u WB
x EX/ME M
1
Control M WB
ME M/WB
EX M WB
IF/ID
Add
Add
4 Add
result
RegW rite
Branch
Shift
left 2
MemWrite
ALUSrc
MemtoReg
Read
Instruction
PC Address register 1
Read
data 1
Read
register 2 Zero
Instruction
Registers Read ALU ALU
memory W rite 0 Read
data 2 result Address 1
register M data
Data M
u
memory u
W rite x
data x
1
0
Write
data
Instruction
16 32 6
[15– 0] Sign ALU
M emRead
extend control
emanate from
[20– 16]
0 ALUOp
M
the control Instruction
[15– 11]
u
x
portions of the 1
RegDst
pipeline registers
Hazard Detection
Hazard conditions:
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs
1b. EX/MEM.RegisterRd = ID/EX.RegisterRt
2a. MEM/WB.RegisterRd = ID/EX.RegisterRs
2b. MEM/WB.RegisterRd = ID/EX.RegisterRt
Eg., in the earlier example, first hazard between sub $2, $1, $3 and
and $12, $2, $5 is detected when the and is in EX stage and the
sub is in MEM stage because
EX/MEM.RegisterRd = ID/EX.RegisterRs = $2 (1a)
Program
execution order
(in instructions)
sub $2, $1, $3 IM Reg DM Reg
or $13, $6, $2
add $14, $2, $2 or $13, $6, $2 IM Reg DM Reg
sw $15, 100($2)
add $14, $2, $2 IM Reg DM Reg
R e g is te rs ALU
D a ta
m e m o ry M
u
x
M
u
x
R e g is te rs
F o r w a rd A ALU
M D a ta
u m e m o ry
x M
u
x
Rs F o rw a rd B
Rt
Rt M
u E X /M E M .R e g iste rR d
Rd
x
F o rw a rd in g M E M /W B .R e g is te rR d
u n it
1. EX hazard
if ( EX/MEM.RegWrite // if there is a write…
and ( EX/MEM.RegisterRd 0 ) // to a non-$0 register…
and ( EX/MEM.RegisterRd = ID/EX.RegisterRs ) ) // which matches, then…
ForwardA = 10
This check is necessary, e.g., for sequences such as add $1, $1, $2; add $1, $1, $3; add $1, $1, $4;
(array summing?), where an earlier pipeline (EX/MEM) register has more recent data
Forwarding Hardware with Control
Called forwarding unit, not hazard detection unit,
because once data is forwarded there is no hazard!
ID/EX
WB
EX/MEM
Control M WB
MEM/WB
IF/ID EX M WB
M
Instruction
u
x
Registers
Instruction Data
PC ALU
memory memory M
u
M x
u
x
IF/ID.RegisterRs Rs
IF/ID.RegisterRt Rt
IF/ID.RegisterRt Rt
M EX/MEM.RegisterRd
IF/ID.RegisterRd Rd u
x
Forwarding MEM/WB.RegisterRd
unit
ID/EX
2 $2 $1
M
In stru ctio n
5 u
x
Registers
Instruction Data
PC ALU
memory memory M
$5 $3
u
M x
u
x
2 1
5 3
M
4 2 u
x
Forwarding
unit
C lo c k 3
a dd $9 , $ 4, $2 or $ 4 , $ 4, $ 2 a nd $4 , $ 2, $5 sub $ 2, . . . b e fo r e < 1 >
IF/ID EX M WB
4 $4 $2
M
I nstr uction
6 u
x
Registers
Instruction Data
PC ALU
memory M
memory $2 $5
u
M x
u
x
2 2
6 5
M 2
4 4 u
x
Forwarding
unit
C lo c k 4
a fte r < 1 > add $9, $4, $2 or $4, $4, $2 a nd $ 4, . . . sub $ 2 , . . .
1
IF/ID EX M WB
4 $4 $4
M
In s tru ctio n
2 u
x
Registers
2 Data
Instruction ALU
PC memory
memory M
$2 $2
u
M x
u
x
4 4
2 2
M 4
2
u
9 4
x
Forwarding
unit
C lo c k 5
a f te r < 2 > a f te r < 1 > a dd $9 , $ 4, $2 or $4, . . . and $4, . . .
1
IF/ID EX M WB
$4
M
In stru ctio n
u
x
Registers
4 Data
Instruction ALU
PC memory
memory $2 M
u
M x
u
x
M 4 4
9 u
x
Forwarding
unit
C lo c k 6
Data Hazards and Stalls
Load word can still cause a hazard:
an instruction tries to read a register following a load instruction that
writes to the same register
Time (in clock cycles)
Program CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
As even a pipeline
dependency goes
or $8, $2, $6 IM Reg DM Reg
backward in time
forwarding will not add $9, $4, $2 IM Reg DM Reg
ID/EX
0
M
u WB
x EX/MEM
1
Control M WB
MEM/WB
EX M WB
IF/ID
Add
4 Add
Add result
RegWrite
Shift Branch
left 2
MemWrite
ALUSrc
MemtoReg
Read
Instruction
Instruction 16 32 6
[15–0] Sign ALU MemRead
extend control
Control signals Instruction
emanate from [20–16]
0 ALUOp
M
the control Instruction u
x
[15–11]
portions of the 1
RegDst
pipeline registers
Hazard Detection Logic to Stall
Hazard detection unit implements the following check if to stall
IF/IDWrite
WB
EX/MEM
M
Control u M WB
x MEM/WB
0
IF/ID EX M WB
PCWrite
M
Instruction
u
x
Registers
Instruction Data
PC ALU
memory memory M
u
M x
u
x
IF/ID.RegisterRs
IF/ID.RegisterRt
IF/ID.RegisterRt Rt M EX/MEM.RegisterRd
IF/ID.RegisterRd Rd u
x
ID/EX.RegisterRt Rs Forwarding MEM/WB.RegisterRd
Rt unit
lw $2, 20($1)
and $4, $2, $5 and$4, $2, $5 IM Reg Reg DM Reg
or $8, $2, $6
add $9, $4, $2
Slt $1, $6, $7 or $8, $2, $6 IM IM Reg DM Reg
bubble
Stall inserted
here
Stall/Bubble in the Pipeline
Or, more
accurately…
Stalling
Execution
example:
lw $2, 20($1)
and $4, $2, $5
or $4, $4, $2
add $9, $4, $2
a nd $ 4 , $ 2 , $ 5 lw $ 2 , 2 0 ( $ 1 ) b e fo re < 1 > b e fo re < 2 > b e fo re < 3 >
Hazard
ID/EX.MemRead
detection
1 unit ID/EX
X
11
WB
IF /ID W rite
EX/MEM
M
Control u M WB
x MEM/WB
0
IF/ID EX M WB
1 $1
P C W rite
M
Instructio n
X u
x
Registers
Instruction Data
PC ALU
memory memory M
$X
u
M x
u
x
X
lw $2, 20($1) 2
M
and $4, $2, $5 u
x
or $4, $4, $2 ID/EX.RegisterRt Forwarding
add $9, $4, $2 unit
C lo c k 2
or $4, $4, $2 a nd $4 , $ 2, $5 lw $ 2 , 2 0 ( $ 1 ) b e fo re < 1 > b e fo r e < 2 >
Hazard
ID/EX.MemRead
detection
2
unit ID/EX
5
00 11
WB
I F /ID W rite
EX/MEM
M
Control u M WB
x MEM/WB
0
IF/ID EX M WB
$2 $1
P C W rite
M
I n structio n
5 u
x
Registers
Instruction Data
PC ALU
memory memory M
$5 $X
u
M x
u
x
2 1
5 X
2 M
lw $2, 20($1) 4 u
x
and $4, $2, $5 ID/EX.RegisterRt Forwarding
or $4, $4, $2 unit
Hazard
ID/EX.MemRead
detection
2 unit ID/EX
5
10 00
WB
IF /ID W r ite
EX/MEM
M
11
Control u M WB
x MEM/WB
0
IF/ID EX M WB
2 $2 $2
P C W r ite
M
I ns tru ctio n
5 u
x
Registers
Data
Instruction ALU
PC memory
memory $5 $5 M
u
M x
u
x
2 2
5 5
M 2
4 4 u
lw $2, 20($1) x
Hazard
ID/EX.MemRead
detection
4
unit ID/EX
2
10 10
I F / ID W rite
WB
EX/MEM
M 0
Control u M WB
x MEM/WB
0
11
IF/ID EX M WB
P C W rit e
4 $4 $2
M
Ins truction
2 u
x
Registers
2 Data
Instruction ALU
PC memory
memory $2 $5 M
u
M x
u
x
4 2
2 5
M 2
lw $2, 20($1) 4 4 u
x
and $4, $2, $5 ID/EX.RegisterRt Forwarding
or $4, $4, $2 unit
Hazard
ID/EX.MemRead
detection
4
unit ID/EX
2
10 10
WB
IF / ID W rite
EX/MEM
M 10
Control u M WB
x MEM/WB
0
0
IF/ID EX M WB
P CW rit e
4 $4 $4
M
I ns truc t io n
2 u
x
Registers
Data
Instruction ALU
PC memory
memory $2 $2 M
u
M x
u
x
4 4
2 2
M 4
lw $2, 20($1) 9 4 u
x
and $4, $2, $5 ID/EX.RegisterRt Forwarding
Hazard
ID/EX.MemRead
detection
unit ID/EX
10 10
WB
I F/ I DW r ite
EX/MEM
M 10
Control u M WB
x MEM/WB
0
1
IF/ID EX M WB
$4
PC W rite
M
Ins tr uct io n
u
x
Registers
4 Data
Instruction ALU
PC memory
memory $2 M
u
M x
u
x
4
2
M 4 4
lw $2, 20($1) 9 u
x
and $4, $2, $5 ID/EX.RegisterRt Forwarding
Move the branch decision from the MEM stage (as in our
current pipeline) earlier to the ID stage
calculating the branch target address involves moving the
branch adder from the MEM stage to the ID stage – inputs to
this adder, the PC value and the immediate fields are already
available in the IF/ID pipeline register
calculating the branch decision is efficiently done, e.g., for
equality test, by XORing respective bits and then ORing all the
results and inverting, rather than using the ALU to subtract and
then test for zero (when there is a carry delay)
with the more efficient equality test we can put it in the ID stage
without significantly lengthening this stage – remember an objective
of pipeline design is to keep pipeline stages balanced
we must correspondingly make additions to the forwarding and
hazard detection units to forward to or stall the branch at the ID
stage in case the branch decision depends on an earlier result
Flushing on Misprediction
Same strategy as for stalling on load-use data hazard…
Zero out all the control values (or the instruction itself) in
pipeline registers for the instructions following the branch that
are already in the pipeline – effectively turning them into nops
– so they are flushed
in the optimized pipeline, with branch decision made in the ID
stage, we have to flush only one instruction in the IF stage – the
branch delay penalty is then only one clock cycle
Optimized Datapath for Branch
IF.Flush
Hazard
detection IF.Flush control zeros out the instruction in the IF/ID
unit
ID/EX
pipeline register (which follows the branch)
M
u
x
WB
EX/MEM
M
Control u M WB
x MEM/WB
0
IF/ID EX M WB
4 Shift
left 2
M
u
x
Registers =
Instruction Data
PC ALU
memory memory M
u
M x
u
x
Sign
extend
M
u
x
Forwarding
unit
Branch decision is moved from the MEM stage to the ID stage – simplified drawing
not showing enhancements to the forwarding and hazard detection units
Reducing Branch Delay
Move hardware to determine outcome to ID
stage
Target address adder
Register comparator
Example: branch taken
36: sub $10, $4, $8
40: beq $1, $3, 7
44: and $12, $2, $5
48: or $13, $2, $6
52: add $14, $4, $2
56: slt $15, $6, $7
...
72: lw $4, 50($7)
Example: Branch Taken
Example: Branch Taken
Data Hazards for Branches
If a comparison register is a destination
of 2nd or 3rd preceding ALU instruction
… IF ID EX MEM WB
beq stalled IF ID
beq stalled IF ID
beq stalled ID