Professional Documents
Culture Documents
SPEC Requirements
f1 f2 Problem
i
Focus
p
Algorithms f5 f3 s q
fp
j
f2() {
f3(s2, &j, &i);
f3
*s2->p = 10; f4
}
i = *s2->q + i;
Prog. Lang./OS
i1: ld r1, b <p1>
ISA
i2: ld r2, c <p1>
i3: ld r5, z <p3>
i4: mul r6, r5, 3 <p3>
i5: add r3, r1, r2 <p1>
Mult1
S reg
Mac1
X reg
Mac2
Add,
Sub,
Shift
Mult2 uArch
Performance
Circuit
Gate
Focus
Device
Source Drain
Appendix A - Pipelining 1
Instruction Set Architecture
Application
Intel Pentium X
AMD K6, Athlon, Opteron
Implementation Transmeta Crusoe TM5x00
Appendix A - Pipelining 2
Instruction Set Architecture
• Strong influence on cost/performance
Appendix A - Pipelining 3
Traditional Issues
• Strongly constrained by the number of
bits available to instruction encoding
• Opcodes/operands
• Registers/memory
• Addressing modes
• Orthogonality
• 0, 1, 2, 3 address machines
• Instruction formats
• Decoding uniformity
Appendix A - Pipelining 4
Introduction
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-Structural Hazards
– Data Hazards
– Control Hazards
Appendix A - Pipelining 5
What Is Pipelining
• Laundry Example
• Ann, Brian, Cathy, Dave A B C D
each have one load of clothes
to wash, dry, and fold
• Washer takes 30 minutes
Appendix A - Pipelining 6
What Is Pipelining
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?
Appendix A - Pipelining 7
What Is Pipelining
Start work ASAP
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
Appendix A - Pipelining 8
What Is Pipelining
Lessons
Pipelining
• Pipelining doesn’t help
6 PM 7 8 9 latency of single task, it helps
throughput of entire workload
Time
• Pipeline rate limited by
T slowest pipeline stage
a 30 40 40 40 40 20
• Multiple tasks operating
s
A simultaneously
k
• Potential speedup = Number
O pipe stages
r B • Unbalanced lengths of pipe
d stages reduces speedup
e • Time to “fill” pipeline and time
r C to “drain” it reduces speedup
Appendix A - Pipelining 9
Problem-01:
Appendix A - Pipelining 10
Solution
• Given-
• Four stage pipeline is used
• Delay of stages = 60, 50, 90 and 80 ns
• Latch delay or delay due to each register = 10 ns
•
• Part-01: Pipeline Cycle Time-
•
• Cycle time
• = Maximum delay due to any stage + Delay due to its register
• = Max { 60, 50, 90, 80 } + 10 ns
• 90 ns + 10 ns
• = 100 ns
Appendix A - Pipelining 11
Solution
• Part-02: Non-Pipeline Execution Time-
•
• Non-pipeline execution time for one instruction
• = 60 ns + 50 ns + 90 ns + 80 ns
• = 280 ns
• Part-03: Speed Up Ratio-
•
• Speed up
• = Non-pipeline execution time / Pipeline execution time
• = 280 ns / Cycle time
• = 280 ns / 100 ns
• = 2.8
Appendix A - Pipelining 12
Solution
• Part-04: Pipeline Time For 1000
Tasks-
• Pipeline time for 1000 tasks
• = Time taken for 1st task + Time taken for remaining 999 tasks
• = 1 x 4 clock cycles + 999 x 1 clock cycle
• = 4 x cycle time + 999 x cycle time
• = 4 x 100 ns + 999 x 100 ns
• = 400 ns + 99900 ns
• = 100300 ns
Appendix A - Pipelining 13
Solution
• Part-05: Sequential Time For 1000 Tasks-
•
• Non-pipeline time for 1000 tasks
• = 1000 x Time taken for one task
• = 1000 x 280 ns
• = 280000 ns
• Part-06: Throughput-
•
• Throughput for pipelined execution
• = Number of instructions executed per unit time
• = 1000 tasks / 100300 ns
Appendix A - Pipelining 14
Problem-02:
Appendix A - Pipelining 15
Solution
• Solution-
•
• Given-
• Four stage pipeline is used
• Delay of stages = 150, 120, 160 and 140 ns
• Delay due to each register = 5 ns
• 1000 data items or instructions are processed
• Cycle time
• = Maximum delay due to any stage + Delay due to its register
• = Max { 150, 120, 160, 140 } + 5 ns
• = 160 ns + 5 ns
• = 165 ns
Appendix A - Pipelining 16
Solution
• Pipeline time to process 1000 data items
• = Time taken for 1st data item + Time taken for
remaining 999 data items
• = 1 x 4 clock cycles + 999 x 1 clock cycle
• = 4 x cycle time + 999 x cycle time
• = 4 x 165 ns + 999 x 165 ns
• = 660 ns + 164835 ns
• = 165495 ns
• = 165.5 μs
Appendix A - Pipelining 17
Problem
• Consider a non-pipelined processor with a clock rate
of 2.5 gigahertz and average cycles per instruction of
4. The same processor is upgraded to a pipelined
processor with five stages but due to the internal
pipeline delay, the clock speed is reduced to 2
gigahertz. Assume there are no stalls in the pipeline.
The speed up achieved in this pipelined processor is
Appendix A - Pipelining 18
Solution
• Cycle Time in Non-Pipelined Processor-
•
• Frequency of the clock = 2.5 gigahertz
• Cycle time
• = 1 / frequency
• = 1 / (2.5 gigahertz)
• = 1 / (2.5 x 109 hertz)
• = 0.4 ns
Appendix A - Pipelining 19
Solution
• Non-pipeline execution time to process 1 instruction
• = Number of clock cycles taken to execute one instruction
• = 4 clock cycles
• = 4 x 0.4 ns
• = 1.6 ns
•
• Cycle Time in Pipelined Processor-
•
• Frequency of the clock = 2 gigahertz
• Cycle time
• = 1 / frequency
• = 1 / (2 gigahertz)
• = 1 / (2 x 109 hertz)
• = 0.5 ns
Appendix A - Pipelining 20
Solution
• Pipeline Execution Time-
•
• Since there are no stalls in the pipeline, so ideally one instruction is executed
per clock cycle. So,
• Pipeline execution time
• = 1 clock cycle
• = 0.5 ns
•
• Speed Up-
•
• Speed up
• = Non-pipeline execution time / Pipeline execution time
• = 1.6 ns / 0.5 ns
• = 3.2
Appendix A - Pipelining 21
Problem-04:
Appendix A - Pipelining 22
Solution
Appendix A - Pipelining 23
What Is MIPS Without
Pipelining Pipelining
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Calc Access Back
IR
L
M
D
Appendix A - Pipelining 24
What Is
Pipelining MIPS Functions
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
Appendix A - Pipelining 25
What Is
Pipelining MIPS Functions
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
Appendix A - Pipelining 26
What Is
Pipelining MIPS Functions
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
Appendix A - Pipelining 28
What Is
Pipelining MIPS Functions
Instruction Instr. Decode Execute Memory Write
Fetch Reg. Fetch Addr. Access Back
Calc Passed To Next Stage
Regs <- A, B;
IR L
M
D
Appendix A - Pipelining 29
The Basic Pipeline For MIPS
Latches between
each stage provide
pipelining.
Appendix A - Pipelining 30
The Basic Pipeline For MIPS
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
Figure 3.3
Appendix A - Pipelining 31
Pipeline Hurdles
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
-- Structural Hazards Limits to pipelining: Hazards prevent next
– Data Hazards instruction from executing during its designated
clock cycle
– Control Hazards
– Structural hazards: HW cannot support this
A.3 How is Pipelining Implemented combination of instructions (single person to fold
A.4 What Makes Pipelining Hard to and put clothes away)
Implement? – Data hazards: Instruction depends on result of
A.5 Extending the MIPS Pipeline to prior instruction still in the pipeline (missing
Handle Multi-cycle Operations 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 32
Pipeline Hurdles
Definition
• conditions that lead to incorrect behavior if not fixed
• Structural hazard
– two different instructions use same h/w in same cycle
• Data hazard
– two different instructions use same storage
– must appear as if the instructions execute in correct order
• Control hazard
– one instruction affects which instruction is next
Resolution
• Pipeline interlock logic detects hazards and fixes them
• simple solution: stall
• increases CPI, decreases performance
• better solution: partial stall
• some instruction stall, others proceed better to stall early than late
Appendix A - Pipelining 33
Structural Hazards
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 34
Structural Hazards
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 35
Structural Hazards
Appendix A - Pipelining 36
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
Appendix A - Pipelining 37
Structural Hazards
Structural hazards are reduced with these rules:
• Each instruction uses a resource at most once
• Always use the resource in the same pipeline stage
• Use the resource for one cycle only
Many RISC ISA’a designed with this in mind
Sometimes very complex to do this. For example, memory of
necessity is used in the IF and MEM stages.
Appendix A - Pipelining 38
Structural Hazards
This is the example on Page 144.
Appendix A - Pipelining 39
Data Hazards
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
Handle Multi-cycle Operations
instruction B
Appendix A - Pipelining 40
Data Hazards
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 41
Data Hazards
Execution Order is:
Write After Read (WAR)
InstrI
InstrJ tries to write operand before InstrI reads i
InstrJ
– Gets wrong operand
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Appendix A - Pipelining 42
Data Hazards
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 43
Data Hazards
Simple Solution to RAW
Appendix A - Pipelining 44
Data Hazards
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 45
Forwarding is the concept of making data
Data Hazards
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 46
The data isn’t loaded until after
Data Hazards 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 47
Data Hazards 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 48
This is another
Appendix A - Pipelining 49
Data Hazards 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 50
Data Hazards Pipeline Scheduling
scheduled unscheduled
54%
gcc
31%
42%
spice
14%
65%
tex
25%
Appendix A - Pipelining 51
Control Hazards
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
-- Structural Hazards
– Data Hazards A control hazard is when we
– Control Hazards need to find the destination
A.3 How is Pipelining Implemented of a branch, and can’t fetch
A.4 What Makes Pipelining Hard to
any new instructions until
Implement? we know that destination.
A.5 Extending the MIPS Pipeline to
Handle Multi-cycle Operations
Appendix A - Pipelining 52
Control Hazard on
Control Hazards Branches
Three Stage Stall
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 53
Control Hazards Branch Stall Impact
• 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 54
Control Hazards Five Branch Hazard
Alternatives
#1: Stall until branch direction is clear
Appendix A - Pipelining 55
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 56
Control Hazards Delayed Branch
• Where to get instructions to fill branch delay slot?
– Before branch instruction
– From the target address: only valuable when branch taken
– From fall through: only valuable when branch not taken
– Cancelling branches allow more slots to be filled
Appendix A - Pipelining 57
Control Hazards Evaluating Branch
Alternatives
Appendix A - Pipelining 58
Control Hazards Pipelining Introduction
Summary
Appendix A - Pipelining 59
Control Hazards Compiler “Static”
Prediction of
The compiler can program what it thinks
the branch direction will be. Here are Taken/Untaken Branches
the results when it does so.
70% 14%
60% 12%
50% 10%
40% 8%
Misprediction Rate
Frequency of Misprediction
30% 6%
20% 4%
10% 2%
0% 0%
gcc
gcc
ora
ora
doduc
doduc
alvinn
alvinn
tomcatv
tomcatv
hydro2d
hydro2d
mdljsp2
mdljsp2
espresso
espresso
swm256
swm256
compress
compress
• 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 61
Control Hazards Evaluating Static
Branch Prediction
Strategies
100000
• Misprediction ignores
frequency of branch 10000
• “Instructions between
10
gcc
doduc
ora
alvinn
tomcatv
hydro2d
mdljsp2
espresso
swm256
compress
Profile-based Direction-based
Appendix A - Pipelining 62
What Makes Pipelining Hard?
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
– Data Hazards
– Control Hazards
A.3 How is Pipelining Implemented
A.4 What Makes Pipelining Hard to
Implement?
A.5 Extending the MIPS Pipeline to
Handle Multi-cycle Operations
Appendix A - Pipelining 63
What Makes Interrupts cause
great havoc!
Pipelining Hard?
Examples of interrupts: There are 5 instructions executing
• Power failing, in 5 stage pipeline when an
interrupt occurs:
• Arithmetic overflow,
• How to stop the pipeline?
• I/O device request,
• How to restart the pipeline?
• OS call, • Who caused the interrupt?
• Page fault
Appendix A - Pipelining 64
What Makes Interrupts cause
Appendix A - Pipelining 65
What Makes Interrupts cause
great havoc!
Pipelining Hard?
• Simultaneous exceptions in more than one pipeline stage, e.g.,
– Load with data page fault in MEM stage
– Add with instruction page fault in IF stage
– Add fault will happen BEFORE load fault
• Solution #1
– Interrupt status vector per instruction
– Defer check until last stage, kill state update if exception
• Solution #2
– Interrupt ASAP
– Restart everything that is incomplete
Another advantage for state update late in pipeline!
Appendix A - Pipelining 66
What Makes Interrupts cause
great havoc!
Pipelining Hard?
Here’s what happens on a data page fault.
1 2 3 4 5 6 7 8 9
i F D X M W
i+1 F D X M W <page fault
i+2 F D X M W <squash
i+3 F D X M W <squash
i+4 F D X M W <squash
i+5 trap > F D X M W
i+6 trap handler > F D X M W
Appendix A - Pipelining 67
What Makes Complex
Instructions
Pipelining Hard?
Complex Addressing Modes and Instructions
• Address modes: Autoincrement causes register change
during instruction execution
– Interrupts? Need to restore register state
– Adds WAR and WAW hazards since writes are no longer the
last stage.
• Condition Codes
Appendix A - Pipelining 68
Handling Multi-cycle Operations
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-
Structural Hazards
– Data Hazards Multi-cycle instructions also
– Control Hazards lead to pipeline complexity.
A.3 How is Pipelining Implemented
A.4 What Makes Pipelining Hard to
Implement?
A very lengthy instruction
A.5 Extending the MIPS Pipeline to
causes everything else in
Handle Multi-cycle Operations the pipeline to wait for it.
Appendix A - Pipelining 69
Multi-Cycle Floating Point
Operations
Floating point gives long execution time.
This causes a stall of the pipeline.
It’s possible to pipeline the FP execution unit so it can initiate new instructions
without waiting full latency. Can also have multiple FP units.
Appendix A - Pipelining 70
Multi-Cycle Floating Point
Operations
Divide, Square Root take 10X to 30X longer than Add
– Interrupts?
– Adds WAR and WAW hazards since pipelines are
no longer same length
1 2 3 4 5 6 7 8 9 10 11
i IF ID EX MEM WB
I+1 IF ID EX EX EX EX MEM WB
I+2 IF ID EX MEM WB
I+3 IF ID EX EX EX EX MEM WB
I+4 IF ID EX MEM WB
I+5 IF ID -- -- EX EX
I+6 IF -- -- ID EX
Notes:
I + 2: no WAW, but this complicates an interrupt
I + 4: no WB conflict
I + 5: stall forced by structural hazard
I + 6: stall forced by in-order issue
Appendix A - Pipelining 71
Summary of Pipelining Basics
• 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 72
Credits
I have not written these notes by myself. There’s a great deal of fancy
artwork here that takes considerable time to prepare.
Appendix A - Pipelining 73
Summary
A.1 What is Pipelining?
A.2 The Major Hurdle of Pipelining-Structural Hazards
– Data Hazards
– Control Hazards
Appendix A - Pipelining 74