Every if you write is guessed before it's known. The pipeline can't wait ~15 cycles to learn which way a branch goes, so a tiny learner in the chip bets on it and runs ahead speculatively. In 2001 that learner became a literal neuron — the perceptron predictor: history bits in, learned weights, one fired bit out. It's the same weighted sum as attention — a gather — and it's been quietly running your code for twenty years.
Branch prediction guesses control — which way the if goes — so the front-end keeps fetching the right instructions. Prefetch guesses data — which memory addresses you'll touch — so the bytes are in cache before you ask. Different target (the branch vs the address), same trick (bet on the future to avoid stalling). And tellingly, prefetch went perceptron too (perceptron prefetch filtering, 2019) — the same neuron now guesses both.
You never address it directly. Your if → the compiler emits cmp; jne → the CPU front-end, seeing that jump, auto-consults the predictor — no instruction says "predict this." The smoke & mirrors is speculation + rollback: it runs down the guessed path, and if wrong, flushes and rewinds — so architecturally it's as if your if ran in order, just faster. You can only nudge it (__builtin_expect, profile-guided opt), never command it.
A real perceptron predictor, training live on a branch pattern. It reads the history (last 8 outcomes as ±1), takes a weighted sum (a gather), fires a taken / not-taken guess, then nudges its weights toward the truth. Patterns it can separate, it nails to ~100%. Pick RANDOM and watch it die at 50% — the Shannon floor, where no predictor beats a coin. (Weights are real; toy 8-bit history.)
Forty years of getting better at one bet: which way does this branch go? Each step remembered more, and aliased less.
No memory at all. The compiler or a fixed rule guesses; loops (backward branches) are bet taken. ~60–70% right. The dumb heuristic.
One little counter per branch — remembers the recent lean, needs two misses to flip. First learned predictor. ~85–90%. Still in every chip as a baseline.
A history register of recent outcomes indexes a table of counters. Now it learns patterns (TTNTTN…), not just bias. The leap from "which way usually" to "which way given the last few."
XOR the global history with the branch's PC to index the table — spreads collisions out, so unrelated branches stop clobbering each other. Cheap, strong, everywhere.
Run a local and a global predictor, plus a third that predicts which one to trust for this branch. Different branches have different personalities; pick the right expert.
One perceptron per branch. History bits as ±1 inputs, signed integer weights, predict by the sign of the weighted sum; train with the perceptron rule. The breakthrough: cost scales linearly with history length, where counter tables blow up exponentially — so it can see long correlations (a branch tied to one 40 back). A real neural net, clocked at GHz. AMD ships descendants of it today.
Several tagged tables indexed by geometrically growing history lengths (2, 4, 8, 16, 64…); the longest matching tag wins. Highest raw accuracy known; the perennial championship winner.
Real cores run both: TAGE for the pattern match, a perceptron-style Statistical Corrector to override it when it's been wrong. ~99% on real code. The neuron and the geometric table, fused.
Look at the rule again: y = Σ wᵢ·xᵢ. That's a weighted sum over the history — attend to past branches, weight each by a learned strength, combine into one decision. The same gather as attention (the gather ↗) — a branch predictor is a one-neuron attention head over time. But one perceptron is a linear classifier, so it has the famous blind spot: it cannot learn XOR (a branch taken only when two history bits disagree) — that's linearly inseparable. That single limitation is the whole reason TAGE, piecewise-linear, and hashed-perceptron hybrids exist: to catch the patterns one straight cut can't.
The full wrapper stack, top to bottom. You touch the top; the predictor lives at the bottom and never shows its face.
x is even comparedcmp finishes — was the guess right?So yes — it is the early-guess half of your if-handler, and you reach it only through that stack. The "mirrors" is that a correct guess is invisible (results as if run in order) and a wrong one is erased (rolled back) — the speculation never leaks into your program's logic, only into its speed.