















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















t^
Pi
li i
Lecture 10 – Pipelining II
overlapped
increasingnumber of
overlapped
number ofinstructions
increasing time
sequential
increasing time
3
Structural hazards– Instructions in different stages need the same
resource, eg, memory controller, arithmetic unit
Data hazards– data not available to perform next operation when
neededneeded
Control hazards– data not available to make branch decision soon
enough
5
Example 1: two instructions need the same resource
p
Solution: add more hardware to eliminate problem– separate instruction and data memory caches or
memory unit ports
Or stall – do things one at a timeOr stall
do things one at a time
6
When an instruction depends on the results of aprevious instruction still in the pipelineprevious instruction still in the pipeline
-^
This is a data dependency
$s
is
add
$s
,
$s1,
$s
written here
add
$s4,
$s3,
$s
$s
is read here
8
One solution: Stall the pipeline until the result is
-^
One solution: Stall the pipeline until the result isavailable– this would create a 3-cycle
p
ipeline bubble
y
p p
add
s0,s1,s
add
s0,s1,s
add
s4,s3,s
stall
9
add
s0,s1,s
add
s4,s3,s
11
sub
$2,
$1,
$
and
$12,
$2,
$
or
$13,
$6,
$
add
$
$
$
add
$14,
$2,
$
sw
$15,
100($2)
How would this code sequence fare in our pipelineddatapath without forwarding?
12
Clock cycle
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
sub
$2, $1, $
IF
ID
EX
MEM
WB
and
$12, $2, $
IF
ID
EX
MEM
WB
and
$12, $2, $
IF
ID
EX
MEM
WB
or
$13, $6, $
IF
ID
EX
MEM
WB
add
$14, $2, $
IF
ID
EX
MEM
WB
add
$^
, $ , $
M M
W
sw
$15, 100($2)
IF
ID
EX
MEM
WB
-^
The ADD instruction is okay because of the register file
-^
The
ADD instruction is okay, because of the register file
design.– Registers are written at the beginning of a clock cycle.– The new value will be available by the end of that cycle
The new value will be available by the end of that cycle.
-^
The SW is no problem at all, since it reads $2 after the SUBfinishes.
14
Clock cycle
1
2
3
4
5
6
7
8
9
sub
$2, $1, $
IF
ID
EX
MEM
WB
and
$12, $2, $
IF
ID
EX
MEM
WB
or
$13, $6, $
IF
ID
EX
MEM
WB
add
$14, $2, $
IF
ID
EX
MEM
WB
sw
$15, 100($2)
IF
ID
EX
MEM
WB
-^
Arrows indicate the flow of data between instructionsArrows indicate the flow of data between instructions.– The tails of the arrows show when register $2 is written.– The heads of the arrows show when $2 is read.
-^
Any arrow that points backwards in time represents a dataAny arrow that points backwards in time represents a datahazard in our basic pipelined processor. Here, hazards existbetween instructions 1 & 2 and 1 & 3.
15
What if the first instruction is lw?
-^
This is a true data hazard: s0 isn’t known until after theMEM stage
We can’t forward back into the past
Either
stall
or
reorder
instructions
lw
s
,0(s2)
add
s4,s3,
s
17
lw
s
0(s2)
lw
s
,0(s2)
add
s4,s3,s
stall
add
s
,s3,s
stall
18
we don’t knowuntil here
bne $s0, $s1, skip
$^
, $
,^
p
add $s4, $s3, $s0...
do we fetch the add
or the
sub
?
skip:
sub $s4, $s3, $s
20
Stall until we know which instruction to execute next– We know the result at the end of the EX state (or
earlier if we compare the registers in the ID stage)Still causes a 1 or 2 cycle stall even with forwarding
the branch will be taken
IF
ID
EX
MEM
WB
bne
$s0,
$s1,
next
IF
ID
EX
MEM
WB
stall
sub
$s4,
$s3,
$s
21