Data Hazards
Philipp Koehn 9 October 2019
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
Data Hazards Philipp Koehn 9 October 2019 Philipp Koehn Computer - - PowerPoint PPT Presentation
Data Hazards Philipp Koehn 9 October 2019 Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019 Data Hazard 1 Definition: instruction waits on result from prior instruction Example add $s0, $t0, $t1 sub $t0, $s0,
Philipp Koehn 9 October 2019
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
1
instruction waits on result from prior instruction
add $s0, $t0, $t1 sub $t0, $s0, $t3 – add instruction writes result to register $s0 in stage 5 – sub instruction reads $s0 in stage 2 ⇒ Stage 2 of sub has to be delayed
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
2
IF
add $s0,$t0,$t1
ID MEM WB EX
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
3
IF
add $s0,$t0,$t1
ID MEM WB EX IF
sub $t0,$s0,$t3
ID MEM WB EX
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
4
IF
lw $s0,20($t0)
200 400 600 800 1000
ID MEM WB EX IF
sub $t0,$s0,$t3
ID MEM WB EX
bubble bubble bubble bubble bubble
1200
"pipeline stall" or "bubble"
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
5
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
6
add $s0, $t0, $t1 sub $t0, $s0, $t3
IF
add $s0,$t0,$t1
200 400 600 800 1000
ID MEM WB EX IF
sub $t0,$s0,$t3
ID MEM WB EX
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
7
Instruction Memory ALU Add Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
8
Registers ALU Add Address Instruction Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Instruction Memory
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
9
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
10
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
11
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
12
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Selector
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
13
– detect if there is a data hazard – forward the right register values
– identify of input registers used in instruction currently in EX (either first or second operand) – identity of output register used in instruction currently in MEM – value of output register used in instruction currently in MEM
– Register value – Control lines for selectors for input to ALU
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
14
– EX.Rs and EX.Rt identify of input registers used in instruction currently in EX (either first or second operand) – MEM.Rd identity of output register used in instruction currently in MEM – MEM.RdValue value of output register used in instruction currently in MEM
– Forward.Rs and Forward.Rt Register value – Hazard.Rs and Hazard.Rt Control lines for selectors for input to ALU
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
15
if (MEM.Rd == EX.Rs) Forward.Rs = MEM.RdValue Hazard.Rs = 1 else Hazard.Rs = 0 if (MEM.Rd == EX.Rt) Forward.Rt = MEM.RdValue Hazard.Rt = 1 else Hazard.Rt = 0
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
16
ALU Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Zero Result Selector IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate Selector Forwarding Unit EX.Rs EX.Rt ForwardA MEM.RdValue MEM.Rd ForwardB HazardB HazardA Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
17
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
18
load $s0, 20($t0) sub $t0, $s0, $t3
IF
lw $s0,20($t0)
200 400 600 800 1000
ID MEM WB EX IF
sub $t0,$s0,$t3
ID MEM WB EX
bubble bubble bubble bubble bubble
1200
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
19
Instruction Memory ALU Add Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
20
Registers ALU Add Instruction Memory Address Instruction Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
21
Registers ALU Add Instruction Memory Address Instruction Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
22
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
23
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
24
load $s0, 20($t0) sub $t0, $s0, $t3
– we need operand value in $s0 – we have not even retrieved it at this stage
– load instruction processing has to move to stage 5 – sub instruction processing has to stall
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
25
insert a "nop" operation ("no operation")
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
26
IF
lw $s0,20($t0)
200 400 600 800 1000 1200
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
27
IF
lw $s0,20($t0)
200 400 600 800 1000 1200
ID IF
sub $t0,$s0,$t3
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
28
IF IF
lw $s0,20($t0)
200 400 600 800 1000 1200
ID
sub $t0,$s0,$t3
EX ID
– load: address calculation – sub: instruction decode
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
29
ID ID IF IF
lw $s0,20($t0)
200 400 600 800 1000 1200
sub $t0,$s0,$t3
EX
Hazard
– load: address calculation – sub: instruction decode
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
30
ID IF IF
lw $s0,20($t0)
200 400 600 800 1000 1200
sub $t0,$s0,$t3
EX
bubble
IF
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
31
ID IF IF
lw $s0,20($t0)
200 400 600 800 1000 1200
sub $t0,$s0,$t3
EX
bubble
MEM
bubble
ID IF
– load: memory retrieve – sub: instruction decode
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
32
ID IF IF
lw $s0,20($t0)
200 400 600 800 1000 1200
sub $t0,$s0,$t3
EX
bubble
MEM
bubble
ID WB EX
bubble
IF
– load: write to register – sub: ALU operation execution
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
33
– detect if there is a data hazard – insert a "nop" instruction into pipeline
– identify of input registers used in instruction currently in ID (either first or second operand) – identity of load register used in instruction currently in EX – control flag that there is indeed a memory read in EX
– overwrite instruction currently in ID with "nop" – reset program counter
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
34
– ID.Rs and ID.Rt identify of input registers used in instruction currently in ID (either first or second operand) – EX.Rd identity of load register used in instruction currently in EX – EX.MemRead control flag that there is indeed a memory read in EX
– ID/EX
– PC reset program counter
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
35
if (EX.MemRead and (EX.Rd = ID.Rs or EX.Rd = ID.Rt)) PC = PC - 4 ID/EX = nop
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
36
Registers Instruction Memory Address Instruction Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Selector PC IF: Instruction Fetch ID: Instruction decoder register file read Hazard Detection Unit ID.Rs ID.Rt EX.Rd ID/EX EX.MemRead Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
37
if (WB.Rd == EX.Rs) Forward.Rs = WB.RdValue Hazard.Rs = 1 else Hazard.Rs = 0 if (WB.Rd == EX.Rt) Forward.Rt = WB.RdValue Hazard.Rt = 1 else Hazard.Rt = 0
where result from first add is used in last add
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019
38
Data Memory ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Selector Forwarding Unit
Philipp Koehn Computer Systems Fundamentals: Data Hazards 9 October 2019