Beruflich Dokumente
Kultur Dokumente
3/24/04++
Value of CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
register $2: 10 10 10 10 10/– 20 – 20 – 20 – 20 – 20
Program
execution
order
(in instructions)
sub $2, $1, $3 IM Reg DM Reg
Fig. 6.36
Data Hazards and Forwarding - Concepts
• Problem page 477
Basic reference figure: fig. 6.30, p. 470
• Where to start looking for the hazard (for example on p. 477)
– For a given “receiving” (reading) instruction,
Look in the EX phase where the ALU needs the data.
– Next, using fig. 6.36: During this execution phase clock cycle for the reading
instruction, “go vertically up” to the “sender” (writer) and see where the writer is
during this same clock cycle. If the writer is not after (ie., before) the WB phase
(MEM/WB), then there is a hazard.
• Data hazard detection
– Assume that all register number fields (5 bits each) are propagated from IF/ID to
ID/EX – rs path must be added rd & rt already there (take early look at fig 6.40).
– Notation (example):
ID/EX.RegisterRs refers to the identification (5 bit field – one of 32) of the rs
register whose 5 bit “name” or identification is found in the pipeline register
ID/EX.
– Use similar notation for other pipeline registers and rt and rd.
Data Hazards Detection – Concepts
• General Rule:
See what “write” register is being written by a previous instruction at
the same clock pulse as the “read” instruction ==> check to see if this
“write” register is the register file or after. If not we have a hazard.
– Or, more concisely, use the logical hazard tests referred to above
See next:
The Essence of a Data Hazard So Far
• Necessary & sufficient conditions for data hazards in example:
“writer” “reader”
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs
1b. EX/MEM.RegisterRd = ID/EX.RegisterRt
Program
execution order
(in instructions)
sub $2, $1, $3 IM Reg DM Reg
Fig. 6.37
Forwarding Details
– EX hazard (1b)
if {EX/MEM.RegWrite // register file is actually being written
and (EX/MEM.RegisterRd ≠ 0) //avoid forwarding $0
and (EX/MEM.RegisterRd = ID/EX.RegisterRt)} // conditions for a hazard
then ForwardB = 10 // use EX/MEM
Forwarding Details (cont)
• Conditions for setting these forwarding control lines.
Type 2 hazard – reading source register set two instructions ago:
– MEM hazard (2a) // register file is actually being written
if {MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 0) //avoid forwarding $0
and (MEM/WB.RegisterRd = ID/EX.RegisterRs)} // conditions for a hazard
then ForwardA = 01 // use MEM/WB
• In general, if the previous instruction is going to write the register file, and the
write register number matches the read register number of ALU inputs A or B,
provided it is not register 0, the steer the MUX to pick up the value from pipeline
register EX/MEM (using the new ForwardA or ForwardB MUX lines).
Registers ALU
Data
memory M
u
x
Before
a. No forwarding
Fig 6.38
ID/EX EX/MEM MEM/WB
M
u
x
Registers
ForwardA ALU
M
u
x
Data
memory M After
u
x
How did rs
get here?→ Rs
Rt
ForwardB
Rt
Rd
M
u EX/MEM.RegisterRd See fig 6.39 for details
x
Forwarding
on ALU MUX operation
MEM/WB.RegisterRd
unit
b. With forwarding
Forwarding Unit
ID/EX
WB
EX/MEM
Control M WB
MEM/WB
IF/ID EX M WB
Selector values
00
M
Instruction
01 u
x
10
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
How rs unit
got there
Fig 6.40
Forwarding –Further Complications for type 2
(p. 483)
• Example:
add $1, $1, $2
add $1, $1, $3
add $1, $1, $4
…
• The 3rd instruction has both a “type 1” and a “type 2” hazard for the same register $1.
The preceding hardware haas a conflict as what to forward.
• There is a forwarding condition from both the WB stage and the MEM stage,
Resolution: choose the MEM stage data because it would be the most recent
result.
• Use this logic for type 2 only if there is not also a type 1 hazard, else use the type 1
logic – gives most recent value:
– MEM hazard (2a)
if {MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 0) //avoid forwarding $0
and (EX/MEM.RegisterRd ≠ID/EX.RegisterRs
and (MEM/WB.RegisterRd = ID/EX.RegisterRs)} // conditions for a hazard
then ForwardA = 01 – use MEM/WB
– MEM hazard (2b)
if {MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 0) //avoid forwarding $0
and (EX/MEM.RegisterRd ≠ID/EX.RegisterRt
and (MEM/WB.RegisterRd = ID/EX.RegisterRt)} // conditions for a hazard
then ForwardB = 01 – use MEM/WB
Forwarding – scenario
• An example illustrating the forwarding design to this point:
←$2
←$2
and $4, $2, $5 IM Reg DM Reg
←$2 ← $4
Fig. 6.44
Can forward only to the “or” and “add” instructions without stalling
$2 still unavailable in EX/MEM for “and”. When sub was the “writing”
instruction, we forwarded from EX/MEM to the ALU for “and”
Solution to “lw” Hazard Problem
• Detect this situation using a “Hazard Detection Unit” as opposed to the previous
“Forwarding Unit”.
• When this situation is detected, instructions which follow are stalled for a clock
cycle in order to give the data time to be forwarded. But the lw will be allowed to
proceed.
• Compare these equations with those for “normal” type 1 and type 2 hazard
forwarding.
Now that we detected the “lw Hazard Problem”,
How Do We Stall
• Since we are detecting this in the ID stage while the lw is in the EX phase,
we must “freeze” the two instructions that follow in the IF and ID stages
for one cycle, but allow the lw to proceed.
– This is accomplished by preventing the PC and IF/ID register from changing.
– New control lines from hazard detection unit needed
PCWrite – allows PC to be written only when asserted
IF/IDWrite - allows IF/ID pipeline register to be written only when asserted
– When these lines are de-asserted the instructions in the IF and ID stages will stay
“as is” for one clock cycle – a freeze.
• Because the lw is allowed to advance to MEM stage, we must guarantee
that the EX stage becomes “benign” – since no instruction is entering it –
we insert a “bubble”, or effectively insert a “nop” there.
– This is accomplished by forcing all zeros in the EX, MEM, and WB control fields
in the ID/EX pipeline register.
– De-asserting all controls in any stage, is effectively inserting a nop instruction.
– These benign control values are then are percolated forward each clock cycle
having no effect on anything – believable?
Stalling the Pipeline (cont).
• See diagram
Inserting a Stall in the Pipeline
Program Time (in clock cycles)
execution CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9 CC 10
order
(in instructions)
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 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
Fig. 6.46