Advanced Topic 36 of 51 Computer Architecture & Logic
Speculative Execution
Executing ahead on predicted paths and discarding if wrong.
How it works
Speculative execution runs instructions before knowing whether they are needed, typically after a predicted branch or before a load address is known to be safe. If the guess was right, results are committed and time is saved; if wrong, the speculated work is discarded and architectural state is restored. It is a key method to keep deep pipelines busy.
Step-by-step walkthrough
- The CPU does not wait for a branch condition.
- It runs instructions of the predicted path.
- If the prediction is right the results are committed.
- If wrong the speculative results are thrown away.
- Side effects in caches may remain, which Spectre attacks exploit.
Where it is used
Modern CPUs speculate past branches, memory dependencies and even faults, and compilers and databases use similar optimistic strategies, such as speculative loads or optimistic concurrency.
Common pitfall
Squashed speculative work still leaves microarchitectural traces in caches and predictors, which the Spectre and Meltdown attacks used to leak secrets across security boundaries. Mitigations such as barriers, retpolines and isolation cost performance. Treat speculative paths as observable when writing security-sensitive code.
Where it fits in the course
Speculative Execution is topic 36 of 51 in Computer Architecture & Logic, rated Advanced. It follows Superscalar Processors. The next topic is Multicore Processors.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Computer Architecture & Logic quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Computer Architecture & Logic quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Speculative Execution?
Where do people use Speculative Execution?
What is the most common mistake with Speculative Execution?
What should I learn before this topic?
How can I practice it?
Is Speculative Execution hard to learn?
How long does it take to learn Speculative Execution?
Which topics are related to Speculative Execution?
What should I study after Speculative Execution?
Related topics
- Superscalar Processors (Computer Architecture & Logic)
- Multicore Processors (Computer Architecture & Logic)
- Out-of-Order Execution (Computer Architecture & Logic)
- Cache Coherence (Computer Architecture & Logic)
- Query Execution Plans (Databases)
- Trusted Execution Environments (Security & Cryptography)