🚀 Why Pipelining?
Think of an assembly line in a factory.
Instead of having one worker build an entire product before the next product even starts, you break the job into stages—each worker performs one stage, and several products are in-flight simultaneously. A pipelined CPU works the same way for instructions.
1. Classic 5-Stage Pipeline
| Stage | Name (Typical) | Main Work |
|---|
| IF | Instruction Fetch | Read next instruction from memory / I-cache |
| ID | Instruction Decode / Register Fetch | Decode opcode, read source registers |
| EX | Execute / ALU | Perform arithmetic / address calculation |
| MEM | Memory Access | Load/store data from D-cache |
| WB | Write Back | Write result to register file |
Instead of finishing an entire instruction before starting the next, each clock cycle the processor:
• fetches instruction i + 4
• decodes instruction i + 3
• executes instruction i + 2
• accesses memory for instruction i + 1
• writes back results for instruction i
Result: after the pipeline is full, one instruction completes every clock cycle.
2. Throughput vs. Latency
• Latency (time for one instruction to go from IF → WB) increases: it now spans 5 cycles (one per stage).
• Throughput (instructions completed per unit time) improves: instead of 1 instruction every 5 cycles (non-pipelined), you get 1 instruction per cycle—up to 5× better, assuming ideal conditions.
Non-pipelined: |---Inst1---|---Inst2---|---Inst3---|
Pipelined: |I1|I2|I3|I4|I5| (steady-state → 1 per cycle)
3. Why Throughput Rises
-
Overlap of Work
Each stage works on different instructions simultaneously, keeping hardware busy every cycle.
-
Higher Clock Frequency
Shorter stages → shorter critical path → pipeline can run at a higher clock (though deeper pipelines have diminishing returns).
-
Resource Replication
Some resources (register file read/write ports, pipeline latches) are duplicated so multiple instructions can be in-flight.
4. Practical Challenges (and Solutions)
| Hazard Type | Problem | Classic Mitigations |
|---|
| Data hazards (RAW, WAR, WAW) | Later instruction needs a result not yet written | Forwarding / bypassing, register renaming |
| Control hazards | Branch outcome unknown until later stage | Branch prediction, speculative execution |
| Structural hazards | Two stages need the same resource in one cycle | Add duplicates (e.g., separate IF & MEM caches) or stall |
Stalls and mis-predictions add “bubbles,” reducing ideal throughput. Sophisticated CPUs (superscalar, out-of-order) layer extra tricks to hide these penalties.
5. Quick Math Example
Non-pipelined CPU:
• 5 ns critical path → 200 MHz clock.
• Executes 1 instruction per 5 ns ⇒ 200 MIPS.
5-stage pipeline:
• Each stage ~1 ns → 1 GHz clock.
• Ideal: 1 instruction per 1 ns ⇒ 1 GIPS (5× throughput).
• If 20 % cycles lost to hazards ⇒ 0.8 GIPS (still 4×).
TL;DR
Pipelining slices instruction execution into discrete stages, allowing multiple instructions to occupy the processor simultaneously. While each individual instruction takes more cycles (higher latency), the overlap means the CPU can finish an instruction every clock—dramatically boosting instruction throughput.