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

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?

Name it

The predictors

  • Bimodal: index a table of 2n 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 q=1−p, the stationary probabilities go as 1,p/q,(p/q)2,(p/q)3, and the accuracy is pP(predict taken)+qP(predict not taken).
  • 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 2n 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?