Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Branch Predictor Simulator

Prerequisites: The CPU Pipeline and Branch Prediction, Branches, Indirect Calls, and Inlining

The AAPL update loop tests if (update.price > threshold) on every update. The CPU Pipeline and Branch Prediction described a predictor that guesses each branch's direction from its history; every wrong guess flushes the pipeline.

Build two such predictors and run them on a branch trace.

  • Branch trace: a record of every conditional branch a program executed, in order.
  • pc (program counter): the address of the branch instruction. Every if in the source has its own pc, and each trace entry names its branch by pc.
  • Outcome: T if the branch was taken, N if it was not.

What to implement

  • simulate_predictor(predictor, index_bits, history_bits, trace) returns the printed lines, in order.
  • predictor is BIMODAL, GSHARE, or BOTH. index_bits is k. history_bits is h, the width of the history register from Part 2.
  • Each line of trace is one entry, in one of the forms below.
  • On stdin, the first three lines are predictor, index_bits, and history_bits. The fourth is the number of trace entries, and each remaining line is one entry.
  • There are three parts, each adding behavior on top of the previous one. One solution must pass all ten tests.
Trace lineMeaning
pc outcomeone branch at pc with outcome T or N
LOOP pc nshorthand for n branches at pc (Part 3)

2-bit saturating counters

Both predictors are built from 2-bit saturating counters. Each holds an integer from 0 to 3, and its value is a state that decides the prediction:

ValueStatePredicts
0strongly not-takenN
1weakly not-takenN
2weakly takenT
3strongly takenT
  • Once the real outcome is known, the counter trains: a taken branch adds 1 unless it is already 3, and a not-taken branch subtracts 1 unless it is already 0.
  • The counter saturates at both ends instead of wrapping, so a branch must surprise a strong state twice in a row before the prediction flips.
  • A predictor keeps a table of 2^k counters, numbered 0 to 2^k - 1. Every counter starts at 1, weakly not-taken.

For each branch, the predictor:

  1. computes an index into its table;
  2. reads the counter there;
  3. predicts T if the counter is 2 or 3, N if it is 0 or 1;
  4. compares the prediction with the outcome and trains the counter.

Part 1: The bimodal predictor

  1. The bimodal predictor indexes its table by the branch's address alone: index = pc mod 2^k, the low k bits of pc, or pc & (2^k - 1).
  2. Each branch therefore trains the counter at its own index. A branch that is usually taken pushes its counter toward 3.
  3. Two branches whose pc values share their low k bits read and train the same counter. This is aliasing, and the simulator does not separate them: if one is usually taken and the other usually not, each undoes the other's training.

Output (BIMODAL and GSHARE modes): one line per branch, then a TOTAL line after the last branch.

pc index counter prediction outcome result
TOTAL branches MISSES misses
  • counter is the value read before training.
  • result is HIT when the prediction matched the outcome and MISS when it did not.
  • branches is the number of branches; misses is the number of mispredictions.

Limits

  • k is between 1 and 12. h is between 0 and k, and it is 0 whenever predictor is BIMODAL.
  • Every pc is between 0 and 109, so pc and every value computed from it fit in a 32-bit int.
  • The trace has between 1 and 104 lines, and every outcome is exactly T or N.
  • The whole trace expands to at most 105 branches.

Tests 1–3 cover this part. They use BIMODAL with history_bits equal to 0.

Part 2: Gshare and the global history register

A bimodal counter learns only a branch's usual direction. It mispredicts at least half of an alternating run T, N, T, N, which is what the price check produces when the price hovers around threshold.

Gshare adds a global history register: an h-bit integer whose bits are the outcomes of the last h branches executed, from any pc (1 for taken, 0 for not taken).

  1. The newest outcome is bit 0. The register starts at 0, as if the last h branches were not taken.
  2. The index is index = (pc ^ history) mod 2^k, where ^ is bitwise XOR: each bit of the result is 1 where exactly one of the two input bits is 1.
  3. The index uses the history from before the current branch, because the predictor must guess before the outcome is known.
  4. After the counter trains, the outcome enters the register: history = ((history << 1) | bit) mod 2^h, where bit is 1 for T and 0 for N. The shift makes room for the new bit; mod 2^h drops the oldest.
  5. The same branch can now train different counters, depending on what happened just before it.
  6. With h equal to 0 the history is always 0, and gshare behaves exactly like the bimodal predictor.

GSHARE mode prints the same per-branch line and TOTAL line as BIMODAL mode.

Tests 4–6 cover this part.

Part 3: Loops and a side-by-side comparison

The branch that closes a loop jumps back to the top on every iteration except the last, where the loop exits.

  1. LOOP pc n stands for n - 1 entries pc T followed by one entry pc N.
  2. It prints exactly what those n entries would print and counts as n branches. LOOP pc 1 is a single pc N.
  3. n is between 1 and 104. LOOP works in every mode.
  4. BOTH mode runs a bimodal predictor and a gshare predictor, with k index bits and h history bits, side by side on the same trace.
  5. Each has its own table of 2^k counters starting at 1. Only the gshare predictor has a history register.

Output (BOTH mode): one line per branch, then a TOTAL line after the last branch.

pc outcome bimodal_prediction bimodal_result gshare_prediction gshare_result
TOTAL branches BIMODAL bimodal_misses GSHARE gshare_misses

Tests 7–10 cover this part. Test 7 uses LOOP in GSHARE mode, and tests 8–10 use BOTH.

Example
Input
BIMODAL
2
0
7
1203 N
1203 T
1203 T
1203 T
1203 N
1203 T
1201 T
Output
1203 3 1 N N HIT
1203 3 0 N T MISS
1203 3 1 N T MISS
1203 3 2 T T HIT
1203 3 3 T N MISS
1203 3 2 T T HIT
1201 1 1 N T MISS
TOTAL 7 MISSES 4
Explanation

The first three lines are the predictor, k = 2, and h = 0, so the table has four counters, each starting at 1.

Branch 1203 reads counter 1203 mod 4 = 3. Its first outcome N matches the prediction, and the counter drops to 0.

The next two T outcomes are both mispredicted while the counter climbs from 0 to 2. The third T is predicted correctly and pushes the counter to 3, strongly taken.

The single N that follows is a miss, but it only lowers the counter to 2, so the prediction stays T and the next T is a hit.

Branch 1201 reads counter 1, which no other branch has trained, so it still predicts N and misses.