Sub topic 2
Why Pipeline? Overlapping Instructions
Assume you know the CPU clock, registers, memory, and simple instructions such as load, add, and branch. This chapter builds on the idea that an instruction passes through several steps before it completes.
The problem with sequential execution
A processor could finish one instruction completely before starting the next one:
- Fetch the instruction from memory.
- Decode it and read its registers.
- Execute its operation.
- Access memory if needed.
- Write the result back.
This approach is simple, but many processor parts remain idle during each step. While one instruction is being decoded, the arithmetic unit is often unused. While another instruction accesses memory, the instruction-fetch unit could already fetch the next instruction.
Pipelining keeps these parts busy by overlapping different instructions.
The pipeline idea
A pipeline divides instruction processing into stages. Each stage performs part of the work during one clock cycle. After an instruction moves to the next stage, the following instruction enters the stage it just left.
This is similar to an assembly line. Several products can be at different stations at the same time. The assembly line does not finish one product faster, but it produces completed products more frequently.
In a classic five-stage pipeline, the stages are:
| Stage | Main operation |
|---|---|
| IF | Fetch the instruction |
| ID | Decode it and read registers |
| EX | Perform an arithmetic operation or calculate an address |
| MEM | Read or write data memory |
| WB | Write a result into a register |
Latency versus throughput
Latency is the time for one instruction to travel through the entire pipeline. Throughput is the rate at which completed instructions leave the pipeline.
For a five-stage pipeline, one instruction still needs about five clock cycles to complete. However, once the pipeline is full, one instruction can complete during every cycle. Pipelining mainly improves throughput, not the latency of an individual instruction.
For n instructions in a k-stage pipeline, assuming no stalls and one instruction entering per cycle:
\[ \text{pipeline cycles} = k + n - 1 \]
The first instruction needs k cycles to reach completion. Each later instruction adds one cycle.
For example, three instructions in a five-stage pipeline complete in:
\[ 5 + 3 - 1 = 7 \text{ cycles} \]
A non-pipelined processor that spends five cycles on each instruction would need:
\[ 3 \times 5 = 15 \text{ cycles} \]
The ideal speedup approaches five as the number of instructions becomes large. For a small number of instructions, the initial filling and final draining of the pipeline reduce the speedup.
When overlap becomes difficult
Overlap works only when stages can operate independently. A later chapter will study hazards, which are conditions that prevent the next instruction from safely advancing. A data dependency, a shared hardware resource, or a branch whose destination is unknown can force the pipeline to wait.
Exercise
A four-stage pipeline processes 20 independent instructions with no stalls. How many clock cycles are required?
Show answer
k + n - 1 = 4 + 20 - 1 = 23 cycles.