Recap of Lecture 12 and Homework Check
- Same C code: 4-5 instructions per element in every ISA.
- Code size 24-48 B; compiler changes it as much as the ISA.
- Fallacies: typical program; flawed ISA fails; flawless ISA.
- Unit 2: load-store, simple ops, regular encoding.
Homework check
- 1. Name the modes: x86 (%rdi,%rdx,4) and ARM [x8], #4.
- 2. Encode sw $t0, -4($sp). Decode 0x00094100.
- 3. Essay: fixed vs variable-length encoding.
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 12
5-8
Plan and learning outcomes
8-16
Module 1: The idea of pipelining laundry, throughput vs latency
16-29
Module 2: The five-stage MIPS pipeline stages, pipeline registers, timing diagram
29-38
Module 3: Pipeline performance speedup, CPI, unbalanced stages, our loop
38-45
Class activity cycles, clock and stage use
45-50
Mistakes, quiz and homework next: hazards
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
SLO 13.1
Describe the five stages of the MIPS pipeline and what each instruction type does in each stage.
→ CLO3 · C2 Understand
SLO 13.2
Draw and read a pipeline timing diagram and explain the job of the pipeline registers.
→ CLO3 · C3 Apply
SLO 13.3
Calculate pipeline cycles, CPI and speedup, including unbalanced stages and register overhead.
→ CLO2 · C3 Apply
- CLO2: Measure and report the performance of computer systems.
- CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: The Idea of Pipelining
Overlap the steps, not the work
A Dhobi Shop: Without and With Overlap
Without overlap: 16 slots = 8 h
Load A
wash
dry
press
fold
Load B
wash
dry
press
fold
Load C
wash
dry
press
fold
Load D
wash
dry
press
fold
With overlap: 7 slots = 3.5 h
Load A
wash
dry
press
fold
Load B
wash
dry
press
fold
Load C
wash
dry
press
fold
Load D
wash
dry
press
fold
Each column = 30 minutes. Same four steps, same people - only the order of work changed.
Throughput, Latency and Speedup
2 h
1 / 30 min
2.29 → 4
latency of one load
throughput when full
speedup
unchanged by overlap
was 1 per 2 h
4 loads → many loads
Ideal speedup = number of stages
Daewoo buses
Only when stages are balanced, a new job starts every slot (no hazards), and there are many jobs.
A bus every 15 min; each trip still takes 2 h.
Module 2: The Five-Stage MIPS Pipeline
IF, ID, EX, MEM, WB
The Five Stages
One clock cycle per stage: a new instruction can start every cycle.
What Each Instruction Does in Each Stage
lw uses all five stages - it is the longest instruction.
Pipeline Registers
IF
ID
EX
MEM
WB
- instruction
- memory
- register
- file (read)
ALU
- data
- memory
- register
- file (write)
IF/ID
ID/EX
EX/MEM
MEM/WB
Pipeline Timing Diagram
lw $t0, 0($s0)
IF
ID
EX
MEM
WB
add $t1, $t2, $t3
IF
ID
EX
MEM
WB
sub $t4, $t5, $t6
IF
ID
EX
MEM
WB
sw $t7, 4($s0)
IF
ID
EX
MEM
WB
and $t8, $t9, $s1
IF
ID
EX
MEM
WB
Cycle 5: all five stages busy, five instructions in flight. 5 instructions → 9 cycles.
Each instruction still takes 5 cycles (latency); one finishes every cycle (throughput).
Why the MIPS ISA Suits a Pipeline
Same-size boxes on a production line
A line runs smoothly when every item has the same size and shape - like fixed-length MIPS instructions.
Module 3: Pipeline Performance
How much faster - in theory and in practice
Cycles, CPI and Ideal Speedup
k stages, n instructions, no stalls
- cycles = k + (n - 1)
- speedup = n x k / (k + n - 1) → k
- CPI = (k + n - 1) / n → 1
For real programs: speedup ≈ number of stages and CPI ≈ 1 - if nothing stalls.
Example: 10 instructions → 5 + 9 = 14 cycles (not 50).
Unbalanced Stages and Register Overhead
800 → 200 ps
clock: single-cycle → pipelined
speedup 4, not 5
220 ps
with 20 ps register overhead
speedup 3.64
The slowest stage sets the clock. Balance the stages; every register adds overhead.
Worked Example (Hennessy and Patterson)
three lines of working
- unpipelined = 1 ns x (0.6 x 4 + 0.4 x 5) = 4.4 ns
- pipelined = (1 + 0.2) ns x CPI 1 = 1.2 ns
- speedup = 4.4 / 1.2 = 3.67
4.4 → 1
1.0 → 1.2 ns
3.67
CPI
clock time
speedup
goes down a lot
goes up a little
time = CPI x clock
Our Milk Loop on a Pipeline
L13_milk.asm (from Lecture 11)
- loop:
- lw $t3, 0($a0)
- addu $v1, $v1, $t3
- addiu $a0, $a0, 4
- bne $a0, $t1, loop
lw → addu and addiu → bne: dependences - next lecture.
3.67x
ideal speedup
before hazards (Lecture 14)
Class Activity: Pipeline Numbers
7 min · pairs
In pairs. Use the formulas on slides 15-17.
4 instructions on an ideal 5-stage pipeline: how many cycles? And 1000 instructions?
Stages 150, 100, 250, 200, 100 ps; register overhead 20 ps. Clock? Speedup over single-cycle for a long program?
Which stages do useful work for sw? For beq?
On the slide 12 diagram, which stage is each instruction in during cycle 6?
Common Mistakes
Wrong idea
Correct idea
✗ Pipelining makes each instruction faster.
✓ Each still takes 5 cycles (latency); throughput rises.
✗ Speedup always equals the number of stages.
✓ Only if balanced, no stalls, many instructions: here 4, not 5.
✗ The clock is set by the average stage time.
✓ By the slowest stage (+ register overhead).
✗ The destination register is written in ID.
✓ In WB, using the number carried in the pipeline registers.
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Name the five MIPS pipeline stages in order.
Q2
Does pipelining reduce the time of ONE instruction? Explain in one line.
Q3
How many cycles do 10 instructions need on an ideal 5-stage pipeline?
Q4
Why must the clock fit the slowest stage?
Q5
Name the pipeline register between EX and MEM and one thing it carries.
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix C.1-C.2.
- Draw the timing diagram for 6 independent instructions. How many cycles?
- Calculate: stages 250, 150, 200, 300, 150 ps; register 25 ps. Single-cycle clock, pipelined clock and speedup for 1,000,000 instructions.
- Think: MEM is split into two 150 ps stages. New clock and speedup? Why is a deeper pipeline not free?
NEXT LECTURE
Lecture 14: Structural and Data Hazards
What stops a new instruction every cycle: shared hardware, and instructions that need a result that is not ready yet. Forwarding and stalls.