Edexcel International Advanced Level Computer Science, point 11.1.3
Definition
Pipelining is a technique in which the processor works on several instructions at once, each in a different stage of the fetch-decode-execute cycle, like an assembly line. As one instruction moves on to the next stage, the following instruction enters the stage it left.
Function and why needed
It increases throughput: the number of instructions completed per unit of time, without making each instruction itself faster. The hardware for each stage is kept busy instead of waiting while other stages work.
a. The five stages
| Stage | Name | What happens |
|---|---|---|
| IF | Instruction fetch | The instruction is fetched from memory (using the PC), and the PC is moved on |
| ID | Instruction decode | The control unit decodes the instruction and reads the registers it needs |
| EX | Execute | The ALU carries out the operation, or works out an address |
| MEM | Memory access | A load or store reads or writes data memory (other instructions do nothing here) |
| WB | Register write back | The result is written into the destination register |
Each stage takes one clock cycle, and has its own hardware, so different instructions can use different stages at the same time.
Operation: without and with pipelining
Without pipelining (each instruction finishes before the next starts), 5 instructions take cycles.
With pipelining:
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| Instr 1 | IF | ID | EX | MEM | WB | ||||
| Instr 2 | IF | ID | EX | MEM | WB | ||||
| Instr 3 | IF | ID | EX | MEM | WB | ||||
| Instr 4 | IF | ID | EX | MEM | WB | ||||
| Instr 5 | IF | ID | EX | MEM | WB |
All 5 instructions finish in 9 cycles.
In general: instructions on a -stage pipeline need cycles, against without. For 5 instructions: cycles against 25 (2.8 times faster). For 1000 instructions: against 5000 (nearly 5 times, close to the number of stages).
Once the pipeline is full (cycle 5), one instruction
completes every cycle.
Benefits
- Higher throughput: close to one instruction per cycle once the pipeline is full.
- Better use of hardware: every stage is busy.
- Faster programs without a faster clock.
- Works especially well with RISC (11.1.2), because simple, same-length instructions fit equal stages.
Drawbacks
- More complex hardware and control.
- The first instruction still takes 5 cycles (latency isn't reduced).
- Hazards can stall the pipeline (below).
- A mispredicted branch wastes work.
- Instructions of different lengths and times (CISC) make it harder.
- Deeper pipelines make hazards more costly.
b. Branch hazards
A hazard is a situation that stops the next instruction executing in its next cycle. A branch hazard (control hazard) happens with a branch or jump: the processor doesn't know which instruction comes next until the branch is decided (in the EX stage), but it has already fetched the following instructions.
If the branch is taken, the instructions fetched after it are the wrong ones and must be discarded (flushed). The pipeline then fetches from the branch target, wasting cycles (the branch penalty).
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Branch | IF | ID | EX | MEM | WB | |||
| Wrong 1 | IF | ID | flushed | |||||
| Wrong 2 | IF | flushed | ||||||
| Target | IF | ID | EX | MEM | WB |
Two cycles are wasted here.
Reducing branch hazards
| Method | How it works |
|---|---|
| Stalling | Pause the pipeline until the branch is decided (simple, but slow) |
| Branch prediction | Guess the outcome (for example "loops branch back") and fetch speculatively. A correct guess costs nothing, and a wrong one is flushed. Modern CPUs predict correctly over 90% of the time |
| Delayed branch | Fill the wasted slot with an instruction that is needed anyway |
| Early decision | Work out the branch in an earlier stage (ID) with extra hardware |
Other hazards (not required): data hazards (an instruction needs a result that isn't written back yet) and structural hazards (two instructions need the same hardware).
Worked example
Task: a program of 100 instructions runs on a 5-stage pipeline with a 1 ns clock cycle. There are no hazards. Compare the run time with and without pipelining. Then suppose 10 branches each waste 2 cycles.
- Without pipelining: cycles ns.
- With pipelining: cycles ns, which is about 4.8 times faster.
- With 10 branch hazards wasting 2 cycles each: cycles ns. Still about 4 times faster, but the branches cost 19% more time. Good branch prediction would avoid most of this.
Exam tip: learn the five stages in order (IF, ID, EX, MEM, WB). You may be asked to complete a pipeline diagram, so shift each instruction one cycle later. Use the formula cycles. For branch hazards, say that the wrong instructions have been fetched and must be discarded, and name branch prediction as a fix.
Check yourself
State the five stages of the pipeline in order
Instruction fetch (IF), instruction decode (ID), execute (EX), memory access (MEM) and register write back (WB).
How many cycles do 10 instructions take on a 5-stage pipeline with no hazards?
5 + 9 = 14 cycles.
What is a branch hazard?
When a branch's outcome isn't known until after the next instructions have been fetched, so the wrong instructions may have to be discarded.
Give one way of reducing the effect of branch hazards
Any one of: branch prediction; stalling; a delayed branch; deciding the branch earlier.
Does pipelining make a single instruction finish faster?
No. Each instruction still takes five cycles; pipelining increases the number of instructions completed per unit time (throughput).