$npx -y skills add mohitmishra786/low-level-dev-skills --skill branch-prediction-and-speculationBranch prediction and speculation skill for CPU security and performance. Use when explaining branch predictors, mispredict penalties, speculative execution, Spectre/Meltdown mitigations, or branchless patterns. Activates on queries about branch prediction, speculative execution,
| 1 | # Branch Prediction and Speculation |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Explain how modern CPUs predict branches, execute speculatively, recover on mispredict, and why speculation created side channels (Spectre/Meltdown) — linking performance tuning with security-aware low-level coding. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Hot loop branchy code underperforms expectations |
| 10 | - Evaluating `likely`/`unlikely` or branchless refactors |
| 11 | - Understanding kernel `retpoline`, IBRS, and similar mitigations |
| 12 | - Secure coding around secret-dependent branches |
| 13 | |
| 14 | ## Workflow |
| 15 | |
| 16 | ### 1. Branch predictor basics |
| 17 | |
| 18 | ``` |
| 19 | Fetch sees conditional branch |
| 20 | ├── Predict direction (taken / not-taken) |
| 21 | ├── Speculatively execute predicted path |
| 22 | └── On resolve: |
| 23 | ├── Correct → commit, ~0 penalty (deep pipelines still cost on mispredict) |
| 24 | └── Wrong → squash, refill from correct PC (10–20+ cycles typical) |
| 25 | ``` |
| 26 | |
| 27 | Patterns: backward branches often predicted taken (loops); forward not-taken. |
| 28 | |
| 29 | ### 2. Performance patterns |
| 30 | |
| 31 | ```c |
| 32 | /* Predictable — tight loop backward branch */ |
| 33 | for (int i = 0; i < n; i++) |
| 34 | sum += a[i]; |
| 35 | |
| 36 | /* Unpredictable — data-dependent */ |
| 37 | if (data[i] > threshold) /* hard to predict */ |
| 38 | rare_path(); |
| 39 | ``` |
| 40 | |
| 41 | Techniques: branchless `cmov`/select, lookup tables, sorting data to reduce branches, splitting hot/cold paths. |
| 42 | |
| 43 | ```c |
| 44 | /* Branchless min (integer) */ |
| 45 | int m = a < b ? a : b; /* compiler may lower to cmov */ |
| 46 | ``` |
| 47 | |
| 48 | ### 3. Compiler hints (use sparingly) |
| 49 | |
| 50 | ```c |
| 51 | #define likely(x) __builtin_expect(!!(x), 1) |
| 52 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 53 | |
| 54 | if (unlikely(ptr == NULL)) |
| 55 | return -EINVAL; |
| 56 | ``` |
| 57 | |
| 58 | Measure with `perf` — hints are not magic on modern predictors. |
| 59 | |
| 60 | ### 4. Speculative execution and side channels |
| 61 | |
| 62 | CPUs may execute instructions before branch outcome is known. If speculated path touches secret-dependent memory, cache state can leak (Spectre variant 1). |
| 63 | |
| 64 | **Mitigations (high level):** |
| 65 | |
| 66 | - Kernel: retpoline, IBRS/IBPB, STIBP (see kernel `cpu_show_mitigations`) |
| 67 | - Compiler: speculative load hardening (`-mspeculative-load-hardening` on Clang) |
| 68 | - Code: constant-time crypto — no secret-dependent branches or indices |
| 69 | |
| 70 | Meltdown (Intel): user load from kernel mapping — fixed by KPTI (separate page tables). |
| 71 | |
| 72 | ### 5. Measurement |
| 73 | |
| 74 | ```bash |
| 75 | perf stat -e branches,branch-misses ./app |
| 76 | ``` |
| 77 | |
| 78 | High `branch-misses` ratio → investigate hot branches. |
| 79 | |
| 80 | ### 6. Agent usage |
| 81 | |
| 82 | ``` |
| 83 | /branch-prediction-and-speculation Make this comparison function constant-time against Spectre-style leakage |
| 84 | ``` |
| 85 | |
| 86 | ## Common Problems |
| 87 | |
| 88 | | Symptom | Cause | Fix | |
| 89 | |---------|-------|-----| |
| 90 | | Loop slower than expected | Mispredicted exit | Peel iterations; branchless tail | |
| 91 | | `likely` no help | Predictor already good | Profile first | |
| 92 | | Secret leak in crypto | Branches on secret bytes | Constant-time algorithms | |
| 93 | | Mitigation regression | KPTI/retpoline overhead | Accept or isolate secrets | |
| 94 | | "Branchless" slower | CMOV still executes both | Benchmark on target CPU | |
| 95 | |
| 96 | ## Related Skills |
| 97 | |
| 98 | - `skills/computer-architecture/cpu-pipelines-and-hazards` — control hazards |
| 99 | - `skills/security/kernel-security` — KPTI, CET, speculation mitigations |
| 100 | - `skills/low-level-programming/cpu-cache-opt` — cache timing channels |
| 101 | - `skills/profilers/hardware-counters` — branch-misses event |
| 102 | - `skills/runtimes/binary-hardening` — userspace hardening overlap |