4.1.18Computer Architecture (Deep)

Pipelining — 5-stage pipeline, each stage

1,848 words8 min readdifficulty · medium6 backlinks

WHAT is a pipeline?

The classic RISC 5-stage pipeline (MIPS-style) stages are:

# Stage Name What it does
1 IF Instruction Fetch Read instruction from memory at the PC
2 ID Instruction Decode / register read Decode opcode, read source registers
3 EX Execute / address calc ALU computes result or memory address
4 MEM Memory access Load/store reads or writes data memory
5 WB Write Back Write result into the register file
Figure — Pipelining — 5-stage pipeline, each stage

Deriving the speedup FROM SCRATCH

HOW we derive the time for nn instructions.

Let each stage take tt time. A single non-pipelined instruction takes all kk stages: Tinstr=ktT_{\text{instr}} = k \cdot t

Non-pipelined, nn instructions run one after another: Tseq=nktT_{\text{seq}} = n \cdot k \cdot t

Pipelined: the clock period must fit the slowest stage, so cycle time =t= t (assuming balanced stages). The first instruction needs kk cycles to fill the pipe. After that, one instruction finishes every cycle. So instructions 2,3,,n2,3,\dots,n add one cycle each: Tpipe=kfillt+(n1)remainingt=(k+n1)tT_{\text{pipe}} = \underbrace{k}_{\text{fill}} \cdot t + \underbrace{(n-1)}_{\text{remaining}} \cdot t = (k + n - 1)\,t


Throughput vs Latency


Why the clock can run faster

In reality stages are unbalanced and registers add latency tregt_{\text{reg}}: tclk=max(stage delays)+tregt_{\text{clk}} = \max(\text{stage delays}) + t_{\text{reg}} This is why we try to balance stages — the slowest stage sets the whole pace.


Common mistakes (Steel-manned)


Active Recall

Recall Name the 5 stages in order and one job of each.

IF (fetch instruction at PC), ID (decode + read registers), EX (ALU compute / address calc), MEM (data memory read/write), WB (write result to register file).

Recall Why is max speedup equal to the number of stages?

Because for large nn one instruction finishes every cycle (throughput 1/t1/t) vs 1/(kt)1/(kt) non-pipelined, a k×k\times ratio; the k1k-1 fill cycles become negligible.

Recall Does pipelining reduce latency of a single instruction?

No. Latency stays kk cycles (slightly worse with register overhead). It improves throughput.


What are the 5 stages of the classic RISC pipeline (in order)?
IF, ID, EX, MEM, WB
What does the IF stage do?
Fetches the instruction from memory at the current PC.
What does the ID stage do?
Decodes the instruction and reads source operands from the register file.
What does the EX stage do?
Performs the ALU operation or calculates a memory address.
What does the MEM stage do?
Accesses data memory (loads read, stores write).
What does the WB stage do?
Writes the result back into the register file.
Formula for pipelined time of n instructions, k stages, stage time t?
T = (k + n - 1) * t
Formula for ideal pipeline speedup?
S = nk / (k + n - 1), approaching k as n→∞.
What is the maximum theoretical speedup of a k-stage pipeline?
k (the number of stages).
What is the purpose of pipeline registers?
To latch each instruction's intermediate state between stages so it isn't overwritten by the next instruction.
Does pipelining reduce single-instruction latency?
No — only throughput improves; latency stays ~k cycles.
What sets the pipeline clock period?
The slowest stage delay plus pipeline-register overhead.
Why do small n give poor speedup?
The k−1 fill (warm-up) cycles dominate when few instructions are processed.

Connections

Concept Map

overlaps steps of

goal

splits execution into

1

latched by

feed

feeds

feeds

feeds

cycle time set by

derives

gives

limit as n grows

Pipelining

Consecutive instructions

Higher throughput

5 RISC stages

IF Instruction Fetch

Pipeline registers

ID Decode and reg read

EX Execute or addr calc

MEM Memory access

WB Write Back

Slowest stage t

T pipe = k+n-1 times t

Speedup = nk / k+n-1

Max speedup = k stages

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek instruction ko complete hone ke liye 5 chote-chote kaam karne padte hain: pehle memory se instruction laana (IF), phir use samajhna aur registers padhna (ID), phir ALU se calculation (EX), phir data memory access (MEM), aur last me result wapas register me likhna (WB). Agar hum ek instruction poora khatam karke hi agla shuru karein, to har waqt sirf ek hi hardware part busy rahega aur baaki khaali baithe rahenge — bahut bekaar.

Pipelining ka jugaad yeh hai: jaise hi instruction 1 IF se nikal kar ID me jaata hai, instruction 2 turant IF me ghus jaata hai. Isse har stage hamesha kaam karta rehta hai, bilkul laundry ya sandwich assembly line ki tarah. Beech me jo "pipeline registers" hote hain wo har instruction ka adha-pakaa kaam hold karke rakhte hain taaki agla instruction usse mita na de.

Yaad rakhna important baat: pipelining se ek single instruction tez nahi hoti — uski latency abhi bhi 5 cycle hi hai. Jo improve hota hai wo throughput hai, yaani per second kitne instructions complete hote hain. Formula: nn instructions ke liye time =(k+n1)t=(k+n-1)t aur speedup =nkk+n1=\frac{nk}{k+n-1}, jo bade nn par stages ki sankhya kk ke kareeb pahunch jaata hai (yahan 5x tak).

Par real life me itna perfect 5x nahi milta — kyunki hazards (data/control), unbalanced stages, aur shuruaati fill cycles speedup ko kam kar dete hain. Isliye next topics hazards aur forwarding bhi padhna zaroori hai.

Go deeper — visual, from zero

Test yourself — Computer Architecture (Deep)

Connections