4.1.18 · D4Computer Architecture (Deep)

Exercises — Pipelining — 5-stage pipeline, each stage

3,543 words16 min readBack to topic

Before we start, one picture to keep in your head the whole way down — the space-time diagram. Rows are instructions, columns are clock cycles, and each cell is which stage that instruction is in.

Figure — Pipelining — 5-stage pipeline, each stage

Look at the diagonal staircase: every cycle, one new instruction enters at the top-left (IF), and after the pipe is full, one instruction leaves at the bottom-right (WB) every single cycle. The slanted "fill" region on the left (before the first WB) and the "drain" region are where the overhead cycles live.


Level 1 — Recognition

Can you name and identify the pieces?

Exercise 1.1

Name the 5 stages of the classic RISC pipeline in order, and give the one-line job of each.

Recall Solution
# Stage Job
1 IF Fetch the instruction from memory at the address in the PC
2 ID Decode the opcode and read the source registers
3 EX ALU computes a result or a memory address
4 MEM Access data memory (load reads, store writes)
5 WB Write the result back into the register file

Mnemonic: "I Do Everything My Way."

Exercise 1.2

In the space-time diagram above, which stage is instruction I3 in during cycle 4?

Recall Solution

Count from I3's start. I3 enters IF in cycle 3 (each new instruction starts one cycle later). So:

  • Cycle 3 → IF
  • Cycle 4 → ID

The rule: instruction is in stage during cycle (with , starting at 1). For , : ID. ✔

Exercise 1.3

What piece of hardware sits between two adjacent stages, and what would go wrong without it?

Recall Solution

A pipeline register (latch) — the IF/ID, ID/EX, EX/MEM, MEM/WB registers. Without it, the output of stage for the current instruction would be overwritten on the next cycle by the next instruction entering stage . The register holds each instruction's own intermediate state as it travels down the pipe.


Level 2 — Application

Plug numbers into the formulas correctly.

Exercise 2.1

A 5-stage pipeline with balanced stages of each runs instructions. Find (a) the non-pipelined time , (b) the pipelined time , (c) the speedup .

Recall Solution

WHY these formulas: non-pipelined runs each instruction fully (all stages) before the next, so times just add. Pipelined overlaps them, so we pay cycles to fill, then 1 cycle each for the remaining .

(a) (b) (c)

Exercise 2.2

Same pipeline (, ). How many instructions are needed to reach a speedup of at least ?

Recall Solution

Set with : So you need at least instructions. Check: exactly. ✔

Exercise 2.3

The pipeline's stages are unbalanced with delays ns, and each pipeline register adds . What is the clock period , and the ideal throughput (instructions/second)?

Recall Solution

WHY the max: every stage shares one common clock; the clock cannot tick faster than the slowest stage can finish, or that stage's work would be cut off. Once full, one instruction completes per cycle, so


Level 3 — Analysis

Reason about behaviour, not just plug-in.

Exercise 3.1

You have a workload of instructions. Design choice: pipeline A has stages of ; pipeline B has stages of (same total work, finer split). Which finishes the workload faster, and by how much?

Recall Solution

Total work per instruction is the same ( ns) in both — B just slices it thinner. B wins: faster, a ratio of . WHY: deeper pipe → shorter cycle → more overlap. The extra fill cycles ( vs ) are tiny against . (In real hardware register overhead and hazards eventually cap this — see Superscalar and Out-of-Order Execution.)

Exercise 3.2

For the same two pipelines, at what do they finish in equal time? Interpret the answer.

Recall Solution

Set : They tie only at (a single instruction). Verify by plugging back in: and . Both ns. ✔ Interpretation: for a single instruction both pipelines just do the full ns of work with no overlap to exploit — latency is identical. For every , the deeper pipe B is strictly faster. Pipelining only pays off across streams.

Exercise 3.3

A pipeline's stages have delays ns except one "hot" stage runs at ns. You may split only the hot stage into two ns halves (making a 6-stage pipe). Ignoring register overhead, does throughput improve? By what factor?

Recall Solution

Before: , throughput instr/ns. After: the hot stage becomes two ns stages; now , throughput instr/ns. Improvement factor . WHY: the slowest stage alone dictates the clock. Splitting the bottleneck lets a different stage (the 5 ns ones) become the new limiter. This is the whole art of stage balancing.


Level 4 — Synthesis

Combine ideas; account for stalls and hazards.

Exercise 4.1

A 5-stage pipeline runs instructions, but hazards force 4 stall (bubble) cycles to be inserted total (see Pipeline Hazards (Data, Control, Structural)). Stage time . Find the real pipelined time and the real speedup versus non-pipelined.

Recall Solution

WHY add stalls to the formula: each bubble is a wasted cycle where no new instruction completes, so it just adds cycles onto the ideal count. Non-pipelined: . Compare to the ideal (no stalls) . The 4 stalls cost us about of speedup.

Exercise 4.2

A program has a fraction of instructions that each cause 1 stall cycle due to a data hazard. With , express the effective CPI (cycles per instruction) for large , then the throughput ratio versus the ideal pipeline. (CPI = average clock cycles each instruction costs; see Clock Frequency and CPI.)

Recall Solution

WHY CPI: for large the fill cycles vanish, so ideal CPI (one instruction finishes per cycle). Each stalling instruction adds its stall cycles to the average. Throughput , so the pipeline runs at of the ideal — about a 17% loss. This is exactly what Forwarding and Bypassing and Branch Prediction fight to recover.

Exercise 4.3 (control + structural hazards)

Same pipeline, large , ideal CPI . Now model two more hazard classes on top:

  • Control hazards: of instructions are branches; the pipeline resolves a branch in EX, so a taken branch flushes the 2 instructions already fetched behind it (a 2-cycle flush penalty). Assume of branches are taken.
  • Structural hazard: the machine has a single memory port shared by IF and MEM. On every load (say of instructions) IF and MEM collide, forcing 1 stall cycle.

Find the effective CPI and the throughput ratio versus the ideal pipeline.

Recall Solution

WHY each term: every hazard just adds its expected extra cycles per instruction on top of the base CPI of 1. We weight each penalty by how often it actually fires.

Control-hazard cost per instruction: A not-taken branch causes no flush here (fetched instructions were correct), so only taken branches are counted — that is why the appears.

Structural-hazard cost per instruction: The IF and MEM stages both want the single memory port on a load cycle; one must wait.

Effective CPI: Throughput ratio — about a 25% loss versus the ideal pipeline. Control hazards () dominate here; that is precisely why deep pipes invest so heavily in Branch Prediction, and why adding a second memory port (a Harvard split of instruction/data memory) removes the structural term entirely.

Exercise 4.4

Amdahl's Law tie-in. A pipeline gives an ideal speedup, but only of your runtime is pipelineable instruction execution; the other is un-pipelineable I/O wait. What is the overall program speedup?

Recall Solution

WHY Amdahl: speedup applies only to the part you accelerate; the untouched part becomes the new bottleneck. Even a perfect engine yields only overall, because the I/O sets a ceiling of that you can never exceed no matter how deep the pipe.


Level 5 — Mastery

Design and reason about cases nobody handed you.

Exercise 5.1 (Design)

You must build a pipeline for a unit of work totalling , with register overhead per stage. Assume you can split the work into equal stages of ns each. Write and the large- throughput . Find the that maximises throughput among . Explain the trend.

Recall Solution

WHY the term: if the total useful work ( ns) is split into equal stages, each stage holds ns of that work — that is the "real" computation time in one stage. WHY the term: every stage ends in a pipeline register (latch) that must physically settle before the next cycle. That latch costs ns per stage regardless of how thin the stage is. So the clock must fit useful work + one latch: . WHY : once the pipe is full, exactly one instruction finishes every clock period , so instructions-per-nanosecond is just the reciprocal of the period. Evaluate:

(ns) (instr/ns)
1 12.5 0.080
2 6.5 0.154
3 4.5 0.222
4 3.5 0.286
6 2.5 0.400
8 2.0 0.500
12 1.5 0.667

Throughput keeps rising to : deeper is better here, because ( ns) is still small next to the useful work per stage. But notice the diminishing returns: doubling from 6→12 only raised by , not . As , ns (pure overhead) and caps at instr/ns — the overhead sets a hard ceiling. Real CPUs also stop deepening because deeper pipes suffer bigger branch mispredict penalties.

The figure below plots that table so you can see the diminishing-returns curve flatten toward the overhead ceiling — read it as the visual companion to the calculation you just did.

Figure — Pipelining — 5-stage pipeline, each stage

Each yellow dot is one row of the table; the dashed pink line is the ceiling instr/ns that no amount of extra depth can beat. Notice how the dots climb steeply at first (huge gain from 1→4 stages) then bend and creep toward the ceiling — exactly the "diminishing returns from register overhead" story of Exercise 5.1.

Exercise 5.2 (Edge / degenerate cases)

Reason through these limiting inputs of and . State each result and why: (a) ; (b) ; (c) ; (d) with fixed.

Recall Solution

(a) : . One instruction can't overlap with anything — no benefit. full latency, same as non-pipelined. (b) : . A "1-stage pipeline" is the non-pipelined machine — nothing to overlap. (c) : divide top and bottom by : . As the term , so . WHY: the fill cycles become a vanishing fraction of a huge instruction stream, so you effectively finish 1 instr/cycle versus instr/cycle non-pipelined — the maximum, equal to the stage count. (d) , fixed: divide top and bottom by : . As the term , so . WHY: with absurdly many stages the fill cost is dwarfed, but you can never overlap more instructions than you actually have — so speedup is capped by , the instruction count, not by . (This assumes ideal balanced stages and ignores , which in reality kills very large .)

Symmetry insight: in the two limits the speedup is bounded by whichever of "stages" () or "instructions" () runs out first — you cannot overlap beyond the scarcer resource.

Exercise 5.3 (Synthesis + judgement)

A vendor sells a 20-stage pipeline claiming " faster." Your workload: instructions, of which are branches, each branch mispredict costing stall cycles (whole pipe flushed). Estimate the real speedup versus a non-pipelined machine, assume of branches mispredict, and judge the vendor's claim.

Recall Solution

Stalls: branches . Mispredicted (use as an expected value). Each costs cycles → stall cycles . Pipelined cycles cycles. Non-pipelined = cycles (each instruction cycles of work, one at a time). Both use the same short cycle time , so compare cycle counts: Judgement: the claim is fantasy for this workload. Two killers: (1) only instructions, so the fill cycles are a big fraction; (2) deep pipes make each mispredict brutally expensive ( cycles). The vendor's number assumes infinite and zero hazards. Lesson: advertised max speedup is a ceiling you rarely approach — real gains depend on stream length and hazard rate. See Branch Prediction and Superscalar and Out-of-Order Execution for how modern CPUs claw some of this back.


Active Recall (mixed)

Recall Quick: speedup formula and its large-

limit? , which approaches (the stage count) as .

Recall What sets the clock period in an unbalanced pipeline?

The slowest (max) stage delay plus the pipeline-register overhead . Splitting any other stage doesn't help.

Recall Why does a single instruction get zero speedup from pipelining?

With there is nothing to overlap; and its latency is still the full stages.

Recall Which hazard class costs the most in a deep pipeline, and why?

Control hazards (branch flushes): a mispredict throws away all the instructions fetched behind the branch, and a deeper pipe means more wasted instructions per flush — hence heavy investment in branch prediction.


Connections