Understanding Pipeline Hazards and Control
Understanding Pipeline Hazards and Control
structural hazards B
data hazards C
control hazards D
exceptions in a pipeline
Task
order
A
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Pipeline Stages in More Detail (cont.) Total Time for Instructions Calculated for Each Component
EX (Execution/Eective Address):
Some instruction stages require less time than others.
Calculates an eective address for accessing memory.
Performs an arithmetic/logical operation on the two register Some instructions require more stages than other instructions.
values.
Performs an arithmetic/logical operation on a register value Instruction Register ALU Data Register Total
and the sign extended immediate value. Instruction class fetch read operation access write time
Load word (lw) 200 ps 100 ps 200 ps 200 ps 100 ps 800 ps
MEM (Memory Access): loads a value from or stores a value Store word (sw) 200 ps 100 ps 200 ps 200 ps 700 ps
into the data cache. R-format (add, sub, AND,
OR, slt)
200 ps 100 ps 200 ps 100 ps 600 ps
WB (Write Back): updates the register le with the result of Branch (beq) 200 ps 100 ps 200 ps 500 ps
an operation or a load.
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Instruction Data
l
w $2,200($0) 800 ps Reg ALU Reg
l
w $3,300($0)
fetch
800 ps
access
Instruction
formats).
fetch
800 ps
Memory access only occurs in load and store instructions.
Program
Accesses to memory must be aligned.
execution 200 400 600 800 1000 1200 1400
Time
order
(in instructions)
Instruction Data
l
w $1,100($0) fetch
Reg ALU
access
Reg
Instruction Data
l
w $2,200($0) 200 ps fetch
Reg ALU
access
Reg
Instruction Data
l
w $3,300($0) 200 ps fetch
Reg ALU
access
Reg
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Resolving Data Hazards with Forwarding Resolving Data Hazards with Stalls
lw $t1,0($t4) IF ID EX MEM WB
lw $t4,8($t0) IF ID EX MEM WB
cycle 1 2 3 4 5 6 7 8 9 10 11 12 13
add $t5,$t1,$t4 IF ID stall EX MEM WB
lw $t1,0($t4) IF ID EX MEM WB
sw $t5,16($t0) IF stall ID EX MEM WB
lw $t2,4($t4) IF ID EX MEM WB
sw $t3,12($t0) IF ID EX MEM WB
sw $t5,16($t0) IF ID EX MEM WB
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
For the following example, ll in when each instruction goes An instruction is control dependent on a branch instruction if
through each stage of the pipeline. the instruction will only be executed when the branch has a
cycle 1 2 3 4 5 6 7 8 9 10 11 12 13
specic result.
lw $3,0($5) An instruction that is control dependent on a branch cannot
add $7,$7,$3 be moved before the branch so that its execution is no longer
lw $4,4($5) controlled by the branch.
sw $7,8($4)
lw $5,0($4) An instruction that is not control dependent on a branch
add $10,$7,$8 cannot be moved after the branch so that its execution is
sub $10,$10,$5 controlled by the branch.
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
not the branch will be taken. bubble bubble bubble bubble bubble
Program
execution Time 200 400 600 800 1000 1200 1400
order
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
(in instructions)
Instruction Data
Resolving Control Hazards by Predicting Not Taken Resolving Control Hazards by Predicting Not Taken (cont.)
add $4, $5, $6 Reg ALU Reg
fetch access
Instruction Data
beq $1, $2, 40 fetch
Reg ALU
access
Reg
200 ps
Instruction Data
lw $3, 300($0) 200 ps Reg ALU Reg
Another solution for control hazards is to predict every
fetch access
The gure below shows there is a one cycle delay when the
conditional branch to be not taken. conditional branch is taken.
The gure below shows there is no delay when the branch is
not taken. Program
execution Time 200 400 600 800 1000 1200 1400
Program order
execution Time 200 400 600 800 1000 1200 1400 (in instructions)
order Instruction Data
(in instructions) add $4, $5, $6 fetch
Reg ALU
access
Reg
Program
execution 200 400 600 800 1000 1200 1400
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Eects from Pipeline Hazards Single Cycle Datapath Separated into Five Parts
IF: Instruction fetch ID: Instruction decode/ EX: Execute/ MEM: Memory access WB: Write back
register file read address calculation
Shift
ADD
Add
result
predictable.
Write 1
0
data Write
data
16 32
Sign-
extend
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Flow of Data in the Datapath Instructions Being Executed Assuming Pipelined Execution
The gure below suggests that each instruction has its own
separate datapath, which is not the case.
Note that each portion of the datapath is being used at most
once during each cycle.
All ow of data in the single cycle datapath goes from left to Time (in clock cycles)
Instruction fetch
Add
Instruction
0 u PC Address Read
Instruction fetch x register 1 Read
M
Instruction
Address 1 data 1
u PC Read
Read Read Zero
x register 1
1 data 1 Instruction register 2 ALU
Registers Read ALU Read
Read Zero memory result Address data 0
Write 0
Instruction register 2 ALU ALU data 2 M
IF/ID Registers Read ID/EX EX/MEM Read MEM/WB register M
memory Address 1 Data u
0 result data u
Write data 2 M Write memory x
M x 1
register Data u data 1
u
Write memory x
x 0
data 1 Write
Add data
Write
16 32
4 Add Add data Sign-
result extend
Shift
16 Sign- 32
left 2
extend
0
M
Instruction
u PC Address Read
x register 1 Read
1 data 1
Read Zero
Instruction register 2 ALU
Registers Read ALU Read
memory result Address data 0
Write 0
data 2 M
register M lw
Data u
u
Write memory x
x 1
data 1 Instruction decode
Write
data
Add Add
result
Shift
left 2
Iw
lw 0
M
Instruction
u PC Address Read Execution
Instruction decode x Read
register 1
1 data 1
Read Zero
Instruction register 2 ALU
Registers Read ALU Read
memory result Address data 1
Write 0
data 2 M
IF/ID ID/EX EX/MEM MEM/WB IF/ID register ID/EX M EX/MEM MEM/WB
Data u
u
Write memory x
x 0
data 1
Write
Add Add data
4 Add Add 4
16 Sign- 32
Add Add
result extend Shift result
Shift
left 2 left 2
0 0
M M
Instruction
Instruction
x register 1 Read x register 1 Read
1 data 1 1 data 1
Read Zero Read Zero
Instruction register 2 ALU Instruction register 2 ALU ALU Read
Registers Read ALU Read Registers 1
memory result Address data 1 memory 0 result Address data
Write data 2
0
M Write Read M
register M register data 2 M u
Data u Data
u
x
u x
Write x memory Write memory
data 1
0
data 1x 0
Write
Write
data data
16 Sign- 32 16 Sign- 32
extend extend
data 1
1
Write
data
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath
16 Sign-
extend
32
Pipeline Control Exceptions
Add Add
0 0
M M
Instruction
Instruction
u PC Address Read u PC Address Read
x register 1 Read Read
x register 1
1 data 1 data 1
1
Read Zero Read Zero
Instruction register 2 ALU register 2
Registers Read ALU Read Instruction ALU ALU Read
memory Address 0 Registers Read 1
0 result data memory result Address data
Write data 2 M Write 0
M data 2 M
register u register M
u Data Data u
x u
Write x memory Write memory x
1 x 0
data 1 data 1
Write Write
data data
16 Sign- 32 16 32
Sign-
extend extend
Iw
Write-back
Intro Hazards IF/ID Pipeline Datapath ID/EX Pipeline Control EX/MEM Exceptions
MEM/WB Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Shift
Add Add
result
Store Instruction in the MEM Stage
left 2
sw sw
0
M
Instruction
u PC Address Read
x register 1 Read Execution
Memory
1 data 1
Read Zero
Instruction register 2 ALU
Registers Read ALU Read
memory result Address data 1
Write 0
data 2 M
register M IF/ID ID/EX EX/MEM MEM/WB
Data u
u
IF/ID Write ID/EX x
EX/MEM memory MEM/WB x
data 0
1
Write
data Add
Add 16 32
Sign- 4 Add Add
result
4
extend
Add Add Shift
Shift result left 2
left 2
0
0 M
Instruction
M u PC Address Read
x register 1 Read
u PC Address Read
Instruction
Read 1 data 1
x register 1 Read Zero
1 data 1
Instruction register 2 ALU
Read Zero Registers Read ALU Read
Instruction ALU ALU memory Address 0
register 2 Read Write 0 result data
1 data 2 M
memory Registers 0 result Address data register M
Write Read M Data u
M u
register data 2 u Write memory x
u Data x 1
x data 1
Write x memory
0
data 1 Write
Write data
data
16 Sign- 32
16 Sign- 32 extend
extend
sw
Write-back
Write
data
16 Sign- 32
instruction.
Write-back
4 Add Add
result
Shift
left 2 Add
4 Add Add
0 result
Shift
M left 2
Instruction
u PC Address Read
x register 1 Read
1 data 1
Read Zero 0
register 2 M
Instruction
Instruction ALU ALU Read
Registers Read u PC Address Read
memory result Address data 1 Read
Write 0 x register 1
data 2 M data 1
register M 1
Data u Read
u Zero
Write memory x
x Instruction register 2 ALU
data 0 Registers Read ALU Read
1 Address 1
memory 0 result data
Write data 2 M
Write register M
Data u
data u
Write memory x
x 0
data 1
16 Sign- 32
extend Write
data
16 Sign- 32
extend
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Datapath Showing All Portions Used for a Load Instruction Multiple-Clock-Cycle Pipeline Diagram of Five Instructions
Time (in clock cycles)
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
IF/ID ID/EX EX/MEM MEM/WB
Program
execution
Add order
4 Add Add (in instructions)
result
Shift
left 2
lw $10, 20($1) IM Reg ALU DM Reg
0
M
Instruction
u PC Address Read
x register 1 Read
1 data 1
Read Zero
register 2
Instruction
memory
Registers Read
ALU ALU
Address
Read
1
sub $11, $2, $3 IM Reg ALU DM Reg
Write 0 result data
data 2 M
register M
Data u
u
Write memory x
x 0
data 1
Write
data add $12, $3, $4 IM Reg ALU DM Reg
16 Sign- 32
extend
Traditional Pipeline Diagram of Five Instruction Pipeline Datapath with Five Instructions Active
Each instruction is in a dierent pipeline stage.
add $14, $5, $6 lw $13, 24 ($1) add $12, $3, $4 sub $11, $2, $3 lw $10, 20($1)
Instruction fetch Instruction decode Execution Memory Write-back
Time (in clock cycles)
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
Program
execution IF/ID ID/EX EX/MEM MEM/WB
order
(in instructions)
Add
Instruction Instruction Data Add
lw $10, 20($1) fetch decode
Execution
access
Write-back 4 Add
result
Shift
left 2
Instruction Instruction Data
sub $11, $2, $3 fetch decode
Execution
access
Write-back
0
Instruction Instruction Data M
add $12, $3, $4 fetch decode
Execution
access
Write-back u PC Address Read
Read
Instruction
x register 1
1 data 1
Instruction Instruction Data
lw $13, 24($1) fetch decode
Execution
access
Write-back Read
register 2
Zero
Instruction ALU ALU Read
Registers Read 1
memory 0 result Address data
Instruction Instruction Data Write data 2 M
add $14, $5, $6 fetch decode
Execution
access
Write-back register M
u Data u
Write memory x
x 0
data 1
Write
data
16 Sign- 32
extend
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
ALU Control Bits Depend on ALUOp and Function Code Control Signal Eects
The table below shows the eects for each signal that controls
ALUOp is set depending on the instruction opcode.
the pipelined datapath.
The ALU control input for R-type instructions is aected by
the Function code. Signal name Effect when deasserted (0) Effect when asserted (1)
RegDst The register destination number for the Write The register destination number for the Write register comes
register comes from the rt field (bits 20:16). from the rd field (bits 15:11).
Instruction Instruction Desired ALU control
RegWrite None. The register on the Write register input is written with the value
opcode ALUOp operation Function code ALU action input
on the Write data input.
LW 00 load word XXXXXX add 0010 ALUSrc The second ALU operand comes from the second The second ALU operand is the sign-extended, lower 16 bits of
SW 00 store word XXXXXX add 0010 register file output (Read data 2). the instruction.
Branch equal 01 branch equal XXXXXX subtract 0110 PCSrc The PC is replaced by the output of the adder that The PC is replaced by the output of the adder that computes
R-type 10 add 100000 add 0010 computes the value of PC + 4. the branch target.
R-type 10 subtract 100010 subtract 0110 MemRead None. Data memory contents designated by the address input are
put on the Read data output.
R-type 10 AND 100100 AND 0000
MemWrite None. Data memory contents designated by the address input are
R-type 10 OR 100101 OR 0001
replaced by the value on the Write data input.
R-type 10 set on less than 101010 set on less than 0111
MemtoReg The value fed to the register Write data input The value fed to the register Write data input comes from the
comes from the ALU. data memory.
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Control Signals Organized by Pipeline Stage Control Signals Passed through Pipeline Registers
Control signals are passed through pipeline registers until they
are used.
The table below shows how the signals are used to control WB
each pipeline stage after instruction decode (ID).
Instruction
Control M WB
Execution/address calculation stage Memory access stage Write-back stage
control lines control lines control lines
Mem- Mem- Reg- Memto- EX M WB
Instruction RegDst ALUOp1 ALUOp0 ALUSrc Branch Read Write Write Reg
R-format 1 1 0 0 0 0 0 1 0
lw 0 0 0 1 0 1 0 1 1
sw X 0 0 1 0 0 1 0 X
beq X 0 1 0 1 0 0 0 X
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
WB
Program
EX M
IF/ID execution
order
(in instructions)
Add
4 Add Add
result
sub $2, $1, $3 IM Reg DM Reg
Shift Branch
RegWrite
left 2
ALUSrc
MemWrite
0
MemtoReg
M
Address
Instruction
u PC Read
x register 1 Read
data 1
and $12, $2, $5 IM Reg DM Reg
1
Read Zero
Instruction register 2 ALU ALU Read
memory Registers Read Address 1
Write 0 result data
data 2 M M
register u
u Data
Write x memory x
data 1 0
or $13, $6, $2 IM Reg DM Reg
Write
data
Instruction
[15–0] 16 Sign- 32 6
ALU
extend control MemRead
Example of Resolving Pipeline Hazards with Forwarding Pipelined Datapath without Forwarding
Value of $2 can be obtained from pipeline registers.
The gure below shows a simplied pipelined datapath without
forwarding.
Time (in clock cycles)
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
Value of register $2: 10 10 10 10 10/–20 –20 –20 –20 –20
Value of EX/MEM: X X X –20 X X X X X
Value of MEM/WB: X X X X –20 X X X X ID/EX EX/MEM MEM/WB
Program
execution
order
(in instructions)
Registers
Reg ALU
sub $2, $1, $3 IM DM Reg
Data
memory M
and $12, $2, $5 IM Reg DM Reg u
x
a. No forwarding
sw $15, 100($2)
IM Reg DM Reg
Forwarding
a. No forwarding
compares a destination register number of an ForwardB
u
x
b. With forwarding
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Datapath Modied to Resolve Data Hazards via Forwarding Forwarding Control Logic for EX Hazard
The forwarding unit takes register numbers as input and
produces control signals as outputs.
ID/EX
WB
if (EX/[Link] and
EX/MEM
Control M WB
MEM/WB
Registers
ALU M
if (EX/[Link] and
u
Instruction x
PC M
memory Data
u
x memory
EX/[Link] != 0 and
EX/[Link] == ID/[Link])
IF/[Link]
IF/[Link]
Rs
Rt ForwardB = 10
IF/[Link] Rt EX/[Link]
M
IF/[Link] Rd u
x
Forwarding MEM/[Link]
unit
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Forwarding Control Logic for MEM Hazard Datapath Allowing Immediate as Second ALU Input
ID/EX EX/MEM MEM/WB
if (MEM/[Link] and
MEM/[Link] != 0 and M
u
not (EX/[Link] and x
MEM/[Link] == ID/[Link]) M
u
M Data
u
ForwardA = 01 x x
memory M
u
x
if (MEM/[Link] and
MEM/[Link] != 0 and
not (EX/[Link] and
M
EX/[Link] != 0 and u
x
EX/[Link] == ID/[Link]) Forwarding
MEM/[Link] == ID/[Link]) unit
ForwardB = 01
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Example Sequence of Instructions with a Load Hazard Control Logic for Load Hazard Detection
A hazard from a load followed by an immediate use of the
loaded register cannot be resolved by forwarding.
Time (in clock cycles)
Program
execution
order
instruction decode (ID) stage.
(in instructions) if (ID/[Link] and
lw $2, 20($1) IM Reg DM Reg (ID/[Link] == IF/[Link] or
ID/[Link] == IF/[Link]))
and $4, $2, $5 IM Reg DM Reg stall the pipeline
Stalls can be inserted in the pipeline by deasserting all the
or $8, $2, $6 IM Reg DM Reg
control signals coming out of the ID stage.
add $9, $4, $2 IM Reg DM Reg
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
How Stalls Are Inserted into the Pipeline Pipeline Datapath with Hazard Detection Unit Added
If it is found that an instruction needs to stall, then a noop is Hazard
detection
ID/[Link]
inserted into the pipeline after the ID stage and the instruction unit
IF/DWrite
stays in the ID stage. ID/EX
WB
EX/MEM
M
Time (in clock cycles) Control u M WB
PCWrite MEM/WB
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9 CC 10 x
IF/ID 0 EX M WB
Program
execution
order M
(in instructions) u
x
Instruction
Registers
lw $2, 20($1) IM Reg DM Reg M
ALU u
PC
Instruction
bubble memory M Data x
u memory
and becomes nop IM Reg DM Reg x
The Impact of a Taken Branch on the Pipeline Reducing the Delay of Branches
If the decision to take a branch occurs in the MEM stage, then
a taken branch will cause a delay of three cycles.
Time (in clock cycles)
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
Program
execution
order If the branch execution can be moved earlier in the pipeline,
(in instructions)
then fewer instructions will need to be ushed.
40 beq $1, $3, 28 IM Reg DM Reg
Compute the branch target address in the ID stage.
Compare the values of two registers for equality in the ID
44 and $12, $2, $5 IM Reg DM Reg stage.
and $12, $2, $5 beq $1, $3, 7 sub $10, $4, $8 before<1> before<2>
Hazard
detection
unit
52 add $14, $2, $2 IM Reg DM Reg
ID/EX
WB
EX/MEM
M
Control u M WB MEM/WB
28
72 lw $4, 50($7) IM Reg DM Reg + x
IF/ID 0 EX M WB
44
72
48
+ Shift
M $4
4 left 2 $1 u
x
Registers =
M
ALU M
Instruction $3 u
u PC M $8
Data
x 72 44 memory x
7 u memory
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath
Sign-
Pipeline Control
x Exceptions
extend
Pipeline Datapath When Branch Is Decoded Pipeline Datapath After Branch Is Taken 10
Forwarding
Branch is now resolved in the ID stage. A taken branch now causes a one cycle stall.
unit
Clock 3
and $12, $2, $5 beq $1, $3, 7 sub $10, $4, $8 before<1> before<2> lw $4, 50($7) Bubble (nop) beq $1, $3, 7 sub $10, . . . before<1>
[Link]
[Link] Hazard
detection
unit
Hazard
detection ID/EX
unit WB
EX/MEM
ID/EX M
Control u M WB MEM/WB
WB
EX/MEM + x
M IF/ID 0 EX M WB
72
Control u M WB MEM/WB
28 76
+ x + Shift
M $1
IF/ID 0 EX M WB 4 left 2 u
44
72 x
48
+ Shift
M $4 Registers =
4 left 2 $1 u ALU
M
x M
u PC Instruction u
Registers M $3 Data x
=
ALU M x 76 72 memory u
M memory
u PC Instruction $3
$8 u x
M Data x Sign-
x 72 44 memory
7 u memory extend
x
Sign-
extend
10
Forwarding
10
unit
Forwarding
unit Clock 4
Clock 3
lw $4, 50($7) Bubble (nop) beq $1, $3, 7 sub $10, . . . before<1>
[Link]
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Problems with Resolving a Branch in the ID Stage 1-Bit Branch Prediction Buer
Small memory indexed by the lower portion of the word
address of the branch instruction during the IF stage.
There are multiple problems with the approach of trying to Each element of this memory contains a bit indicating if the
resolve a branch in the ID stage. branch was last taken or not.
Will require new forwarding logic for the equality test. If the prediction is found to be incorrect, then the bit is
May introduce new data hazards if one or both register values inverted.
are not yet available. Branch prediction buer (BPB) with 1024 entries:
If the pipeline is deeper (has more stages), which is common,
then it is just infeasible to resolve the branch in the second
stage.
Instruction Address 1 0
Solutions
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX00 0 1
Predict the branch result. __________
1 2
Delay the execution of the branch.
...
Uses a 10 bit index into a 1024 BPB. 1 1023
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
...
Predict not taken Predict not taken
Taken
L3: Not taken
addiu $t1,$t1,1
slti $t0,$t1,100
bne $t0,$zero,L1
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
not taken
Uses a 10 bit index into a 1024 BTB. 1023
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Hazard
An exception or an interrupt is an event other than regular
detection
unit transfers of control (branches, jumps, calls, returns) that
changes the normal ow of instruction execution.
ID/EX
WB
MEM/WB
Control
M
u M WB EX/MEM
An exception refers to any unexpected change in control ow
without distinguishing if the cause is internal or external.
0 x
IF/ID EX M WB
+
4
+
Shift
An interrupt means that the event is externally caused.
left 2
M
u
=
x type of event from where? MIPS terminology
Registers
M Instruction Data
u PC ALU
memory memory M
x
M
u
x
I/O device request external interrupt
u
x
syscall internal exception
Sign-
extend
arithmetic overflow internal exception
M
page fault internal exception
u
x
Fowarding
unit
undefined instruction internal exception
hardware malfunction either exception or interrupt
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath Pipeline Control Exceptions
[Link]
Hazard
detection
unit
M
ID/EX u
EX/MEM
M
u
x
Forwarding
unit
Registers =
12 M
M Instruction
$7 u
80000180 u PC M
x 80000180 54 memory $1 Data x
u
Intro Hazards Pipeline Datapath Pipeline Control Exceptions Intro Hazards Pipeline Datapath
Sign-
Pipeline Control
x
memory
Exceptions
extend
Handling an Arithmetic Exception in the Pipeline Handling an Arithmetic Exception in the Pipeline (cont.) 15 $1
M
u
x
13 12
Clock 6 Forwarding
unit
The address after the add is saved in the EPC and ush signals Instructions are now converted to bubbles in the pipeline.
cause control values in the pipeline registers to be cleared.
sw $26, 1000($0) bubble (nop) bubble bubble or $13, . . .
lw $16, 50($7) slt $15, $6, $7 add $1, $2, $1 or $13, . . . and $12, . . . [Link]
[Link] [Link]
[Link] [Link]
[Link] Hazard
Hazard detection
detection unit
unit
M 00
M ID/EX u
ID/EX u 0 0 x
WB 0 EX/MEM
0
WB
10
0 x M M
M EX/MEM 0
M 000 WB 00
0 M 10
Control u u MEM/WB
Control u M 000 WB MEM/WB x
Cause
Cause u IF/ID + 0 0 EX 0 x M WB
IF/ID + 0 x 0 EX 50 EPC 0 x M WB
1
80000180
58 EPC
54 + M
58 Shift
+ Shift
M $2
4 left 2 u
4 left 2 $6 u x
x Registers =
Registers
13 ALU M
12
=
M M u
u Instruction
M Instruction
$7 u
80000180 PC
memory
M Data x
80000180 u PC M x 80000184 u
x 80000180 54 memory $1 Data x memory
u memory
x
x Sign-
extend
Sign-
extend
13
M
13 12 u
M
15 u x
$1
x Clock 7 Forwarding
Clock 6 Forwarding unit
unit
M
superpipelining 13
Registers = ALU M
u Fallacy: Pipelining is easy.
Means more stages in the [Link]
80000180 u PC Instruction
x
There are a lot of issues (forwarding, hazards, exceptions) to
memory Data
x 80000184 memory
pipeline and advance to each stage during each cycle. Dynamic scheduling became more feasible as logic became
Lowers the cycles per instruction (CPI). much faster than memory.
Increases the number of pipeline stalls.
Pitfall: Failure to consider instruction set design can adversely
dynamic scheduling impact pipelining.
Allows instructions to be executed out of order when Variable length instructions and dierent running times can
instructions that previously entered the pipeline are stalled or lead to imbalance among pipeline stages and complicate
require additional cycles. hazard detection.
Allows for useful work during some instruction stalls. Complicated addressing modes complicates pipeline control.
Often increases cycle time and energy usage.