Title: Data Hazards and Stalls
1Data Hazards and Stalls
- Data hazards and forwarding
- Data Hazards and Stalls
2An Example
Assuming that register R2 10 initially. And
the sub instruction will result in R2 -20.
3See PH Fig. 6.28 3rd Ed or 4.52 4th Ed
Pipelined dependencies in a five instruction
sequence using simplified datapaths to show the
dependencies.
Note only add and sw get the right value R2 -
20
4An Example (contd)
Hazard condition (between sub and and at the
indicated stages) EX/MEM.RegisterRd
ID/EX.RegisterRs 2 General
1a. EX/MEM.RegisterRd ID/EX.RegisterRs 1b. EX/ME
M.RegisterRd ID/EX.RegisterRt 2a. MEM/WB.Registe
rRd ID/EX.RegisterRs 2b. MEM/WB.RegisterRd
ID/EX.RegisterRt
Question can you identify which of the 4
conditions here captures the hazards condition
between sub and or ?
5Forwarding/Bypassing
- ALU inputs can also from pipeline registers.
- Extra multiplexors.
- Hazard detection units.
6See PH Fig. 6.29 3rd Ed or 4.53 4th Ed
- The dependencies between the pipeline registers
move forward in time, so it is possible to supply
the inputs to the ALU needed by the and
instruction and or instruction by forwarding the
results found in the pipeline registers rather
than stall.
7See PH Fig. 6.30 3rd Ed or 4.54 4th Ed
The ALU and pipeline register before adding
forwarding.
8See PH Fig. 6.30 3rd Ed or 4.54 4th Ed
Forwarding control and the multiplexors are added
9See PH Fig. 6.31 3rd Ed or 4.55 4th Ed
The control values for the forwarding
multiplexors.
10Forwarding Conditions
- 1. 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
11Cond
- 2. 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
12Note Here both EX and MEM stages may have the
exact hazard conditions. What should we do ?
Priority in Forwarding
- Add 1, 1, 2
- Add 1, 1, 3
- Add 1, 1, 4
- .
13Note When both EX and MEM stages may have the
exact hazard conditions.
Priority in Forwarding
- 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
14See PH Fig. 6.32 3rd Ed or 4.56 4th Ed
- The datapath modified to resolve hazards via
forwarding.
15The Snapshots of Our Example Through Forwarding
16Some conventions
- The bold lines are those active in a clock cycle.
- The italicized register numbers in color with a
red circle indicate a hazard. - The forwarding unit is highlighted by shading it
when it is forwarding data to the ALU. - The in the place of operands means that their
identity is information not needed by the stage.
17See PH Fig. 6.14.11 3rd Ed CD (For more practice
section)
- Clock cycles 3 of the instruction sequence.
18See PH Fig. 6.14.11 3rd Ed CD (For more practice
section)
- Clock cycles 4 of the instruction sequence.
19See PH Fig. 6.14.12 3rd Ed CD (For more practice
section)
- Clock cycles 5 of the instruction sequence.
20See PH Fig. 6.14.12 3rd Ed CD (For more practice
section)
- Clock cycles 6 of the instruction sequence.
21Hazards and Stalls
22See PH Fig. 6.34 3rd Ed or 4.58 4th Ed
- A pipelined sequence of instructions.
23Bubble
- The way stalls are really inserted into the
pipeline
24See PH Fig. 6.35 3rd Ed or 4.59 4th Ed
The way stalls are really inserted into the
pipeline (Another View)
25Forwarding with Load and Stores
26A Hazard Detection Unit
In the ID stage, the following should be checked
by the hazard detection unit, so it can insert
the stall between a load and its use. if
(ID/EX.MemRead and ((ID/EX.RegisterRt
IF/ID.RegisterRs) or (ID/EX.RegisterRt
IF/ID.RegisterRt)) stall the pipeline
27Pipelined control overview, showing the two
multiplexors for forwarding, the hazard detection
unit, and the forwarding unit.
28Snapshots of the running example through the
pipeline
29See PH Fig. 6.14.14 3rd Ed CD (For more practice
section)
- Clock cycles 2 of the instruction sequence in the
example.
30See PH Fig. 6.14.14 3rd Ed CD (For more practice
section)
- Clock cycles 3 of the instruction sequence in the
example.
31See PH Fig. 6.14.15 3rd Ed CD (For more practice
section)
- Clock cycles 4 of the instruction sequence in the
example.
32See PH Fig. 6.14.15 3rd Ed CD (For more practice
section)
- Clock cycles 5 of the instruction sequence in the
example.
33See PH Fig. 6.14.16 3rd Ed CD (For more practice
section)
- Clock cycles 6 of the instruction sequence in the
example.
34See PH Fig. 6.14.16 3rd Ed CD (For more practice
section)
- Clock cycles 7 of the instruction sequence in the
example. - (note the forwarding of 4)
35Branch Hazards and Handling
- Branch hazards
- Branch hazard handling methods
- Static
- Dynamic