Tournament Predictors, the Return Stack and the Bit Budget
S2.12 ended with two predictors, each good at other branches. This page lets them compete for every branch. It then repairs the one target that no table can remember, and counts what all of it costs.
You Will Be Able To
- Explain how a chooser picks between two predictors, and when it changes its mind.
- Follow a return stack through calls and returns, and say what a stack that is too small loses.
- Count the bits of a predictor, part by part.
- Lesson
- S2.28
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
A function is called from two places. Its return goes back to the first place, then to the second. A table that remembers the last target is wrong both times. Yet the core itself knows the answer: it saved the return address at the call.
Before You Start
You need the 2-bit table and gshare from S2.12, and the branch target buffer from S2.27.
You need calls, returns and the return address from S2.3, and a stack: the last thing put in is the first taken out.
| Question | |
|---|---|
| 1 | How does a tournament predictor decide which of its two predictors to use? |
| 2 | When does a chooser move? |
| 3 | Why is a branch target buffer bad at returns? |
| 4 | What does a return stack hold, and what happens when it is full? |
| 5 | Which part of a predictor costs the most bits? |
The Idea: Let Them Compete, and Remember the Calls
Some branches want a plain counter, and some want history. So keep both predictors, and ask both. A third small table decides whose guess is used.
That third table holds a chooser for each entry. It is one more 2-bit counter. It counts towards the predictor that was right when the other was wrong. The whole thing is a tournament predictor.
The direction is now well served. One target is not: the target of a return. A function returns to wherever it was called from, and that changes from call to call.
The fix uses what a call already does. A call stores its return address. Let the fetch store it too, on a small stack of its own. This is the return stack.
A call puts its return address on the stack. That is a push. A return takes the newest address off again. That is a pop. Calls and returns come in pairs, so the popped address is the right target.
All these tables cost bits, and a chip has a limit for them. The sum of all stored bits is the bit budget of the predictor.
New Words
| Word | Meaning |
|---|---|
| Tournament predictor | Two predictors and a chooser that picks one of the two guesses for each branch. |
| Chooser | A 2-bit counter that says which of two predictors to trust. It moves towards the one that was right alone. |
| Return stack | A small stack beside the fetch that holds the return addresses of the calls that have not returned yet. |
| Bit budget | The number of stored bits that a predictor may use, all tables together. |
One Picture: Who Guesses What
- Memory
- Control / FSM
- Processor
- Two guesses
Both predictors are asked for every branch. Often they agree. Sometimes they do not.
- The chooser
A third table holds a 2-bit counter for each entry. At 0 and 1 it passes on the guess of the plain table. At 2 and 3 it passes on the guess of gshare.
- The target of a branch or a call
The branch target buffer gives the address where this instruction went the last time.
- The target of a return
The buffer also remembers that an entry belongs to a return. Then the target is taken from the top of the return stack.
Read the diagram as text
- 2-bit table: guesses from the address alone
- gshare: guesses from the address and the last outcomes
- Chooser: a 2-bit counter for each entry: which of the two to trust
- Branch target buffer: the last target, and a mark for a return
- Return stack: the return addresses of the calls that are still open
- Next PC: a target, or the PC plus 4
- 2-bit table → Chooser: guess 1
- gshare → Chooser: guess 2
- Chooser → Next PC: taken?
- Branch target buffer → Return stack: this is a return
- Branch target buffer → Next PC: the last target
- Return stack → Next PC: the top address
The direction comes from a tournament of two predictors. The target comes from the target buffer, or for a return from the stack.
| Part | Size | Bits |
|---|---|---|
| 2-bit table | 16 counters | 32 |
| gshare | 16 counters and 4 history bits | 36 |
| Choosers | 16 counters | 32 |
| Branch target buffer | 32 entries of 59 bits: valid, a tag of 25 bits, a target of 32 bits, the mark for a return | 1888 |
| Return stack | 4 addresses of 32 bits | 128 |
| All together | 2116 |
The whole direction predictor is 100 bits: about 5 percent. The targets are the expensive part.
The Smallest Example, Worked by Hand
The program "calls" calls a function f from two places, at 10 and at 18. Then it calls a second function g, at 1c. Follow the stack.
| Step | What happens | The stack after it | The return goes to | The target buffer would say |
|---|---|---|---|---|
| 1 | the call at 10: push 14 | 14 | ||
| 2 | f returns: pop | empty | 14 | nothing yet |
| 3 | the call at 18: push 1c | 1c | ||
| 4 | f returns: pop | empty | 1c | 14: wrong |
| 5 | the call at 1c: push 20 | 20 | ||
| 6 | g returns: pop | empty | 20 | nothing yet |
The addresses are those of the recorded trace. In the next round, step 2 goes to 14 again, and the target buffer says 1c.
The return of f goes to 14 and to 1c in turn. The target buffer always holds the other one. The stack holds the right one, because the call just before put it there.
The Rules
Rule 1: Both predictors guess for every branch, and both learn from every branch.
The chooser only selects which guess is used.
Rule 2: A chooser moves only when exactly one predictor was right.
It counts towards that one. When both were right or both wrong, it stays.
Rule 3: A call pushes the address behind it. A return pops.
The popped address is the guess for the target of the return.
Rule 4: The fetch must know that an instruction is a return.
The branch target buffer marks the entry. So the first time a return is met, the stack is not used.
Rule 5: A full stack loses its oldest address at the next push.
A return that finds the stack empty falls back on the target buffer.
Rule 6: Every stored bit counts against the budget.
A target costs far more bits than a direction.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8. It describes tournament predictors: several predictors for a branch, and a selector that works like a 1-bit or 2-bit predictor and favours the one that was more accurate. Its section 4.11 lists a return stack of 8 entries in one real core. How a return stack is pushed and popped is not described in the books of this library. It is given here as commonly taught, and it is not checked against a source. The sizes, the fallback to the target buffer, and the rule that a full stack loses its oldest address are choices of this lab. The cycle counts are sums from the measured cycles of ap_rv32, which has none of these parts: docs/soc/ap_rv32-blueprint.md, sections 8.4 and 14.2.
Now Run It
The lab at the end of this page is test code. It holds the whole predictor of the picture and replays three recorded traces.
The first part follows the alternating branch at 0c of "pattern", and shows the tournament at work.
n | did | table says | gshare says | chooser | tournament says
1 | T | N | N | 1 | N wrong
2 | N | T | N | 1 | T wrong
3 | T | N | N | 2 | N wrong
4 | N | T | T | 2 | T wrong
5 | T | N | N | 2 | N wrong
6 | N | T | N | 2 | N right
7 | T | N | T | 3 | T right
8 | N | T | N | 3 | N rightRule 2 at work: read row 2. The table says taken and is wrong. gshare says not taken and is right. In row 3 the chooser has moved from 1 to 2.
From row 3 on the chooser passes on the guess of gshare. gshare is still warming up in rows 3 to 5. From row 6 on it is right, and so is the tournament.
Rule 1 at work: the plain table goes on learning, although nobody listens to it for this branch.
trace | branches | 2-bit table | gshare | tournament
loops | 132 | 111 | 103 | 111
pattern | 160 | 77 | 139 | 147
calls | 80 | 27 | 63 | 62On "loops" the tournament equals the plain table. On "pattern" it is better than both of its parts: 147 against 77 and 139. It uses the table for one branch and gshare for another.
On "calls" it gets 62, one fewer than gshare alone. The choosers need a warm-up of their own.
the returns of calls, fetched right
stack depth | returns | right
0 | 60 | 19
4 | 60 | 58Rules 3 and 4 at work: without the stack, 19 returns are right. Those are the returns of g, which has one caller. With the stack, 58 are right. Only the first time of each of the two returns is lost.
trace | ap_rv32, measured | with the predictor | and the return stack
loops | 515 | 329 | 329
pattern | 554 | 362 | 362
calls | 813 | 603 | 525For "calls", the tournament and the target buffer bring 813 cycles down to 603. The return stack brings 603 down to 525. It makes 39 more returns right, and each saves 2 cycles.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two predictors and a chooser | One larger predictor | Each branch gets the kind of predictor that suits it. A third table, and a warm-up for it. |
| The chooser moves only when the two differ in being right | Move it after every branch | It measures which one is better, not how good both are. It learns nothing while the two agree. |
| A stack for returns | A larger target buffer | A return gets the address of its own call. 128 bits here, and it must be told what is a call and what a return. |
| A full stack drops the oldest address | Refuse the push | The newest calls return first, and they stay right. The outermost returns are lost. |
| Mark returns in the target buffer | Decode the instruction first | The guess is ready in IF. One more bit for each entry, and the first time is always missed. |
None of this is in ap_rv32. Its fetch has no table at all, and the hand table above shows why a real fetch wants one.
The Trap: A Return Stack That Is Too Small
A fourth recorded program, "nest", has calls three deep. The main loop calls a function a from two places. a calls b, and b calls c.
00 addi x5, x0, 6
04 addi x7, x0, 0
08 loop: jal x1, a
0c addi x7, x7, 16
10 jal x1, a
14 addi x5, x5, -1
18 bne x5, x0, loop
1c ebreak
20 a: addi x20, x1, 0
24 jal x1, b
28 addi x1, x20, 0
2c jalr x0, 0(x1)
30 b: addi x21, x1, 0
34 jal x1, c
38 addi x1, x21, 0
3c jalr x0, 0(x1)
40 c: addi x7, x7, 1
44 jalr x0, 0(x1)Try stacks of several depths.
the returns of nest, fetched right
stack depth | returns | right
0 | 36 | 22
1 | 36 | 22
2 | 36 | 22
4 | 36 | 33
trace | ap_rv32, measured | with the predictor | and the return stack
nest | 310 | 196 | 174Without a stack, 22 of the 36 returns are right. With a stack of depth 1 or of depth 2: still 22. The stack seems to do nothing.
Rule 5 at work: the third call finds a stack of depth 2 full. The oldest address is lost, and that is the return address of a.
The returns of c and b then find their addresses. But those two never needed a stack: each has one caller. The return of a, the only one with two callers, finds the stack empty.
So the stack that is too small helps exactly the returns that needed no help. With a depth of 4, 33 returns are right, and 310 measured cycles become 174 in the sum.
At Scale
This section looks ahead. The predictor of this page has 2116 bits. Real ones are larger, and the proportions stay.
Not on ap_rv32
The core ap_rv32 has no predictor, no target buffer and no return stack. The cycles of the row "measured" are real. Every other cycle count on this page is a sum.
How deep are real calls
Real programs call much deeper than three. A stack of a few entries still helps, because the innermost calls return first and most often. That is common practice.
Guessing ahead of the outcome
A real fetch pushes and pops as it guesses. After a wrong guess the stack must be put right again. That is common practice, and not built here.
The other jumps through a register
A jalr that is not a return can go to many places, as in a switch statement. Real cores keep one more predictor for those.
Try It
Run the lab. In the first table, find the row in which the chooser first trusts gshare.
In the line that sets the chooser, remove the condition, so that it moves after every branch towards gshare being right. Which trace loses most?
Change the depth of the return stack in the last three replay lines from 4 to 1. Does any cycle count change? Why not, for these three programs?
n | did | table says | gshare says | chooser | tournament says 1 | T | N | N | 1 | N wrong 2 | N | T | N | 1 | T wrong 3 | T | N | N | 2 | N wrong 4 | N | T | T | 2 | T wrong 5 | T | N | N | 2 | N wrong 6 | N | T | N | 2 | N right 7 | T | N | T | 3 | T right 8 | N | T | N | 3 | N right trace | branches | 2-bit table | gshare | tournament loops | 132 | 111 | 103 | 111 pattern | 160 | 77 | 139 | 147 calls | 80 | 27 | 63 | 62 the returns of calls, fetched right stack depth | returns | right 0 | 60 | 19 4 | 60 | 58 trace | ap_rv32, measured | with the predictor | and the return stack loops | 515 | 329 | 329 pattern | 554 | 362 | 362 calls | 813 | 603 | 525Check Yourself
Common Mistakes
Wrong Update only the predictor that the chooser picked.
The other one stops learning, and the chooser can never find out that it has become better.
Fix Let both learn from every branch.
Wrong Predict a return with the branch target buffer alone.
It holds where the function returned the last time. With two callers that is the wrong place.
Fix Push at each call, and pop at each return.
Wrong Size the return stack by the number of functions.
What counts is how many calls are open at the same time.
Fix Size it by the depth of the calls, and expect to lose the outermost returns beyond that.
Remember
- A tournament predictor asks two predictors and lets a 2-bit chooser pick. The chooser moves only when exactly one of the two was right.
- A return stack is pushed by a call and popped by a return. It finds return addresses that a target buffer cannot.
- A full stack loses its oldest address. A stack that is too small loses the returns of the outermost calls.
- Count every stored bit. Here the direction takes 100 bits and the targets 1888. For "calls": 813 cycles measured on ap_rv32, 525 as a sum with the whole predictor.