C8 · Lesson 40 · Pipelining and hazards
Five instructions in flight, and what gets in their way
A single-cycle processor leaves most of its hardware idle: while the data memory serves a load, the instruction memory, the register file and the ALU have nothing to do. Pipelining puts registers between the stages so that each stage works on a different instruction, like a laundry where the washer starts the next load while the dryer runs. Five instructions are in progress at once and the clock can run several times faster. The catch is that instructions depend on each other: one needs a result another hasn't finished, or a branch changes which instructions should have been started. Those hazards decide how close a real pipeline gets to its ideal.
- A processor's 1000 ps of logic is split into k equal stages, each followed by a pipeline register that costs r ps. Each extra stage also adds stall cycles from hazards, at the rate h per instruction per stage.
- The curve is the time per instruction, the cycle time times the CPI. Drag k up from 1: it falls fast at first, then flattens, then rises as register overhead and stalls take over.
- Set h to 0: deeper is always better, though with diminishing returns. Raise h and the best depth moves down.
Predict
A five-stage pipeline with no stalls runs 1000 instructions. How many clock cycles does it take?
- The first instruction takes 5 cycles to come out of the pipeline, and after that one finishes every cycle: 5 + 999. For long programs that's a CPI very close to 1, at a clock about five times faster than single-cycle.
Name it
Five stages
Fetch (read the instruction), Decode (read registers, build the immediate), Execute (ALU), Memory (load or store), Writeback (write the register file). Each instruction moves one stage per cycle:
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
lw t0, 0(a0) | F | D | E | M | W | ||
add t1, t2, t3 | F | D | E | M | W | ||
sub t4, t5, t6 | F | D | E | M | W |
The clock period is set by the slowest stage plus the pipeline register's overhead, . N instructions in a k-stage pipeline with no stalls take cycles.
Hazards
- Data hazards.
add t0, ...followed bysub t1, t0, ...: the sub reads t0 in Decode before the add has written it. Forwarding sends the ALU result straight from the pipeline register back to the ALU's input, so no time is lost. A load-use hazard is worse: a loaded value isn't ready until the end of Memory, so an instruction that uses it immediately must stall one cycle even with forwarding. Compilers schedule an independent instruction into that slot when they can. - Control hazards. A branch is decided in Execute, by which time two more instructions have been fetched. If the processor predicted "not taken" and the branch is taken, those two are flushed: a 2-cycle penalty.
- Structural hazards. Two stages needing one resource at once, such as fetching an instruction and loading data from a single memory. Separate instruction and data caches avoid it.
CPI with stalls
with each F the fraction of instructions (or of branches) involved.
Worked example
A design's five stages take 200, 150, 120, 200 and 80 ps, and each pipeline register adds 40 ps. Compare it with the single-cycle version on a billion instructions at a CPI of 1.15.
Show the solution
- Single-cycle. 750 ps a cycle at a CPI of 1: 0.75 s.
- The pipelined clock. The slowest stage plus a register:
- The pipelined time. 2.7 times faster.
- Why less than 5. The stages aren't equal, the registers cost time, and stalls raise the CPI.
Answer 0.276 s pipelined against 0.75 s single-cycle, 2.7 times faster.
Worked example
25% of instructions are loads, and 40% of loads are followed at once by an instruction that uses the value. 15% are branches, half of them taken, predicted not taken with a 2-cycle penalty. What is the CPI?
Show the solution
- Load-use stalls.
- Branch penalties.
- Add them to the base CPI.
Answer a CPI of 1.25.
Worked example
Without forwarding, an instruction that uses the result of the one just before it waits 2 cycles, and 30% of instructions do. With forwarding, only loads stall, 1 cycle, and 25% of instructions are loads of which 40% are used at once. Compare the CPIs.
Show the solution
- Without forwarding.
- With forwarding.
- Compare. A handful of multiplexers and wires make the processor 45% faster.
- The stall that remains. A load's data comes out of the memory stage, too late for the next instruction's execute stage; compilers hide it by scheduling an independent instruction into the gap.
Answer a CPI of 1.6 without forwarding and 1.1 with it, 45% faster.
Check
A 5-stage pipeline with no stalls runs 100 instructions.
(a)
How many cycles does it take?
(b)
How many instructions finish per cycle, on average?
Check
A pipeline's slowest three stages take 150, 180 and 200 ps (the others less), and each pipeline register adds 50 ps.
(a)
What is the clock period?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
(b)
What is the clock frequency?
Check
0.2 of a program's instructions are loads, 0.3 of which are used immediately; 0.2 are branches, 0.6 of which are mispredicted at a 2-cycle penalty.
(a)
What is the CPI?
(b)
What percentage of the cycles are lost to mispredictions?
Check
A processor's 800 ps of logic is split into 10 equal stages, each with a 30 ps register, and there are no stalls.
(a)
What is the cycle time?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
(b)
How many times the single-cycle throughput (with one register) is that?
Check
Which pair stalls a five-stage pipeline for a cycle even with full forwarding?
Check
Put the five pipeline stages in order.
- Execute in the ALU
- Write the result back to the register file
- Decode it and read registers
- Fetch the instruction
- Access data memory
Check
Without forwarding, 30% of instructions wait 2 cycles for the one before. With forwarding only load-use pairs stall 1 cycle: 20% loads, 50% of them used at once.
(a)
What's the CPI without forwarding?
(b)
And with it?
Check
A single-cycle design's T picoseconds of logic is split evenly into k stages, each with o of register overhead.
Step 1
What's the pipelined clock period?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What's the speedup at a CPI of 1, against a single-cycle clock of T?
Step 3
What does the speedup approach for very many stages?
Explain why
Why does a load followed immediately by an instruction using its result cost a stall, even with forwarding?
Forwarding can only pass along a value that already exists. An ALU result exists at the end of the Execute stage, just in time for the next instruction's Execute in the following cycle. A load's value doesn't exist until the end of the Memory stage, one stage later, but the next instruction wants it at the start of its Execute stage, which is the same cycle as the load's Memory stage. There's no way to forward backward in time, so the dependent instruction has to wait one cycle, and a bubble goes down the pipeline in its place. If the compiler can put an independent instruction between them, the bubble disappears.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.