Skip to content
GATEiit

GATE CSE · Computer Organization and Architecture

Instruction pipelining and pipeline hazards

How an instruction pipeline overlaps execution: cycle time, total time, speedup and throughput, structural, data and control hazards, forwarding and stalls, and CPI with branch and load penalties.

Before you start

  • Instruction formats →

    Fixed-length, load-store instructions are what make a clean 5-stage pipeline possible.

  • Memory hierarchy →

    Separate instruction and data caches remove a structural hazard; cache misses add stalls.

  • CPI and execution time

    Time = instructions × CPI × cycle time is the base of every speedup question.

Concept notes

1. Pipeline timing and speedup

A k-stage pipeline splits each instruction into k steps done by separate hardware, so up to k instructions are in flight at once. Registers (latches) between stages hold intermediate results.

Cycle time is set by the slowest stage: τ=max⁡i(ti)+tlatch.

For n instructions with no stalls, the first takes k cycles and each later one finishes one cycle after the previous: Tpipe=(k+n−1) τ.

Speedup over the non-pipelined machine (which takes ∑ti per instruction, with no latches): S=n∑iti(k+n−1) τ. With equal stages, no latch delay and n→∞, S→k. That is the ideal maximum. Unequal stages, latch delay and stalls all lower it.

Throughput is instructions completed per unit time, about 1/τ for large n. Efficiency =S/k.

Pipelining does not make one instruction faster; its latency is kτ, often more than the non-pipelined time. It raises throughput.

Read whether the question's non-pipelined baseline is “sum of stage delays” or “same number of cycles at the same clock”. They give different speedups.

12345678clock cycleI1IFIDEXMEMWBI2IFIDEXMEMWBI3IFIDEXMEMWBI4IFIDEXMEMWB4 instructions finish in 5 + 4 - 1 = 8 cycles
Space-time diagram of a 5-stage pipeline with no hazards

2. Hazards and stalls

A hazard stops the next instruction from entering its stage in the next cycle. The pipeline then stalls (inserts bubbles).

Hazard Cause Remedies
Structural two instructions need the same hardware in one cycle (for example one memory port for fetch and data) duplicate resources (split I/D caches), stall
Data an instruction needs a result not yet written forwarding, stalls, instruction scheduling
Control the next fetch address depends on a branch not yet resolved stall, predict, delayed branch

Data dependences between an earlier instruction I and a later J:

  • RAW (read after write, true dependence): J reads what I writes.
  • WAR (anti-dependence): J writes what I reads.
  • WAW (output dependence): both write the same register.

In the classic in-order 5-stage pipeline (IF, ID, EX, MEM, WB) every instruction reads registers in ID and writes only in WB, in program order, so only RAW causes hazards. WAR and WAW become hazards with out-of-order execution or writes in different stages.

With stalls, the effective CPI is CPI=1+average stall cycles per instruction, and execution time =n×CPI×τ (plus the fill time, which is negligible for large n).

3. Forwarding and load-use stalls

Take the 5-stage pipeline with the usual convention: the register file is written in the first half of a cycle and read in the second half, so an instruction in ID can read a value written by WB in the same cycle.

Without forwarding, a consumer's ID must be in the producer's WB cycle or later. For back-to-back dependent instructions that is 2 stall cycles; with one independent instruction between them, 1 stall.

Forwarding (bypassing) sends a result from the EX/MEM or MEM/WB latch straight to the ALU input:

  • ALU result to the very next instruction: 0 stalls.
  • Load-use: a load's data is available only after MEM, so a dependent instruction right after a load still needs 1 stall.
Producer → next instruction No forwarding Full forwarding
ALU op → ALU op 2 0
Load → ALU op 2 1

Compilers reduce load-use stalls by scheduling: moving an independent instruction between the load and its use.

To count cycles, draw the space-time chart row by row. Each instruction starts IF one cycle after the previous one's IF (unless that IF itself is stalled), and an instruction can never overtake the one ahead of it.

4. Control hazards and branch penalty

A branch's outcome and target are known only in some later stage. Instructions fetched after the branch before that point may be wrong. The branch penalty is the number of cycles lost; if the branch resolves in stage s, the penalty is typically s−1 cycles when the pipeline simply stalls.

With branch frequency b and penalty p: CPI=1+b⋅p.

Techniques

  • Stall (flush) on every branch: CPI =1+b p.
  • Predict not taken: keep fetching sequentially; pay p only for taken branches. With taken fraction t, CPI=1+b t p.
  • Dynamic prediction (branch history, branch target buffer): pay p only on mispredictions.
  • Delayed branch: the instruction(s) right after the branch always execute; the compiler fills these delay slots with useful independent instructions.
  • Resolve earlier (compute the target and compare in ID): reduces p.

Deeper pipelines give a shorter cycle but larger branch and load penalties, so speedup does not keep growing with k.

Speedup with stalls, compared with a non-pipelined machine of the same clock that takes k cycles per instruction: S=k1+stalls per instruction.

Formulas to remember

Cycle time
τ=max⁡iti+tlatch
Pipelined time for n instructions
T=(k+n−1) τ
SpeedupTends to k for equal stages, no latches, large n
S=n∑iti(k+n−1) τ
CPI with stalls
CPI=1+stall cycles per instruction
Branch penaltyPredict not taken: b branch fraction, t taken fraction, p penalty
CPI=1+b t p
Speedup with stallsSame clock; non-pipelined takes k cycles
S=k1+stalls per instruction

Common mistakes

  • Using the average stage delay instead of the maximum for the cycle time.
  • Forgetting the latch delay, or adding latches to the non-pipelined baseline.
  • Writing n k cycles instead of k+n−1 for a pipeline without stalls.
  • Assuming forwarding removes the load-use stall.
  • Charging the branch penalty on every branch when the processor predicts not taken; only taken branches pay.
  • Mixing up the two non-pipelined baselines (sum of stage delays vs k cycles of the pipelined clock).

Worked examples

Try each step yourself, then reveal the working.

Example 1: Unequal stages with latch delay

numerical

A 5-stage pipeline has stage delays 150, 120, 160, 140 and 130 ps, and each latch adds 10 ps. Find the time to run 100 instructions with no stalls, and the speedup over a non-pipelined unit that takes the sum of the stage delays.

  1. Step 1: Find the cycle time.

    Try it on paper first.

Example 2: Counting cycles with and without forwarding

numerical

On the 5-stage pipeline (register write in the first half, read in the second half of a cycle), how many cycles do these four instructions take without forwarding and with full forwarding?

I1: ADD R1, R2, R3 I2: SUB R4, R1, R5 I3: LW R6, 0(R4) I4: AND R7, R6, R8

  1. Step 1: List the dependences.

    Try it on paper first.

Example 3: CPI with predict-not-taken

numerical

In a 5-stage pipeline, 20% of instructions are branches and 60% of branches are taken. The processor predicts not taken; a taken branch costs 3 stall cycles. Ignore other stalls. Find the CPI and the speedup over a non-pipelined machine with the same clock that takes 5 cycles per instruction.

  1. Step 1: Find the average stalls per instruction.

    Try it on paper first.

Example 4: Classifying dependences

conceptual

Classify every dependence in:

I1: R1 = R2 + R3 I2: R4 = R1 * R5 I3: R1 = R6 - R7 I4: R5 = R1 + R4

Which of them cause stalls in an in-order 5-stage pipeline?

  1. Step 1: Look for a later instruction reading an earlier write.

    Try it on paper first.

Previous year questions

14 official GATE CSE questions on this topic, newest first.

Practise these 14 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.