S47 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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 2n entries holds 2n+1 bits.
  • Different branches that share an entry alias. With B active branches spread uniformly over 2n entries, a given branch shares its entry with at least one other with probability 1−(1−2−n)B−1.

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

ΔCPI=fbranch×(1−accuracy)×penalty

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
  1. At 90% accuracy. ΔCPI=0.2×0.1×15=0.30 for a total CPI of 0.80.
  2. At 97% accuracy. ΔCPI=0.2×0.03×15=0.09 for a total CPI of 0.59.
  3. Speedup. 0.80/0.59=1.36
  4. 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
  1. Table size. 4096×2=8192bits which is 1 KB.
  2. Chance of sharing. The branch's entry is free only if none of the other 1999 branches lands there: 1−(1−1/4096)1999=0.39
  3. 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
  1. Direction mispredictions. 0.2×0.04×14=0.112
  2. Target misses in the BTB. 0.12×0.05×2=0.012
  3. Total. 0.112+0.012=0.124 cycles per instruction, about 12% on a core with a base CPI near 1.
  4. 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 212 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 2n 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°.

Explain why

Why does a 2-bit counter predict loops better than a 1-bit predictor?

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.