Full Byte Developer.

Pipelining and why CPUs speculate

CPUs overlap instruction stages, then predict branches so the pipe stays full. Wrong guesses get squashed.

Published here , updated · 8 sources

CPUs overlap instruction work across stages (fetch, decode, execute, memory, writeback in the teaching strip) so more than one instruction is in flight at once [1][3]. Branches would empty that pipe, so cores predict and execute ahead [4]. Wrong guesses get discarded from architectural state, then the correct path continues [4][5].

Quick start: watch five stages overlap

Run this tiny teaching simulator. It does not model a real SKU. It only prints how a five-stage strip fills when one new instruction enters each cycle, matching the MIT OCW / textbook story [2][3].

bash
python3 - <<'PY'
stages = ["IF", "ID", "EX", "MEM", "WB"]
print("teaching 5-stage overlap (one new instruction per cycle after fill)")
for c in range(8):
    row = []
    for i in range(5):
        s = c - i
        if 0 <= s < 5:
            row.append(f"I{i+1}={stages[s]}")
    print(f"cycle {c}: " + " | ".join(row))
print("filled pipe at cycle 4: five instructions occupy five stages")
PY

Captured on this host:

text
teaching 5-stage overlap (one new instruction per cycle after fill)
cycle 0: I1=IF
cycle 1: I1=ID | I2=IF
cycle 2: I1=EX | I2=ID | I3=IF
cycle 3: I1=MEM | I2=EX | I3=ID | I4=IF
cycle 4: I1=WB | I2=MEM | I3=EX | I4=ID | I5=IF
cycle 5: I2=WB | I3=MEM | I4=EX | I5=ID
cycle 6: I3=WB | I4=MEM | I5=EX
cycle 7: I4=WB | I5=MEM
filled pipe at cycle 4: five instructions occupy five stages

At cycle 4 the pipe is full: five instructions sit in five stages. That is temporal parallelism in OpenStax's wording [1]. A branch mispredict would force a redirect and throw away wrong-path work from architectural state [4][5].

See pipelining as an assembly line of stages

OpenStax: designers divide bulky single-cycle work into phases. After fetch finishes instruction 1, fetch can start instruction 2 while decode works on 1. Different phases work on different instructions without the programmer wiring it [1]. Motivation: a single bulky circuit was slow and power-hungry because the clock had to fit the slowest instruction [1].

MIT OCW 6.004 labels a common five-stage teaching strip: IF (fetch), RF/ID (read registers / decode), ALU/EX, MEM, WB [3]. CSAPP's public Chapter 4 preview teaches a five-stage Y86 pipeline where one instruction can enter per cycle and hazards must preserve sequential ISA behavior [2]. Prefer a diagram over runnable "attack-shaped" code. Teaching models use about five stages; real cores vary. Do not invent a product pipeline depth without that product's diagram.

pipeline fill then branch speculate IF ID EX MEM WB overlap: I1 in WB while I5 in IF (teaching model) [3] predict branch path execute ahead [4] keep pipe full wrong guess: squash architectural discard [5] redirect to correct path

Five teaching stages overlap so later instructions enter while earlier ones finish. A bad branch prediction executes ahead, then squashes wrong-path results from architectural state and redirects onto the correct path.

Why the core guesses the next path

Intel: speculative execution executes instructions ahead of knowing they are required so the processor need not wait for prior instructions to resolve; results may be discarded if the work was not needed [4]. The common form is control-flow prediction; on a wrong prediction the speculative work is discarded and the processor redirects to the correct path [4][6].

Intel's refined terminology: if a prediction is correct, dependent instructions can retire (become architecturally visible); if incorrect, dependents are invalid and must be squashed (transient), then execution resumes with the correct value [5]. Forms include control-flow speculation and data speculation such as forwarding from a load before intervening stores resolve [6].

MIT OCW states the same strategy in classroom language: make an intelligent guess, continue, and back up if wrong [3]. Dan Luu's branch-prediction essay and Fabian Giesen's "Speculatively speaking" post are clear secondary explainers for predict-then-discard performance thinking [7][8].

Keep architectural squash separate from microarchitectural leftovers

Intel: wrong-path instructions must be squashed and do not affect architectural states, while speculative operations can still affect microarchitectural state (caches, TLBs, predictors, and similar structures). Side-channel research observes such effects [4][6]. Name that research class at a high level. Do not ship Flush+Reload steps, Spectre gadgets, or timing-attack recipes here.

Arm vendor whitepapers likewise treat speculation as part of high-performance implementation freedom; cite them only at overview level if you dig further, and still omit gadget how-tos.

Hazards that force stalls even without branches

CSAPP's pipeline chapter is explicit that hazards must preserve sequential ISA behavior even while stages overlap [2]. Data hazards (a later instruction needs a result that is still in flight), structural hazards (two stages want the same unit), and control hazards (branches) are the usual classroom set. Forwarding and stalling are the safe teaching answers. Speculation is the performance answer for control hazards once you accept wrong-path work that can be squashed [2][3][5].

When someone says "the CPU is waiting," ask which hazard. Waiting on a load miss is a memory latency story (see what is latency). Waiting on a mispredicted branch is a control-flow story. Those are different knobs in a profiler.

Mistakes that keep showing up in design reviews

Calling a five-stage textbook strip "my laptop's pipeline depth": product cores vary; cite the product diagram or stay with the teaching model [3].

Treating speculation as a bug: primary Intel docs present it as a performance technique with residual side-channel research implications [4].

Claiming squash means "nothing in the chip changed": architectural state is protected; microarchitectural state can still move [4][6].

Inventing a universal branch-predictor accuracy percentage without a measured workload and a named predictor: leave the number out [7].

Shipping PoC timing code on a teaching page: diagram-only plus the simulator above is enough for this on-ramp.

Depth: retire, squash, and what "architecturally visible" means

Retire means the instruction's results become part of the architectural state the programmer's model can observe: registers and memory as the ISA defines them [5]. Squash means those speculative dependents never retire on the wrong path; execution resumes with the correct value [5]. That is why a correct program still sees sequential semantics even while the core ran ahead.

Performance essays by Luu and Giesen are useful for intuition about why predictors exist and how costly a flush feels in practice [7][8]. They are secondary. For definitions that bind vendor language, stay on Intel's speculative-execution pages and the OpenStax / MIT / CSAPP teaching stack [1][2][3][4][5].

Dark navy hero of a four-chamber teal pipeline with one glowing amber token traveling straight through the stages, while a second faint speculative token peels into a side branch and fades as a teal barrier closes, all on a faint grid with no typography.
Stages overlap so the pipe stays full. A wrong branch prediction is thrown away.

Keep this guide

By Full Byte Developer

Filed under Dev explainers, Science

Questions about this guide

What is instruction pipelining?

OpenStax describes splitting single-cycle work into phases such as fetch, decode, issue, execute, and commit so different instructions occupy different phases at once (temporal parallelism).

Why speculate at all?

Intel's speculative-execution intro says cores execute ahead of knowing work is required so they need not stall waiting for branches or similar dependencies. Wrong-path work is discarded from architectural state.

Does squash mean caches never change?

No. Intel documents that speculative ops must not become architecturally visible when wrong, while microarchitectural state such as caches can still change. That class of side-channel research exists; this page does not teach attacks.

Is a five-stage pipe what my laptop core uses?

Five stages are a teaching model (MIT OCW, many textbooks). Real product pipelines vary. Do not invent a SKU depth without that product's diagram.

Sources

Next in this stack