G27
Self-checkedTier 1 11 min readcoa.pipelining

Instruction pipelining

Overlapping the execution of instructions: the stages and pipeline registers, the space-time diagram, fill and drain, latency versus throughput, the clock set by the slowest stage plus register overhead, balancing and splitting stages, and why uniform RISC instructions pipeline well.

Why it exists

In the instruction cycle a processor fetches, decodes, executes and writes the result of one instruction, then starts the next. While the ALU is executing, the instruction memory sits idle and the decoder sits idle. Pipelining puts every part of the hardware to work at once: it splits the instruction into stages, gives each stage its own hardware, and lets several instructions be in different stages in the same clock cycle, like cars on an assembly line. One instruction is no faster, but one finishes every cycle. GATE asks for the number of cycles, the speedup, the clock period of a pipeline and what limits it; the next page does the full arithmetic, and the hazard pages deal with what goes wrong.

Prerequisites

You need the steps of the instruction cycle, which become the pipeline's stages.

Precise definitions

Pipeline. A processing unit divided into kk stages. A pipeline register (latch) between consecutive stages holds the results of one stage while the next one uses them. On each clock cycle every instruction moves one stage forward and a new instruction enters the first stage.

The classic five stages (the textbook teaching pipeline):

StageNameWork
IFinstruction fetchread the instruction at PC, compute PC + 4
IDinstruction decodedecode, read the source registers
EXexecuteALU operation, or compute the address of a load, store or branch
MEMmemory accessread or write the data memory (loads and stores only)
WBwrite backwrite the result into a register

Other splits are common in questions: a 4-stage pipeline (fetch, decode, execute, write back), or the six-stage one used in some books (fetch instruction, decode instruction, calculate operands, fetch operands, execute, write operand). Always use the number of stages the question gives.

Space-time (pipeline) diagram. A table with a row per instruction and a column per clock cycle; each cell names the stage the instruction occupies. It is the main tool for counting cycles. In a diagram without stalls, instruction ii (i=1,2,…i = 1, 2, \ldots) is in stage ss (s=1,…,ks = 1, \ldots, k) during cycle i+s−1i + s - 1.

Fill, steady state and drain. For the first k−1k - 1 cycles the pipeline is filling: some stages are idle. From cycle kk on, every stage is busy and one instruction completes per cycle. After the last instruction is fetched the pipeline drains.

Latency and throughput.

  • The latency of one instruction is the time from entering the first stage to leaving the last: kk cycles. Pipelining does not shorten it (and with register overhead it lengthens it).
  • The throughput is the number of instructions completed per unit time: ideally one per cycle.

Clock cycle. All stages share one clock, so the period must cover the slowest stage plus the delay of the pipeline register (setup time and clock-to-output delay):

τpipe=max⁡s(stage delays)+dreg.\tau_{pipe} = \max_s(\text{stage delay}_s) + d_{reg}.

If the stages are unequal, the fast stages wait for the slowest one (waste). Balancing the stages, by moving work between them or splitting the slowest one in two, shortens the clock but adds a stage and a register.

What the pipeline registers carry. Everything later stages need: the instruction fields, the register values read, the ALU result, the data loaded, and the control signals for the remaining stages. The control information travels with its instruction, so each stage knows what to do for the instruction currently in it.

Why RISC pipelines well. Fixed-length instructions make the fetch and decode stages uniform; simple instruction formats let decoding and register reading happen together; loads and stores are the only instructions that access memory, so the MEM stage is used predictably; and each instruction needs each stage at most once and in the same order. Instructions of widely different lengths and complexity (many CISC instructions) leave stages unbalanced and some instructions needing a stage several times, which is why they are translated to simple internal operations before pipelining.

Stall (bubble). A cycle in which an instruction is held in its stage, or in which a stage does nothing useful, because the instruction cannot proceed. Stalls lower throughput below one instruction per cycle; their causes are the subject of pipeline hazards.

How it works

The first animation is the baseline: three instructions on a machine without a pipeline. One instruction runs through all five stages before the next starts, so at any moment four of the five stages are idle.

Without a pipeline: three instructions one after another: start

Pipeline simulated, not typed
cycle123456789101112131415
I1
I2
I3
IF
-
ID
-
EX
-
MEM
-
WB
-
cycles so far
0
completed
0
cycles per completed instruction
-

Step 0

The pipeline is empty. Each row is an instruction, each column a clock cycle, and a cell says which stage of the 5-stage pipeline (IF, ID, EX, MEM, WB) the instruction is in during that cycle.

0 / 15
computed by script: cycle-by-cycle simulation of an in-order pipeline. python3 scripts/gen-coa-pipe-traces.py

The second animation is the same hardware pipelined. Step through it and watch the fill (cycles 1 to 4), the full pipeline (cycle 5 onward: five different instructions in five different stages) and the drain. Six instructions finish in k+n−1=10k + n - 1 = 10 cycles, against 30 without the pipeline.

A 5-stage pipeline, six instructions, no stalls: start

Pipeline simulated, not typed
cycle12345678910
I1
I2
I3
I4
I5
I6
IF
-
ID
-
EX
-
MEM
-
WB
-
cycles so far
0
completed
0
cycles per completed instruction
-

Step 0

The pipeline is empty. Each row is an instruction, each column a clock cycle, and a cell says which stage of the 5-stage pipeline (IF, ID, EX, MEM, WB) the instruction is in during that cycle.

0 / 10
computed by script: cycle-by-cycle simulation of an in-order pipeline. python3 scripts/gen-coa-pipe-traces.py

The third animation shows what a stall does to the diagram. It is the subject of the next pages; for now notice that a held instruction blocks all those behind it, and every extra cycle is paid by everything after it.

The same pipeline when two instructions are held up: start

Pipeline simulated, not typed
cycle12345678910111213
I1
I2
I3
I4
I5
I6
IF
-
ID
-
EX
-
MEM
-
WB
-
cycles so far
0
completed
0
cycles per completed instruction
-

Step 0

Same pipeline, but I3 must stay in ID for 2 extra cycles and I5 for 1 extra cycle (for example because each is waiting for a result; the hazard pages explain why and how to shorten the wait). Every instruction behind a held one is blocked as well.

0 / 13
computed by script: cycle-by-cycle simulation of an in-order pipeline. python3 scripts/gen-coa-pipe-traces.py

Worked examples

EX 1Counting cycles with and without a pipeline

Easy

A 5-stage pipeline with one-cycle stages and no stalls. How many cycles do nn instructions need with and without pipelining?

#include <stdio.h>
 
int main(void) {
    int k = 5;
    long ns[5] = {1, 2, 6, 100, 1000000};
    for (int i = 0; i < 5; i++) {
        long n = ns[i];
        long plain = n * k, piped = n + k - 1;
        printf("n = %7ld: without pipeline %7ld cycles, with %7ld cycles, speedup %.3f\n", n, plain, piped, (double)plain / (double)piped);
    }
    return 0;
}
n =       1: without pipeline       5 cycles, with       5 cycles, speedup 1.000
n =       2: without pipeline      10 cycles, with       6 cycles, speedup 1.667
n =       6: without pipeline      30 cycles, with      10 cycles, speedup 3.000
n =     100: without pipeline     500 cycles, with     104 cycles, speedup 4.808
n = 1000000: without pipeline 5000000 cycles, with 1000004 cycles, speedup 5.000

Reading it. Without a pipeline: nknk cycles. With it: the first instruction takes kk cycles and each of the other n−1n - 1 finishes one cycle later, so k+n−1k + n - 1. A single instruction gains nothing; the speedup climbs towards kk as nn grows but never reaches it.

EX 2Reading the space-time diagram

Easy

In the second animation (six instructions), what is every stage doing in cycle 6?

Instruction ii is in stage ss in cycle i+s−1i + s - 1, so in cycle 6: IF has I6 (6+1−16 + 1 - 1), ID has I5, EX has I4, MEM has I3, WB has I2. I1 finished at the end of cycle 5. In the same cycle every stage works on a different instruction, which is exactly why the hardware is fully used. Step the animation to cycle 6 and compare with the stage boxes below the diagram.

EX 3When does the last instruction finish?

Medium

A kk-stage pipeline has a clock period τ\tau and no stalls. The nn-th instruction enters stage 1 in cycle nn and leaves stage kk at the end of cycle n+k−1n + k - 1, so it completes at time (n+k−1) τ(n + k - 1)\,\tau. For k=6k = 6, n=1000n = 1000, τ=2 ns\tau = 2\ \text{ns}: (1000+6−1)×2=2010 ns(1000 + 6 - 1) \times 2 = 2010\ \text{ns}, against 1000×6×2=12000 ns1000 \times 6 \times 2 = 12000\ \text{ns} without pipelining at the same clock.

EX 4Splitting the slowest stage

Medium

The five stages take 12, 8, 10, 14 and 6 ns and each pipeline register adds 1 ns. The clock period is 14+1=1514 + 1 = 15 ns. Split the 14 ns stage into two stages of 7 ns: the stages are now 12, 8, 10, 7, 7, 6. The slowest is 12, so the period becomes 12+1=1312 + 1 = 13 ns: instructions complete 15/13≈1.1515/13 \approx 1.15 times as fast. The price: one more stage and register, and the latency rises from 5×15=755 \times 15 = 75 ns to 6×13=786 \times 13 = 78 ns. The next split would have to cut the 12 ns stage, since it is now the limit. Splitting a stage that is not the slowest changes nothing.

EX 5Is the pipelined machine faster for one instruction?

GATE level

Using the same stage delays, an instruction on the non-pipelined machine needs 12+8+10+14+6=5012 + 8 + 10 + 14 + 6 = 50 ns (no registers between the stages). On the pipelined machine the same instruction takes 5×15=755 \times 15 = 75 ns. So a single instruction is slower, by 25 ns, but a long run of instructions is faster: one finishes every 15 ns instead of every 50 ns. Pipelining improves throughput, not latency. A question that asks "time for the first instruction" has the answer kτk\tau, not a speedup.

Cheat box

Cheat box

  • kk stages; one instruction enters and one completes per cycle once full.
  • Cycles for nn instructions with no stalls: k+n−1k + n - 1. Without a pipeline: nknk.
  • Instruction ii is in stage ss in cycle i+s−1i + s - 1.
  • Clock period =max⁡(stage delay)+dreg= \max(\text{stage delay}) + d_{reg}. Latency =k×= k \times period. Throughput →1\to 1 instruction per period.
  • Pipelining raises throughput; latency of one instruction does not fall.
  • Speedup << kk. Unbalanced stages waste the fast ones. Split the slowest stage to speed the clock up.
  • RISC (fixed length, load/store) pipelines easily. Stalls reduce throughput.

Traps and GATE patterns

Connections

The stages come from the instruction cycle and the hardware in each is the datapath cut by pipeline registers. A regular instruction set (ISA design) is what makes the stages uniform. The exact speedup arithmetic is on pipeline speedup and CPI, and the causes of stalls are in pipeline hazards.

PYQs

3 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2016 · NATQ16 · page 457 of the book
Suppose the functions F and G can be computed in
5 and 3 nanoseconds by functional units UF and UG,
respectively. Given two instances of UF and two
instances of UG, it is required to implement the
computation F(G(Xi)) for 1 ≤ i ≤ 10. Ignoring all
other delays, the minimum time required to
complete this computation is
nanoseconds.
GATE 2015 · NATQ19 · page 457 of the book
Consider the following reservation table for a
pipeline having three stages S1, S2 and S3.

The minimum average latency (MAL) IS
GATE 2009 · MCQ · 2 marksQ31 · page 459 of the book
Consider a 4 stage pipeline processor. The number of
cycles needed by the four instructions I1, I2, I3, I4 in
stages S1, S2, S3, S4 is shown below.

What is the number of cycles needed to execute the
following loop?
For (i = 1 to 2) {I1; I2; I3; I4;}
  • (a)16
  • (b)23
  • (c)28
  • (d)30 Computer Organization and Architecture

Self-check

Question 1

How many cycles does a 4-stage pipeline need for 20 instructions with no stalls?

Show answer

k+n−1=4+20−1=23k + n - 1 = 4 + 20 - 1 = 23 cycles.

Question 2

Stage delays are 5, 7, 6, 9 and 4 ns with a 1 ns register. What is the clock period and the time for the first instruction?

Show answer

Period =9+1=10= 9 + 1 = 10 ns. The first instruction needs 5×10=505 \times 10 = 50 ns.

Question 3

In a 5-stage pipeline with no stalls, which instruction is in the MEM stage in cycle 9?

Show answer

Instruction ii is in stage 4 (MEM) in cycle i+3i + 3, so i=6i = 6.

Question 4

Why does splitting a stage that is not the slowest not speed up the pipeline?

Show answer

The clock period is set by the slowest stage, which is unchanged. The split only adds a stage and a register.

Revision card

One-screen revision

  • kk stages with registers between; one instruction in each stage per cycle.
  • No stalls: k+n−1k + n - 1 cycles; instruction ii in stage ss at cycle i+s−1i + s - 1.
  • Period =max⁡(stage)+dreg= \max(\text{stage}) + d_{reg}; latency =k×= k \times period.
  • Throughput improves, one-instruction latency does not.
  • Split the slowest stage to shorten the clock.

References

  • ·Hennessy and Patterson, Computer Organization and Design, Chapter 4 The Processor (an overview of pipelining, a pipelined datapath and control)
  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., the Pipelining chapter (basic concepts, pipeline performance)
  • ·Stallings, Computer Organization and Architecture, Instruction pipelining chapter (pipelining strategy, pipeline performance)

Verification

The space-time diagrams are produced by a cycle-by-cycle simulation (scripts/gen-coa-pipe-traces.py) and re-checked by tests/coa-pipe-traces.test.ts (one instruction per stage per cycle, stage order, in-order finish, cycles = k + n - 1 for the ideal pipeline). scripts/enum-checks/coa_pipe.py compares the schedule with an independent tick-by-tick model on 4000 random stall patterns and checks the speedup formulas with exact fractions. The cycle-count table is produced by the C program on this page (npm run verify:c). The five stages and their work are the textbook teaching pipeline; real pipelines differ in depth and in what each stage does, and GATE questions may give another stage count. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never