사용 방법
- 설치 명령을 복사하고 자신의 에이전트 환경에 설치하기 전에 소스 패키지를 확인하세요.
- 필요한 도구, 인증 정보, 지원 환경은 원본 문서에서 확인하세요.
- 입력과 원하는 산출물을 설명하고 출처와 결과를 대조하세요.
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill branch-prediction-and-speculation시작 전 확인
- 설치에는 모델 구독, API 한도, 파일 접근 권한이 포함되지 않습니다.
- 별점 수와 설치 수는 활동 지표이며 독립적인 품질·보안 검증이 아닙니다.
원본 저장소의 제목, 예시, 코드, 표, 링크, 이미지를 유지해 표시합니다.
Branch Prediction and Speculation
Purpose
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.
When to Use
- Hot loop branchy code underperforms expectations
- Evaluating
likely/unlikelyor branchless refactors - Understanding kernel
retpoline, IBRS, and similar mitigations - Secure coding around secret-dependent branches
Workflow
1. Branch predictor basics
Fetch sees conditional branch
├── Predict direction (taken / not-taken)
├── Speculatively execute predicted path
└── On resolve:
├── Correct → commit, ~0 penalty (deep pipelines still cost on mispredict)
└── Wrong → squash, refill from correct PC (10–20+ cycles typical)Patterns: backward branches often predicted taken (loops); forward not-taken.
2. Performance patterns
/* Predictable — tight loop backward branch */
for (int i = 0; i < n; i++)
sum += a[i];
/* Unpredictable — data-dependent */
if (data[i] > threshold) /* hard to predict */
rare_path();Techniques: branchless cmov/select, lookup tables, sorting data to reduce branches, splitting hot/cold paths.
/* Branchless min (integer) */
int m = a < b ? a : b; /* compiler may lower to cmov */3. Compiler hints (use sparingly)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
if (unlikely(ptr == NULL))
return -EINVAL;Measure with perf — hints are not magic on modern predictors.
4. Speculative execution and side channels
CPUs may execute instructions before branch outcome is known. If speculated path touches secret-dependent memory, cache state can leak (Spectre variant 1).
Mitigations (high level):
- Kernel: retpoline, IBRS/IBPB, STIBP (see kernel
cpu_show_mitigations) - Compiler: speculative load hardening (
-mspeculative-load-hardeningon Clang) - Code: constant-time crypto — no secret-dependent branches or indices
Meltdown (Intel): user load from kernel mapping — fixed by KPTI (separate page tables).
5. Measurement
perf stat -e branches,branch-misses ./appHigh branch-misses ratio → investigate hot branches.
6. Agent usage
/branch-prediction-and-speculation Make this comparison function constant-time against Spectre-style leakageCommon Problems
| Symptom | Cause | Fix |
|---|---|---|
| Loop slower than expected | Mispredicted exit | Peel iterations; branchless tail |
likely no help | Predictor already good | Profile first |
| Secret leak in crypto | Branches on secret bytes | Constant-time algorithms |
| Mitigation regression | KPTI/retpoline overhead | Accept or isolate secrets |
| "Branchless" slower | CMOV still executes both | Benchmark on target CPU |
Related Skills
skills/computer-architecture/cpu-pipelines-and-hazards— control hazardsskills/security/kernel-security— KPTI, CET, speculation mitigationsskills/low-level-programming/cpu-cache-opt— cache timing channelsskills/profilers/hardware-counters— branch-misses eventskills/runtimes/binary-hardening— userspace hardening overlap



