S4 · Lesson 12 · Predictor lab
Bimodal and gshare predictors, a 2-bit counter's accuracy and a return stack, in Python
Branch predictors are judged on traces, and the quickest way to understand them is to build them. This lab writes a bimodal predictor of 2-bit counters and a gshare predictor that adds global history, runs both on branches with different patterns, works out exactly how well a 2-bit counter does on a branch that's simply random, and simulates a return-address stack to see where it breaks. Each result connects to a formula from lessons 7 and 8.
- The curve is the long-run accuracy of a 2-bit saturating counter on a branch taken independently with probability p, from the counter's stationary distribution.
- On a random branch it does slightly worse than always predicting the majority outcome (the dashed line), because a run of the minority outcome sometimes flips it.
- On patterned branches, which most are, history-based predictors do far better than either.
Predict
A loop branch is taken nine times and then not taken once, over and over. What accuracy does a bimodal predictor of 2-bit counters reach, and what does gshare with 12 bits of history reach?
The counter sits at strongly taken and mispredicts only the loop exit, then is right again on the next taken: 9 of 10. Gshare's history register sees the last 12 outcomes, which tell exactly where in the loop the branch is, so each position gets its own counter, and after a short warm-up even the exit is predicted.
Name it
The predictors
- Bimodal: index a table of 2-bit counters with the low n bits of the branch address; predict taken in states 2 and 3; count up on taken, down on not taken, saturating at 0 and 3.
- gshare: the same table, indexed by the address XOR an h-bit global history of recent outcomes (shifted in after each branch).
- A 2-bit counter on a random branch: its state is a Markov chain; with taken probability p and , the stationary probabilities go as , and the accuracy is .
- Return-address stack: push on calls, pop on returns; with depth d, recursion deeper than d overwrites the oldest entries, so the returns from the deepest calls beyond d are mispredicted.
Check
Write bimodal(trace, n) and gshare(trace, n, h): trace is a list of (pc, taken) pairs; counters start at 2 (weakly taken). Return each predictor's accuracy.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write two_bit_accuracy(p): the long-run accuracy of one 2-bit saturating counter on a branch taken independently with probability p (0 < p < 1), from the stationary distribution.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write ras_misses(seq, depth): seq is a list of ('call', addr) and ('ret', addr) events; a return-address stack of the given depth pushes on calls (dropping its oldest entry when full) and pops on returns. Count returns whose predicted address is wrong or missing.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write alias_prob(branches, n): the probability that a given branch shares its entry in a table of entries with at least one of the other branches, placed uniformly; and alias_sim(branches, n, trials, seed): estimate it by simulation.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a 2-bit counter predict a random 70%-taken branch worse than simply always predicting taken?
Always predicting taken is right exactly 70% of the time. The counter usually sits in a taken state, but every so often two not-taken outcomes arrive together and push it into a not-taken state. While it's there it predicts not taken, which for this branch is right only 30% of the time, until two taken outcomes push it back. Those excursions follow random runs that carry no information about the future, so each costs accuracy, and the long-run result is about 64%. Hysteresis helps when outcomes come in patterns or long runs; on independent outcomes, any reaction to recent history only adds mistakes.