Pipelines and branch prediction, in brief
Video 023 of 140 in Virtualization Foundations, in the unit “CPUs: sockets, cores, threads and caches”. 3:47, with English and Hindi audio.
Modern chips do not finish one instruction before they start the next. They overlap the steps, and they guess ahead. Those two ideas explain much of how fast a processor really runs. This article covers the pipeline, which lets several instructions be partly done at once, and branch prediction, which keeps the pipeline full when a program reaches a choice. It also shows when a wrong guess costs time.
What you will learn
- A pipeline overlaps the steps of several instructions, so more of them finish in the same time.
- A branch is an instruction that chooses between two paths, and the chip cannot know the choice early.
- Branch prediction guesses the path and keeps going. A wrong guess throws away work and costs time.
- Random branches make predictions fail often, so the same amount of work can run at different speeds.
How overlapping steps keeps the chip busy
Think of laundry. Washing, drying and folding are three steps. If you finish one load before you start the next, the machines sit idle. A better way is to overlap. While one load dries, the next load goes into the washer.
A CPU pipeline works the same way. Its stages are fetch, decode, execute and write back. Each stage is its own piece of hardware, so while one instruction executes, the next one is being decoded, and a third is being fetched. Each instruction still passes through all four stages. The gain comes from the overlap, so more instructions finish in the same time.
Why a branch can stall the pipeline
The trouble starts at a branch. A branch is an instruction that chooses between two paths, such as the test in an if statement, or the check at the end of a loop. The chip wants to fetch the next instruction now, but the choice is not known until the test is worked out, several stages later.
So the chip does not sit idle. It guesses which path the program will take, and it fetches along that path. This guess is called branch prediction.
How branch prediction guesses, and what a wrong guess costs
- Most branches follow a pattern. A loop check usually says keep going, many times in a row, until the end.
- The chip keeps a record of how recent branches went, and it guesses that they will go the same way again.
- When the guess is wrong, the work on the wrong path is thrown away. The chip loses several cycles, then carries on.
A real case shows the effect. A Harbor Point Logistics program checks thousands of orders in a loop, and each check is a branch. If the orders are alike, the chip predicts well and the loop runs quickly. If the mix is random, the guesses fail more often, and the loop may slow down.
When a long pipeline makes wrong guesses worse
Random branches make the chip guess wrong often. Each wrong guess throws away work and costs time. A longer pipeline has more stages in flight, so a wrong guess loses more work.
Note: You notice this when speed changes with the data, even though the amount of work stays the same. Look at the kind of data before you blame the program.
Terms
- Pipeline
- A chip design in which the steps of several instructions overlap, so that more instructions finish in the same time.
- Branch
- An instruction that chooses between two paths of a program, such as an if test or a loop check.
- Branch prediction
- The chip's guess of which path a branch will take, so that it can keep fetching instructions.
Check yourself
Why does a pipeline make a program faster without shortening each instruction?
Several instructions are partly done at once, so more of them finish in the same time, even though each one still passes through every stage.
What happens when the chip's branch guess is wrong?
The work done on the wrong path is thrown away, and the chip loses several cycles before it carries on along the right path.
Why can the same amount of work run at different speeds?
If the data is random, branch guesses fail more often, so more work is thrown away and the program slows down.
Previous: Cache misses, with real numbers. Next: x86-64 and Arm: two instruction families.