S4 · Lesson 7 · Branch prediction
Two-bit counters, correlating and gshare predictors, tournaments, target buffers and return stacks
A modern core fetches several instructions per cycle into a pipeline 15 or more stages deep, so at any moment it holds dozens of instructions past the last branch it has actually resolved. If it guessed a branch wrong, all of that work is thrown away. With branches every five or six instructions, prediction accuracy is one of the strongest levers on performance: going from 90% to 97% correct can speed a wide core up by a third. Predictors went from one bit per branch to two-bit counters, then to tables indexed by the history of recent branches, and today to perceptron and TAGE designs that are right well over 95% of the time.
- Misprediction rate of a loop's closing branch against the loop's trip count N, when the loop runs again and again.
- A 1-bit predictor (solid) misses twice per loop: on the exit and again on the first iteration of the next run. A 2-bit counter (dashed) misses only the exit.
- A predictor that keeps h bits of branch history (dotted line) learns the whole pattern when N ≤ h + 1 and predicts the exit too. Longer loops still cost one miss per run.
Predict
A loop runs 10 iterations, and the program runs the whole loop many times. What fraction of the loop branch's executions does a 2-bit counter mispredict?
10%: one miss per run of the loop, on the exit. The counter sits in "strongly taken"; the exit moves it only to "weakly taken", so the first iteration of the next run is still predicted taken. A 1-bit predictor would flip to "not taken" on the exit and miss the first iteration too: 20%. A predictor with 10 or more bits of history sees nine takens in a row and knows the exit is next: 0%.
Name it
Counters and tables
- A branch history table (BHT) is indexed by low bits of the branch's address. A 2-bit saturating counter per entry predicts taken in its two upper states and needs two wrong outcomes to change its mind. A table of entries holds bits.
- Different branches that share an entry alias. With B active branches spread uniformly over entries, a given branch shares its entry with at least one other with probability .
Using history
- Correlating predictors index by the outcomes of recent branches as well:
if (a) …; if (a && b) …correlate. - gshare indexes the counter table by the branch address XOR an h-bit global history register, spreading each branch's patterns across the table.
- Tournament predictors run a local and a global predictor and use a third table of counters to choose per branch. TAGE keeps several tables indexed by geometrically longer histories and uses the longest that matches.
Targets and returns
- A branch target buffer (BTB) caches each taken branch's target so fetch can redirect in the same cycle. A BTB miss on a taken branch costs a few cycles even when the direction was predicted right.
- A return address stack pushes on calls and pops on returns; it predicts returns perfectly while the call depth fits.
The cost
Worked example
Branches are 20% of instructions and a misprediction costs 15 cycles on a core whose base CPI is 0.5. Compare 90% and 97% accuracy.
Show the solution
- At 90% accuracy. for a total CPI of 0.80.
- At 97% accuracy. for a total CPI of 0.59.
- Speedup.
- Why accuracy matters more over time. The wider and deeper the core, the more accuracy is worth, because the base CPI shrinks while the penalty grows.
Answer the 97% predictor gives a CPI of 0.59 against 0.80, making the core 1.36 times faster.
Worked example
A program has 2000 active branches and a 4096-entry table of 2-bit counters. How many bits is the table, and what's the chance a given branch shares its entry?
Show the solution
- Table size. which is 1 KB.
- Chance of sharing. The branch's entry is free only if none of the other 1999 branches lands there:
- Harmful or not. Many of those collisions are harmless (the branches agree), but some are destructive, which is why larger tables, tagged tables (as in TAGE) and hashing with history improve accuracy.
Answer the table is 8192 bits (1 KB), and a given branch shares its entry with probability 0.39.
Worked example
Branches are 20% of instructions, predicted 96% right with a 14-cycle misprediction penalty. Taken branches are 12% of instructions, and 5% of them miss in the BTB, costing 2 cycles each. What CPI do branches add?
Show the solution
- Direction mispredictions.
- Target misses in the BTB.
- Total. cycles per instruction, about 12% on a core with a base CPI near 1.
- Which matters more. The direction predictor matters most, but a small BTB can cost as much as a few points of accuracy.
Answer branches add 0.124 cycles per instruction.
Check
A loop of 8 iterations runs many times. What's a 1-bit predictor's misprediction rate on its closing branch?
Check
The same loop, 8 iterations, with a 2-bit counter. What's the misprediction rate?
Check
Branches are 0.15 of instructions, the predictor is 0.9 accurate, and a misprediction costs 20 cycles. How much CPI do mispredictions add?
Check
A core has base CPI 0.4, 0.18 branches and a 12-cycle misprediction penalty. What speedup comes from improving accuracy from 0.92 to 0.98?
Check
500 active branches share a predictor of entries, indexed uniformly. What's the probability a given branch shares its entry with at least one other?
Check
Branches are 0.15 of instructions and 0.6 of them are taken. The BTB finds the target of 0.95 of taken branches; a BTB miss costs 3 cycles. What CPI does it add?
Check
Branches are 0.15 of instructions, predicted with accuracy 0.9 and a 20-cycle penalty, using a table of 2-bit counters (n = 14) shared by 500 active branches.
(a)
What CPI do mispredictions add?
(b)
How many bits is the table?
(c)
What is the chance a given branch shares its entry?
Check
A core has base CPI c; branches are a fraction f of instructions, predicted with accuracy a and a penalty of P cycles.
Step 1
What is its CPI?
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 accuracy would reach a CPI of T?
Explain why
Why does a 2-bit counter predict loops better than a 1-bit predictor?
A loop branch is taken every iteration except the last. A 1-bit predictor simply remembers the last outcome, so the exit flips it to "not taken", and the next time the loop starts it mispredicts the first iteration as well: two misses each time the loop runs. A 2-bit counter needs two consecutive surprises to change its prediction. The single not-taken exit only moves it from strongly to weakly taken, so it still predicts taken when the loop starts again. It pays only for the exit, halving the misses, and in general it ignores one-off anomalies while still adapting to a branch whose behavior really changes.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.