Skip to content
Chethan Yadav_
Computer Architecture

COA/Lesson 4 of 6/7 min read

Pipelining

Instruction pipelining, the classic five-stage pipeline, speedup, structural, data and control hazards, forwarding, stalls and branch prediction.


On this page

Pipelining

Pipelining overlaps the execution of multiple instructions, like an assembly line. While one instruction executes, the next is decoded and the one after is fetched.

It does not make a single instruction faster (latency stays the same or grows slightly). It increases throughput: ideally one instruction finishes every cycle.

Laundry analogy: wash, dry, fold. Doing loads one at a time takes 3 units each; pipelined, a new load finishes every unit once the pipeline is full.

Five-Stage Pipeline

The classic RISC (MIPS) pipeline:

StageNameWork
IFInstruction FetchRead instruction at PC, increment PC
IDInstruction DecodeDecode, read registers
EXExecuteALU operation or address calculation
MEMMemory AccessLoad or store data
WBWrite BackWrite result to register file
Cycle:   1    2    3    4    5    6    7
I1:      IF   ID   EX   MEM  WB
I2:           IF   ID   EX   MEM  WB
I3:                IF   ID   EX   MEM  WB

Pipeline registers between stages (IF/ID, ID/EX, EX/MEM, MEM/WB) hold intermediate values. The clock period is set by the slowest stage, so stages should be balanced.

Pipeline Speedup

For n instructions on a k-stage pipeline with no stalls:

Non-pipelined time = n * k cycles
Pipelined time     = k + (n - 1) cycles
Speedup            = (n * k) / (k + n - 1)

As n grows large, speedup approaches k.

Worked example: 100 instructions, 5 stages. Non-pipelined: 500 cycles. Pipelined: 5 + 99 = 104 cycles. Speedup = 500 / 104 = 4.8.

If stage delays are unequal (for example 200, 100, 200, 200, 100 ps), the cycle time is the max stage delay plus pipeline register overhead.

Why real speedup is lower than k: unbalanced stages, register overhead, and hazards that force stalls.

Pipeline Hazard

A situation that prevents the next instruction from executing in its designated cycle.

HazardCauseMain fixes
StructuralTwo instructions need the same hardwareDuplicate resources
DataAn instruction needs a result not yet producedForwarding, stalls, reordering
ControlNext PC unknown until a branch resolvesPrediction, delayed branch, flushing

Structural Hazard

Two stages need the same hardware resource in the same cycle. Example: with a single memory, IF of one instruction and MEM of a load collide.

Fix: separate instruction and data caches (modified Harvard), multiple register file ports, duplicated functional units.

Data Hazard

An instruction depends on the result of an earlier instruction still in the pipeline.

TypePatternNameIn simple in-order pipeline
RAWRead after writeTrue dependenceCommon, must handle
WARWrite after readAnti-dependenceOnly with out-of-order
WAWWrite after writeOutput dependenceOnly with out-of-order
ADD R1, R2, R3     # writes R1 in WB (cycle 5)
SUB R4, R1, R5     # reads R1 in ID (cycle 3): RAW hazard

Operand Forwarding

Also called bypassing. Send the ALU result straight from the EX/MEM or MEM/WB pipeline register to the ALU input of the next instruction, instead of waiting for write back.

With forwarding, the ADD then SUB sequence above runs with no stall.

Load-use hazard

Forwarding cannot fully fix a load followed immediately by a use, because the data is only available after MEM:

LW  R1, 0(R2)
ADD R3, R1, R4     # needs R1 at EX, but it arrives after MEM: 1 stall

The compiler can hide this by scheduling an independent instruction into the slot.

Pipeline Stall

A bubble (NOP) inserted to delay dependent instructions until data is ready. The hazard detection unit freezes the PC and IF/ID register and injects a bubble into ID/EX.

Effect on performance:

Effective CPI = Ideal CPI + stall cycles per instruction
Speedup = Pipeline depth / (1 + stall cycles per instruction)

Example: 5-stage, 20% of instructions are loads, half of them followed by a dependent use (1 stall each). Stall cycles per instruction = 0.2 * 0.5 * 1 = 0.1, so CPI = 1.1.

Control Hazard

Branches decide the next PC late (EX stage in the basic pipeline). Instructions fetched after the branch may be wrong and must be flushed.

Branch penalty: cycles wasted per mispredicted or unresolved branch. With branches being about 15 to 25% of instructions, this matters a lot.

Fixes:

  • Stall until the branch resolves (simple, slow).
  • Resolve earlier: compute the branch condition and target in ID to cut the penalty to 1 cycle.
  • Delayed branch: always execute the instruction after the branch (delay slot); the compiler fills it with useful work. Used in early MIPS.
  • Branch prediction: guess and continue; flush if wrong.

Branch Prediction

Static prediction

Fixed rule: predict not taken, or predict backward branches taken (loops) and forward ones not taken.

Dynamic prediction

Learn from history at runtime.

  • 1-bit predictor: remember the last outcome. A loop mispredicts twice per run (first and last iterations).
  • 2-bit saturating counter: must be wrong twice before changing the prediction. States: strongly taken, weakly taken, weakly not taken, strongly not taken.
  • Branch Target Buffer (BTB): caches target addresses so the fetch can redirect in the IF stage.
  • Correlating / global history and TAGE predictors in modern CPUs reach accuracy above 95%.

Misprediction cost grows with pipeline depth: modern CPUs with 15 to 20 stages lose 15+ cycles per miss. This is why sorted data can make a loop with an if run much faster: the branch becomes predictable.

Spectre exploits speculative execution after branch prediction: mispredicted instructions leave traces in the cache even though their results are discarded.

Beyond the simple pipeline

Superscalar

Issue multiple instructions per cycle using several parallel pipelines or functional units. IPC can exceed 1. Requires checking dependencies among instructions issued together.

Out-of-Order Execution

Execute instructions as soon as their operands are ready instead of in program order, then retire them in order using a reorder buffer so exceptions stay precise. Register renaming removes WAR and WAW false dependencies. Tomasulo's algorithm is the classic scheme.

Deeper pipelines

More stages allow a higher clock rate, but increase branch misprediction penalties and power. The Pentium 4 (up to 31 stages) showed the limits; modern designs use about 14 to 20.

TechniqueExploitsLimit
PipeliningOverlap of stagesHazards, stage balance
SuperscalarMultiple issueInstruction-level parallelism in code
Out-of-orderDynamic schedulingWindow size, power
VLIWCompiler schedulingCompiler complexity, code size

Quick revision

  • Pipelining improves throughput, not single-instruction latency.
  • Five stages: IF, ID, EX, MEM, WB; cycle time set by the slowest stage.
  • Ideal speedup = nk / (k + n - 1), approaching k.
  • Hazards: structural (resources), data (dependencies), control (branches).
  • RAW is the true dependence; forwarding fixes most RAW hazards except load-use (1 stall).
  • Effective CPI = 1 + stall cycles per instruction.
  • 2-bit predictors avoid flipping on a single misprediction; deep pipelines make mispredictions expensive.
  • Superscalar issues several instructions per cycle; out-of-order execution uses renaming and a reorder buffer.