Computer Science and IT‹›⌂ 0:00 studied

Module 3 · 12 min read · Free chapter

Instruction pipelining and hazards

In short

A k-stage pipeline runs n instructions in (k + n − 1) cycles, so the speed-up nk/(k + n − 1) approaches k. Hazards cause stalls: forwarding removes most data stalls but a load-use pair still needs one, and branches add frequency times penalty to the CPI.

See it in 3DInstruction pipeline as an assembly line: stages, stalls, forwarding and branches. Turn it, cut it open, or just press Watch.
What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: the tiffin line

Picture a school kitchen packing 500 tiffins. If one cook fills each box completely, adding rice, dal, sabzi, roti and the lid before touching the next box, and each item takes one minute, then every box takes five minutes. All 500 boxes then need 2,500 minutes.

Now line up five helpers, each adding one item and passing the box on. The first box still takes five minutes to come out, but after that a finished box leaves every minute. The 500 boxes now take about 504 minutes, almost five times faster.

A processor does the same with instructions, and the trick is called pipelining. Each instruction is split into stages, and while one instruction is being executed, the next is being decoded and the one after that is being fetched.

Why it matters. Pipelining is the main reason a modern processor finishes close to one instruction per clock cycle. Its catch, which this chapter studies, is that some instructions must wait for others, and every wait is lost speed.

The five-stage pipeline

The classic pipeline has five stages, each taking one clock cycle. IF fetches the instruction from memory, ID decodes it and reads its registers, and EX does the ALU work. MEM reads or writes data memory for loads and stores, and WB writes the result into the register file.

Between stages sit pipeline latches (registers such as IF/ID and ID/EX) that hold each instruction's partial results. They let every stage work on a different instruction in the same cycle without mixing them up.

Cycle   1    2    3    4    5    6    7    8
I1      IF   ID   EX   MEM  WB
I2           IF   ID   EX   MEM  WB
I3                IF   ID   EX   MEM  WB
I4                     IF   ID   EX   MEM  WB

This space-time diagram shows the main facts at a glance. The first instruction still needs 5 cycles, which is the latency, but from cycle 5 onwards one instruction finishes every cycle, which is the throughput. Four instructions finish in 8 cycles instead of 20.

Why it matters. Almost every pipeline question in GATE is solved by drawing this diagram. Practise it until you can place stalls and branches in it without thinking.

Speed-up and throughput numericals

Let a pipeline have kk stages and cycle time τ\tau, and let it run nn instructions with no stalls. The first instruction takes kk cycles and each of the other n−1n - 1 instructions adds one more cycle.

Tpipe=(k+n−1)τ,Tnonpipe=nkτ,S=nkk+n−1T_{\mathit{pipe}} = (k + n - 1)\tau, \qquad T_{\mathit{nonpipe}} = nk\tau, \qquad S = \frac{nk}{k + n - 1}

As nn becomes large, SS approaches kk, so a kk-stage pipeline can be at most kk times faster. Throughput approaches 1τ\dfrac{1}{\tau} instructions per second, and efficiency is Sk\dfrac{S}{k}, the fraction of stage slots doing useful work.

Worked example 1. A 5-stage pipeline with τ\tau = 2 ns runs 100 instructions. Then Tpipe=(5+99)×2=208T_{\mathit{pipe}} = (5 + 99) \times 2 = 208 ns and Tnonpipe=100×5×2=1000T_{\mathit{nonpipe}} = 100 \times 5 \times 2 = 1000 ns, so S=1000208≈4.81S = \dfrac{1000}{208} \approx 4.81.

Real stages are not equal. The clock must suit the slowest stage, and each latch adds its own delay, so the cycle time is the largest stage delay plus the latch delay. The non-pipelined machine has no latches, so it takes the plain sum of the stage delays.

Worked example 2. Five stages take 150, 120, 160, 140 and 130 ps, and each latch adds 10 ps. So τ=160+10=170\tau = 160 + 10 = 170 ps, while one non-pipelined instruction takes 150+120+160+140+130=700150 + 120 + 160 + 140 + 130 = 700 ps. For 1,000 instructions the times and speed-up are as follows.

Tpipe=(5+999)×170=170680 ps,S=1000×700170680≈4.10T_{\mathit{pipe}} = (5 + 999) \times 170 = 170680 \text{ ps}, \qquad S = \frac{1000 \times 700}{170680} \approx 4.10

Why it matters. The gap between 4.10 and the ideal 5 comes from unequal stages and latch delays, and designers balance stages to close it. GATE numericals ask for exactly these four numbers: cycle time, total time, speed-up and throughput.

Structural, data and control hazards

The ideal pipeline assumes that each instruction can enter its next stage every cycle. A hazard is anything that breaks this, forcing the pipeline to stall, which inserts an empty slot called a bubble. There are three kinds.

A structural hazard happens when two instructions need the same hardware in the same cycle. For example, with a single memory port, a load in its MEM stage and a later instruction in its IF stage both want memory. Separate instruction and data caches remove this common case.

A data hazard happens when an instruction needs a value that an earlier instruction has not yet produced. Dependences come in three types, named by the order of the two accesses.

  • Read after write (RAW): the second instruction reads a register that the first writes, as in ADD R1, R2, R3 followed by SUB R4, R1, R5, and this is a true dependence.
  • Write after read (WAR): the second instruction writes a register that the first reads, which is a name clash rather than a real flow of data.
  • Write after write (WAW): both instructions write the same register, so their order must be kept.

In a simple in-order five-stage pipeline, registers are read in ID and written in WB, always in program order. So WAR and WAW cannot go wrong there, and only RAW causes stalls; the other two matter in out-of-order machines.

A control hazard comes from branches. The pipeline fetches the next instructions before it knows whether the branch is taken, so if the guess is wrong those instructions must be thrown away.

Why it matters. Each hazard type has its own cure: more hardware for structural hazards, forwarding for data hazards and prediction for control hazards. GATE often gives a code fragment and asks you to list every RAW dependence.

Stalls and forwarding

Take ADD R1, R2, R3 followed at once by SUB R4, R1, R5. ADD writes R1 in its WB stage, in cycle 5, but SUB wants to read R1 in its ID stage, in cycle 3. Suppose the register file is written in the first half of a cycle and read in the second half; then SUB can read R1 in cycle 5 at the earliest, so it needs two stall cycles.

Cycle   1    2    3    4    5    6    7    8
ADD     IF   ID   EX   MEM  WB
SUB          IF   --   --   ID   EX   MEM  WB      (no forwarding)
SUB          IF   ID   EX   MEM  WB                (with forwarding)

Forwarding, also called bypassing, removes most of these stalls. The result of ADD already exists at the end of its EX stage, in the EX/MEM latch, so extra wires carry it straight to the ALU input for SUB in cycle 4. With forwarding from the EX/MEM and MEM/WB latches, back-to-back ALU instructions need no stall at all.

Loads are the exception. In LOAD R1, 0(R2) followed by ADD R4, R1, R5, the loaded value appears only at the end of MEM, in cycle 4, but ADD wants it at the start of its EX, also in cycle 4. So one stall is unavoidable even with forwarding, and this is called the load-use hazard.

Worked example. Five instructions run on a five-stage pipeline with forwarding, and one load-use pair causes the only stall. Total cycles are k+n−1+stalls=5+5−1+1=10k + n - 1 + \text{stalls} = 5 + 5 - 1 + 1 = 10, against 9 for an ideal run.

Compilers reduce load-use stalls by scheduling, which means moving an independent instruction into the slot between the load and its use. If no such instruction exists, the hardware inserts the bubble by itself.

Why it matters. Forwarding is cheap and removes most data stalls, which is why every real pipeline has it. GATE questions state whether forwarding exists and how the register file is read and written, and those two lines decide the answer.

CPI with stalls, branch penalty and prediction

An ideal pipeline has a CPI (cycles per instruction) of 1. Stalls add to it, so the effective CPI is one plus the average stall cycles per instruction. For balanced stages, the speed-up over a non-pipelined machine that takes kk cycles per instruction follows directly.

CPI=1+stall cycles per instruction,S=kCPI\mathit{CPI} = 1 + \text{stall cycles per instruction}, \qquad S = \frac{k}{\mathit{CPI}}

A branch is known to be taken only when it is resolved, often in EX. The instructions fetched after it in the meantime are wasted if the branch is taken, and the number of lost cycles is the branch penalty pp. If a fraction ff of instructions are branches and the pipeline always stalls, then CPI=1+fp\mathit{CPI} = 1 + fp; with 20 percent branches and p=3p = 3, CPI becomes 1.6.

Predict-not-taken keeps fetching the next instructions and pays the penalty only when the branch is taken. Static schemes use a fixed rule, such as predicting backward branches as taken, because loops branch backwards. Dynamic schemes remember history, and a 2-bit saturating counter changes its guess only after two wrong guesses in a row, so a loop exit does not spoil the next run of the loop.

A delayed branch takes a different route. The instruction right after the branch, in the delay slot, always executes, and the compiler fills it with useful work so that nothing is wasted.

Worked example. In a five-stage pipeline with forwarding, 30 percent of instructions are loads and 40 percent of loads are followed at once by a user of the value. Also, 20 percent are branches with p=2p = 2 under predict-not-taken, and 60 percent of branches are taken.

CPI=1+0.3×0.4×1+0.2×0.6×2=1+0.12+0.24=1.36\mathit{CPI} = 1 + 0.3 \times 0.4 \times 1 + 0.2 \times 0.6 \times 2 = 1 + 0.12 + 0.24 = 1.36

The speed-up over a non-pipelined machine taking 5 cycles per instruction at the same clock is 51.36≈3.68\dfrac{5}{1.36} \approx 3.68.

Why it matters. Branches are about one instruction in five in ordinary code, so their penalty is the biggest single loss in many pipelines. Deeper pipelines make pp larger, which is why good prediction becomes more important as clocks get faster.

Descriptive lens: how written papers ask this

Written papers for engineering services, IT officer posts and state Assistant Programmer exams ask about this topic in four ways:

  1. Explain pipelining with a space-time diagram and derive the speed-up formula.
  2. Classify hazards with an example of each and state how each is removed.
  3. Analyse a code fragment, mark its dependences and count the stalls with and without forwarding.
  4. Discuss branch handling methods such as prediction and delayed branches.

Practice questions (our own, not previous-year questions).

  1. Derive the speed-up of a kk-stage pipeline for nn instructions and show that it can never exceed kk. (10 marks)
  2. Explain structural, data and control hazards with one example each, and describe a hardware or software remedy for each. (10 marks)
  3. For the sequence LOAD R1, 0(R2); ADD R3, R1, R4; SUB R5, R3, R6; STORE R5, 0(R7), draw the five-stage timing with and without forwarding and count the cycles. (15 marks)

Answer plan for question 1.

  • Define a kk-stage pipeline with cycle time τ\tau and state the assumption of no stalls and equal stages.
  • Show with a space-time diagram that the first instruction finishes after kk cycles and each later one adds one cycle.
  • Write the pipelined time as (k+n−1)τ(k + n - 1)\tau and the non-pipelined time as nkτnk\tau.
  • Divide the two to get S=nkk+n−1S = \dfrac{nk}{k + n - 1}.
  • Take the limit as nn grows to show that SS approaches kk but never exceeds it, because the denominator is always at least nn.
  • Remark that unequal stages, latch delays and hazards keep the real speed-up below kk.

Quick revision

  • A kk-stage pipeline keeps kk instructions in flight, raising throughput but not cutting latency.
  • The five stages are IF, ID, EX, MEM and WB, separated by latches.
  • Pipelined time is (k+n−1)τ(k + n - 1)\tau, and speed-up nkk+n−1\dfrac{nk}{k + n - 1} tends to kk.
  • With unequal stages, the cycle time is the slowest stage plus the latch delay.
  • Hazards are structural, data and control, and in a simple in-order pipeline only RAW data hazards stall.
  • Forwarding removes ALU-to-ALU stalls, but a load-use pair still needs one stall.
  • Effective CPI is one plus stall cycles per instruction, and a branch adds fpfp to it.
  • Predict-not-taken pays only on taken branches, and a 2-bit counter changes after two wrong guesses.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 13 answered

  1. Pipelining mainly improves:

  2. In the classic five-stage pipeline, register values are read in which stage?

  3. A 5-stage pipeline with a 2 ns cycle runs 100 instructions without stalls. The total time is:

  4. As the number of instructions grows very large, the speed-up of a kk-stage pipeline approaches:

  5. Stage delays are 150, 120, 160, 140 and 130 ps and each latch adds 10 ps. The pipeline cycle time is:

  6. ADD R1, R2, R3 followed by SUB R4, R1, R5 creates which dependence?

  7. Two instructions needing a single memory port in the same cycle cause a:

  8. With full forwarding, a LOAD followed at once by an instruction using the loaded value needs:

  9. Without forwarding, with registers written in the first half and read in the second half of a cycle, an instruction using the result of the instruction just before it needs:

  10. 30% of instructions are loads, 40% of them cause a 1-cycle load-use stall, and there are no other stalls. The CPI is:

  11. A balanced 5-stage pipeline has an effective CPI of 1.25. Its speed-up over the non-pipelined machine at the same clock is:

  12. 20% of instructions are branches with a 3-cycle penalty, and the pipeline stalls on every branch. The CPI is:

  13. A 2-bit saturating branch predictor changes its prediction after:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Why can the speed-up of a k-stage pipeline not exceed k?

Because the speed-up nk/(k + n − 1) only approaches k as n grows, and stalls, latches and unequal stages keep it lower.

How is the pipeline cycle time found with unequal stages?

Take the slowest stage delay and add the latch delay.

What is a load-use hazard?

It occurs when an instruction uses a value loaded by the instruction just before it; even with forwarding it needs one stall cycle.

How do branches affect CPI?

CPI becomes 1 plus the branch fraction times the penalty, counted only for taken branches under predict-not-taken.

More in this module: Computer organisation and architecture

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Organization and Architecture (IIT Madras, 106106166): pipelining and hazards