Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
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 |
§1 The Idea of Pipelining
§1.1 The dhobi shop
Slide L13-S06
A dhobi (washerman) shop does four steps for each customer's load: wash, dry, press (iron) and fold, 30 minutes each, with one person or machine per step. Without overlap the shop finishes one load before starting the next: 4 loads x 2 hours = 8 hours. With overlap, load B goes into the washer as soon as load A moves to the dryer. Once the line is full, all four steps work at the same time and one load comes out every 30 minutes: 4 loads need 7 slots = 3.5 hours. This overlapping of steps is called pipelining (pipeline = a line of stages that work at the same time on different jobs).
§1.2 Throughput, latency and speedup
Slide L13-S07
Word | Meaning | Dhobi shop |
|---|---|---|
Latency (delay) | Time ONE job takes from start to finish | Still 2 hours per load |
Throughput | Jobs finished per unit of time | 1 load per 30 min when full (was 1 per 2 h) |
Speedup | Old time / new time | 8 / 3.5 = 2.29 for 4 loads; → 4 for many loads |
Pipelining does not make one job faster; it raises throughput. The ideal speedup equals the number of stages, but only if (1) the stages take equal time, (2) a new job can start every slot (no waiting) and (3) there are many jobs. Real-world: a Daewoo terminal sends a bus to Lahore every 15 minutes; each trip still takes 2 hours.
§2 The Five-Stage MIPS Pipeline
§2.1 The stages
Slide L13-S09
Stage | What happens | Main hardware |
|---|---|---|
IF - instruction fetch | Read the instruction at address PC; compute PC + 4 | Instruction memory, adder |
ID - decode and register read | Decode the fields; read rs and rt; sign-extend the immediate | Register file (read ports) |
EX - execute / address | ALU operation, or base + offset for lw/sw, or compare for a branch | ALU |
MEM - memory access | lw reads and sw writes the data memory | Data memory |
WB - write back | Write the result into rd (R-type) or rt (lw) | Register file (write port) |
Each stage takes one clock cycle, so a new instruction can start every cycle.
§2.2 Stage use by instruction
Slide L13-S10
Instruction | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
add $t0, $t1, $t2 | fetch | read $t1, $t2 | $t1 + $t2 | - | write $t0 |
lw $t0, 8($s0) | fetch | read $s0 | $s0 + 8 | read memory | write $t0 |
sw $t0, 8($s0) | fetch | read $s0, $t0 | $s0 + 8 | write memory | - |
beq $t0, $t1, L | fetch | read $t0, $t1 | compare; target | - | - |
lw is the only instruction that needs all five stages, so it is the longest. Every instruction still passes through all five stages so the timing stays regular; an unused stage does nothing.
§2.3 Pipeline registers
Slide L13-S11
A pipeline register (latch) between two stages saves, at every clock edge, everything the next stage needs, so the following instruction cannot overwrite it. They are named after the stages they separate.
Register | Carries (examples) |
|---|---|
IF/ID | Instruction word, PC + 4 |
ID/EX | Values of rs and rt, sign-extended immediate, register numbers, control signals |
EX/MEM | ALU result or address, value to store (sw), destination register number, control |
MEM/WB | Loaded data or ALU result, destination register number |
The destination register number travels with its instruction to WB, so WB writes the right register even though four newer instructions have entered the pipeline. Real-world: the tray moving between kitchen stations carries the order slip with it.
§2.4 Timing diagrams
Slide L13-S12
One row per instruction, one column per clock cycle, the stage in each cell (Hennessy and Patterson, Fig. C.1). Five independent instructions need 9 cycles. In cycle 5 every stage holds a different instruction - five instructions in progress at once. This overlap is instruction-level parallelism (ILP).
Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
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 |
How to draw one: write the instructions down the left; start row 1 at cycle 1; start each next row one cycle later; write IF ID EX MEM WB in five consecutive cells. Read the answer to 'which stage in cycle c?' straight down column c.
§2.5 Why the MIPS ISA suits a pipeline
Slide L13-S13
ISA feature (Unit 2) | Pipeline benefit |
|---|---|
Fixed 32-bit instructions (Lectures 10, 12) | IF always fetches 4 bytes; PC + 4; simple decode (x86 needs to find where each instruction ends) |
rs and rt in the same bits in R and I format (Lecture 10, P11) | ID reads the registers while it is still decoding |
Memory operands only in lw/sw (Lectures 7-8) | Address in EX, access in MEM: one pass through the pipeline |
Aligned operands (Lecture 8) | One memory access per load or store |
§3 Pipeline Performance
§3.1 Cycles, CPI and ideal speedup
Slide L13-S15
Formulas |
n | Cycles (k = 5) | Speedup | Average CPI |
|---|---|---|---|
1 | 5 | 1.00 | 5.00 |
5 | 9 | 2.78 | 1.80 |
10 | 14 | 3.57 | 1.40 |
100 | 104 | 4.81 | 1.04 |
1,000,000 | 1,000,004 | 5.00 | 1.000004 |
Each instruction still needs 5 cycles from start to finish (latency), but on average one finishes every cycle, so CPI → 1. For real programs (billions of instructions) the fill and drain cycles do not matter; stalls do (Lecture 14).
§3.2 Unbalanced stages and register overhead
Slide L13-S16
Stage times IF 200, ID 100, EX 200, MEM 200, WB 100 ps (Patterson and Hennessy). A single-cycle machine needs a clock of 800 ps (the whole lw). The pipelined clock must fit the slowest stage: 200 ps. Speedup = 800 / 200 = 4, not 5. With 20 ps pipeline-register overhead the clock is 220 ps and the speedup 3.64. Three lw instructions: single-cycle 2400 ps; pipelined (5 + 2) x 200 = 1400 ps.
Rule: the slowest stage plus the register overhead sets the clock. Balance the stages. |
§3.3 Worked example (Hennessy and Patterson)
Slide L13-S17
40% ALU (4 cycles), 20% branch (4 cycles), 40% memory (5 cycles) |
This is the CPU time equation of Lecture 4: time per instruction = CPI x clock time. Pipelining cuts the CPI (4.4 → 1) but lengthens the clock a little (1.0 → 1.2 ns).
§3.4 Our milk loop
Slide L13-S18
L13_milk.asm (the Lecture 11 loop, version 4) |
Single-cycle (800 ps) | 5-stage pipeline (200 ps) | |
|---|---|---|
Instructions executed (MARS) | 44 | 44 |
Cycles | 44 | 44 + 4 = 48 |
Time | 35,200 ps | 9,600 ps |
Speedup | 3.67 |
Ideal figure - it ignores hazards: lw is followed at once by addu, which needs the loaded value, and bne needs the pointer just updated by addiu. Lecture 14 counts the stall cycles these cause.
§4 Common Mistakes
Slide L13-S20
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 with balanced stages, no stalls and many instructions (4 in the 200/100 ps example). |
The clock is set by the average stage time. | By the slowest stage plus register overhead. |
The destination register is written in ID. | In WB, using the register number carried in the pipeline registers. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Pipelining | Overlapping the steps of many jobs | Dhobi shop: one load washing while another dries |
Stage | One step of the pipeline, one clock cycle | One station on a car-wash line |
Latency | Time for one job, start to finish | 2 hours per laundry load |
Throughput | Jobs finished per unit of time | One load every 30 minutes |
Pipeline register (latch) | Storage between stages, written at each clock edge | The tray carrying a dish and its order slip |
Balanced | All stages take the same time | Every station needs exactly 30 minutes |
Clock skew | The clock edge reaching parts of the chip at slightly different times | Two watches that differ by a few seconds |
Fill / drain | Cycles to start and to empty the pipeline | First load needs 2 h before anything is ready |
ILP | Instruction-level parallelism: instructions overlapping in time | Several orders being cooked at once |
Self-Check Questions
- Explain the difference between latency and throughput with the dhobi shop. (§1.2)
- Name the five stages and the hardware used in each. (§2.1)
- Which stages does sw use? Why does lw take the longest? (§2.2)
- What does the ID/EX register carry, and why is the destination register number kept until WB? (§2.3)
- Draw the timing diagram for 6 independent instructions. How many cycles? (§2.4)
- Give two features of the MIPS ISA that make pipelining easy. (§2.5)
- How many cycles do 100 instructions need on an ideal 5-stage pipeline? What is the speedup? (§3.1)
- Stages 150, 100, 250, 200, 100 ps and 20 ps register overhead: clock and speedup? (§3.2)
- Repeat the Hennessy and Patterson example with 50% memory operations. (§3.3)
- Why is the milk-loop speedup of 3.67 only an ideal value? (§3.4)
Answers: 10 cycles; 104 cycles, speedup 4.81; clock 270 ps, speedup 2.96; 50% memory: 1 x (0.5 x 4 + 0.5 x 5) = 4.5 ns, 4.5 / 1.2 = 3.75.
Further Reading
- Hennessy and Patterson (2012): Appendix C, sections C.1 (what is pipelining) and C.2 (the classic five-stage pipeline).
- Patterson and Hennessy, Computer Organization and Design: Chapter 4.5-4.6 (pipelining overview, pipelined datapath).