TactivergeLite

Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 8

8. Performance and pipelining

Version 1 · Technically checked (automated checks only) · 4 source lessons · Full interactive version

Introduction

How fast is a processor? The Iron Law splits the answer into three factors: instructions per program, cycles per instruction and time per cycle. This chapter finds what limits the clock period, then shows how pipelining overlaps instructions like an assembly line, and why hazards stop the overlap from ever being perfect.

Figure 8.1. Four instructions in a five-stage pipeline (described in words)

A grid with four instructions as rows and eight clock cycles as columns. Each instruction passes through IF, ID, EX, MEM and WB, starting one cycle after the one before. All four finish in 8 cycles instead of the 20 cycles they would take one after another.

Explanation

Built from 4 School lessons. Each part is the lesson's own explanation.

8.1 How fast can the clock tick?

Why can't a processor designer just set the clock to 100 GHz and make everything 25 times faster?

The critical path

Every path from a register output through gates to a register input must settle within one clock period. The longest one sets the clock. In a ripple-carry adder it is the carry rippling through every column: about 2 gate delays per bit, so a 32-bit ripple adder is roughly eight times slower than a 4-bit one.

Shortening it

Designers attack the critical path with faster circuits (carry-lookahead adders compute carries in parallel at the cost of more gates), or by pipelining: putting registers in the middle of a long path so each piece fits a shorter period. The clock speeds up; each operation now takes more cycles, but a new one can start every cycle.

More cycles or faster cycles?

Run time = cycles × T_clk. A single-cycle design does everything in one long cycle; a multi-cycle design (like the 3-step model in the stepper) uses several short ones. Which wins depends on how evenly the work splits: the iron law T = IC × CPI × T_clk keeps every trade-off honest.

Derivation: The register-to-register timing constraint
  1. At a clock edge the launching register's output changes after t_clk→Q.

  2. The new value travels through combinational logic; the slowest route, the critical path, takes t_logic.

  3. The capturing register needs its input stable for t_setup before the next edge.

  4. So the next edge must come no sooner than T_clk ≥ t_clk→Q + t_logic + t_setup, and f_max = 1/T_clk.

  5. Example: 0.2 + 10 × 0.1 + 0.1 = 1.3 ns, so f_max = 1/1.3 ns ≈ 769 MHz.

Common mix-ups

Open the Lite lesson: How fast can the clock tick?

8.2 Single-cycle, multi-cycle and the Iron Law

Chip A runs at 4 GHz, chip B at 2 GHz. B finishes your program first. How?

Single-cycle datapath

Every instruction completes in one long clock cycle, so CPI = 1, but the cycle must be long enough for the slowest path (instruction memory → register read → ALU → data memory → register write, for a load). Simple instructions waste most of the cycle.

Multi-cycle datapath

Break execution into steps (fetch, decode, execute, memory, write-back) and give each step one short cycle. Instructions use only the steps they need, so CPI is now an average (perhaps 4) but the cycle is about the length of one step. Hardware can be reused across steps: one memory and one ALU instead of separate ones.

Three factors, coupled

A richer instruction set lowers IC but may raise CPI or the cycle time. A deeper pipeline shortens the cycle but raises CPI when hazards hit. A good comparison quotes all three and measures them on the same program, compiled the same way, which is why benchmark suites exist.

Derivation: The Iron Law, from units
  1. Time per program = (instructions / program) × (cycles / instruction) × (seconds / cycle).

  2. The units cancel: instructions × cycles/instruction × seconds/cycle = seconds. So T = IC × CPI × t_clk = IC × CPI / f.

  3. IC is set by the algorithm, the compiler and the instruction set. CPI by the microarchitecture (pipelining, caches, prediction). t_clk by the circuit and the process technology.

  4. Speed-up of B over A = T_A / T_B = (IC_A/IC_B) × (CPI_A/CPI_B) × (f_B/f_A).

  5. Example: A has IC = 10⁹, CPI = 2, 4 GHz: T = 0.5 s. B has IC = 0.8×10⁹, CPI = 1.1, 2 GHz: T = 0.44 s. B wins by 0.5/0.44 ≈ 1.14× despite half the clock.

Common mix-ups

Open the Lite lesson: Single-cycle, multi-cycle and the Iron Law

8.3 Pipelining: an assembly line for instructions

A laundry takes 30 minutes to wash, 40 to dry and 20 to fold. How long do four loads take if you never wait for one to finish before starting the next?

Throughput versus latency

Pipelining does not make one instruction faster: it still passes through every stage, and register overhead makes it slightly slower. What improves is throughput: once full, the pipeline completes one instruction every cycle. Ideal CPI approaches 1 for long runs.

The classic five stages

IF fetches the instruction, ID decodes it and reads registers, EX uses the ALU, MEM accesses data memory, WB writes the result back. Registers between stages hold each instruction's state so the stages can work on different instructions at once.

Balance and overhead

The clock must suit the slowest stage, and every stage boundary adds a register's setup and clock-to-output delay. Deeper pipelines get diminishing returns and, as you will see, pay more for every hazard. Modern designs settle on roughly 10–20 stages as a compromise.

Derivation: How much faster can a pipeline be?
  1. Unpipelined: each instruction takes T, so n instructions take n·T.

  2. Split into k stages. If perfectly balanced, each stage is T/k, but each needs a pipeline register adding overhead o. Cycle = T/k + o.

  3. Pipelined time for n instructions = (k + n − 1)(T/k + o).

  4. Speed-up S = n·T / ((k + n − 1)(T/k + o)). As n → ∞, S → T/(T/k + o) = k / (1 + k·o/T).

  5. With no overhead the limit is k. With o = 0.1 T, a 5-stage pipe tops out at 5/1.5 ≈ 3.3×; a 20-stage pipe at 20/3 ≈ 6.7×, not 20×.

  6. Unbalanced stages are worse: the cycle is set by the slowest stage, so S ≤ T / max(stage delay).

Common mix-ups

Open the Lite lesson: Pipelining: an assembly line for instructions

8.4 Hazards, forwarding and branch prediction

An instruction needs a number the previous one hasn't finished computing. Does the whole assembly line stop?

Data hazards

A read-after-write hazard occurs when an instruction needs a register that an earlier, still-in-flight instruction will write. Without help, the reader stalls in ID until the writer reaches WB: 2 bubbles for adjacent instructions in the classic 5-stage pipe. Forwarding (bypassing) wires the ALU output straight back to the ALU input, removing the stall for ALU results.

The load-use stall

A load's data appears only at the end of MEM. Even with forwarding, an instruction that uses it immediately must wait one cycle. Compilers hide this by scheduling an independent instruction between the load and its use, which is why the 'Load then use' program in the pipeline diagram has an unrelated sub in the middle.

Control and structural hazards

A branch is not resolved until EX, so the pipeline has already fetched 2 instructions that may be wrong. Stalling on every branch wastes those cycles always; predict not taken wastes them only when the branch is taken; dynamic predictors learn each branch's habit and are right well over 90% of the time on typical code. A structural hazard arises when two stages need one resource, like a single memory port for fetch and data: hence separate instruction and data caches.

Hazards in the Iron Law

Every stall adds to CPI: CPI = 1 + (data stalls per instruction) + (branch fraction × mispredict rate × penalty) + (memory stalls). Deeper pipelines raise the branch penalty, so a faster clock can lose to a shorter pipe with a better predictor.

Common mix-ups

Open the Lite lesson: Hazards, forwarding and branch prediction

Worked examples

How fast can the clock tick?: example 1

A 16-bit ripple adder has 2 gate delays per bit plus 1, at 0.08 ns each, between registers with t_cq = 0.15 ns and t_su = 0.1 ns. Find f_max.

Solution

Gate delays = 2 × 16 + 1 = 33; t_logic = 2.64 ns. T = 0.15 + 2.64 + 0.1 = 2.89 ns. f_max ≈ 346 MHz.

Single-cycle, multi-cycle and the Iron Law: example 1

A mix is 50% ALU (4 steps), 20% loads (5), 10% stores (4), 20% branches (3). Step time 200 ps. Compare with an 800 ps single-cycle design.

Solution

CPI = 0.5×4 + 0.2×5 + 0.1×4 + 0.2×3 = 4.0. Time per instruction = 4.0 × 200 ps = 800 ps: a tie. Shorten the step to 180 ps (balancing stages better) and multi-cycle wins: 720 ps.

Pipelining: an assembly line for instructions: example 1

Stages of 200, 150, 250, 300 and 100 ps, register overhead 20 ps. Find the cycle time and the speed-up for many instructions versus the 1000 ps unpipelined design.

Solution

Cycle = slowest stage + overhead = 300 + 20 = 320 ps. Limit speed-up = 1000/320 ≈ 3.1×, well below 5× because the stages are unbalanced.

Hazards, forwarding and branch prediction: example 1

lw r1, 0(r2) then add r3, r1, r4, with forwarding. Stalls?

Solution

  1. The loaded value exists only after MEM; add's EX must come the cycle after lw's MEM, one cycle later than it would without the dependency.
Hazards, forwarding and branch prediction: example 2

Branches are 15% of instructions; a predictor is wrong 8% of the time; the penalty is 3 cycles. Extra CPI?

Solution

0.15 × 0.08 × 3 = 0.036. With base CPI 1, effective CPI 1.036.

Interactive figures, in words

Figure 8.2 from Pipelining: an assembly line for instructions

Choose 'Independent ALU ops', turn forwarding on and choose 'Perfect predictor'. Read the diagonal staircase and check the cycle count against k + n − 1. Then pick the other programs to see the ideal break: that is next lesson's topic.

The pipeline table has one row per instruction and one column per clock cycle; each instruction's IF, ID, EX, MEM and WB cells shift one column to the right of the previous instruction's, forming a staircase. For 5 independent instructions it ends at cycle 9.

Figure 8.3 from Hazards, forwarding and branch prediction

Every stall cycle adds to CPI, and this calculator shows how much. Set branches to 20%, mispredict to 10% and penalty to 2: read the CPI. Raise the penalty to 15 (a deep pipeline) and watch the branch slice grow. Pin a design, then trade a faster clock for a bigger penalty and read the speed-up.

The Iron Law calculator shows CPI as base + branch stalls + memory stalls. With 20% branches, 10% mispredicted at 2 cycles, and 30% memory instructions missing 5% at 40 cycles, CPI = 1 + 0.04 + 0.6 = 1.64, and T = IC × CPI ÷ f.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. t_clk→Q = 0.2 ns, logic = 1.0 ns, t_setup = 0.1 ns. What is f_max, in MHz (nearest whole number)?
    Answer it in “How fast can the clock tick?”
  2. What sets the maximum clock frequency of a synchronous design?
    Answer it in “How fast can the clock tick?”
  3. A program executes 750 million instructions with an average CPI of 0.8 on a 2 GHz processor. How long does it take, in milliseconds?
    Answer it in “Single-cycle, multi-cycle and the Iron Law”
  4. Design A: CPI 1.6 at 3 GHz. Design B: CPI 1.0 at 2.4 GHz, same instruction count. What is B's speed-up over A?
    Answer it in “Single-cycle, multi-cycle and the Iron Law”
  5. 127 instructions run on a 4-stage pipeline. Hazards insert 6 stall cycles in total. How many clock cycles until the last instruction completes?
    Answer it in “Pipelining: an assembly line for instructions”
  6. A 1,200 ps datapath is split into 6 perfectly balanced stages with 50 ps register overhead each. What is the speed-up limit for very long programs?
    Answer it in “Pipelining: an assembly line for instructions”
  7. lw r5, 4(r2) / add r6, r5, r1. Which hazard and, with forwarding, how many stall cycles?
    Answer it in “Hazards, forwarding and branch prediction”
  8. On the classic 5-stage pipeline with no forwarding, a chain of 4 ALU instructions each uses the previous one's result. How many total cycles? (Ideal is 8.)
    Answer it in “Hazards, forwarding and branch prediction”

Engineering applications

Comparing processors fairly

A higher clock frequency does not guarantee a faster program: if the faster clock needs more cycles per instruction, the Iron Law can come out worse. Engineers compare time for the same program.

Real-time control

A control loop must finish within its sample period every time. Pipeline stalls and branch mispredictions make timing vary, so real-time systems often measure worst-case time, not average time.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Pipelining
Pipelining splits instruction execution into stages and overlaps them, so that while one instruction executes the next is being decoded and the one after is being fetched. Field Guide
Pipeline hazards and forwarding
A hazard is a situation where the next instruction cannot proceed on schedule. Data hazards happen when an instruction needs a result an earlier one has not finished producing. Field Guide
Clock speed, cycles and CPI
Execution time equals the number of instructions times the average cycles per instruction (CPI) divided by the clock frequency. Field Guide
Amdahl's law
Amdahl's law says the overall speed-up from improving part of a task is limited by the fraction of time that part takes. Field Guide
Propagation delay
Propagation delay is the time a gate's output takes to respond to a change at its input. The slowest path between flip-flops, the critical path, sets the fastest safe clock. Field Guide

Project: Pipeline or not?

A single-cycle design has a 10 ns critical path. Splitting it into five stages adds 0.5 ns of register overhead per stage, and 20 % of instructions stall for one cycle. Estimate the time per instruction for both designs and decide whether pipelining pays, showing every step.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 7. Instructions and the datapath9. The memory hierarchy →