forwarding assignment
forwarding assignment
0
ID/EX
WB EX/MEM
PCSrc
Control M WB MEM/WB
IF/ID EX M WB
4
Add
P Add
C Shift
RegWrite left 2
Read Read
register 1 data 1 MemWrite
ALU
Read Instruction Zero
Read Read
address [31-0] 0
register 2 data 2 Result Address
Write
1 Data
Instruction register MemToReg
memory
memory Registers ALUOp
Write
data ALUSrc Write Read
1
data data
Instr [15 - 0] Sign
RegDst
extend MemRead
0
Instr [20 - 16]
0
Instr [15 - 11]
1
2. PIPELINE DIAGRAM REVIEW
Clock cycle
1 2 3 4 5 6 7 8 9
Clock cycle
1 2 3 4 5 6 7 8 9
• The SUB instruction does not write to register $2 until clock cycle 5.
This causes two data hazards in our current pipelined datapath.
• The AND reads register $2 in cycle 3. Since SUB hasn’t modified
the register yet, this will be the old value of $2, not the new one.
• Similarly, the OR instruction uses register $2 in cycle 4, again
before it’s actually updated by SUB.
4. A CLOSER LOOK AT DEPENDENT INSTRUCTIONS
Clock cycle
1 2 3 4 5 6 7 8 9
Clock cycle
1 2 3 4 5 6 7 8 9
IM Reg DM Reg
sub $2, $1, $3
IM Reg DM Reg
or $13, $6, $2
IM Reg DM Reg
add $14, $2, $2
Clock cycle
1 2 3 4 5 6 7
Clock cycle
1 2 3 4 5 6 7
Clock cycle
1 2 3 4 5 6 7
▪ The ALU result generated in the EX stage is normally passed through the
pipeline registers to the MEM and WB stages, before it is finally written to
the register file.
▪ This is an abridged diagram of our pipelined datapath.
PC
ALU
Registers
Instruction
memory
Data
memory
Rt 0
0
Rd
1
5. FORWARDING BASIS
▪ Since the pipeline registers already contain the ALU result, we could just
forward that value to subsequent instructions, to prevent data hazards.
— In clock cycle 4, the AND instruction can get the value $1 - $3 from
the EX/MEM pipeline register used by sub.
— Then in cycle 5, the OR can get that same result from the MEM/WB
pipeline register being used by SUB.
Clock cycle
1 2 3 4 5 6 7
IM Reg DM Reg
sub $2, $1, $3
IM Reg DM Reg
and $12, $2, $5
IM Reg DM Reg
or $13, $6, $2
6. FORWARDING HARDWARE
▪ A forwarding unit selects the correct ALU inputs for the EX stage.
— If there is no hazard, the ALU’s operands will come from the register
file, just like before.
— If there is a hazard, the operands will come from either the EX/MEM
or MEM/WB pipeline registers instead.
▪ The ALU sources will be selected by two new multiplexers, with control
signals named ForwardA and ForwardB.
IM Reg DM Reg
sub $2, $1, $3
IM Reg DM Reg
and $12, $2, $5
IM Reg DM Reg
or $13, $6, $2
6. FORWARDING HARDWARE
PC
0
1
2
Registers ForwadA
Instruction ALU
memory 0
1 Data
2 memory
1
ForwardB
Rt 0
0
Rd
1
6. FORWARDING HARDWARE
IM Reg DM Reg
IM Reg DM Reg
and $12, $2, $5
7. FORWARDING HARDWARE
IM Reg DM Reg
IM Reg DM Reg
and $12, $2, $5
▪ The first ALU source comes from the pipeline register when necessary.
if (EX/MEM.RegWrite = 1
and EX/MEM.RegisterRd = ID/EX.RegisterRs)
then ForwardA = 2
▪ The second ALU source is similar.
if (EX/MEM.RegWrite = 1
and EX/MEM.RegisterRd = ID/EX.RegisterRt)
then ForwardB = 2
IM Reg DM Reg
IM Reg DM Reg
and $12, $2, $5
7. FORWARDING HARDWARE
• A MEM/WB hazard may occur between an instruction in the EX
stage and the instruction from two cycles ago.
• One new problem is if a register is updated twice in a row.
add $1, $2, $3
add $1, $1, $4
sub $5, $5, $1
IM Reg DM Reg
add $1, $2, $3
IM Reg DM Reg
add $1, $1, $4
IM Reg DM Reg
sub $5, $5, $1
8. FORWARDING UNIT
if (MEM/WB.RegWrite = 1
and MEM/WB.RegisterRd = ID/EX.RegisterRs
and (EX/MEM.RegisterRd ≠ ID/EX.RegisterRs or
EX/MEM.RegWrite = 0)
then ForwardA = 1
if (MEM/WB.RegWrite = 1
and MEM/WB.RegisterRd = ID/EX.RegisterRt
and (EX/MEM.RegisterRd ≠ ID/EX.RegisterRt or EX/MEM.RegWrite
= 0)
then ForwardB = 1
8. FORWARDING UNIT
PC
0
1
2
ForwardA
Registers
Instruction ALU
memory 0
1 Data
2 memory
1
ForwardB
Rt 0
0
Rd
1 EX/MEM.RegisterRd
Rs ID/EX.
RegisterRt
Forwarding
Unit MEM/WB.RegisterRd
ID/EX.
RegisterRs
8. FORWARDING UNIT
ID/EX.RegisterRs EX/MEM.RegisterRd
MEM/WB.RegisterRd
ID/EX.RegisterRt EX/MEM.RegWrite MEM/WB.RegWrite
(The two RegWrite signals are not shown in the diagram, but they
come from the control unit.)
The fowarding unit outputs are selectors for the ForwardA and
ForwardB multiplexers attached to the ALU. These outputs are
generated from the inputs using the equations on the previous
pages.
Some new buses route data from pipeline registers to the new muxes.
9. EXAMPLE
IF: or $13, $6, $2 ID: and $12, $2, $5 EX: sub $2, $1, $3
PC
101
2 0
102 101
1
2
5
0
Registers
Instruction ALU
103
memory X 105 0
103
1 -2
Data
X 2 memory
1
0
5 (Rt) 0
0
2
12 (Rd) 2
1 EX/MEM.RegisterRd
2 (Rs) ID/EX.
RegisterRt
Forwarding
3
Unit
ID/EX. 1 MEM/WB.RegisterRd
RegisterRs
9. EXAMPLE
IF: add $14, $2, $2 ID: or $13, $6, $2 EX: and $12, $2, $5 MEM: sub $2, $1, $3
PC
102
6 0
106 -2
1
2
2
2
Registers
Instruction ALU -2
105
memory X 102 0
105
1 104
Data
X 2 memory
1
0
2 (Rt) 0
0
12
13 (Rd) 12
1 EX/MEM.RegisterRd
6 (Rs) ID/EX.
RegisterRt
2
Forwarding
5
Unit
2 MEM/WB.RegisterRd
ID/EX.
RegisterRs
-2
9. EXAMPLE
IF: sw $15, 100($2) ID: add $14, $2, $2 EX: or $13, $6, $2 MEM: and $12, $2, $5 WB: sub
$2, $1, $3
PC
106
2 0
-2 106
1
2
2
0
Registers
Instruction ALU 104
102
memory 2 -2 0
-2
1 -2
Data
-2 2
-2 memory
X
1
-2
1
2 (Rt)
9. EXAMPLE
0
13
0
14 (Rd) 13
1 EX/MEM.RegisterRd 2
2 (Rs) ID/EX.
RegisterRt
12
Forwarding
2 Unit
ID/EX. 6 2 MEM/WB.RegisterRd
EX/MEM RegisterRs
104
-2
9. EXAMPLE
WB EX/MEM
Control M WB MEM/WB
IF/ID EX M WB
PC
Read Read 0
register 1 data 1 1
Addr Instr 2
Read ALU
register 2 Zero
ALUSrc
Write Read Result Address
0
Instruction register data 2
1 0 Data
memory
Write Registers 2 memory
data 1
Write Read
Instr [15 - 0] 1
RegDst data data
Extend
Rt 0
0
Rd
1 EX/MEM.RegisterRd
Rs
Forwarding
Unit
MEM/WB.RegisterRd
11. HANDLING DEPENDENT STORES AND LOADS
1 2 3 4 5 6
IM Reg DM Reg
add $1, $2, $3
PC
Read Read 0
register 1 data 1 1
Addr Instr 2
Read ALU
register 2 Zero
ALUSrc
Write Read Result Address
0
Instruction register data 2
1 0 Data
memory
Write Registers 2 memory
data 1
Write Read
Instr [15 - 0] 1
RegDst data data
Extend
Rt 0
0
Rd
1 EX/MEM.RegisterRd
Rs
Forwarding
Unit
MEM/WB.RegisterRd
11. HANDLING DEPENDENT STORES AND LOADS
PC
Read Read 0
register 1 data 1 1
Addr Instr 2
Read ALU
register 2 Zero
ALUSrc
Write Read Result Address
0
Instruction register data 2
1 0 Data
memory
Write Registers 2 memory
data 1
Write Read
Instr [15 - 0] 1
RegDst data data
Extend
Rt 0
0
Rd
1 EX/MEM.RegisterRd
Rs
Forwarding
Unit
MEM/WB.RegisterRd
11. HANDLING DEPENDENT STORES AND LOADS
A harder case:
1 2 3 4 5 6
IM Reg DM Reg
lw $1, 0($2)
THANK YOU