All episodes · From One Switch to a CPU · Episode 12

CPU Pipelining: How Processors Overlap Instructions

Pressing play loads the video from YouTube.

Four loads of laundry take twelve hours one at a time, but only six if you overlap them. Processors use the same trick.
We overlap fetch, decode and execute so one instruction finishes every tick, then deal with what goes wrong: a data hazard, a stall, forwarding the answer straight out of the ALU, and jumps that throw away instructions already in the pipeline.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Four loads of laundry. Each one needs washing, drying, and folding: an hour each. One load at a time, start to finish, takes twelve hours. But the moment the first load moves to the dryer, the washer is free. Start the next one. Now all four are done in six hours. Same machines, half the time. Today: the same trick, inside a CPU.

Last time, our CPU did one instruction at a time: fetch, decode, execute, then the next. While it executes, the fetch part just waits. So let's overlap them. While the first instruction executes, the second is being decoded, and the third is being fetched. Each stage hands its work to the next on every clock tick, through small registers in between. That's a pipeline. One instruction still takes three ticks from start to finish. But once the pipeline is full, one instruction finishes on every single tick. Real processors cut the work into more stages: five in the classic textbook design, and more than a dozen in many modern chips.

But overlapping causes trouble. Look at these two instructions: ADD R1, R2, then SUB R3, R1. The subtraction needs the new R1. But when it reads its registers in the decode stage, the addition hasn't finished yet. It would get the old value. This is called a data hazard.

One fix: stall. Hold the second instruction back for a tick, and let a bubble, an empty slot, flow down the pipeline instead. Correct, but slower.

A better fix: forwarding. The answer already exists at the output of the ALU. Wire it straight back to the ALU's input, so the next instruction can use it right away. No waiting. Most processors do this all the time.

Jumps cause the other big hazard. By the time a jump is decided, the pipeline has already fetched the instructions after it. If the jump is taken, those instructions are wrong. They're thrown away, and the pipeline refills from the new address. With deep pipelines, every wrong guess wastes many ticks. So modern CPUs try to guess where each jump will go. That's next time.

So: a pipeline overlaps instructions, so one finishes on every tick. Hazards get in the way, and stalls and forwarding keep the answers right. Next time: the tricks that make modern CPUs fast: guessing jumps, running instructions out of order, and using many cores at once. See you there.

Next episode: the tricks that make modern CPUs fast. Branch prediction, out-of-order execution and many cores.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal