Better Predictors: Global History, Gshare and Local History
A 2-bit counter knows one thing about a branch: which way it mostly goes. A branch that alternates has no "mostly". This page gives the predictor a memory of the last outcomes.
You Will Be Able To
- Explain what a history register holds, and how it changes after each branch.
- Work out which counter gshare uses, from an address and a history.
- Say when global history, local history, or no history is the best choice.
- Lesson
- S2.12
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
In S2.11 one branch was taken, not taken, taken, not taken. A 2-bit counter got it wrong 40 times in 40. Yet anybody who sees the last outcome can tell the next one.
Before You Start
You need the 2-bit counter from S2.11, and the counter table with its index from S2.27.
You need the XOR of two numbers, bit by bit, and a shift register.
| Question | |
|---|---|
| 1 | What does a history register hold? |
| 2 | How does gshare choose its counter? |
| 3 | What is the difference between global and local history? |
| 4 | Why can a predictor with history be worse than a plain counter table? |
| 5 | How many outcomes should a predictor remember? |
The Idea: The Last Outcomes Say What Comes Next
Keep the last few outcomes in a shift register: 1 for taken, 0 for not taken. After each branch, shift the new outcome in. This is the history register, and what it holds is the branch history.
Now use the history to choose the counter. A branch then has several counters, one for each history it meets. Each counter learns what the branch does after that one history.
An alternating branch needs two of them. After "taken" the counter learns "not taken". After "not taken" the other one learns "taken". Both are soon right every time.
One history register can serve all branches. Then it holds what the last branches of the whole program did. That is global history. It also catches a branch that depends on another branch.
How do the address and the history choose one counter together? The common answer is an XOR of the two. The predictor built that way is called gshare.
The other way is a history for each branch by itself: local history. It sees the pattern of one branch, and nothing of the others.
New Words
| Word | Meaning |
|---|---|
| Branch history | The outcomes of the last branches, in order: taken or not taken. |
| History register | A shift register that holds the branch history. Each decided branch shifts one bit in. |
| Global history | One history for all branches of the program together. |
| Local history | A history for each branch by itself. Here: for each entry of a small table. |
| gshare | A predictor whose counter is chosen by the XOR of address bits and the global history. |
| Correlated branches | Branches whose outcomes depend on each other, so that one tells something about the next. |
One Picture: Address XOR History
- Processor
- Memory
- Control / FSM
- Two inputs
The branch at 0c would use entry 3 of a plain table. The history register holds the last four outcomes: T N T T.
- One counter
T N T T is 1011 in bits. Entry 3 is 0011. The XOR of the two is 1000: counter 8.
- The guess
Counter 8 gives the guess. It is the counter for "this branch, after exactly these four outcomes".
- Learning
When the branch is decided, counter 8 counts up or down. Then the outcome is shifted into the history register, and the oldest outcome falls out.
Read the diagram as text
- PC: the address of the branch: its low bits
- History register: the last 4 outcomes of all branches
- XOR: mixes the two, bit by bit
- Counter table: sixteen 2-bit counters
- Guess: taken at a count of 2 or 3
- Outcome: what the branch really did, from EX
- PC → XOR: entry 3
- History register → XOR: T N T T
- XOR → Counter table: counter 8
- Counter table → Guess: the count
- Outcome → Counter table: count up or down
- Outcome → History register: shift in
The same branch uses different counters after different histories. Each counter learns what follows one history.
| Predictor | What chooses the counter | Bits in this lab |
|---|---|---|
| 2-bit table | the address | 16 counters: 32 |
| gshare | the address XOR the last 4 outcomes of all branches | 16 counters and 4 history bits: 36 |
| Local history | the last 4 outcomes of this branch | 16 histories of 4 bits and 16 counters: 96 |
gshare costs almost nothing more than the plain table. The local predictor needs a table of histories as well.
The Smallest Example, Worked by Hand
Take a branch that alternates: T, N, T, N. Give it a history of one outcome, and one counter for each history. Both counters start at 1, and the history starts at N.
| Time | History | Counter used | Says | It did | The counter after |
|---|---|---|---|---|---|
| 1 | N | the counter "after N": 1 | N wrong | T | 2 |
| 2 | T | the counter "after T": 1 | N right | N | 0 |
| 3 | N | the counter "after N": 2 | T right | T | 3 |
| 4 | T | the counter "after T": 0 | N right | N | 0 |
| 5 | N | the counter "after N": 3 | T right | T | 3 |
| 6 | T | the counter "after T": 0 | N right | N | 0 |
One wrong guess, then right every time. The plain 2-bit counter of S2.11 was wrong every time.
Each counter sees only one kind of moment. The counter for the history N always meets a taken branch, so it climbs to 3. The counter for the history T always meets the other outcome, so it falls to 0.
Neither counter has a mixed habit any more. The history has split one hard branch into two easy ones.
The Rules
Rule 1: The history register shifts in one bit for each decided branch.
The newest outcome is at one end. The oldest falls out at the other.
Rule 2: A counter is chosen by the address and the history together.
In gshare: the entry of the plain table, XOR the history bits.
Rule 3: Each counter learns what follows one history.
It is updated like any 2-bit counter: up for taken, down for not taken.
Rule 4: Global history is shared by all branches.
It can catch a branch that depends on other branches. Any branch without a pattern scrambles it for all.
Rule 5: Local history belongs to one branch.
It catches the pattern of that branch, whatever the others do.
Rule 6: Every history that a branch meets needs a warm-up of its own.
More history bits mean more counters in use for each branch, and more first times.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8, part "Dynamic Branch Prediction". It describes correlating predictors, which use the behaviour of recently executed branches as well as the branch itself. It says that the global behaviour acts like extra index bits. Harris and Harris (ARM edition), section 7.7.3, says only that more history can raise the accuracy. The name gshare, the XOR of address and history, and the two-level form of the local predictor are not in these two books. They are described here as commonly taught, and they are not checked against a source. The sizes are choices of this lab. Every count is recorded from the lab. The branches come from the real core ap_rv32, which has no predictor: docs/soc/ap_rv32-blueprint.md, section 8.4.
Now Run It
The lab at the end of this page is test code. It replays the recorded branches with three predictors: the 2-bit table of S2.27, gshare, and local history.
00 addi x5, x0, 40
04 addi x9, x0, 0
08 loop: andi x6, x5, 1
0c beq x6, x0, even
10 addi x9, x9, 1
14 even: andi x7, x5, 3
18 bne x7, x0, skip
1c addi x9, x9, 16
20 skip: beq x6, x7, same
24 addi x9, x9, 256
28 same: addi x5, x5, -1
2c bne x5, x0, loop
30 ebreakThe first part follows the branch at 0c of "pattern". It is taken in every second round of a loop. The loop has four branches, so the history shows one whole round.
n | did | last | counter | gshare says
1 | T | NNNN | 3 | N wrong
2 | N | TNTT | 8 | N right
3 | T | NTNT | 6 | N wrong
4 | N | TTNT | 14 | T wrong
5 | T | NTTT | 4 | N wrong
6 | N | TNTT | 8 | N right
7 | T | NTNT | 6 | T right
8 | N | TTNT | 14 | N right
9 | T | NTTT | 4 | T right
10 | N | TNTT | 8 | N right
the branch at 0c: 40 times | 2-bit table right=0 | gshare right=36 | local history right=37Rule 2 at work: read row 2. The history is T N T T, and the counter is 8. Rows 6 and 10 have the same history and use the same counter.
Rule 3 at work: rows 3 and 7 both have the history N T N T and use counter 6. In row 3 it has not learned yet. In row 7 it says taken, and it is right.
From row 6 on, every guess is right. In all, gshare gets 36 of 40. The 2-bit table gets 0.
Row 4 is wrong for another reason. Counter 14 had been pushed towards taken by another branch that landed on it. gshare has aliasing too.
The second part counts all branches of the three traces.
trace | branches | 2-bit table | gshare | local history
loops | 132 | 111 | 103 | 102
pattern | 160 | 77 | 139 | 139
calls | 80 | 27 | 63 | 57On "pattern", both predictors with history get 139 of 160, or 87 percent. The plain table gets 77.
Rules 4 and 5 at work: "calls" has one branch that follows a scrambled number. It fills the global history with noise, yet gshare still gets 63 of 80. Local history gets 57.
Rule 6 at work: on "loops" the plain table wins, 111 against 103 and 102. Its branches have no pattern to find, and every extra counter only adds first times.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| XOR of address and history | Put address bits and history bits side by side | All index bits carry both. Two different pairs can give the same counter. |
| One global history | A history for each branch | A few bits in all, and it sees how branches depend on each other. One noisy branch disturbs every other. |
| Local history | Global history | The pattern of one branch stays clean. It needs a table of histories, read before the counter. |
| Shift in the real outcome | Shift in the guess | The history is always true. It is known only when the branch is decided, two cycles after the fetch. |
| A short history | A long history | A short warm-up. Patterns longer than the history are not seen. |
The last row of the table has no best answer. That is the trap of this page.
The Trap: A Longer History Can Be Worse
If four outcomes of history are good, six should be better. Try it: gshare with 64 counters, and a history of 0 to 6 outcomes.
gshare with 64 counters: right guesses for each length of its history
outcomes remembered | loops | pattern | calls
0 | 111 | 77 | 27
1 | 109 | 115 | 46
2 | 108 | 96 | 56
3 | 107 | 110 | 55
4 | 103 | 139 | 64
5 | 97 | 138 | 61
6 | 90 | 136 | 59- loops
- pattern
- calls
- Outcomes that gshare remembers 0loops = 84 · pattern = 48 · calls = 34
No history: this is the plain counter table. "loops" is at its best here, and the other two are poor.
- Outcomes that gshare remembers 4loops = 78 · pattern = 87 · calls = 80
Four outcomes: "pattern" and "calls" reach their best. Their branches repeat with a period of four rounds or less.
- Outcomes that gshare remembers 6loops = 68 · pattern = 85 · calls = 74
Six outcomes: every trace is lower than at four. Each branch now spreads over more counters, and each counter must be trained first.
Read the plot as a table
| Outcomes that gshare remembers | loops | pattern | calls |
|---|---|---|---|
| 0 | 84 | 48 | 34 |
| 1 | 83 | 72 | 58 |
| 2 | 82 | 60 | 70 |
| 3 | 81 | 69 | 69 |
| 4 | 78 | 87 | 80 |
| 5 | 73 | 86 | 76 |
| 6 | 68 | 85 | 74 |
More history helps a branch that follows a pattern. It costs a branch that has none, in a run as short as these.
Read the column of "loops". It only falls: 111 right guesses with no history, 90 with six outcomes.
The branches of "loops" are simple. With six outcomes of history, each of them meets many different histories. Each history has a counter of its own that starts untrained.
The other two traces peak at four outcomes and fall a little after that. Their patterns are four rounds long or less.
So no single history length is right for all branches. S2.28 lets two predictors run side by side, and picks one of them for each branch.
At Scale
This section looks ahead. The traces here have 80 to 160 branches. A real program runs billions, and a warm-up of a few hundred no longer counts.
Not on ap_rv32
The core ap_rv32 has no predictor. These counts say what a predictor beside its fetch would have guessed for the branches it executed.
Long runs forgive long histories
In a long run each counter is trained once and then used many times. So real predictors afford much longer histories than this lab. That is common practice.
The history is late
A branch is decided two cycles after it is fetched. A real fetch shifts its guess into the history at once and repairs the history after a wrong guess. That is common practice, and not built here.
Which predictor for which branch
Some branches want history and some do not. S2.28 keeps both predictors and learns which to trust for each branch.
Try It
Run the lab. In the first table, find two rows with the same history. Do they use the same counter?
Set BITS to 1 and run again. How many of its 40 times does gshare get right for the branch at 0c now? Why is one outcome of global history not enough here?
Set LOCAL to 1. Which trace loses most for the local predictor?
n | did | last | counter | gshare says 1 | T | NNNN | 3 | N wrong 2 | N | TNTT | 8 | N right 3 | T | NTNT | 6 | N wrong 4 | N | TTNT | 14 | T wrong 5 | T | NTTT | 4 | N wrong 6 | N | TNTT | 8 | N right 7 | T | NTNT | 6 | T right 8 | N | TTNT | 14 | N right 9 | T | NTTT | 4 | T right 10 | N | TNTT | 8 | N right the branch at 0c: 40 times | 2-bit table right=0 | gshare right=36 | local history right=37 trace | branches | 2-bit table | gshare | local history loops | 132 | 111 | 103 | 102 pattern | 160 | 77 | 139 | 139 calls | 80 | 27 | 63 | 57Check Yourself
Common Mistakes
Wrong Update the counter that the new history points to.
The guess came from the counter chosen by the old history. That one must learn the outcome.
Fix Update the counter that was used, then shift the history.
Wrong Make the history as long as the table allows.
Each branch then spreads over many counters, and each must be trained before it helps.
Fix Choose the length by measuring on real traces.
Wrong Expect global history to find the pattern of every branch.
It holds the outcomes of all branches. A branch without a pattern fills it with noise.
Fix Use local history where a branch must be seen alone.
Remember
- A history register holds the outcomes of the last branches. Each decided branch shifts one bit in.
- gshare chooses its counter by the XOR of address bits and the global history. Local history uses the outcomes of the branch itself.
- Each counter learns what follows one history. An alternating branch becomes two steady ones.
- On the recorded traces: "pattern" 77 right with a plain table, 139 with history. "loops" 111 with the plain table, 103 with gshare. ap_rv32 has no predictor.