1. Gyanastra
  2. Computer Organisation and Architecture
  3. Pipeline Hazards
  4. Control Hazard from Branch Instructions and Stall Cycles
Free lesson, no account needed 6 min read 14 practice questions after it
Asked in past papers9 times
  • GATE CSE202420222020201820142013200820072006

Computer Organisation and Architecture · Pipeline Hazards

Control Hazard from Branch Instructions and Stall Cycles

Overview

Pipeline hazards are situations that prevent the next instruction in the instruction stream from executing in its designated clock cycle.
Control hazards, caused by branch instructions, force the pipeline to discard wrongly fetched instructions and waste clock cycles, raising the average clocks per instruction above the ideal value of 1.

Key concepts

  • Pipeline hazard: any situation that prevents the next instruction in the instruction stream from executing in its designated clock cycle.
  • Structural hazard: caused by resource conflicts, where one or more pipeline stages try to use the same hardware resource.
  • Control hazard (control dependency): caused by branch/control instructions, where the next instruction to fetch is not known in time. The branch example belongs to this category.
  • Data hazard (data dependency): caused by dependencies between the data used by instructions.
  • Five-stage instruction pipeline: Instruction Fetch (IF), Instruction Decode (ID), Operand Fetch (OF), Execute (EX), Write Back (WB).
  • Ideal pipelining: the next instruction starts exactly one clock cycle after the previous one and finishes exactly one clock cycle after the previous one finishes, so one instruction completes per clock cycle.
  • Stalls (stall cycles): the wasted cycles during which wrongly fetched instructions are stopped (flushed) and fetching restarts from the correct target address; they are a complete waste of time.
  • CPI (clocks per instruction): the average number of clock cycles per instruction; it increases due to control instructions.

How it works

  1. In the ideal pipeline, one instruction completes every clock cycle.
  2. A branch instruction at address 100 says "branch to instruction 200", so the next instruction to execute should be 200, not 101.
  3. The processor only knows the next instruction is 200 after the branch has progressed far enough in the pipeline; the target address may be known at the decode stage or only later (e.g., at the execute stage).
  4. Either way, the instructions fetched and sitting in the pipeline behind the branch (101, 102, …) are the wrong instructions.
  5. All wrongly fetched instructions must be stopped (flushed), and fetching must restart from the correct target address; these wasted cycles are the stall cycles.
  6. Because of such instructions, the pipeline delivers fewer than one instruction per clock on average — for example, 1.2 or 1.5 clocks per instruction instead of 1 — so the average CPI increases.

Formulas

  • CPIavg=Total clock cyclesTotal instructions executed\text{CPI}_{avg} = \dfrac{\text{Total clock cycles}}{\text{Total instructions executed}} — CPIavg\text{CPI}_{avg} is the average clocks per instruction; it equals 1 in an ideal pipeline and becomes greater than 1 with branch stalls (e.g., 1.2 or 1.5).

  • Stall cycles from one branch=number of instructions fetched after the branch (before the target is known) that must be discarded\text{Stall cycles from one branch} = \text{number of instructions fetched after the branch (before the target is known) that must be discarded} — in a 5-stage pipeline where the target is known at the end of stage kk, the number of discarded instructions =k−1= k - 1.

  • Branch control hazard timeline

  • Shows how instructions fetched after a branch are discarded as stall cycles once the target address is known.

Worked examples

Branch instruction example

  • Given: the instruction at address 100 is a branch to address 200.
  • Step 1: In the ideal pipeline, after fetching instruction 100, the next fetch would be 101.
  • Step 2: The correct next instruction is 200, but the pipeline only discovers this after the branch is decoded/executed (target known at the decode or execute stage).
  • Step 3: Meanwhile, the instructions fetched after 100 (101, 102, …) are wrong and must all be stopped — these are the stall cycles.
  • Step 4: Fetching restarts from 200. The wasted cycles are pure overhead, so the average clocks per instruction rises above 1 (e.g., 1.2 or 1.5).
Gantt chart: Control Hazard from Branch Instructions and Stall Cycles
Figure. Control Hazard from Branch Instructions and Stall Cycles

Comparison

Hazard typeCauseExample
Structural hazardTwo or more pipeline stages need the same resource at the same timeResource conflict between stages
Control hazard (control dependency)Branch/control instructions; next PC not known in timeBranch at address 100 jumping to 200
Data hazard (data dependency)Dependence of an instruction on data produced by an earlier instruction(covered later)

Revision summary

  • The three hazard categories are structural, control, and data; the branch example is a control dependency.
  • The example pipeline has five stages: IF, ID, OF, EX, WB.
  • Ideal pipelining completes one instruction every clock cycle; branch stalls break this.
  • After a branch at 100 to 200, the correct next instruction is 200, not 101.
  • The target may be known at decode or at execute — either way, wrongly fetched instructions must be discarded.
  • Discarded cycles are called stalls or stall cycles — pure waste of time.
  • Stalls raise the average clocks per instruction above 1 (e.g., 1.2 or 1.5), i.e., CPIavg>1\text{CPI}_{avg} > 1.
  • The fixes for hazards are studied after examining each hazard type.

Previous-year exam insights

  • Branch penalty (stall cycles per branch) equals the number of stages between IF and the stage where the branch outcome/target is resolved; with a branch predictor that is always correct the penalty is 0, and with predict-not-taken the penalty is paid only for taken branches.
    Asked inGATE 2022GATE 2006
  • Average CPI with hazards = base CPI + (fraction of instructions causing a hazard) x (stall cycles per such instruction);
    total execution time = instruction count x CPIavg\mathrm{CPI}_{avg} x clock period (or / clock frequency).
    Asked inGATE 2024GATE 2022GATE 2006
  • Delayed branching: the instruction in the branch delay slot (the instruction immediately after the branch in memory) is always executed, whether or not the branch is taken; a useful delay-slot instruction must not depend on the branch and must be safe to execute on both paths.
    Asked inGATE 2008
  • In a pipeline with variable stage latencies, the clock period is set by the slowest stage, and the number of cycles for a sequence equals (number of stages - 1) + sum over instructions of the cycles its slowest stage needs; stalls from structural hazards add when two instructions contend for the same stage.
    Asked inGATE 2018GATE 2007GATE 2006
  • Control-hazard remedies: stall (freeze IF), flush/discard wrongly fetched instructions, branch prediction (static or dynamic), and delayed branching; each reduces but does not necessarily eliminate the branch penalty.
    Asked inGATE 2022GATE 2008
  • Speedup of a pipeline over non-pipelined execution = (non-pipelined time per instruction) / (pipelined CPIavg\mathrm{CPI}_{avg} x clock period ratio);
    ideal speedup approaches the number of stages only when CPI = 1 and clock periods are equal.
    Asked inGATE 2024
Pages 1–2 of 2