Skip to main content

4.2 Pipelining — Arithmetic & Instruction Pipelining

Pipelining overlaps the execution of multiple instructions by decomposing the execution path into sequential stages. This chapter covers both arithmetic pipelining for floating-point operations and instruction pipelining for general CPU design, including hazard detection and resolution strategies.

Learning Objectives

  • Explain the concept of instruction pipelining and how it improves CPU throughput.
  • Construct space-time diagrams for both arithmetic and instruction pipelines.
  • Calculate pipeline speedup, throughput, and efficiency using standard performance equations.
  • Classify pipeline hazards into structural, data, and control categories.
  • Evaluate data hazard resolution techniques including forwarding, stalling, and reordering.
  • Analyze control hazard solutions including branch prediction, delayed branch, and BTB.

Prerequisites

Before studying this chapter, ensure you have reviewed the following topics:

1. Introduction to Pipelining

Pipelining is an implementation technique where multiple instructions are overlapped in execution. It acts like an industrial assembly line: instead of waiting for a single product to finish all production stages before starting the next, new products enter stage 1 as soon as the previous product moves to stage 2.

Pipelining represents **temporal parallelism** rather than spatial parallelism. Instead of duplicating the execution hardware (spatial), we split the single execution path into sequential stages and overlap execution times (temporal).

In a pipelined processor, the clock cycle time ($T_{\text{clk}}$) is limited by the propagation delay of the slowest pipeline stage, plus the latching setup time of the intermediate registers. Let $\tau_i$ be the delay of stage $i$, and $d$ be the register latch delay. The clock cycle duration is:

$$T_{\text{clk}} = \max(\tau_i) + d$$

2. Arithmetic Pipelining

Arithmetic pipelines are used to segment complex mathematical calculations, such as floating-point arithmetic or multiplication. A classic example is **Floating-Point Addition**, which is typically decomposed into four distinct stages:

  1. Compare Exponents: Subtract exponents to find the difference.
  2. Align Mantissa: Shift the mantissa of the smaller number to align it with the larger exponent.
  3. Add/Subtract Mantissa: Perform addition or subtraction on the aligned mantissas.
  4. Normalize Result: Shift the mantissa back and update the exponent to fit the standard floating-point representation.
1. Compare Exp 2. Align Mant 3. Add Mant 4. Normalize
Figure 4.2.1: 4-Stage Arithmetic Pipeline for Floating-Point Addition. Data moves sequentially through exponent comparison, mantissa alignment, mantissa addition, and result normalization.

3. Instruction Pipelining — The 5-Stage Pipeline

Instruction pipelining overlaps phases of the instruction cycle. A classic RISC processor employs a **5-stage instruction pipeline**:

  • Instruction Fetch (IF): Read the instruction from memory using the Program Counter (PC).
  • Instruction Decode (ID): Decode the instruction and read operands from register file.
  • Execute (EX): Run the instruction through the ALU (e.g., execute addition or compute memory address).
  • Memory Access (MEM): Read or write data from/to memory (load and store operations only).
  • Write Back (WB): Write the final computed value back into the register file.

To allow independent stage operations, **Pipeline Registers** (IF/ID, ID/EX, EX/MEM, MEM/WB) are placed between adjacent stages to hold state variables and data for the next clock cycle.

4. Space-Time Diagrams

A **Space-Time Diagram** (also called a pipeline schedule) is a grid demonstrating instruction execution cycles. The vertical axis represents the pipeline stages (space), while the horizontal axis represents clock cycles (time).

The diagram below shows the startup transient (filling the pipeline), the steady-state (full throughput), and the flushing transient:

Table 4.2.1: Space-Time Diagram of 5 Instructions in a 5-stage Pipeline
Instruction / Cycle C1 C2 C3 C4 C5 C6 C7 C8 C9
I1 (ADD) IF ID EX MEM WB
I2 (SUB) IF ID EX MEM WB
I3 (AND) IF ID EX MEM WB
I4 (OR) IF ID EX MEM WB
I5 (XOR) IF ID EX MEM WB

5. Pipeline Performance Analysis

Let $n$ be the number of instructions, and $k$ be the number of pipeline stages. Let $t_{\text{clk}}$ be the clock cycle duration.

  • The total time taken to execute $n$ instructions on a non-pipelined sequential CPU is: $$T_{\text{seq}} = n \times k \times t_{\text{clk}}$$
  • The total time taken to execute $n$ instructions on a pipelined CPU is: $$T_{\text{pipe}} = [k + (n - 1)] \times t_{\text{clk}}$$

The ideal performance metrics are defined as:

$$S = \frac{T_{\text{seq}}}{T_{\text{pipe}}} = \frac{n \times k}{k + n - 1}$$

Equation 4.2.2: Speedup (S) of a k-stage pipeline processing n instructions.

$$T_p = \frac{n}{[k + (n - 1)] \times t_{\text{clk}}}$$

Equation 4.2.3: Pipeline Throughput (Tp), representing instructions executed per second.

$$E = \frac{S}{k} = \frac{n}{k + n - 1}$$

Equation 4.2.4: Pipeline Efficiency (E), indicating the average utilization percentage of all stages.

6. Pipeline Hazards — Taxonomy

In practice, the ideal speedup is rarely achieved due to **pipeline hazards** — conditions that prevent the next instruction in the instruction stream from executing in its designated clock cycle. Hazards force the pipeline to stall, introducing "bubble" cycles (CPI increases above 1.0).

Hazards are classified into three primary categories:

  1. Structural Hazards: Occur when two instructions require the same hardware resource simultaneously (e.g., a single memory port used for both instruction fetch and data read).
  2. Data Hazards: Occur when an instruction depends on the result of a previous instruction that has not yet completed execution (RAW, WAR, WAW).
  3. Control Hazards: Occur when the pipeline makes a decision based on a branch instruction before the branch condition is resolved.

7. Structural Hazards

A structural hazard represents a resource conflict. For example, if a CPU has a unified cache memory (containing both data and instructions) with a single read/write port, a conflict occurs when instruction $I_1$ attempts to read data in cycle 4 (MEM stage) while instruction $I_4$ attempts to fetch its instruction (IF stage).

Resolutions:

  • Resource Duplication: Separate caches for instructions (I-Cache) and data (D-Cache) (Harvard memory architecture).
  • Stalling: The hardware control unit stalls the fetch phase of the conflicting instruction, inserting a bubble cycle.

8. Data Hazards

Data hazards occur when the execution order of instructions violates register dependency rules:

  • Read After Write (RAW): Instruction B attempts to read a register before Instruction A writes to it. This is a *true dependency* and the most common hazard.
  • Write After Read (WAR): Instruction B attempts to write to a register before Instruction A reads it. This *anti-dependency* does not occur in simple in-order 5-stage pipelines, but is common in out-of-order execution.
  • Write After Write (WAW): Instruction B attempts to write to a register before Instruction A writes to it. This *output dependency* occurs only in out-of-order execution engines.

Exam Tip: Data Dependencies

In exam questions, a RAW dependency is identified when the destination register of instruction $I_i$ is one of the source registers of a subsequent instruction $I_{i+1}$ or $I_{i+2}$.

9. Data Hazard Resolution

To prevent incorrect calculations, the processor must detect data hazards and resolve them:

1. Pipeline Stalling (Inserting Bubbles)

The hazard detection unit halts the IF and ID stages, allowing the execution stages to continue. This introduces a "bubble" cycle that pushes the dependent read phase back until the write phase completes.

2. Data Forwarding / Bypassing

The result computed by the ALU is available at the end of the EX stage (in the EX/MEM register) before it is physically written to the register file in the WB stage. **Data Forwarding** routes this computed result directly back to the input of the ALU for the next cycle, eliminating stalls for ALU-to-ALU dependencies.

IF ID EX MEM WB EX/MEM -> ID/EX (Forwarding)
Figure 4.2.2: Forwarding Path from EX/MEM output register back to the ALU input (ID/EX). This bypasses the register file writeback, preventing ALU execution stalls.

10. Control Hazards & Branch Prediction

Control hazards are caused by branch instructions. In a 5-stage pipeline, a branch decision is typically computed in the EX stage (cycle 3). By that time, two subsequent instructions have already entered the fetch and decode stages. If the branch is *taken*, these two instructions are incorrect and must be flushed (converting them to NOPs), causing a **2-cycle branch penalty**.

Resolutions:

  • Branch Delay Slot: The instruction following a branch is always executed, regardless of whether the branch is taken. Compilers attempt to place a safe, useful instruction here.
  • Static Prediction: Always predict "Taken" or "Not-Taken". Modern compilers predict backward branches (like loops) as Taken, and forward branches as Not-Taken.
  • Dynamic Prediction: Uses runtime history to predict outcomes. A popular mechanism is the **2-Bit Saturating Counter**, which requires two consecutive mispredictions to change its prediction direction, avoiding mistakes on loop exits.
11 Strong T 10 Weak T 01 Weak NT 00 Strong NT
Figure 4.2.3: FSM of a 2-Bit Saturating Counter. States (Strong/Weak Taken, Strong/Weak Not-Taken) transition based on whether the branch is resolved as Taken or Not-Taken.

11. Worked Examples

Worked Example 4.2.1 (Numerical)

Pipeline Performance Calculations

Problem: A 6-stage instruction pipeline operates at a clock frequency of 1.2 GHz. Calculate the speedup, throughput, and efficiency of this pipeline when executing 240 instructions, assuming ideal conditions with zero hazard stalls.

Solution:

Given: $k = 6$ stages, $n = 240$ instructions, $f = 1.2 \text{ GHz} = 1.2 \times 10^9 \text{ Hz}$.

  1. Pipelined Speedup ($S$): Using Equation 4.2.2: $$S = \frac{n \times k}{k + n - 1} = \frac{240 \times 6}{6 + 240 - 1} = \frac{1440}{245} \approx 5.88\times$$ *(Ideal maximum speedup is $k = 6$, so $5.88$ represents high utilization).*
  2. Throughput ($T_p$): $$T_p = \frac{n}{[k + (n - 1)] \times t_{\text{clk}}} = \frac{n \times f}{k + n - 1}$$ $$T_p = \frac{240 \times 1.2 \times 10^9}{245} = \frac{2.88 \times 10^{11}}{245} \approx 1.175 \times 10^9 \text{ instructions/sec (or 1.175 GIPS)}$$
  3. Efficiency ($E$): $$E = \frac{S}{k} = \frac{5.88}{6} \approx 0.98 \text{ (or 98.0\%)}$$

Conclusion: The pipeline operates at **5.88× speedup**, achieving a throughput of **1.175 GIPS** and **98.0% stage efficiency**.

Worked Example 4.2.2 (Analytical)

RAW Data Hazard Trace

Problem: Trace the execution of the following instructions in a 5-stage pipeline:

I1: ADD R1, R2, R3    ; R1 = R2 + R3
I2: SUB R4, R1, R5    ; R4 = R1 - R5

Identify the RAW hazard on register R1. Trace the pipeline stages cycle-by-cycle under (a) a stalling resolution scheme, and (b) a forwarding resolution scheme.

Solution:

  1. Stalling Resolution Scheme: Without forwarding, $I_2$ cannot read $R_1$ until $I_1$ writes it in the WB stage (cycle 5). Because register reads occur in ID, $I_2$ must stall: * Cycle 1: $I_1$ (IF) * Cycle 2: $I_1$ (ID), $I_2$ (IF) * Cycle 3: $I_1$ (EX), $I_2$ (ID - hazard detected, stall inserted) * Cycle 4: $I_1$ (MEM), $I_2$ (ID - stalled) * Cycle 5: $I_1$ (WB), $I_2$ (ID - stalled, registers read at end of WB) * Cycle 6: $I_2$ (EX) * *Execution takes 6 cycles to execute 2 stages; 2 stall cycles inserted.*
  2. Forwarding Resolution Scheme: With forwarding, the ALU result of $I_1$ is available at the end of the EX stage (cycle 3) in the EX/MEM register. The forwarding unit routes this directly to the ALU input for cycle 4: * Cycle 1: $I_1$ (IF) * Cycle 2: $I_1$ (ID), $I_2$ (IF) * Cycle 3: $I_1$ (EX), $I_2$ (ID) * Cycle 4: $I_1$ (MEM), $I_2$ (EX - ALU inputs forwarded from EX/MEM) * Cycle 5: $I_1$ (WB), $I_2$ (MEM) * *Execution takes 5 cycles; zero stalls required.*
Worked Example 4.2.3 (Numerical)

Branch Penalty and Effective CPI

Problem: A processor executes a program with an ideal CPI of 1.0. The instruction mix contains 20% branch instructions. The hardware uses a static branch predictor that achieves a 75% prediction accuracy. If a mispredicted branch incurs a 3-cycle penalty, calculate the effective CPI of this processor.

Solution:

Let $\text{CPI}_{\text{ideal}} = 1.0$. The penalty stalls added per instruction is:

$$\text{Stall Cycles per Instruction} = \text{Branch Frequency} \times \text{Misprediction Rate} \times \text{Penalty Cycles}$$
  1. Branch Frequency = $0.20$
  2. Misprediction Rate = $1.0 - 0.75 = 0.25$
  3. Penalty Cycles = $3$
$$\text{Stall Cycles} = 0.20 \times 0.25 \times 3 = 0.05 \times 3 = 0.15 \text{ stall cycles per instruction}$$ $$\text{CPI}_{\text{effective}} = \text{CPI}_{\text{ideal}} + \text{Stall Cycles} = 1.0 + 0.15 = 1.15$$

Conclusion: Due to control hazard branch penalties, the effective CPI increases from 1.0 to **1.15**.

12. Practice Numerical Problems

Problem 4.2.1: A 5-stage pipeline has stage delays of 150ps, 180ps, 220ps, 160ps, and 140ps. The intermediate register latch delay is 15ps. Calculate the clock cycle time ($T_{\text{clk}}$) and the maximum speedup compared to a non-pipelined processor.

Answer: Clock cycle is $220\text{ps} + 15\text{ps} = 235\text{ps}$. Speedup is $$\frac{850\text{ps}}{235\text{ps}} = 3.62\times$$ (non-pipelined cycle time is sum of stages = $850\text{ps}$).

Problem 4.2.2: A pipeline has 8 stages and executes 500 instructions. Calculate the efficiency of the pipeline.

Answer: $$E = \frac{500}{8 + 500 - 1} = \frac{500}{507} = 98.6\%$$.

13. Assessment Bank (GATE & University)

GATE Questions

GATE Q4.2.1: Consider a 5-stage pipeline (IF, ID, EX, MEM, WB) executing the following code:

LW  R1, 0(R2)      ; R1 = Mem[R2]
AND R3, R1, R4     ; R3 = R1 & R4

If data forwarding is supported, what is the minimum number of stall cycles required between the LW and AND instructions?

(A) 0   (B) 1   (C) 2   (D) 3

Answer & Analysis: **(B)**. This is a load-use RAW hazard. In a LW instruction, the data is loaded from memory only at the end of the MEM stage (cycle 4). The dependent AND instruction requires this value at the start of the EX stage (cycle 4). Even with forwarding, data cannot flow backward in time. Thus, 1 stall cycle (bubble) must be inserted between LW and AND to align the MEM output of LW with the EX input of AND. Correct option is (B).

GATE Q4.2.2: A CPU has a 5-stage instruction pipeline. If 30% of instructions are branches and the branch penalty is 2 cycles, what is the average CPI of the processor assuming all other instructions take 1 cycle?

(A) 1.0   (B) 1.3   (C) 1.6   (D) 1.9

Answer & Analysis: **(C)**. CPI = 1.0 (ideal) + branch overhead. Branch overhead = $0.30 \text{ (branch frequency)} \times 2 \text{ (penalty)} = 0.60$. Effective CPI = $1.0 + 0.60 = 1.60$. Correct option is (C).

University Exam Questions

UQ 4.2.1: Explain the three types of pipeline hazards with examples for each. How does data forwarding resolve RAW hazards? (8 marks)

UQ 4.2.2: Draw a space-time diagram for a 4-stage arithmetic pipeline performing floating-point addition on four pairs of operands. (5 marks)

Viva Voce Questions

VQ 4.2.1: Why can't data forwarding resolve load-use hazards without inserting a stall?

Model Answer: In a load instruction, data is read from memory only at the end of the MEM stage. A dependent instruction requires this operand at the start of its EX stage. Because these stages occur in the same clock cycle, the data cannot be forwarded without delaying the dependent instruction by 1 cycle (stalling it), which shifts the EX stage to the next clock cycle when the loaded data is available in the MEM/WB register.

🎯 Key Takeaways

  • Pipelining overlaps execution of instructions, increasing instruction throughput without replicating resources.
  • Space-Time Diagrams visualize pipeline schedules and identify steady-state operations.
  • Hazards are the primary bottleneck of pipelines, classified into structural, data, and control types.
  • Data Forwarding routes computed results directly to ALU inputs, eliminating most RAW stalls.
  • Branch Predictors use execution history to anticipate control flow, minimizing branch penalties.

References

  1. Syed Arsalaan, "Handwritten COA Notes", 2026, Pages 112–114. [Local Verified]
  2. David A. Patterson and John L. Hennessy, "Computer Organization and Design: The Hardware/Software Interface", 2018, RISC-V Edition, Morgan Kaufmann. [Pending Verification]
  3. William Stallings, "Computer Organization and Architecture: Designing for Performance", 2019, 11th Edition, Pearson. [Pending Verification]