S4 · Lesson 10 · Speculation and the reorder buffer
Executing past unresolved branches, committing in order, precise exceptions and memory disambiguation
Tomasulo's algorithm lets instructions execute out of order, but it can't look past a branch it hasn't resolved, and branches come every five or six instructions. Speculation removes that limit: the core follows the predicted path, executes everything there as if the prediction were right, and keeps the results in a reorder buffer until it knows. Instructions leave the buffer (commit) strictly in program order, so a wrong guess or an exception can be undone by discarding everything after it. How much of that speculative work turns out useful depends steeply on prediction accuracy and on how far ahead the window reaches.
- The fraction of a full speculative window that's on the correct path, against the window size W, when one instruction in 1/f is a branch and each is predicted correctly with probability a.
- An instruction k branches ahead of the oldest unresolved one is useful with probability , so the far end of a big window is mostly wasted work unless prediction is very accurate.
- At 95% accuracy and a branch every 5 instructions, a 200-entry window is only 44% useful; at 99%, 83%.
Predict
A core keeps 200 instructions in flight, with a branch every 5 instructions predicted 95% correctly. About how many of the 200 are on the right path, on average?
About 87. The window spans 40 branches, and the youngest instruction is on the right path only if all 40 predictions are right: = 13%. Summing over the window gives 87 useful instructions. At 99% accuracy it's 166. A bigger window pays off only with a better predictor, which is why the two grew together.
Name it
The reorder buffer
Each instruction gets a reorder buffer (ROB) entry at issue, in program order. Results go to the ROB (or to a renamed physical register) when execution finishes; commit takes completed instructions from the head, in order, and updates the architectural state.
- A mispredicted branch reaching the head (or detected earlier) squashes every younger entry and redirects fetch.
- An exception is taken only when the faulting instruction reaches the head, so all older instructions have finished and no younger one has changed state: precise exceptions.
- Stores write memory only at commit, from a store buffer.
How far speculation reaches
With a branch every 1/f instructions predicted with accuracy a, a window of W instructions spans B = fW branches. The k-th branch ahead is on the correct path with probability , and the expected number of correct-path instructions is
Memory disambiguation
A load may execute before older stores whose addresses aren't known yet. The load-store queue checks each store's address, when it becomes known, against younger loads that already ran; a match means the load got stale data and must be squashed and replayed. Predictors learn which loads tend to conflict.
Limits
Commit width caps IPC; the ROB size caps the window (Little's law, lesson 9); and every squashed instruction cost energy for nothing.
Worked example
Find the useful part of a 200-instruction window at 95% and 99% accuracy, with a branch every 5 instructions, and of a 64-instruction window at 95%.
Show the solution
- 200 entries at 95%. The window spans B = 40 branches: which is 44% of the window.
- 200 entries at 99%. which is 83%.
- 64 entries at 95%. The window spans B = 12.8 branches: which is 75%.
- Compare. Tripling the window at 95% gained only 39 useful instructions, while the better predictor on the big window doubled them.
Answer 87 useful instructions (44%) and 166 (83%) in the 200-entry window at 95% and 99%, and 48 (75%) in the 64-entry window at 95%.
Worked example
A load executes speculatively and reads address 0x1000. Later, an older store's address resolves to 0x1000. What must happen, and what if it resolves to 0x2000?
Show the solution
- Same address, 0x1000. The load read a value the store hasn't written yet, so the load and everything after it that used its value are squashed and replayed. The store's data is then forwarded from the store queue.
- Different address, 0x2000. There's no conflict, and the speculation saved the cycles the load would have spent waiting.
- Why speculate. Conflicts are rare for most loads, so speculating and occasionally replaying beats always waiting.
Answer at 0x1000 the load and its dependents are squashed and replayed with the store's forwarded data; at 0x2000 nothing happens, and the load's early execution saved time.
Worked example
A 128-instruction window sees a branch every 5 instructions, predicted with 97% accuracy. How much of the window is on the correct path?
Show the solution
- Branches in the window.
- Correct-path instructions. which is 71% of the window.
- Why it matters. Each misprediction near the head throws away everything behind it, so accuracy and window size have to grow together.
Answer about 90 instructions, 71% of the window, are on the correct path.
Check
Each branch is predicted correctly with probability 0.95. What's the probability that an instruction 10 branches past the oldest unresolved one is on the correct path?
Check
A 512-instruction window has a branch every 1/0.15 instructions, each predicted with accuracy 0.97. How many instructions are on the correct path, on average?
Check
A core fetches and executes 4 instructions per cycle, but only 0.9 of them turn out to be on the correct path, and it commits at most 4 per cycle. What's its best IPC?
Check
A 4-wide core with a 224-entry ROB averages 50 cycles from dispatch to commit. What IPC can it sustain at best?
Check
Loads are 0.25 of instructions, 0.01 of them are caught reading before a conflicting older store and replayed, and each replay costs 20 cycles. What CPI does that add?
Check
A full 128-entry window has a branch every 5 instructions predicted with accuracy 0.95. On average, how many of its instructions are wasted work?
Check
A window of 256 instructions sees a branch every 5 instructions, predicted with accuracy 0.9.
(a)
How many branches does it span?
(b)
How many of its instructions are on the correct path, on average?
(c)
What is the most any window could have on the correct path?
Check
A branch comes every k instructions, predicted with accuracy a; the window holds W instructions.
Step 1
How many branches does the window span?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many instructions are on the correct path on average?
Step 3
What fraction of the window is useful?
Explain why
Why do out-of-order cores commit instructions in program order, when they've already executed them out of order?
Executing out of order is safe as long as nobody can observe the order, and committing in order is what guarantees that. Until an instruction commits, its result lives in a temporary place, a reorder buffer entry or a renamed register that the architectural state doesn't yet point to. If an older branch turns out mispredicted or an older instruction faults, everything younger can simply be thrown away, and the program's visible state is exactly what it would have been on an in-order machine. That gives precise exceptions, lets the operating system restart a faulting instruction cleanly, and makes speculation past branches recoverable. The cost is buffering every in-flight result until its turn.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.