跳到正文
cccskills
免费在 GitHub 公开

branch-prediction-and-speculation

Use when explaining branch predictors, mispredict penalties, speculative execution, Spectre or Meltdown mitigations, or branchless code. Not for pipeline stage theory: use cpu-pipelines-and-hazards.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(2)

  • SKILL.md5.2 KB
  • agents/openai.yaml223 B

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Branch prediction and speculation

Contract

FieldBound contract
TriggerBranchy hot code underperforms, a likely or branchless refactor needs judgment, a kernel mitigation such as retpoline or KPTI needs explaining, or code branches on secret data.
AuthorityRead-only. The skill runs perf stat on a user-named binary, reads sysfs, and answers in chat. Nothing on disk changes, so there is nothing to roll back. No remote mutation.
Side effectChat output only. perf stat writes counters to stdout.
DoneThe answer names the branch that mispredicts or the speculation path that leaks, gives a measured branch-misses count where a binary exists, and states the fix with the condition under which it helps.

Inputs

  • Code or hot loop (required): the source or disassembly around the branch in question.
  • Binary and workload (optional): needed for a measured verdict. Without them the answer is a hypothesis.
  • Threat model (optional): whether the question is performance only or also side-channel safety.

Procedure

  1. Explain the mechanism in one pass. The front end predicts a direction for each conditional branch, executes the predicted path, and on resolve either commits or squashes the wrong-path work and refetches from the correct address. The squash cost grows with the distance from fetch to resolve, so a deeper pipeline pays more per mispredict. Backward branches are usually loop closers and predict taken; a data-dependent forward branch with no pattern is the hard case. Done when: the user can say why a given branch is predictable or not.
  2. Measure before changing code. Done when: a branch-misses count and its ratio to branches for the real workload is recorded, or no binary exists and the answer is marked as unmeasured.
perf stat -e branches,branch-misses ./app

Read the ratio against the workload, not against a fixed number: a tight loop over sorted data should show a ratio near zero, while a parser over random input can sit far higher and still be at its floor. Only a ratio that drops after a change proves the change.

  1. Pick the remedy for a mispredicting branch. Done when: one remedy is chosen and the condition under which it wins is stated.
    • Sort or partition the data so the branch becomes a run of one direction.
    • Replace the branch with a select. int m = a < b ? a : b; may lower to cmov. The select executes both operands every time, so it wins only when the branch mispredicts often; on a predictable branch it loses.
    • Split hot and cold paths so the rare path leaves the hot cache line.
    • Peel the loop exit or handle the tail without a branch when the exit mispredicts.
  2. Apply compiler hints last. __builtin_expect(!!(x), 1) and __builtin_expect(!!(x), 0) steer code layout, not the dynamic predictor. On a current out-of-order core the predictor already learns most static patterns, so the hint helps layout of the cold path and little else. Measure after adding one. Done when: the hint is kept only with a measured win.
  3. Cover the security side when the branch touches secrets. Speculation past a bounds check can load secret-dependent memory and leave its address in cache state (Spectre variant 1). Meltdown let a user load read a kernel mapping before the fault retired; KPTI separates the page tables. Done when: the applicable mitigation layer is named.
    • Kernel: read the state from /sys/devices/system/cpu/vulnerabilities/ (one file per issue, such as spectre_v1, spectre_v2, meltdown, l1tf). Retpoline, IBRS, IBPB, and STIBP appear in the spectre_v2 text. A host booted with mitigations=off reports Vulnerable here.
    • Compiler: Clang's -mspeculative-load-hardening masks pointers on the speculative path.
    • Code: constant-time algorithms with no secret-dependent branch or index. This is the only layer that protects a secret from a same-process timing channel.

For the pipeline model behind the penalty, use cpu-pipelines-and-hazards. For cache timing channels, use cpu-cache-opt. For the kernel mitigation set (KPTI, CET), use kernel-security.

Failure and recovery

Failure classBehavior
No binary or workloadDeliver the mechanism and the candidate fixes as hypotheses. Mark the answer unmeasured.
perf stat deniedReport the perf_event_paranoid value the tool prints and the capability it needs. Do not change the sysctl.
Hint shows no gainThe predictor already handled the branch. Remove the hint and profile for the real bottleneck.
Branchless version slowerThe branch was predictable and the select now executes both operands. Revert and benchmark on the target CPU.
Mitigation regresses throughputName the mitigation and its cost. Isolating the secret-handling code is the alternative; do not recommend disabling mitigations.

Output

A chat answer that names the mechanism, the measured branch-misses figure when a binary exists, one chosen remedy with its winning condition, and the mitigation layer that applies when secrets are involved.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Use when explaining System V AMD64, ARM AAPCS, RISC-V psABI, stack frames, variadic calls, or FFI register rules. Not for the Rust FFI binding layer: use rust-ffi.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when configuring ADC sampling time, DMA-driven ADC, calibration, or DAC channel setup on bare-metal MCUs. Not for the DMA stream itself: use dma-baremetal.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

af-xdp

免费

Use when creating AF_XDP sockets, configuring UMEM and XSK rings, writing an XDP redirect program, or choosing copy versus zero-copy mode. Not for full kernel bypass: use dpdk.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when a completed session needs an agent-environment retrospective. Not for an engineering retrospective from telemetry: use engineering-retrospective.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when a redacted, trimmed agent transcript must be appended to a GitHub PR or issue body, with human approval and preview. Not for automated or model-initiated insertion.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

agents-md

免费

Use when a repo needs agent setup, AGENTS.md added or made lean, CLAUDE.md audited, or agent instructions scored or pruned. Not for remote, credential, publish, deploy, or irreversible changes.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

查看 OutlineDriven 的全部技能

报告此技能的问题