OCR H446, section 1.1.1 (d)
The idea
Without pipelining, the processor finishes one instruction completely before it starts the next. While one stage (say, execute) is busy, the other parts of the processor sit idle.
Pipelining overlaps the stages. While one instruction is being executed, the next is being decoded, and the one after that is being fetched. The processor works on several instructions at once, each at a different stage. It's like a car factory assembly line: each station does one job, and a new car enters as soon as the first station is free.
A three-stage pipeline
Take three stages: F (fetch), D (decode) and E (execute), with each stage taking one clock cycle.
Without pipelining
Four instructions take cycles.
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Instruction 1 | F | D | E | |||||||||
| Instruction 2 | F | D | E | |||||||||
| Instruction 3 | F | D | E | |||||||||
| Instruction 4 | F | D | E |
With pipelining
Each instruction starts one cycle after the one before. The four instructions take only 6 cycles.
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Instruction 1 | F | D | E | |||
| Instruction 2 | F | D | E | |||
| Instruction 3 | F | D | E | |||
| Instruction 4 | F | D | E |
For instructions in a pipeline of stages, the total is cycles:
For a long program, the pipeline completes about one instruction per cycle, instead of one every three. The processor's throughput (instructions completed per second) goes up, and the hardware is used more efficiently.
Note that each single instruction still takes three cycles. It is the rate of completion that improves, not the time for one instruction.
Problems with pipelining
A pipeline works best when instructions are independent and arrive in order. Two things can disrupt it.
1. Branches
A branch instruction changes the PC, so the instructions that have already been fetched behind it may be the wrong ones.
BRA 20 <- the processor does not know until execute that it jumps
ADD 5 <- already fetched, but it must not run
SUB 6 <- already fetched, but it must not run
When a branch is taken, the wrong instructions in the pipeline must be thrown away (flushed), and fetching starts again from the new address. The cycles are wasted. Processors reduce the damage with branch prediction: they guess which way a branch will go, and fetch ahead on that guess.
2. Dependencies between instructions
One instruction may need the result of the one before it:
ADD 10 ; result goes in the accumulator
STA 11 ; needs the new value of the accumulator
The second instruction can't proceed until the first has finished, so the pipeline must stall (wait), leaving a gap. Clever ordering of instructions, or forwarding results early, can reduce stalls.
Advantages and disadvantages
| Advantages | Disadvantages |
|---|---|
| Higher throughput: more instructions completed per second | More complex hardware to design |
| Hardware is kept busy, which is more efficient | Branches and dependencies cause stalls and flushes |
| Works well with simple, fixed-length instructions (as in RISC) | A wrong branch prediction wastes the cycles spent |
| Gives more speed without raising the clock speed | Doesn't make one instruction faster |
Worked example
Task: a processor has a 4-stage pipeline (fetch, decode, execute, write back), with 1 cycle per stage. How many cycles are needed for 10 instructions with and without pipelining?
- Without pipelining: cycles.
- With pipelining: cycles.
- The pipeline is about times faster, which is close to the number of stages (4). This assumes no branches or stalls. Each of those would add wasted cycles.
Exam tip: describe pipelining as different instructions being at different stages at the same time. Say it improves throughput, not the time for an individual instruction, and mention the problem with branches (the pipeline must be flushed).
Check yourself
What is pipelining?
Overlapping the stages of different instructions, so that while one is being executed the next is being decoded and the one after is being fetched.
Why does a branch instruction cause a problem for a pipeline?
The instructions already fetched after the branch may be the wrong ones, so they have to be discarded and fetching restarts from the new address, which wastes cycles.
How many cycles do 8 instructions take in a 3-stage pipeline with no stalls?
cycles.
Does pipelining make a single instruction finish sooner?
No. Each instruction still takes the same number of cycles. Pipelining increases the number of instructions completed per second.