Pipelining Lecture
Pipelining Lecture
1
Laundry Example
Ann, Brian, Cathy, Dave A B C D
each have one load of clothes
Appendix A - Pipelining 2
6 PM 7 8 9 10 11 Midnight
Time
30 40 20 30 40 20 30 40 20 30 40 20
T
a A
s
k
B
O
r
d C
e
r
D
Sequential laundry takes 6 hours for 4 loads
If they learned pipelining, how long would laundry take?
3
6 PM 7 8 9 10 11 Midnight
Time
30 40 40 40 40 20
T
a A
s Pipelined laundry takes
k 3.5 hours for 4 loads
B
O
r
d C
e
r
D
4
What Is
Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
IR <- Mem[PC]
NPC <- PC + 4
IR L
M
D
5
What Is
Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
A <- Regs[IR6..IR10];
B <- Regs[IR10..IR15];
IR L
Imm <- ((IR16) ##IR16-31
M
D
6
What Is
Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
A <- A func. B
cond = 0;
IR L
M
D
7
What Is
Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
A = Mem[prev. B]
or
IR L
Mem[prev. B] = A
M
D
8
What Is
Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
Regs <- A, B;
IR L
M
D
9
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
ALU
Ifetch Reg DMem Reg
I
n
s
ALU
t Ifetch Reg DMem Reg
r.
ALU
O Ifetch Reg DMem Reg
r
d
ALU
e Ifetch Reg DMem Reg
10
A.The Major Hurdle of Pipelining-
Structural Hazards
-- Structural Hazards
– Data Hazards Limits to pipelining: Hazards prevent next
– Control Hazards instruction from executing during its
designated clock cycle
◦ Structural hazards: HW cannot support this
combination of instructions (single person
to fold and put clothes away)
◦ Data hazards: Instruction depends on result
of prior instruction still in the pipeline
(missing sock)
◦ Control hazards: Pipelining of branches &
other instructions that change the PC
◦ Common solution is to stall the pipeline
until the hazard is resolved, inserting one
or more “bubbles” in the pipeline
Appendix A - Pipelining 11
Definition
conditions that lead to incorrect behavior if not fixed
Structural hazard
Resolution
Pipeline interlock logic detects hazards and fixes them
some instruction stall, others proceed better to stall early than late
Appendix A - Pipelining 12
When two or
Time (clock cycles) more different
instructions want
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 to use same
hardware
I Load Ifetch
ALU
Reg DMem Reg resource in same
n cycle
s Instr 1
ALU
Ifetch Reg DMem Reg
e.g., MEM uses
t the same memory
r. port as IF as
ALU
Ifetch Reg DMem Reg
Instr 2 shown in this
slide.
O
ALU
r Instr 3 Ifetch Reg DMem Reg
d
e
ALU
Ifetch Reg DMem Reg
Instr 4
r
Figure 3.6
Appendix A - Pipelining 13
Time (clock cycles)
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 This is another
way of looking
at the effect of
I Load Ifetch
ALU
Reg DMem Reg
a stall.
n
s
ALU
Reg
t Instr 1
Ifetch Reg DMem
r.
ALU
Ifetch Reg DMem Reg
Instr 2
O
r
Stall Bubble Bubble Bubble Bubble Bubble
d
e
r
ALU
Ifetch Reg DMem Reg
Instr 3
Figure 3.7
Appendix A - Pipelining 14
This is another way to represent the stall we saw on
the last few pages.
Appendix A - Pipelining 15
Dealing with Structural Hazards
Stall
low cost, simple
Increases CPI
good performance
Replicate resource
good performance
16
Structural hazards are reduced with these rules:
Each instruction uses a resource at most once
17
This is the example on Page 144.
Appendix A - Pipelining 18
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
-- Structural Hazards
These occur when at any time, there are
– Data Hazards instructions active that need to access the
– Control Hazards same data (memory or register) locations.
A.3 How is Pipelining Implemented
Where there’s real trouble is when we have:
A.4 What Makes Pipelining Hard to
Implement?
instruction A
A.5 Extending the MIPS Pipeline to instruction B
Handle Multi-cycle Operations
Appendix A - Pipelining 19
Execution Order is:
Read After Write (RAW)
InstrI
InstrJ tries to read operand before InstrI writes it
InstrJ
I: add r1,r2,r3
J: sub r4,r1,r3
Caused by a “Dependence” (in compiler nomenclature).
This hazard results from an actual need for
communication.
Appendix A - Pipelining 20
Execution Order is:
Write After Read (WAR)
InstrI
InstrJ tries to write operand before InstrI reads i
◦ Gets wrong operand
InstrJ
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Appendix A - Pipelining 21
Execution Order is:
Write After Write (WAW)
InstrI
InstrJ tries to write operand before InstrI writes it
InstrJ
◦ Leaves wrong result ( InstrI not InstrJ )
I: sub r1,r4,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Appendix A - Pipelining 22
Simple Solution to RAW
Appendix A - Pipelining 23
Time (clock cycles)
IF ID/RF EX MEM WB
I
ALU
Reg Reg
add r1,r2,r3Ifetch DMem
n
s
ALU
Ifetch Reg DMem Reg
t sub r4,r1,r3
r.
ALU
Ifetch Reg DMem Reg
and r6,r1,r7
O
r
ALU
Ifetch Reg DMem Reg
d or r8,r1,r9
e
ALU
Ifetch Reg DMem Reg
r xor r10,r1,r11
The use of the result of the ADD instruction in the next three instructions causes a
hazard, since the register is not written until after those instructions read it.
Figure 3.9
Appendix A - Pipelining 24
Forwarding is the concept of making data
available to the input of the ALU for
subsequent instructions, even though the
generating instruction hasn’t gotten to WB
Forwarding To Avoid in order to write the memory or registers.
Data Hazard
Time (clock cycles)
I
n
ALU
add r1,r2,r3 Ifetch Reg DMem Reg
s
t
ALU
Reg
r. sub r4,r1,r3 Ifetch Reg DMem
ALU
Ifetch Reg DMem Reg
r and r6,r1,r7
d
e
ALU
Ifetch Reg DMem Reg
r or r8,r1,r9
ALU
Ifetch Reg DMem Reg
xor r10,r1,r11
Figure 3.10
Appendix A - Pipelining 25
The data isn’t loaded until after
the MEM stage.
Time (clock cycles)
ALU
lw r1, 0(r2) Ifetch Reg DMem Reg
n
s
t
ALU
Ifetch Reg DMem Reg
sub r4,r1,r6
r.
ALU
Ifetch Reg DMem Reg
and r6,r1,r7
r
d
e
ALU
Ifetch Reg DMem Reg
r or r8,r1,r9
There are some instances where hazards occur, even with forwarding.
Figure 3.12
Appendix A - Pipelining 26
The stall is necessary as shown
here.
Time (clock cycles)
I
n
lw r1, 0(r2)
ALU
Ifetch Reg DMem Reg
s
t
r.
ALU
sub r4,r1,r6 Ifetch Reg Bubble DMem Reg
O
r
d Bubble
ALU
Ifetch Reg DMem Reg
e and r6,r1,r7
r
ALU
Bubble Ifetch Reg DMem
or r8,r1,r9
There are some instances where hazards occur, even with forwarding.
Figure 3.13
Appendix A - Pipelining 27
This is another
representation
of the stall.
Appendix A - Pipelining 28
Pipeline Scheduling
lw Rb, b
lw Rc, c
lw Re, e
Add Ra, Rb, Rc
lw Rf, f
sw a, Ra
sub Rd, Re, Rf
sw d, Rd
Appendix A - Pipelining 29
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
-- Structural Hazards
– Data Hazards A control hazard is when
– Control Hazards we need to find the
A.3 How is Pipelining Implemented destination of a branch,
A.4 What Makes Pipelining Hard to
and can’t fetch any new
Implement? instructions until we know
A.5 Extending the MIPS Pipeline to that destination.
Handle Multi-cycle Operations
Appendix A - Pipelining 30
Control Hazards
ALU
10: beq r1,r3,36 Ifetch Reg DMem Reg
ALU
14: and r2,r3,r5 Ifetch Reg DMem Reg
ALU
18: or r6,r1,r7 Ifetch Reg DMem Reg
ALU
Reg
22: add r8,r1,r9 Ifetch Reg DMem
ALU
36: xor r10,r1,r11 Ifetch Reg DMem Reg
Appendix A - Pipelining 31
Control Hazards
If CPI = 1, 30% branch, Stall 3 cycles => new CPI = 1.9!
(Whoa! How did we get that 1.9???)
Two part solution to this dramatic increase:
◦ Determine branch taken or not sooner, AND
◦ Compute taken branch address earlier
MIPS Solution:
◦ Move Zero test to ID/RF stage
◦ Adder to calculate new PC in ID/RF stage
must be fast
can't afford to subtract
compares with 0 are simple
Greater-than, Less-than test signbit, but not-equal must OR all bits
more general compares need ALU
◦ 1 clock cycle penalty for branch versus 3
In the next chapter, we’ll look at ways to avoid the branch all together.
Appendix A - Pipelining 32
Control Hazards
#1: Stall until branch direction is clear
Appendix A - Pipelining 33
Control Hazards Five Branch Hazard
Alternatives
#4: Execute Both Paths
branch instruction
sequential successor1
sequential successor2
........
sequential successorn Branch delay of length n
branch target if taken
Appendix A - Pipelining 34
Control Hazards
Appendix A - Pipelining 35
Control Hazards
Appendix A - Pipelining 36
Control Hazards
Two strategies
◦ Backward branch predict taken, forward branch not taken
◦ Profile-based prediction: record branch behavior, predict
branch based on prior run
Appendix A - Pipelining 37
Interrupts cause
great havoc!
Appendix A - Pipelining 38
Hazards limit performance
◦ Structural: need more HW resources
◦ Data: need forwarding, compiler scheduling
◦ Control: early evaluation & PC, delayed branch, prediction
Increasing length of pipe increases impact of hazards; pipelining
helps instruction bandwidth, not latency
Interrupts, Instruction Set, FP makes pipelining harder
Compilers reduce cost of data and control hazards
◦ Load delay slots
◦ Branch delay slots
◦ Branch prediction
Appendix A - Pipelining 39