# Pipelining and why CPUs speculate

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

Source: https://fullbytedeveloper.com/blog/cpu-pipelining-speculate
Published here: 2026-11-21, updated 2026-12-03

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.

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Teaching five-stage pipeline fills left to right across cycles IF ID EX MEM WB, then a predict branch path box feeds a wrong-guess squash box that discards architectural results and redirects to the correct path.">
  <defs>
    <marker id="pipeSpecArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">pipeline fill then branch speculate</text>
  <rect x="24" y="56" width="88" height="48" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="48" y="86" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">IF</text>
  <path d="M112 80 H140" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipeSpecArrow)"/>
  <rect x="148" y="56" width="88" height="48" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="172" y="86" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">ID</text>
  <path d="M236 80 H264" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipeSpecArrow)"/>
  <rect x="272" y="56" width="88" height="48" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="294" y="86" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">EX</text>
  <path d="M360 80 H388" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipeSpecArrow)"/>
  <rect x="396" y="56" width="88" height="48" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="414" y="86" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">MEM</text>
  <path d="M484 80 H512" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipeSpecArrow)"/>
  <rect x="520" y="56" width="88" height="48" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="542" y="86" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">WB</text>
  <text x="24" y="140" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">overlap: I1 in WB while I5 in IF (teaching model) [3]</text>
  <rect x="24" y="160" width="300" height="96" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="40" y="192" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">predict branch path</text>
  <text x="40" y="220" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">execute ahead [4]</text>
  <text x="40" y="244" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">keep pipe full</text>
  <path d="M324 208 H372" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipeSpecArrow)"/>
  <rect x="380" y="160" width="316" height="96" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="396" y="192" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">wrong guess: squash</text>
  <text x="396" y="220" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">architectural discard [5]</text>
  <text x="396" y="244" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">redirect to correct path</text>
</svg>
</figure>

*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].

## Peek at mitigation-related CPU flags (no attack steps)

On Linux you can list CPU feature flags that vendors document in the context of speculative-execution hardening. This is a presence check, not an exploit recipe and not a claim about your cloud neighbor.

```bash
lscpu | rg -o 'ibrs|ibpb|stibp|ssbd|md_clear|flush_l1d' | sort -u
```

Captured on this host (x86_64 KVM guest, Intel Xeon reported by `lscpu`). Sandbox capture on the review host; re-run on your box before you treat the numbers as a lab result.

```text
flush_l1d
ibpb
ibrs
md_clear
ssbd
stibp
```

Those strings show up in the flags line. They do not teach Flush+Reload, Spectre gadgets, or timing recipes. Stop at naming the research class [4][6].

## 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](/blog/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].

## Related reading

[What is latency](/blog/what-is-latency), [concurrency vs parallelism in plain words](/blog/concurrency-vs-parallelism-plain), [CPU vs GPU vs NPU vs TPU](/blog/cpu-vs-gpu-vs-npu-vs-tpu).


Wrong-path work can leave microarchitectural traces even when architectural state stays clean, so treat squash as an ISA promise, not a claim that the chip never moved.

Browse the [Science topic](https://fullbytedeveloper.com/blog/topic/science).

Send this to the friend who never understood why CPUs guess which way an if goes.

Image: 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.
https://fullbytedeveloper.com/media/cpu-pipelining-speculate/cpu-pipelining-speculate-venice.webp
Stages overlap so the pipe stays full. A wrong branch prediction is thrown away.

## Sources

1. [OpenStax Intro CS 5.6 Processor architectures, accessed 2026-11-20](https://openstax.org/books/introduction-computer-science/pages/5-6-processor-architectures)
2. [CSAPP Chapter 4 preview PDF, accessed 2026-11-20](https://csapp.cs.cmu.edu/public/ch4-preview.pdf)
3. [MIT OCW 6.004 Computation Structures C15, accessed 2026-11-20](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)
4. [Intel: Introduction to speculative side channel methods, accessed 2026-11-20](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/introduction-speculative-side-channel-methods.html)
5. [Intel: Refined speculative execution terminology, accessed 2026-11-20](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/best-practices/refined-speculative-execution-terminology.html)
6. [Intel: Hardware behavior related to speculative execution, accessed 2026-11-20](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)
7. [Dan Luu: Branch prediction, related teaching essay](https://danluu.com/branch-prediction/)
8. [Fabian Giesen: Speculatively speaking, related performance essay](https://fgiesen.wordpress.com/2013/03/04/speculatively-speaking/)
