Pipelining & Vector Processing
5-stage instruction pipeline, Speedup formula, Structural, Control, and Data hazards with solution mechanisms
- Understand the 5-stage instruction pipeline (IF, ID, EX, MEM, WB)
- Derive Speedup, Throughput, and Efficiency formulas for pipelines
- Resolve Structural, Control (Branch), and Data hazards (RAW, WAR, WAW)
- Apply Operand Forwarding, Stalling, and Vector Processing principles
๐ฏ Weightage: 7.8% โ Conceptually dense topic; hazards and speedup numericals are frequently tested at GATE level.
1 Pipelining Basics
1.1 Concept of Pipelining Assembly-Line Analogy
- Pipelining is a technique where multiple instructions are overlapped in execution โ while one instruction is being decoded, another can be fetched, and so on โ similar to a car assembly line where different stations work on different cars simultaneously.
- ๐ Analogy: Imagine a car wash with 4 stages โ Wash โ Rinse โ Wax โ Dry. Without pipelining, one car goes through all 4 stages before the next car even starts. With pipelining, as soon as Car 1 moves from Wash to Rinse, Car 2 can enter Wash โ all stations stay busy, dramatically increasing throughput (cars/hour), even though a single car still takes the same total time to finish.
1 โฑ๏ธ WITHOUT PIPELINING (Sequential)2 Car1: [Wash][Rinse][Wax][Dry]3 Car2: [Wash][Rinse][Wax][Dry]4 Car3: [Wash][Rinse][Wax][Dry]5
6 โก WITH PIPELINING (Overlapped)7 Car1: [Wash][Rinse][Wax][Dry]8 Car2: [Wash][Rinse][Wax][Dry]9 Car3: [Wash][Rinse][Wax][Dry]๐ข Key Insight: Pipelining improves throughput (instructions completed per unit time), NOT the latency of a single instruction.
1.2 Uniprocessing vs Multiprocessing
| Feature | ๐ฆ Uniprocessing | ๐ง Multiprocessing |
|---|---|---|
| Number of processors | One CPU | Two or more CPUs |
| Execution | Sequential (or pipelined within 1 CPU) | Truly parallel across multiple CPUs |
| Complexity | Lower | Higher (needs synchronization, coherence) |
| Throughput | Limited by single CPU speed | Scales with number of processors (ideally) |
| Example | Classic single-core pipelined CPU | Modern multicore servers |
1.3 IBM 801 Historical RISC Pipelining Example
- The IBM 801 (1970s, John Cocke) is regarded as the first RISC (Reduced Instruction Set Computer) design.
- It demonstrated that a simple, reduced instruction set with a fixed instruction format enables efficient pipelining โ since every instruction takes a predictable, uniform path through pipeline stages.
- This philosophy directly influenced modern RISC architectures (MIPS, ARM, RISC-V), which are all designed with pipelining efficiency as a first-class goal.
1.4 Basic 5-Stage Pipeline (IF ID EX MEM WB)
1โโโโโโโโ โโโโโโโโ โโโโโโโโ โโโโโโโโ โโโโโโโโ2โ IF โโโโบโ ID โโโโบโ EX โโโโบโ MEM โโโโบโ WB โ3โโโโโโโโ โโโโโโโโ โโโโโโโโ โโโโโโโโ โโโโโโโโ4 Fetch Decode Execute Memory Write5 Access Back| Stage | Full Form | What Happens |
|---|---|---|
| ๐ต IF | Instruction Fetch | Instruction is fetched from memory using the address in PC; PC incremented |
| ๐ข ID | Instruction Decode | Opcode is decoded, operands/registers are read from the register file |
| ๐ก EX | Execute | ALU performs the arithmetic/logic operation (or computes memory address) |
| ๐ MEM | Memory Access | Memory is read (LOAD) or written (STORE) if the instruction requires it |
| ๐ด WB | Write Back | Result is written back into the destination register |
Pipeline Execution Diagram (No Hazards)
| Instr \ 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 |
โ With an ideal 5-stage pipeline and no hazards, one instruction completes every clock cycle after the pipeline fills up.
### โ ๏ธ Common Traps & Exam Tips - ๐ Pipelining improves throughput, not the execution time of a SINGLE instruction (a single instruction still takes 5 cycles to fully complete โ latency is unchanged or even slightly worse due to pipeline overhead). - ๐ IBM 801 is tested as the "first RISC machine" โ a common historical MCQ fact. - ๐ Don't confuse Uniprocessing with pipelining โ a single pipelined CPU is still "uniprocessing" (one CPU), just internally overlapping instruction stages; true multiprocessing needs multiple physical CPUs.
2 Performance & Speedup
2.1 Speedup Formula Derivation
Without pipelining, n instructions each taking k cycles (for a k-stage pipeline) run sequentially:
1Time (Non-Pipelined) = n ร k cyclesWith pipelining, the pipeline fills up in k cycles for the first instruction, then completes one instruction per cycle thereafter:
1Time (Pipelined) = k + (n โ 1) cycles๐ฆ Formula Box โ Speedup ``Speedup (S) = Time(Non-Pipelined) / Time(Pipelined) = (n ร k) / (k + (n โ 1))`As n โ โ (very large number of instructions):`Maximum Speedup โ k (equal to the number of pipeline stages)``
2.2 Ideal CPI = 1 Case
- In an ideal pipeline (no hazards, no stalls), exactly one instruction completes per clock cycle after the pipeline is full โ CPI (Cycles Per Instruction) = 1.
- This is the theoretical best case; real pipelines have CPI slightly greater than 1 due to hazards/stalls.
2.3 Throughput and Efficiency Formulas
๐ฆ Formula Box ``Throughput = Number of Instructions Completed / Total Time Taken Efficiency = Speedup Achieved / Number of Pipeline Stages (k) = Actual Speedup / Maximum Possible Speedup``
2.4 Problems That Reduce Ideal Pipelining Performance
- Hazards (Structural, Data, Control) โ force stalls/bubbles, breaking the "1 instruction per cycle" ideal.
- Unequal stage delays โ pipeline clock must be set by the SLOWEST stage, wasting time in faster stages.
- Pipeline fill/drain overhead โ the initial fill time (
kcycles) and any drain time are "wasted" relative to steady-state throughput, especially significant for SHORT instruction sequences.
Solved Numericals
Q1. A 5-stage pipeline executes 100 instructions with no hazards. Find the speedup over non-pipelined execution.
Solution
: Non-Pipelined Time = 100 ร 5 = 500 cycles Pipelined Time = 5 + (100 โ 1) = 104 cycles Speedup = 500 / 104 โ 4.81
Q2. For an infinitely long instruction stream on a 4-stage pipeline, find the maximum theoretical speedup.
Solution
: Maximum Speedup = k = 4 (as n โ โ)
Q3. A pipeline has 6 stages, each taking 20 ns. Find the time to execute 50 instructions using pipelining.
Solution
: Pipelined Time = k + (nโ1) cycles = 6 + 49 = 55 cycles Total Time = 55 ร 20 ns = 1100 ns
Q4. For Q3, find the non-pipelined execution time and the resulting speedup.
Solution
: Non-Pipelined Time = n ร k ร (cycle time) = 50 ร 6 ร 20 = 6000 ns Speedup = 6000 / 1100 โ 5.45
Q5. A pipeline achieves a speedup of 3.5 with 5 stages. Find the efficiency.
Solution
: Efficiency = Speedup / k = 3.5 / 5 = 0.7 (70%)
Q6. A non-ideal pipeline has an average CPI of 1.4 (due to hazards) instead of the ideal CPI = 1, running at a clock rate of 2 GHz, for a 1000-instruction program. Find the actual execution time.
Solution
: Total Cycles = CPI ร Instructions = 1.4 ร 1000 = 1400 cycles Execution Time = 1400 / (2ร10โน) = 700 ns (0.7 ฮผs)
### โ ๏ธ Common Traps & Exam Tips - ๐ The formulaTime(Pipelined) = k + (nโ1)is the SINGLE most important pipelining formula โ memorize the derivation, not just the result. - ๐ Maximum Speedup equals the number of stages (k), achieved only in the theoretical limit of infinite instructions โ for finiten, actual speedup is always LESS thank. - ๐ Efficiency is a fraction between 0 and 1 (or 0โ100%) โ if your calculated efficiency exceeds 1, you've made an arithmetic error.
3 Structural & Control Hazards
3.1 Structural Hazard Definition & Example
- Structural Hazard: Occurs when two or more instructions need the SAME hardware resource at the same clock cycle (resource conflict).
- Example: If instruction and data share a single memory (Von Neumann style), an instruction in the MEM stage (accessing data) and another instruction in the IF stage (fetching the next instruction) may both need memory access simultaneously โ causing a conflict.
1Instr \ Cycle 1 2 3 4 52I1 (LOAD) IF ID EX MEM WB3I2 -- IF ID EX MEM โ wants IF while I1 wants MEM (cycle 4) โ CONFLICT if single memory port๐ ๏ธ Solution: Use separate instruction and data memories/caches (Harvard-style organization) to avoid resource contention.
3.2 Control Hazard Definition & Example
- Control Hazard: Occurs due to branch instructions โ the pipeline doesn't know WHICH instruction to fetch next until the branch is resolved (evaluated), potentially fetching the WRONG instructions.
- Example:
1I1: BEQ R1, R2, LABEL ; branch instruction2I2: ADD R3, R4, R5 ; fetched next by default โ but might be WRONG if branch is taken!If the branch is taken, I2 (and possibly I3) were wrongly fetched and must be discarded (flushed), wasting cycles.
3.3 Solutions to Control Hazards
| Technique | Description |
|---|---|
| ๐ฎ Branch Prediction | Hardware GUESSES the branch outcome (taken/not-taken) in advance and speculatively fetches accordingly; if wrong, pipeline is flushed and restarted (misprediction penalty) |
| โณ Delayed Branching | Compiler places a useful, branch-independent instruction in the "delay slot" right after the branch โ this instruction executes regardless of the branch outcome, so no cycles are wasted |
| ๐ฏ Branch Target Buffer (BTB) | A small cache that stores previously computed branch target addresses, allowing the CPU to quickly fetch from the predicted target BEFORE the branch is fully decoded/resolved |
### โ ๏ธ Common Traps & Exam Tips - ๐ Structural hazards are solved via hardware duplication (e.g., separate memories, extra ALUs) โ a hardware-level fix; NOT solvable by software/compiler alone. - ๐ Delayed branching is a compiler-level (software) technique, while branch prediction/BTB are hardware-level techniques โ a frequent classification trap. - ๐ Control hazards specifically arise from branch/jump instructions โ don't confuse with data hazards (which arise from data dependency between instructions, not control flow).
4 Data Hazards & Solutions
4.1 RAW, WAR, WAW Hazards
| Hazard | Full Form | Condition | Example |
|---|---|---|---|
| ๐ด RAW | Read After Write | An instruction tries to READ a register BEFORE a previous instruction has WRITTEN to it | I1: ADD R1, R2, R3 I2: SUB R4, R1, R5 (I2 reads R1 before I1 writes it) |
| ๐ก WAR | Write After Read | An instruction tries to WRITE a register BEFORE a previous instruction has READ the old value | I1: ADD R4, R1, R2 I2: SUB R1, R3, R5 (I2 writes R1 before I1 reads old R1) โ occurs mainly with out-of-order execution |
| ๐ข WAW | Write After Write | Two instructions WRITE to the same register, and must complete in the correct final order | I1: ADD R1, R2, R3 I2: SUB R1, R4, R5 (both write R1 โ final value must reflect I2, the later instruction) |
๐ข RAW is the MOST common and problematic hazard in simple in-order pipelines (also called a "true dependency"); WAR and WAW ("false/name dependencies") mainly matter in out-of-order/superscalar pipelines.
4.2 Solutions to Data Hazards
| Technique | Description |
|---|---|
| โก Forwarding / Bypassing | The ALU result is "forwarded" directly from the EX/MEM stage output back to the EX stage input of a dependent instruction, WITHOUT waiting for it to be written back to the register file first |
| โธ๏ธ Stalling (Pipeline Bubble) | The pipeline is deliberately paused (inserting "bubble"/NOP cycles) until the dependency is resolved, when forwarding alone isn't sufficient (e.g., LOAD-USE hazard) |
| ๐ Compiler-Based Reordering | The compiler rearranges independent instructions to be placed BETWEEN dependent ones, hiding the hazard latency without hardware stalls |
Pipeline Diagram RAW Hazard with Stalling (No Forwarding)
1I1: ADD R1, R2, R32I2: SUB R4, R1, R5 โ depends on R1 from I13
4Cycle: 1 2 3 4 5 6 75I1 (ADD) IF ID EX MEM WB6I2 (SUB) IF ID* ID* ID EX MEM WB7 (stall) (stall) โ waits for R1 to be written back in cycle 5(Without forwarding, I2 must stall until I1's WB stage completes, in cycle 5, before I2 can read the correct R1 value in ID.)
Pipeline Diagram Same Hazard WITH Forwarding
1Cycle: 1 2 3 4 52I1 (ADD) IF ID EX MEM WB3I2 (SUB) IF ID EX MEM WB4 โ5 Forwarded directly from I1's EX/MEM output into I2's EX stage input6 (no stall needed!)Solved Numerical (Mirrors GATE 2010-style)
Q. Consider the following instruction sequence on a 5-stage pipeline (IF, ID, EX, MEM, WB) with full forwarding implemented (EX/MEM and MEM/WB โ EX), EXCEPT that a LOAD-USE hazard (a register loaded from memory, immediately used by the NEXT instruction) still requires 1 stall cycle (since the loaded value isn't available until after MEM stage):
1I1: LOAD R1, 0(R2)2I2: ADD R3, R1, R4 โ uses R1 immediately after LOAD3I3: SUB R5, R6, R7Find the total number of cycles to complete all 3 instructions.
Solution
: - I1 (LOAD): IF(1) ID(2) EX(3) MEM(4) WB(5) - I2 (ADD) needs R1, but R1 is only available after I1's MEM stage (cycle 4) โ with forwarding from MEM/WB, I2's EX stage must occur in cycle 5 or later โ 1 stall cycle required for I2. I2: IF(2) ID(3) stall EX(5) MEM(6) WB(7) - I3 (SUB), independent, follows right behind I2 (also delayed by the stall): I3: IF(3) stall ID(5) EX(6) MEM(7) WB(8) Total cycles = 8 (compared to the hazard-free ideal of k + (nโ1) = 5 + 2 = 7 cycles) โ 1 extra cycle lost due to the LOAD-USE hazard stall.
4.3 Comparison of All Three Hazard Types
| Hazard Type | Definition | Example Cause | Typical Solution(s) |
|---|---|---|---|
| ๐งฑ Structural | Two instructions need the SAME hardware resource simultaneously | Shared memory port for instruction fetch & data access | Separate instruction/data memory (Harvard), duplicate hardware units |
| ๐ Data | An instruction depends on the result of a prior instruction not yet available (RAW/WAR/WAW) | ADD R1,.. followed immediately by SUB ..,R1,.. | Forwarding/bypassing, stalling, compiler reordering |
| ๐ฆ Control | Next instruction to fetch is uncertain due to a branch/jump | BEQ followed by sequential instructions that may be wrong | Branch prediction, delayed branching, branch target buffer |
### โ ๏ธ Common Traps & Exam Tips - ๐ RAW = "true dependency" โ cannot be eliminated by reordering alone, only mitigated via forwarding/stalling; WAR/WAW = "false dependency" โ can often be eliminated entirely via register renaming. - ๐ LOAD-USE hazard is a classic case where even FULL forwarding cannot avoid at least 1 stall cycle, because the data simply isn't ready until after the MEM stage โ a favorite GATE numerical trap. - ๐ Forwarding solves MOST data hazards without stalling, but NOT all โ always check if the producing instruction's result is available in time for the consuming instruction's EX stage.
5 Vector Processing Unit
5.1 Need for Vector Processing
- Scalar processors operate on one data element at a time โ inefficient for applications involving large, uniform data sets (scientific computing, graphics, matrix operations) where the SAME operation must be applied repeatedly across many elements.
- Vector Processing allows a SINGLE instruction to operate on an ENTIRE array (vector) of data elements, dramatically improving throughput for such data-parallel workloads โ a hardware realization of the SIMD concept (recall Flynn's Taxonomy from Chapter 9.1).
5.2 Vector Processor Architecture
1 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ2 โ Vector Registers โ3 โ V1: [a1][a2][a3]...[an] โ4 โ V2: [b1][b2][b3]...[bn] โ5 โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ6 โผ7 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ8 โ Pipelined Functional Unit โ9 โ (e.g., Vector Adder) โ10 โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ11 โผ12 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ13 โ Result Vector Register โ14 โ V3: [a1+b1][a2+b2]...[an+bn] โ15 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ- Vector Registers: Hold entire arrays of data (not just a single scalar value).
- Pipelined Functional Units: The ALU itself is pipelined internally, so once the pipeline fills, one result (e.g., one element sum) emerges per cycle.
5.3 How Vector Processing Works (Pipelined Vector Operations)
- A single vector instruction (e.g.,
VADD V3, V1, V2) is broken down internally into a pipeline of per-element operations. - Because the functional unit is deeply pipelined, elements are fed in one after another โ while element 1 is in the "multiply" stage, element 2 can already be in "fetch," and so on โ achieving very high throughput for the entire vector operation.
5.4 Types of Vector Processing
| Type | Description |
|---|---|
| ๐ฆ Memory-to-Memory | Vector operands are read directly from MEMORY and results are written directly back to MEMORY (no intermediate vector registers) โ simpler but slower due to memory bandwidth limits |
| ๐ง Register-to-Register | Vector operands are first loaded into VECTOR REGISTERS, operated on, and results stored back into vector registers (memory accessed only for bulk load/store) โ faster, more common in modern vector processors |
5.5 Lanes in Vector Processors
- A lane is a hardware "channel" containing a full set of functional units (ALU, etc.) capable of processing ONE vector element independently.
- Multiple lanes allow a vector processor to work on several elements of the vector SIMULTANEOUSLY (true parallelism), not just pipelined sequential elements โ e.g., a processor with 4 lanes can process 4 vector elements per cycle instead of just 1.
1 Lane 1 Lane 2 Lane 3 Lane 42V1: [a1] [a2] [a3] [a4] โ3V2: [b1] [b2] [b3] [b4] โโโบ All 4 additions4 โผ โผ โผ โผ happen in PARALLEL5V3: [a1+b1] [a2+b2] [a3+b3] [a4+b4] โ (same cycle!)๐ข More lanes = more elements processed per cycle = higher throughput (at the cost of more hardware/area).
### โ ๏ธ Common Traps & Exam Tips - ๐ Vector processing is the hardware embodiment of SIMD from Flynn's Taxonomy โ a common cross-chapter linking question. - ๐ Register-to-Register vector processors are generally FASTER than Memory-to-Memory ones (avoid repeated memory bandwidth bottlenecks) โ a frequently tested comparison. - ๐ Don't confuse pipelining within a lane (sequential elements processed with overlap, one per cycle after fill) with multiple lanes (truly parallel/simultaneous processing of DIFFERENT elements in the SAME cycle) โ these are two independent mechanisms for speeding up vector operations, and can be combined.
Quick Revision Summary All Formulas & Concepts Together
1โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ2 PIPELINE TIMING3 Time (Non-Pipelined) = n ร k [n = instructions, k = stages]4 Time (Pipelined) = k + (n โ 1)5
6 SPEEDUP7 Speedup (S) = Time(Non-Pipelined) / Time(Pipelined)8 = (n ร k) / (k + n โ 1)9 Maximum Speedup (nโโ) = k (number of pipeline stages)10
11 EFFICIENCY & THROUGHPUT12 Efficiency = Speedup / k13 Throughput = Instructions Completed / Total Time14
15 CPI-BASED EXECUTION TIME16 Execution Time = CPI ร Number of Instructions ร Clock Cycle Time17 Ideal Pipelined CPI = 1 (no hazards)18โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโKey Conceptual Recap
- Pipelining overlaps instruction execution stages (IF-ID-EX-MEM-WB) to boost throughput, not single-instruction latency. IBM 801 = first RISC, pioneer of pipelining-friendly design.
- Speedup approaches the number of stages
kas instruction countn โ โ; real speedup is always less due to fill/drain overhead and hazards. - 3 Hazard Types: ๐งฑ Structural (resource conflict โ duplicate hardware), ๐ Data (RAW/WAR/WAW โ forwarding/stalling/reordering), ๐ฆ Control (branches โ prediction/delayed branching/BTB).
- RAW = true dependency (unavoidable, needs forwarding/stall); WAR/WAW = false dependency (fixable via renaming).
- LOAD-USE hazard: even with full forwarding, at least 1 stall cycle is unavoidable โ classic GATE numerical trap.
- Vector Processing: SIMD in hardware form; Register-to-Register faster than Memory-to-Memory; Lanes = parallel hardware channels (true simultaneous element processing), distinct from pipelining (overlapped sequential processing).
Mastery check
How well can you recall this?
Finished this lesson?