The Reorder Buffer: Retiring in Order, Speculation and Recovery
Instructions now finish in any order. Then one of them traps, or a branch turns out to be guessed wrong. Which results count? The core needs one place where order is put back.
You Will Be Able To
- Say why results must become final in program order, and how a reorder buffer does it.
- Read a cycle of the buffer: what enters, what is finished, what retires.
- Explain what a core must undo after a wrong branch guess, and what a store may not do before it retires.
- Lesson
- S2.32
- Time
- 40 min
- Runs on
- Your browser
- Check
- 6 questions
S2.31 let instructions overtake each other. S2.29 demanded a precise trap. The two only fit together through a buffer.
Before You Start
You need renaming from S2.31: the rename table, the free list, and instructions that start when ready.
You need the precise trap from S2.29 and the guess "not taken" from S2.10.
| Question | |
|---|---|
| 1 | Why must results become final in program order? |
| 2 | Which entry of the reorder buffer may retire? |
| 3 | What is speculation? |
| 4 | What is undone after a wrong guess? |
| 5 | When may a store write memory? |
The Idea: Finish in Any Order, Count in Program Order
A program is written as a sequence. A trap handler, and a debugger, expect that sequence: all of this done, none of that.
An out-of-order core breaks the sequence inside. So it keeps a list of every instruction in flight, in program order.
The list is the reorder buffer. An instruction enters at its tail when it is renamed. That step is called dispatch.
It executes whenever it can. Its result waits in a physical register. Its entry is marked as finished.
It leaves at the head, and only when it is the oldest and finished. To leave is to retire. Only then does its result count.
This also solves the branch. The core does not wait for a branch to be decided. It guesses and fetches on along the guess.
Executing instructions behind a guess is speculation. They are in the buffer, but none of them can retire before the branch.
If the guess was wrong, every entry younger than the branch is thrown away. That is a squash. Nothing they did had counted yet.
The real core of this track, ap_rv32, finishes instructions in order and needs no such buffer. What follows is a model.
New Words
| Word | Meaning |
|---|---|
| Reorder buffer | A list of the instructions in flight, in program order. Often written ROB. |
| Dispatch | To put a renamed instruction into the reorder buffer, at the tail. |
| Head, tail | The oldest entry of the buffer, and the place where the newest one is added. |
| Retire, retired | To leave the buffer at the head. From then on the result of the instruction is final. Also called commit. |
| Speculation | Executing instructions before it is known that they should run at all. |
| Squash | To throw away the entries behind a wrong guess, before any of them has retired. |
| Recovery | Putting the core back as it was at the branch: the buffer, the rename table and the free list. |
| Store buffer | A place where the address and data of a store wait until the store retires. |
One Picture: The Buffer and Its Two Ends
- Control / FSM
- Memory
- Processor
- Bus
- Enter in order
Renamed instructions take the next entries of the buffer, in program order. The newest is the tail.
- Execute in any order
An entry starts when its inputs are ready. When it is finished, its entry is marked.
- Leave in order
Only the oldest entry may leave, and only if it is finished. A finished younger one waits behind it.
- Only then is it final
At retirement a result belongs to the program: a store writes memory, and an old register place is freed.
- A guess, and its price
Behind a branch the fetch follows a guess. If the branch finishes and the guess was wrong, every younger entry is squashed.
Read the diagram as text
- Rename and dispatch: in program order, two in a cycle
- Reorder buffer: one entry for each instruction in flight, oldest at the head
- Execute: whenever the inputs are ready
- Retire: from the head, only if finished
- Registers and memory: what the program can see
- Branch guess: the fetch goes on along the guess
- Rename and dispatch → Reorder buffer: enter at the tail
- Reorder buffer → Execute: start when ready
- Execute → Reorder buffer: mark: finished
- Reorder buffer → Retire: leave at the head
- Retire → Registers and memory: now it counts
- Branch guess → Rename and dispatch: instructions behind a guess
- Execute → Branch guess: the guess was wrong: squash
The middle of the core runs in any order. Its two ends, dispatch and retirement, stay in program order.
| Field | Meaning | Used when |
|---|---|---|
| The instruction | Which row of the program | always |
| Finished in cycle | When its result is ready | at retirement: only a finished head may leave |
| The new place | The physical register it writes | at a squash: back to the free list |
| The old place | Where that register name was before | at a squash: back into the rename table. At retirement: freed |
| Guessed wrong | A branch whose guess will turn out wrong | when it is finished: squash what is behind it |
The old place answers the open question of S2.31. A physical register is given back when the write after it retires.
The Smallest Example, Worked by Hand
| Address | Instruction | Finished in cycle | Retires in cycle |
|---|---|---|---|
| 14 | lw x5, 0(x8) | 5 | 6 |
| 18 | add x6, x5, x9 | 6 | 7 |
| 1c | sw x6, 4(x8) | 7 | 8 |
| 20 | addi x5, x10, 1 | 6 | 8 |
| 24 | add x7, x5, x5 | 7 | 9 |
From a run of the model. The load at 14 takes 2 cycles. Two instructions may enter, and two may retire, in a cycle.
Look at the addi at 20. It needs only x10. It is finished in cycle 6, one cycle before the store at 1c.
May it retire in cycle 7? No. The store at 1c is older and is still in the buffer. The addi is not at the head.
In cycle 8 the store, finished in cycle 7, is the head and leaves. The addi is right behind it, and leaves in the same cycle.
So the addi sat finished in the buffer for one cycle. That wait is the price of order.
What was gained? Suppose the store at 1c had trapped. The addi had run, but it had changed nothing that counts. It is thrown away.
The Rules
Rule 1: Instructions enter the buffer in program order.
Renamed, at the tail. If the buffer is full, dispatch waits.
Rule 2: An entry executes when its inputs are ready, and is then marked finished.
This is the out-of-order part, as in S2.31.
Rule 3: Only a finished head may retire.
So results become final in program order. A trap is taken when its instruction is the head.
Rule 4: At retirement the old place of the written register is freed.
No instruction in flight can still read it.
Rule 5: Behind a branch, the fetch follows a guess, and those instructions are speculative.
They execute, but they cannot retire before the branch does.
Rule 6: A wrong guess squashes every younger entry.
Their new places go back to the free list, and the rename table gets its old entries back.
Rule 7: A store writes memory only when it retires.
A register can be renamed back. A word in memory or in a device cannot.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.10. It has the commit unit with a reorder buffer, in-order commit, speculation, and stores held until commit. Its section 4.11 describes a real core with a reorder buffer and renaming that executes up to six operations in a cycle. Harris and Harris, Digital Design and Computer Architecture (ARM edition), section 7.7.5, has out-of-order execution. Both books use other processors than ours. The loop on this page is the trace of the real core ap_rv32. The real core ran it in order, in 30 cycles. It has no reorder buffer, and it guesses nothing beyond "not taken". Every number of the reorder buffer is from a model. Its choices are: 8 entries, two in and two out in a cycle, a load of 2 cycles, every branch guessed not taken.
Now Run It
The lab at the end of this page replays the loop of S2.15: three rounds, 19 instructions. Every branch is guessed "not taken".
Each row is one cycle. "in" lists what is dispatched, "done" what is finished in this cycle, "out" what retires.
1 | in: 00 04 | done: -- | out: -- | holds 2
2 | in: 08 0c | done: 00 04 | out: -- | holds 4
3 | in: 10 14 | done: 08 | out: 00 04 | holds 4
4 | in: 18 1c | done: 0c 14 | out: 08 | holds 5
5 | in: 20 24 | done: 10 18 | out: 0c | holds 6
6 | in: -- | done: 1c 20 24 | out: 10 14 | holds 4
7 | squash: 20 24 | fetch goes to 0c
7 | in: 0c 10 | done: -- | out: 18 1c | holds 2
8 | in: 14 18 | done: -- | out: -- | holds 4
9 | in: 1c 20 | done: 0c 14 18 | out: -- | holds 6
10 | in: 24 | done: 10 1c | out: 0c | holds 6
11 | squash: 20 24 | fetch goes to 0c
11 | in: 0c 10 | done: -- | out: 10 14 | holds 4
12 | in: 14 18 | done: -- | out: 18 1c | holds 4
13 | in: 1c 20 | done: 0c 14 18 | out: -- | holds 6
14 | in: 24 | done: 10 1c | out: 0c | holds 6
15 | in: -- | done: 20 24 | out: 10 14 | holds 4
16 | in: -- | done: -- | out: 18 1c | holds 2
17 | in: -- | done: -- | out: 20 24 | holds 0
retired=19 squashed=4 cycles=17Rule 2 at work: row 4. The addi at 14 is finished in the same cycle as the load at 0c. The add at 10, between them, is not.
Rule 3 at work: row 5 retires only the load at 0c. The addi at 14 is finished and waits. Row 6 retires 10 and 14 together.
Rule 5 at work: row 4 dispatches the branch at 1c. Row 5 goes on at 20 and 24, as if the branch were not taken.
Row 6: the branch is finished, and so are the two instructions behind it. The store at 20 has run. It has not retired.
Rule 6 at work: the line "squash: 20 24". The branch was taken. Both entries are thrown away, and the fetch goes to 0c.
The same happens in cycle 11 for the second round. In the third round the guess is right: 20 and 24 retire in row 17.
The last line: 19 instructions retired, 4 squashed, 17 cycles. The real core, in order, took 30 cycles for the same loop.
The two numbers are not a fair race. The model has no fetch stage and no penalty for the redirect. It shows the mechanism.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Retire in program order | Make each result final when it is finished | A precise trap, and a clean undo. Finished instructions wait behind an unfinished head. |
| Speculate behind a branch | Stop the fetch until the branch is finished | Work goes on during the wait. Wrong work costs energy, and must be undone. |
| Keep the old place in the entry | Copy the whole rename table at every branch | A few bits in each entry. Recovery walks back through the squashed entries. |
| Free a place at retirement | Free it when the next write is renamed | A squashed write can be undone, because the old value is still there. Places are held longer. |
| Hold stores until they retire | Write memory when the store executes | A squashed store leaves no mark. Later loads must look into the store buffer. |
| A buffer of fixed size | As many entries as needed | Known cost. When it is full, dispatch stops. |
The size sets how far the core can look ahead. With four entries, the model needs 18 cycles for this loop, one more than with eight.
The Trap: Letting a Finished Store Write
Read rows 5 to 7 again. The store at 20 was dispatched in cycle 5 and finished in cycle 6. In cycle 7 it was squashed.
It belongs to a path that the program never took. In the real run, this store happens once, after the third round.
Suppose the model wrote memory when a store is finished. Then the sum of one word would be in memory after round one.
In ordinary memory the right value would overwrite it later. Now let the address be a device: a UART sends a byte that was never meant.
No recovery can call that byte back. So "finished" must not mean "visible". Only the head of the buffer may touch the world.
At Scale
This section looks ahead, and names what real cores do. Eight entries show the mechanism. Real buffers are far larger.
What real cores do
One well-known desktop core renames registers, keeps a reorder buffer and executes up to six operations in a cycle. The textbook of Patterson and Hennessy describes it.
Small cores stay in order
A core like ap_rv32 has none of this. For a microcontroller, a small area and a known timing matter more than IPC. That is common practice.
Prediction decides everything
Every wrong guess throws away all work behind the branch. So these cores carry the predictors of S2.12 and S2.28.
Speculation and security
Squashed instructions change no register and no memory word. They can still leave traces in a cache. S9.5 looks at what a chip must keep apart.
Try It
Run the lab. Find the two squash lines, and the cycle in which each squashed instruction was dispatched.
Set SIZE to 4 and run again. In which cycle does dispatch first take one instruction where it took two before?
In load_program, set outcome[0] and outcome[1] to 0. The model now believes that no branch was taken. How many instructions retire, and why is that run not the real program?
1 | in: 00 04 | done: -- | out: -- | holds 2 2 | in: 08 0c | done: 00 04 | out: -- | holds 4 3 | in: 10 14 | done: 08 | out: 00 04 | holds 4 4 | in: 18 1c | done: 0c 14 | out: 08 | holds 5 5 | in: 20 24 | done: 10 18 | out: 0c | holds 6 6 | in: -- | done: 1c 20 24 | out: 10 14 | holds 4 7 | squash: 20 24 | fetch goes to 0c 7 | in: 0c 10 | done: -- | out: 18 1c | holds 2 8 | in: 14 18 | done: -- | out: -- | holds 4 9 | in: 1c 20 | done: 0c 14 18 | out: -- | holds 6 10 | in: 24 | done: 10 1c | out: 0c | holds 6 11 | squash: 20 24 | fetch goes to 0c 11 | in: 0c 10 | done: -- | out: 10 14 | holds 4 12 | in: 14 18 | done: -- | out: 18 1c | holds 4 13 | in: 1c 20 | done: 0c 14 18 | out: -- | holds 6 14 | in: 24 | done: 10 1c | out: 0c | holds 6 15 | in: -- | done: 20 24 | out: 10 14 | holds 4 16 | in: -- | done: -- | out: 18 1c | holds 2 17 | in: -- | done: -- | out: 20 24 | holds 0 retired=19 squashed=4 cycles=17Check Yourself
Common Mistakes
Wrong Let an instruction retire as soon as it is finished.
An older instruction may still trap, or be a branch that was guessed wrong.
Fix Retire only from the head, in program order.
Wrong Let a speculative store write memory.
After a squash the write cannot be taken back.
Fix Keep it in a store buffer until it retires.
Wrong After a wrong guess, clear the buffer and keep the rename table.
The table points at places of instructions that never ran.
Fix Put the old place of every squashed write back into the table.
Remember
- The reorder buffer holds the instructions in flight in program order: in at the tail, out at the head.
- Only a finished head retires. So results are final in order, and a trap is precise.
- Speculation executes behind a guess. A wrong guess squashes the younger entries and repairs the rename table.
- A store writes memory only at retirement. ap_rv32 is in-order and has no reorder buffer.