◄ UD0   the gather (perceptron = a gather)   the clock / pipeline   → the transformer
PSĒPHOS · microarchitecture · the trained guess

The branch predictor

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.

predict = sign( w₀ + Σ wᵢ·xᵢ )  ·  xᵢ = last branches as ±1  ·  learn: wᵢ += actual·xᵢ

your two questions, straight

is it prefetch?

No — its sibling. Both are speculation.

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.

do I only reach it through a wrapper?

Yes — it's deliberately invisible.

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.

1 · watch the neuron learn your branch

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.)

pattern:

2 · static dirt to a neuron — the lineage

Forty years of getting better at one bet: which way does this branch go? Each step remembered more, and aliased less.

≤ 1980 · STATIC

Always-taken / backward-taken-forward-not

No memory at all. The compiler or a fixed rule guesses; loops (backward branches) are bet taken. ~60–70% right. The dumb heuristic.

1981 · SMITH

The 2-bit saturating counter (bimodal)

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.

1991 · YEH & PATT

Two-level adaptive — history × pattern table

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."

1993 · McFARLING

gshare — hash the history with the address

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.

1996 · TOURNAMENT

Hybrid meta-prediction (Alpha 21264)

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.

2001 · JIMÉNEZ & LIN

The perceptron predictor — a neuron in silicon

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.

2006 · SEZNEC

TAGE — tagged, geometric history lengths

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.

now · HYBRIDS

TAGE-SC-L & hashed perceptron (Zen, Intel)

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.

3 · why it's a gather (and where it breaks)

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.

4 · the smoke & mirrors — from your if to the bet

The full wrapper stack, top to bottom. You touch the top; the predictor lives at the bottom and never shows its face.

you writeif (x > 0) { … } — a logical fork in your story
compileremits cmp x,0 ; jle else — a conditional branch instruction (the ISA's "if")
front-endsees the jle's address, auto-consults the predictor — no opcode asked it to
predictorperceptron fires taken / not-taken in ~1 cycle, before x is even compared
speculatethe pipeline runs down the guessed path — dozens of instructions deep, all provisional
resolvecycles later the real cmp finishes — was the guess right?
commit / flushright → keep it, you never knew. wrong → flush + rewind ~15 cycles, take the other path, and train the weights

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.