Speculative Execution
Speculative execution is when the CPU runs instructions down a predicted (but unconfirmed) path, ready to commit the work if the guess was right and discard it if wrong.
Why?
Waiting for every branch or cache miss to fully resolve before continuing would stall the pipeline for hundreds of cycles. Speculating lets the CPU keep useful work in flight, and crucially, start the next cache miss while the first is still pending.
Is this just branch prediction?
See Branch Prediction for the full comparison. One-liner: prediction is the bet, speculation is the play.
Hardware
On a predicted branch, the CPU executes the inferred target before the branch resolves. It pairs with register renaming so speculative writes land in a fresh set of physical registers while the originals are preserved: if right, commit; if wrong, discard and roll back.
Microarchitectural side channels
Rolled-back speculation still leaves traces in cache state and predictor state. That residual state is the channel behind Spectre and Meltdown.
Software (from L13)
Coarse-grained variant at the thread level: spawn the maybe-needed computation in parallel with the gating one, and throw its result away on a miss.
fn do_work(x: i32, y: i32, threshold: i32) -> i32 {
let t1 = thread::spawn(move || long_calculation(x, y));
let t2 = thread::spawn(move || second_long_calculation(x, y));
let val = t1.join().unwrap();
let v2 = t2.join().unwrap();
if val > threshold { return val + v2; }
return val;
}This is control speculation: you guess whether the second call is needed. Value Speculation is the data flavor (guess what value feeds the second call) and is the home for the shared timing model and safety caveats.