CIE AS & A Level Computer Science, section 15.1 (Processors, parallel processing and virtual machines)
The idea
Without pipelining, the processor finishes one instruction completely (fetch, decode, execute) before starting the next, so parts of the processor are idle. With pipelining, the next instruction starts its fetch while the previous one is being decoded, so several instructions are in progress at once.
Three-stage example
| Clock cycle | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Instruction 1 | Fetch | Decode | Execute | ||
| Instruction 2 | Fetch | Decode | Execute | ||
| Instruction 3 | Fetch | Decode | Execute |
Three instructions take 5 cycles with pipelining, not 9, so the throughput (instructions completed per second) is greater.
Problems
- Branch instructions: a jump changes which instruction comes next, so the instructions already fetched into the pipeline may be wrong and must be discarded (the pipeline is flushed), wasting cycles.
- Interrupts: when an interrupt happens, the instructions in the pipeline must be dealt with (usually the pipeline is cleared and the state saved), and then the interrupted program is resumed, so some work is repeated.
- Data dependencies: an instruction may need a result that has not been produced yet.
Reducing the problems
Branch prediction guesses which way a branch will go, so the right instructions are already in the pipeline.
Pipelining and RISC
Fixed-length, one-cycle instructions make pipelining simpler, which is why RISC suits it.
Common mistake: saying pipelining makes one instruction run faster. Each instruction still takes the same time; it is the number completed per second (throughput) that rises. Also, a branch causes a flush, not a speed-up.
Exam tip: draw a table with a row for each instruction, shifted one cycle each, and count the total cycles. When describing a problem, mention the branch or interrupt and say the pipeline has to be flushed.
Check yourself
How many cycles are needed to run 5 instructions in a 3-stage pipeline?
cycles.
Why can a branch instruction slow a pipeline?
The instructions already fetched may be wrong, so the pipeline is flushed and cycles are wasted.
What does pipelining increase?
The throughput, the number of instructions completed in a given time.