Lec13 Data Hazards
Lec13 Data Hazards
• Pipelined Implementation
• Datapath and Control
Today’s Lecture
• Pipeline Hazards
• Data Hazards
• Control Hazards
PIPELINE HAZARDS
Structural Hazards: A structural hazard in the laundry room would occur if we used a washer-dryer
combination instead of a separate washer and dryer
Arise from resource conflicts when the hardware cannot support all possible combinations of instructions in
simultaneous overlapped instructions.
E.g., suppose you have a single – not separate – instruction and data memory in pipeline with one read port. in
the same clock cycle. the first instruction is accessing data from memory while the fourth instruction is fetching an
instruction from that same memory. Without two memories, our pipeline could have a structural hazard.
Program
execution 2 4 6 8 10 12 14
Time
order
(in instructions)
Instruction Data
lw $1, 100($0) Reg ALU Reg
fetch access
Pipelined
Instruction Data
lw $2, 200($0) 2 ns Reg ALU Reg
fetch access
Instruction Data
lw $3, 300($0) 2 ns Reg ALU Reg
fetch access
Instruction Data
lw $4, 400($0) Reg ALU Reg
2 ns fetch access
2 ns 2 ns 2 ns 2 ns 2 ns
-When an instruction is stalled all instructions issued later from the stalled
instruction also stalled.
IM REG ALU DM
IM REG ALU
If the results can be moved from where the ADD produces it, the EX/MEM
register, to where the SUB needs it, the ALU input latches, then the need for stall
can be avoided.
ALU result from the EX/MEM register is always fedback to ALU input latches.
Forwarding logic detects whether previous ALU operation has written the register
corresponding to a source for the current ALU operation, if so, then control logic
selects the forwarded result as the ALU input rather than value read from register
file.
Forward results not only from the immediately previous instruction, but possibly
from instruction started three clock cycles earlier.
So we have to add EX/MEM.RegisterRd ≠ 0 to the first hazard condition (1a and 1b)
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
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 // if there is a write…
and ( MEM/WB.RegisterRd ≠ 0 ) // to a non-$0 register…
and not ( EX/MEM.RegWrite and EX/MEM.RegisterRd ≠ 0) // and not already a register match
and (EX/MEM.RegisterRd = ID/EX.RegisterRs)) with earlier pipeline register…
and ( MEM/WB.RegisterRd = ID/EX.RegisterRs ) ) // but match with later pipeline
register, then…
ForwardA = 01
ForwardB = 00 ID/EX The second ALU operand comes from the register file
ForwardB = 01 MEM/WB The second ALU operand is forwarded from data memory
or an earlier ALU result
ForwardB = 10 EX/MEM The second ALU operand is forwarded from prior ALU
result
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
Datapath with forwarding hardware and control wires – certain details, e.g., branching hardware,
are omitted to simplify the drawing Note: so far we have only handled forwarding to R-type instructions
LW R2, 20(R1)
OR R8, R2, R6
Hazard ID/EX.MemRead
detection
unit ID/EX
WB
IF/IDWrite
EX/MEM
M
Control u M WB
x MEM/WB
0
IF/ID EX M WB
PCWrite
M
Instruction
u
x
Registers
Instruction ALU Data
PC memory M
memory
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
Datapath with forwarding hardware, the hazard detection unit and controls wires – certain details, e.g., branching
hardware are omitted to simplify the drawing
BITS Pilani, Pilani Campus
• Control or Branch Hazard: When the proper instruction
cannot execute in the proper pipeline clock cycle
because the instruction that was fetched is not the one
that is needed; that is, the flow of instruction addresses
is not what the pipeline expected.
• Arises from the need to make a decision based on the
result of control flow instruction while others (in pipeline)
are executing because we must begin fetching the
instruction following the branch on the very next clock
cycle.
The outcome of branch taken (prediction wrong) is decided only when beq is in the MEM stage, so the following
three sequential instructions already in the pipeline in their EX, ID, and IF stages have to be flushed and execution
resumes at lw
The instructions fetched after the branch
instruction are ignored and the fetch is restarted
once the branch target is known.
- Find out if branch is taken (or) not taken, earlier in the pipeline.
To complete this decision by the end of ID, move zero testing into
that cycle. Both PCs (taken and untaken) must be computed early.