Systems Concepts · COL7001, IIT Delhi
Branch Predictor Design
I built and measured the textbook branch predictors in a trace-driven simulator, found where they stop improving, and then designed a TAGE + loop predictor that fits in the same 64 Kbit budget and almost eliminates mispredictions on real programs.
The problem
A pipelined CPU guesses which way each conditional branch goes before it knows. Each wrong guess flushes the pipeline, so the predictor's accuracy shows up directly in performance. Here it is measured in MPKI: mispredictions per thousand branches, where lower is better.
The rules: every predictor gets at most 64 Kbit of state, all of it declared through a checked storage API, and is run on six synthetic traces (loops, alternating, biased, correlated, mixed, and one large trace with 19.8K static branches) plus two real programs, lu_cb and lu_ncb, with 6 × 5M branches each. The custom design is ranked on hidden real-program traces, so the goal was to generalise rather than tune to the traces I had.
Standard predictors
I implemented four classic designs: a 1-bit last-outcome table, a 2-bit saturating-counter table, gshare (PC XOR global history), and a tournament predictor that uses a chooser to pick between a local-history and a global-history component. A sweep script rewrote each predictor's size constants, rebuilt it and ran every trace in parallel, which produced the curves below.
On the synthetic traces, more storage keeps helping. On the real traces every curve goes flat after about 512 entries, because the real programs have only about 2.7K static branches. gshare's jump at 32K bits comes from aliasing between hot branches.
The wall: every standard design gets stuck at about 11.6 MPKI on lu_cb. A single loop branch makes up 83% of that trace and runs 16 iterations (taken 15 times, then not taken). Predicting the exit needs at least 16 bits of history, while these predictors keep 14 bits or fewer. Adding table entries cannot fix that.
My design: TAGE + loop predictor
lu_cb needs at least 16 bits of history (about 272 to cover its outer loop), and lu_ncb needs about 250. TAGE provides long histories cheaply by spreading them over a few tagged tables whose history lengths grow geometrically. A loop predictor then removes the one miss per loop run that history alone leaves behind.
Bimodal base
16K two-bit counters. It handles the easy, strongly biased branches and is the fallback whenever no tagged table matches.
Six tagged tables
256 entries each, with an 11-bit tag, a 3-bit counter and a useful bit. History lengths are 2, 6, 17, 48, 139 and 400. The longest table whose tag matches provides the prediction.
Loop predictor
128 entries, 4-way, each recording a loop's trip count, current iteration, confidence, age and direction. An entry overrides TAGE only after the same trip count has been seen three times in a row.
History and indexing
A 512-bit circular history buffer with folded histories updated in O(1) per branch, needed because one register holds only 64 bits. Every index uses pc ^ (pc >> 2), so both 4-byte-aligned and unaligned PCs reach the whole table.
The maths behind the predictors
All results use one metric. The simulator replays a trace of conditional branches, and for each one the predictor guesses the direction before seeing the outcome:
Saturating counters. The 2-bit predictor keeps a counter \(c \in \{0,1,2,3\}\) per table entry, predicts taken when \(c \ge 2\), and moves one step toward each outcome:
gshare XORs the branch address with the global history register to spread correlated outcomes across the table:
- \(\mathit{GHR}\)
- the last 14 branch outcomes, one bit each
- \(\oplus\)
- bitwise XOR
- \(\gg\)
- right shift
Why every Part A design stalls on lu_cb. Its hot branch follows the periodic pattern \((1^{15}0)^\ast\): taken 15 times, then not taken. To predict the 0, a history-based predictor must tell apart the 16 positions inside one period, which is only possible if its history reaches back to the previous exit. That needs a history length \(L \ge 16\), while gshare and the tournament predictor use at most 14 bits. More table entries cannot help.
TAGE. Table \(T_i\) (\(i = 1,\dots,6\)) is indexed and tagged by a hash of the PC and the most recent \(L_i\) history bits, with history lengths in a geometric series:
Folded histories. A 400-bit history cannot be hashed from scratch on every branch. Each table keeps the history folded (XORed) down to \(n\) bits, its index width, and updates it in \(O(1)\) when a new outcome \(h_0\) arrives and the outcome \(h_{L}\) leaves the window:
Loop predictor. An entry records a trip count \(N\), the current iteration \(k\) and a confidence \(c\). It overrides TAGE only when confident:
Storage budget. Every bit is accounted for against the 64 Kbit limit:
Finally, every index uses the mixed address \(h(\mathit{pc}) = \mathit{pc} \oplus (\mathit{pc} \gg 2)\). The synthetic traces have 4-byte aligned PCs, so their two low bits are always 0, and plain \(\mathit{pc} \bmod 2^n\) would only ever reach a quarter of each table.
What the experiments showed
Without the loop predictor, the real traces improve only once the longest history reaches about 250 to 280. With it, MPKI stays near 0.1 at every length. I chose 400 because it sits on a broad plateau rather than at a point tuned to one trace.
- The loop predictor matters most. Adding it took
lu_ncbfrom 0.15 to 0.08 MPKI andlu_cbfrom 0.79 to 0.11. - The size of TAGE barely affects real traces. Going from 4 to 9 tables, or using 6- to 10-bit tags, changed real MPKI by at most 0.02. The
largetrace is limited by capacity (the base table supplies 97.6% of its predictions), so I moved bits from the tagged tables to the base table. - Mixing the PC bits fixed a hidden 4× capacity loss. Synthetic PCs are 4-byte aligned, so
pc & maskreached only a quarter of each table. Mixing the bits cutlargefrom 81.5 to 64.7 MPKI. - A subtle loop-predictor bug. Some entries learned the wrong direction and then became confident in a trip count of 0. Now an exit after 0 iterations flips the entry's direction. After that fix, real MPKI stays between 0.090 and 0.095 for any table size or number of ways.
| Design step | Bits | Syn | large | lu_cb | lu_ncb |
|---|---|---|---|---|---|
| TAGE, 3 tables, history ≤ 64 | 45,120 | 27.59 | 81.05 | 0.86 | 0.93 |
| 8 tables, history 4–200 | 57,862 | 26.19 | – | 0.81 | 0.91 |
| History 2–400 | 58,119 | 25.90 | – | 0.79 | 0.15 |
| + loop predictor | 62,734 | 24.91 | 83.57 | 0.11 | 0.08 |
| 16K base, 6 × 256 tagged | 61,180 | 24.73 | 81.47 | 0.10 | 0.09 |
| + PC mix (final) | 61,180 | 21.71 | 64.66 | 0.10 | 0.08 |
Ablation: MPKI after each design step.
Full results
All eight designs on every trace, including three others I tried for the custom part (bi-mode, perceptron and a plain 3-table TAGE). The best value in each column is underlined.
| Synthetic traces | Real | Mean | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Predictor | Bits | Loops | Altern. | Biased | Correl. | Mixed | Large | lu_cb | lu_ncb | Syn | Real |
| 1-bit | 65,536 | 71.81 | 90.33 | 23.71 | 100.06 | 65.15 | 71.45 | 23.82 | 1.99 | 70.42 | 12.90 |
| 2-bit | 65,536 | 35.91 | 83.99 | 13.81 | 100.19 | 46.60 | 58.79 | 11.95 | 1.11 | 56.55 | 6.53 |
| gshare | 32,782 | 8.47 | 11.76 | 25.16 | 29.38 | 35.03 | 85.41 | 22.29 | 1.03 | 32.53 | 11.66 |
| Tournament | 59,405 | 2.83 | 9.11 | 13.51 | 29.12 | 18.75 | 85.06 | 11.59 | 0.96 | 26.40 | 6.28 |
| Bi-mode | 49,165 | 8.39 | 11.55 | 15.01 | 27.47 | 28.56 | 77.29 | 11.62 | 1.01 | 28.05 | 6.31 |
| Perceptron | 59,168 | 4.94 | 8.34 | 12.03 | 27.69 | 15.80 | 71.74 | 1.03 | 1.00 | 23.42 | 1.01 |
| TAGE (3 tables) | 45,120 | 2.85 | 12.45 | 17.01 | 27.35 | 24.85 | 81.05 | 0.86 | 0.93 | 27.59 | 0.89 |
| Mine: TAGE + loop | 61,180 | 0.04 | 8.74 | 12.97 | 26.72 | 17.10 | 64.66 | 0.10 | 0.08 | 21.71 | 0.09 |
MPKI, lower is better. Real-trace columns are means over 6 traces of 5M branches each. The final design still trails a full-budget 2-bit table on large (64.7 vs 58.8), which is limited by capacity.