Académique Documents
Professionnel Documents
Culture Documents
Pipeline: Hazards
Lecturer: Prof. Hong Jiang
Courtesy of Prof. Yifeng Zhu, U. of Maine
Fall, 2006
CSCE430/830
Pipeline Hazards
Pipelining Outline
Introduction
Defining Pipelining
Pipelining Instructions
Hazards
Structural hazards
Data Hazards
Control Hazards
Performance
Controller implementation
CSCE430/830
Pipeline Hazards
Pipeline Hazards
Where one instruction cannot immediately
follow another
Types of hazards
Structural hazards - attempt to use the same resource by
two or more instructions
Control hazards - attempt to make branching decisions
before branch condition is evaluated
Data hazards - attempt to use data before it is ready
CSCE430/830
Pipeline Hazards
Structural Hazards
Attempt to use the same resource by two or
more instructions at the same time
Example: Single Memory for instructions and
data
Accessed by IF stage
Accessed at same time by MEM stage
Solutions
Delay the second access by one clock cycle, OR
Provide separate memories for instructions & data
This is what the book does
This is called a Harvard Architecture
Real pipelined processors have separate caches
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
LW
IF/ID
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
SW
LW
IF/ID
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
SUB
SW
IF/ID
LW
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
ADD
IF/ID
SW
ID/EX
EX/MEM
LW
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
ADD
ID/EX
EX/MEM
SW
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
SUB
IF/ID
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
ID/EX
EX/MEM
MEM/WB
ADD
ADD
4
<<2
PC
ADDR
RD
Instruction
Memory
32
5
5
RN1
RN2
WN
WD
RD1
Register
File RD2
16
CSCE430/830
E
X
T
N
D
32
ALU
M
U
X
Zero
ADDR
Data
Memory
WD
RD
M
U
X
Pipeline Hazards
lw $r0, 10($r1)
sw $r3, 20($r4)
CSCE430/830
CC 1
CC 2
CC 3
CC 4
CC 5
CC 6
IM
REG
ALU
DM
REG
IM
REG
ALU
DM
IM
REG
ALU
DM
IM
REG
ALU
CC 7
CC 8
REG
REG
DM
REG
Pipeline Hazards
lw $r0, 10($r1)
sw $r3, 20($r4)
CSCE430/830
CC 1
CC 2
CC 3
CC 4
CC 5
CC 6
IM
REG
ALU
DM
REG
IM
REG
ALU
DM
IM
REG
ALU
DM
IM
REG
ALU
CC 7
CC 8
Memory Conflict
REG
REG
DM
REG
Pipeline Hazards
CSCE430/830
Ifetch
Reg
Bubble
DMem
Reg
DMem
Bubble Bubble
Ifetch
Reg
Reg
Bubble
ALU
Instr 3
Reg
Reg
ALU
Stall
Ifetch
DMem
ALU
O
r
d
e
r
Instr 2
Reg
ALU
I Load Ifetch
n
s
t Instr 1
r.
Bubble
DMem
Reg
Pipeline Hazards
Structural Hazards
Some common Structural Hazards:
Memory:
weve already mentioned this one.
Floating point:
Since many floating point instructions require many cycles, its easy for
them to interfere with each other.
CSCE430/830
Pipeline Hazards
Structural Hazards
Dealing with Structural Hazards
Stall
low cost, simple
Increases CPI
use for rare case since stalling has performance effect
Pipeline hardware resource
useful for multi-cycle resources
good performance
sometimes complex e.g., RAM
Replicate resource
good performance
increases cost (+ maybe interconnect delay)
useful for cheap or divisible resources
CSCE430/830
Pipeline Hazards
Structural Hazards
CSCE430/830
Pipeline Hazards
Structural Hazards
We want to compare the performance of two machines. Which machine is faster?
Machine A: Dual ported memory - so there are no memory stalls
Machine B: Single ported memory, but its pipelined implementation has a clock
rate that is 1.05 times faster
Assume:
Ideal CPI = 1 for both
Loads are 40% of instructions executed
CSCE430/830
Pipeline Hazards
Cycle Timeunpipelined
Ideal CPI Pipeline depth
Speedup
Pipeline Hazards
Structural Hazards
We want to compare the performance of two machines. Which machine is faster?
Machine A: Dual ported memory - so there are no memory stalls
Machine B: Single ported memory, but its pipelined implementation has a 1.05
times faster clock rate
Assume:
Ideal CPI = 1 for both
Loads are 40% of instructions executed
SpeedUpA = Pipeline Depth/(1 + 0) x (clockunpipe/clockpipe)
= Pipeline Depth
SpeedUpB = Pipeline Depth/(1 + 0.4 x 1)
x (clockunpipe/(clockunpipe / 1.05)
= (Pipeline Depth/1.4) x 1.05
= 0.75 x Pipeline Depth
SpeedUpA / SpeedUpB = Pipeline Depth / (0.75 x Pipeline Depth) = 1.33
CSCE430/830
Pipeline Hazards
Pipelining Summary
Pipeline Depth
1 + Pipeline stall CPI
CSCE430/830
Pipeline Hazards
Review
Speedup of pipeline
Speedup =
Pipeline Depth
1 + Pipeline stall CPI
CSCE430/830
Pipeline Hazards
Pipelining Outline
Introduction
Defining Pipelining
Pipelining Instructions
Hazards
Structural hazards
Data Hazards
Control Hazards
Performance
Controller implementation
CSCE430/830
Pipeline Hazards
Pipeline Hazards
Where one instruction cannot immediately
follow another
Types of hazards
Structural hazards - attempt to use same resource twice
Control hazards - attempt to make decision before
condition is evaluated
Data hazards - attempt to use data before it is ready
CSCE430/830
Pipeline Hazards
Data Hazards
Data hazards occur when data is used before
it is ready
Time (in clock cycles)
CC 1
Value of
register $2: 10
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
10
10
10/ 20
20
20
20
20
DM
Reg
Program
execution
order
(in instructions)
sub $2, $1, $3
or $13, $6, $2
sw $15, 100($2)
IM
Reg
IM
DM
Reg
IM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
Reg
Reg
DM
Reg
The use of the result of the SUB instruction in the next three instructions causes a
data hazard, since the register $2 is not written until after those instructions read it.
CSCE430/830
Pipeline Hazards
Data Hazards
Execution Order is:
InstrI
InstrJ
I: add r1,r2,r3
J: sub r4,r1,r3
CSCE430/830
Pipeline Hazards
Data Hazards
Execution Order is:
InstrI
InstrJ
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Called an anti-dependence by compiler writers.
This results from reuse of the name r1.
CSCE430/830
Pipeline Hazards
Data Hazards
Execution Order is:
InstrI
InstrJ
I: sub r1,r4,r3
J: add r1,r2,r3
K: mul r6,r1,r7
CSCE430/830
Pipeline Hazards
or $13, $6, $2
sw $15, 100($2)
1a:
1b:
2a:
2b:
CSCE430/830
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
10
10
10/ 20
20
20
20
20
IF/ID
IM
ID/EX
EX/MEM MEM/WB
Reg
IM
DM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
DM
Reg
IM
Reg
Reg
Reg
DM
EX/MEM.RegisterRd = ID/EX.RegisterRs
EX/MEM.RegisterRd = ID/EX.RegisterRt
MEM/WB.RegisterRd = ID/EX.RegisterRs
MEM/WB.RegisterRd = ID/EX.RegisterRt
Reg
EX hazard
MEM hazard
Pipeline Hazards
Data Hazards
Solutions for Data Hazards
Stalling
Forwarding:
connect new value directly to next stage
Reordering
CSCE430/830
Pipeline Hazards
0
add $s0 ,$t0,$t1
STALL
STALL
sub $t2, $s0 ,$t3
IF
ID
EX
MEM
10
W
s0
12
16
18
$s0
written
here
IF
R
s0
EX
MEM
WB
$s0 read
here
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
IF
ID
ID
EX
MEM
10
12
16
18
W
s0
new value
of s0
IF
R
s0
EX
MEM
WB
Pipeline Hazards
IF
ID
ID
EX
MEM
10
12
16
18
W
s0
new value
of s0
STALL
CSCE430/830
IF
R
s0
EX
MEM
WB
Pipeline Hazards
This is another
representation
of the stall.
Data Hazards
LW
R1, 0(R2)
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
R8, R1, R9
LW
R1, 0(R2)
CSCE430/830
R8, R1, R9
IF
WB
ID
EX
MEM
WB
IF
ID
stall
EX
MEM
WB
IF
stall
ID
EX
MEM
WB
stall
IF
ID
EX
MEM
WB
Pipeline Hazards
Forwarding
Key idea: connect data internally before it's stored
Time (in clock cycles)
CC 1
Value of
register $2: 10
Program
execution
order
(in instructions)
sub $2, $1, $3
or $13, $6, $2
sw $15, 100($2)
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
10
10
10/ 20
20
20
20
20
IF/ID
IM
ID/EX
EX/MEM
Reg
IM
MEM/WB
DM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
DM
Reg
IM
Reg
Reg
Reg
DM
Reg
Pipeline Hazards
No Forwarding
CSCE430/830
Pipeline Hazards
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
X
X
10
X
X
10
20
X
10/ 20
X
20
20
X
X
20
X
X
20
X
X
20
X
X
DM
Reg
Program
execution order
(in instructions)
sub $2, $1, $3
or $13, $6, $2
sw $15, 100($2)
CSCE430/830
IM
Reg
IM
Reg
IM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
DM
Reg
Reg
DM
Reg
Assumption:
The register file forwards values that are read
and written during the same cycle.
Pipeline Hazards
CSCE430/830
Pipeline Hazards
Review
Speedup of pipeline
Speedup =
Pipeline Depth
1 + Pipeline stall CPI
CSCE430/830
Pipeline Hazards
Pipelining Outline
Introduction
Defining Pipelining
Pipelining Instructions
Hazards
Structural hazards
Data Hazards
Control Hazards
Performance
Controller implementation
CSCE430/830
Pipeline Hazards
Forwarding
CSCE430/830
Pipeline Hazards
SUB
ADD
1
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
EX Hazard: SUB result not written until its WB, ready at end
of its EX, needed at start of ADDs EX
EX/MEM Forwarding: forward $s0 from EX/MEM to ALU input
in ADD EX stage (CC4)
CSCE430/830
Pipeline Hazards
SUB
ADD
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
SUB
ADD
OR
IF
ID
IF
EX
MEM
WB
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
MEM Hazard: SUB result not written until its WB, stored in
MEM/WB, needed at start of ORs EX
MEM/WB Forwarding: forward $s0 from MEM/WB to ALU
input in OR EX stage (CC5)
CSCE430/830
Pipeline Hazards
SUB
ADD
OR
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
Program
execution
order
(in instructions)
sub $2, $1, $3
or $13, $6, $2
sw $15, 100($2)
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
10
10
10/ 20
20
20
20
20
IF/ID
IM
ID/EX
EX/MEM MEM/WB
Reg
IM
DM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
DM
Reg
IM
Reg
Reg
Reg
DM
Reg
EX hazard
MEM hazard
Pipeline Hazards
Data Hazards
Solutions for Data Hazards
Stalling
Forwarding:
connect new value directly to next stage
Reordering
CSCE430/830
Pipeline Hazards
0
add $s0 ,$t0,$t1
STALL
STALL
sub $t2, $s0 ,$t3
IF
ID
EX
MEM
10
W
s0
12
16
18
$s0
written
here
IF
R
s0
EX
MEM
WB
$s0 read
here
CSCE430/830
Pipeline Hazards
CC 2
CC 3
CC 4
CC 5
CC 6
CC 7
CC 8
CC 9
10
X
X
10
X
X
10
20
X
10/ 20
X
20
20
X
X
20
X
X
20
X
X
20
X
X
DM
Reg
Program
execution order
(in instructions)
sub $2, $1, $3
or $13, $6, $2
sw $15, 100($2)
CSCE430/830
IM
Reg
IM
Reg
IM
DM
Reg
IM
Reg
DM
Reg
IM
Reg
DM
Reg
Assumption:
The register file forwards values that are read
and written during the same cycle.
Reg
DM
Reg
Pipeline Hazards
Forwarding
00
01
10
00
01
10
Add hardware to feed back ALU and MEM results to both ALU inputsPipeline Hazards
CSCE430/830
Controlling Forwarding
Need to test when register numbers match in
rs, rt, and rd fields stored in pipeline registers
"EX" hazard:
EX/MEM - test whether instruction writes register file and
examine rd register
ID/EX - test whether instruction reads rs or rt register and
matches rd register in EX/MEM
"MEM" hazard:
MEM/WB - test whether instruction writes register file and
examine rd (rt) register
ID/EX - test whether instruction reads rs or rt register and
matches rd (rt) register in EX/MEM
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
Pipeline Hazards
LW
ADD
CSCE430/830
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
LW
ADD
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
LW doesnt write $s0 to Reg File until the end of CC5, but
ADD reads $s0 from Reg File in CC3
CSCE430/830
Pipeline Hazards
LW
ADD
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
LW
ADD
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
LW
ADD
IF
ID
EX
MEM
WB
IF
ID
bubbl
ID
e
EX
MEM
WB
Pipeline Hazards
LW
ADD
IF
ID
EX
IF
ID
bubbl
ID
e
WB
EX
MEM
WB
Pipeline Hazards
LW
NOP
ADD
IF
ID
EX
MEM
WB
bubbl
IF
e
bubbl
ID
e
bubbl
EX
e
bubbl
MEM
e
bubbl
WB
e
ID
EX
MEM
IF
WB
Pipeline Hazards
LW $s0, 100($t0)
ADD $t2, $s0, $t3
LW
ADD
CSCE430/830
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
Pipeline Hazards
LW
ADD
IF
ID
EX
MEM
WB
IF
ID
ID
EX
MEM
WB
Pipeline Hazards
CSCE430/830
$2, $1, $3
$12, $2, $5
$13, 100($2)
$14, $2, $2
$15, 100($2)
$4, $7, $15
Pipeline Hazards
$t0,
$t2,
$t2,
$t0,
0($t1)
4($t1)
0($t1)
4($t1)
CSCE430/830
$t0,
$t2,
$t0,
$t2,
0($t1)
4($t1)
4($t1)
0($t1)
Pipeline Hazards
CSCE430/830
Pipeline Hazards
Pipelining Outline
Next class
Introduction
Defining Pipelining
Pipelining Instructions
Hazards
Structural hazards
Data Hazards
Control Hazards
Performance
Controller implementation
CSCE430/830
Pipeline Hazards
Pipeline Hazards
Where one instruction cannot immediately
follow another
Types of hazards
Structural hazards - attempt to use same resource twice
Control hazards - attempt to make decision before
condition is evaluated
Data hazards - attempt to use data before it is ready
CSCE430/830
Pipeline Hazards
Control Hazards
A control hazard is when we need to find the
destination of a branch, and cant fetch any new
instructions until we know that destination.
A branch is either
Taken: PC <= PC + 4 + Immediate
Not Taken: PC <= PC + 4
CSCE430/830
Pipeline Hazards
Control Hazards
Ifetch
Reg
Ifetch
Reg
ALU
Ifetch
Reg
ALU
18: or r6,r1,r7
Reg
ALU
Ifetch
ALU
Ifetch
Reg
Reg
DMem
Reg
DMem
Reg
DMem
ALU
DMem
Reg
DMem
Reg
Pipeline Hazards
Branch Hazards
Just stalling for each branch is not practical
Common assumption: branch not taken
When assumption fails: flush three
instructions
Time (in clock cycles)
Program
execution
CC 1
CC 2
order
(in instructions)
40 beq $1, $3, 7
48 or $13, $6, $2
72 lw $4, 50($7)
IM
CC 3
Reg
IM
CC 4
CC 5
DM
Reg
Reg
IM
DM
Reg
IM
CC 6
CC 8
CC 9
Reg
DM
Reg
IM
CC 7
Reg
DM
Reg
Reg
DM
Reg
(Fig. 6.37)
CSCE430/830
Pipeline Hazards
Pipeline Hazards
Pipeline Hazards
CSCE430/830
Pipeline Hazards
Predict
assume an outcome and continue fetching (undo if
prediction is wrong)
lose cycles only on mis-prediction
Delayed branch
specify in architecture that the instruction
immediately following branch is always executed
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
add $r4,$r5,$r6
beq $r0,$r1,tgt
IF
10
ID
EX
MEM
WB
IF
ID
EX
MEM
12
16
18
WB
STALL
BUBBLE BUBBLE BUBBLE BUBBLE BUBBLE
IF
sw $s4,200($t5)
beq
writes PC
here
CSCE430/830
ID
EX
MEM
WB
new PC
used here
Pipeline Hazards
add $r4,$r5,$r6
beq $r0,$r1,tgt
tgt:
sw $s4,200($t5)
IF
10
12
ID
EX
MEM
WB
IF
ID
EX
MEM
WB
IF
ID
EX
MEM
16
18
WB
Fetch assuming
branch taken
CSCE430/830
Pipeline Hazards
add $r4,$r5,$r6
beq $r0,$r1,tgt
tgt:
sw $s4,200($t5)
(incorrect - STALL)
IF
10
ID
EX
MEM
WB
IF
ID
EX
MEM
IF
12
16
18
WB
IF
or $r8,$r8,$r9
ID
EX
MEM
WB
Squashed
instruction
CSCE430/830
Pipeline Hazards
a31a30a11a2a1a0
branch instruction
1K-entry BHT
10-bit index
1
Instruction memory
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
11
NT
T
T
Predict Not
Taken
01
10
Predict Taken
NT
NT
T
00
Predict Not
Taken
NT
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
Pipeline Hazards
Prediction accuracy of 4K-entry 2-bit prediction buffer vs. infinite 2-bit buffer
increasing buffer size from 4K does not significantly improve performance
CSCE430/830
Pipeline Hazards
CSCE430/830
beq $R1,$R2,20
add $R4,$R5,$R6
lw $R3,400($R0)
Pipeline Hazards
CSCE430/830
Pipeline Hazards
CSCE430/830
Pipeline Hazards
MIPS Instructions
31
CSCE430/830
6 bits
5 bits
5 bits
5 bits
5 bits
op
rs
rt
rd
6 bits
5 bits
5 bits
16 bits
op
rs
rt
offset
6 bits
shamt funct
6 bits
26 bits
op
address
0
0
R-Format
I-Format
J-Format
Pipeline Hazards
CSCE430/830
if ($s1==$s1) PC = PC + 4 + 4*25
Pipeline Hazards