Download Basic Pipelining in Computer System Architecture - Assignment | CMSC 411 and more Assignments Computer Science in PDF only on Docsity!
CMSC 411
Computer Systems Architecture
y Lecture 5
Basic Pipelining (cont )Basic Pipelining (cont.)
d edu
Administrivia^ •^ Homework problems for Unit 1 due today^ •^ Homework problems for Unit 3 posted soon
CMSC 411 - 5 (from Patterson)
2
Forwarding to Avoid LW-SW Data HazardFigure A.8, Page A-
Time (clock cycles)
II ns t
add r1,r2,r
Reg
DMemALU
Ifetch^
Reg
r. Or
lw r4, 0(r1)
Reg
U LDMem
Ifetch^
Reg
Reg
DMemALU
Ifetch^
Reg
r de r
sw r4,12(r1)or^
r8 r6 r
Reg
DMem AL
Ifetch^
Reg Reg
DMemALU
Ifetch^
Reg
or^
r8,r6,r
xor r10,r9,r
Reg
DMemALU
Ifetch^
Reg
CMSC 411 - 5 (from Patterson)
3
Data Hazard Even with ForwardingFigure A.9, Page A-20 Time (clock cycles)
I^
l^
r^
0 (r2)
R
LUDM
If t h^
R
I n s t
lw^
r1, 0(r2)
sub r
r
r
Reg
DMemAL
Ifetch^
Reg
Reg
LU
DMem
Ifetch^
Reg
t r. O
sub
r4,r1,r
and r
r
r
Reg
AL
DMem
Ifetch^
Reg
Reg
ALU
DMem
Ifetch^
Reg
Or d e
and
r6,r1,r
or^
r^
r^
r
g
A Reg
ALU
DMem
Ifetch^
Reg
CMSC 411 - 5 (from Patterson)
4
r^
or^
r8,r1,r
A
Data Hazard Even with Forwarding(Similar to Figure A.10, Page A-21)
Time (clock cycles) Time (clock cycles) In s^
lw r1, 0(r2)
Reg
DMemALU
Ifetch^
Reg
s t r.
,^ (^
sub r4,r1,r
Reg Ifetch
ALU
DMem^
Reg
Bubble
O^ r d e
,^ ,
and r6,r1,r
Ifetch
ALU
DMem^
Reg
Bubble
Reg
or^
r8,r1,r
e r
Ifetch
ALU
DMem
Bubble
Reg
CMSC 411 - 5 (from Patterson)
5
Software Scheduling Instead
Try producing fast code for
a = b + c;a^
b^
c;
d = e – f;
assuming a, b, c, d ,e, and f in memory.Slow code:
LW^
Rb,b LW^
Rc,c
Fast code:
LW^
Rb,b LW^
Rc,c
ADD
Ra,Rb,Rc SW^
a,Ra LW^
Re,e
LW^
Re,e ADD
Ra,Rb,Rc LW^
Rf,f
LW^
Rf,f SUB
Rd,Re,Rf SW^
d,Rd
SW^
a,Ra SUB
Rd,Re,Rf SW^
d,Rd
CMSC 411 - 5 (from Patterson)
Compiler optimizes for performance. Hardware checks for safety.
Control hazards • Question
: When do we find out that the PC needs
to be modified?^ – Answer
: In pipeline stage ID of a branch instruction
–^ So, if a branch is not-taken (i.e., if the PC is not modified),need a one-cycle delay • Question: When is a taken branch’s addressknown? –^ ALU used to compute, so EX stage –^ Need two (or three) cycle delay
CMSC 411 - 5 (from Patterson)
7
Example •^ If branch in 30% of instructions, then instead ofexecuting 1 instruction per cycle, have 70% ofi^
t^
ti^
ti^
i^1
l^
d 30%
f
instructions executing in 1 cycle and 30% ofinstructions executing in 2 cycles • An average of .7 + .6 = 1.3 cycles per instruction
g^
y^
p
-^ Worse by 30%
CMSC 411 - 5 (from Patterson)
Four Branch Hazard Alternatives#4: Delayed Branch
-^ Define branch to take place AFTER a following instruction^ branch instructionsequential successor
1
sequential successor
2
Branch delay of length
n
........ sequential successor
n
branch target if taken
y^
g
-^ 1 slot delay allows proper decision and branch targetaddress in 5 stage pipeline –^ MIPS uses this
CMSC 411 - 5 (from Patterson)
13
Scheduling Branch Delay Slots (Fig A.14)^ add
R1,R2,R3if R2=0 then A. From before branch
B. From branch target
C. From fall through add^
R1,R2,R
if R1=0 then
sub R4,R5,R
delay slot
add^
R1,R2,R
if R1=0 then^ d l^
l t
delay slot sub^
R4 R5 R
delay slot
sub^
R4,R5,R
becomes
becomes
becomes add^
R1,R2,R
if R2=0 then^ add R1,R2,R
add^
R1,R2,R
if R1=0 then
if R1=0 then^ sub R4,R5,R
-^ A is the best choice, fills delay slot & reduces instruction count (IC)
if^ R
0 then sub R4,R5,R6^ CMSC 411 - 5 (from Patterson)
14
,^
y^
(^ )
-^ In B, the
sub
instruction may need to be copied, increasing IC
-^ In B and C, must be okay to execute
sub
when branch fails
Scheduling branch delay slot •^ If taken from before branch^ –
branch must not depend on rescheduled instruction – always improves performance
•^ If taken from branch target•^ If taken from branch target
-^ must be OK to execute rescheduled instructions if branch nottaken, and may need to duplicate insts.
f^
i^
d^ h
b
h t k
-^ performance improved when branch taken
•^ If taken from fall through
-^ must be OK to execute insts if branch taken–^ must be OK to execute insts. if branch taken –^ improves performance when branch not take
n
CMSC 411 - 5 (from Patterson)
15
Delayed Branch
y
•^ Compiler effectiveness for single branch delay slot:Compiler
effectiveness for single branch delay slot:
–^ Fills about 60% of branch delay slots –^ About 80% of instructions executed in branch delay slots usefulin computation –^ About 50% (60% x 80%) of slots usefully filled • Delayed Branch downside: As processor go todeeper pipelines and multiple issue the branchdeeper pipelines and multiple issue, the branchdelay grows and need more than one delay slot –^ Delayed branching has lost popularity compared to moreexpensive but more flexible dynamic approaches
p^
y^
pp
-^ Growth in available transistors has made dynamic approachesrelatively cheaper
CMSC 411 - 5 (from Patterson)
16
Evaluating Branch Alternatives^ Pi
li^
d^
Pipeline depth
Assume:
4% unconditional branch,
Pipeline speedup =
Pipeline depth
1 +Branch frequency
×Branch penalty
6% conditional branch- untaken,
10% conditional branch-taken
Scheduling
Branch
CPI
speedup v.
speedup v.
Sc^
edu^
g^
a^ c^
C^
speedup
speedup
scheme
penalty
unpipelined
stall
Stall pipeline
Predict taken
4.^
Predict taken
4.^
Predict not taken 1
Delayed branch 0.
CMSC 411 - 5 (from Patterson)
17
Pipelining Summary •^ Pipelining can speed instruction execution(throughput) •^ But need to deal with structural hazards, datahazards, and control hazards •^ Next•^ Next
-^ How to handle exceptions? –^ How to handle long instructions, such as floating pointarithmetic?arithmetic?
CMSC 411 - 5 (from Patterson)
The problem •^ Question: What makes pipelining hard toimplement? •^ Answer
: Surprises
•^ Technical names for surprises:
exceptions– exceptions – faults – interrupts
CMSC 411 - 5 (from Patterson)
19
Some examples of exceptions •^ Request for I/O •^ Arithmetic troubles: overflow or underflow •^ Page fault: data not in (physical) memory •^ Illegal address, giving a memory protection
i^ l tiviolation
•^ Hardware failure
CMSC 411 - 5 (from Patterson)
The most difficult exceptions... •^ ... are those that occur within EX or MEM stages and need tobe handled in a restartable wayWh
diffi
lt? H
dli
i^ l^
d
-^ Wh
y difficult? Handling one includes: – the next IF gets a "trap instruction" – until the trap is taken, turn off all "writes" for the faulting instructionand those that follow itand those that follow it – what does the trap do?^ »^
The trap transfers control to the exception handling routine in theoperating system, which saves the PC of the faulting instructionand handles the faultand handles the fault
-^ the task is then resumed, using the saved PC and the MIPS instructionRFE or something like it • Note: May need to save several PCs if delayed branches are
y^
y
involved
CMSC 411 - 5 (from Patterson)
25
Exceptions (cont.) •^ Ideally, pipeline can be interrupted so thatinstructions before the fault complete. Then wantt^
t^ t
ti^
j^ t
ft^
th^
f^ lti
to restart execution just after the faultinginstruction -
precise exception handling
•^ This is the right way to do it, but sometimes
g^
y^
architects/manufacturers take shortcuts
CMSC 411 - 5 (from Patterson)