Skip to content

cpu_guard: build-time warning when a target feature has no guard row - #360

Merged
AdaWorldAPI merged 3 commits into
masterfrom
ccr-9f3b8075-74u47l-guard-drift
Oct 13, 2026
Merged

AdaWorldAPI merged 3 commits into
masterfrom
ccr-9f3b8075-74u47l-guard-drift

Conversation

@AdaWorldAPI

@AdaWorldAPI AdaWorldAPI commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

What

build.rs compares CARGO_CFG_TARGET_FEATURE with the feature names in cpu_guard's x86_64 FEATURES table. If the build enables a feature that has no row, it emits a cargo:warning. The x86_64 baseline (sse, sse2, x87) is excluded.

Why

The guard checks each row with cfg!(target_feature = ...). A feature that is compiled in but has no row is never compared with the running CPU, so the guard lets the binary through and it can still die with SIGILL. This happens when a toolchain bump lets LLVM enable a feature the table predates. The existing the_table_covers_every_feature_a_cpu_model_can_enable test checks a hand-copied list. This check reads the features the actual build enabled.

Shape

  • src/cpu_guard_drift.rs holds the pure parser and filter. build.rs and a test module in cpu_guard.rs both include it with include!.
  • build.rs is added to the Cargo.toml include list so it is packaged.

Evidence

  • Tests:
    • the parser reads exactly the 54 FEATURES rows, in order;
    • an input containing avx10.1 reports only avx10.1;
    • the table plus the baseline reports nothing.
  • Disable runs (each red, then restored):
    • dropping the table filter fails both filter tests;
    • dropping the => check fails the parser test.
  • End to end:
    • On stable at -Ctarget-cpu=sapphirerapids it stays silent.
    • On nightly at -Ctarget-cpu=sapphirerapids it warns: cpu_guard has no row for enabled target features: amx-bf16, amx-int8, amx-tile, clflushopt, ermsb, lahfsahf, movdir64b, movdiri, prfchw.
  • cargo clippy --lib --tests -- -D warnings and cargo fmt --check are clean.

Limits

  • On stable rustc, CARGO_CFG_TARGET_FEATURE omits unstable features (avx10.x, amx-*). The guard's cfg! misses them the same way, so on stable the guard and this check are blind together.
  • Cargo hides build-script warnings from git and registry dependencies. The warning therefore shows when building ndarray itself, not inside a consumer.

Open

The nightly output lists the features the guard cannot see. Adding rows for them (AMX needs XCR0 tile-state gating) is a separate change.

🤖 Generated with Claude Code

https://claude.ai/code/session_011BMJnDBAfzcuQh5HeT39Rh


Generated by Claude Code

Summary by CodeRabbit

  • Build & Compatibility
    • Builds now warn when an enabled x86_64 CPU feature is not covered by the compatibility checks. If the relevant compatibility information cannot be read, the check is skipped with a warning. Other architectures are unaffected.
  • Tests
    • Added checks for detecting uncovered CPU features and confirming that the listed features and baseline are recognized.

build.rs compares CARGO_CFG_TARGET_FEATURE with the feature names in
cpu_guard's x86_64 table and emits cargo:warning for any enabled feature
the guard cannot check (baseline sse/sse2/x87 excluded). Such a feature is
compiled in but never compared with the running CPU, so the guard passes a
binary that can still die with SIGILL.

The parser and filter live in src/cpu_guard_drift.rs, included by build.rs
and by three tests: the parser reads exactly the 54 FEATURES rows, an
avx10.1 input is reported, and table plus baseline reports nothing.

Limits: stable rustc omits unstable features (avx10.x, amx-*) from the cfg,
for the guard and the build script alike; cargo hides build-script warnings
from git and registry dependencies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011BMJnDBAfzcuQh5HeT39Rh
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

Your organization has reached its usage spending cap. Adjust your spending cap in the billing tab.

Next included review available in 47 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 62 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 77454d32-7206-41be-8b10-897484d84c0e
📥 Commits

Reviewing files that changed from the base of the PR and between d8f8305 and 9869e02.

📒 Files selected for processing (3)
  • .claude/blackboard.md
  • src/cpu_guard.rs
  • src/cpu_guard_drift.rs

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 27d6265c-f5c8-450e-afea-27ef9727a0e2
📥 Commits

Reviewing files that changed from the base of the PR and between 3f404c5 and d8f8305.

📒 Files selected for processing (4)
  • Cargo.toml
  • build.rs
  • src/cpu_guard.rs
  • src/cpu_guard_drift.rs

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

📝 Walkthrough

The package now includes build.rs in its published file list. On x86_64, the build script checks enabled target features against the CPU guard table and warns about unchecked features.

Changes: CPU Guard Drift Check

Layer / File(s) Summary
Build-time CPU guard drift check
Cargo.toml, build.rs, src/cpu_guard.rs, src/cpu_guard_drift.rs
The build script checks enabled x86_64 features against names parsed from the guard table and warns about unchecked features. Tests verify table parsing, feature detection, and consistency with all 54 guard rows. The package include list adds build.rs.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Cargo
  participant BuildScript as build.rs
  participant GuardTable as src/cpu_guard.rs
  participant DriftHelpers as cpu_guard_drift helpers
  Cargo->>BuildScript: Run build script
  BuildScript->>BuildScript: Read target features
  BuildScript->>GuardTable: Read guard table
  BuildScript->>DriftHelpers: Find enabled features missing from table
  BuildScript-->>Cargo: Emit warning for unchecked features
Loading

Suggested reviewers: claude

Merge Risk: ⚪ Minimal · up to d8f83

No actionable issue remains from the supplied evidence; the change is mergeable after normal checks.

Pre-merge checks · Passed 5
Passed Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: adding a build-time warning when an enabled target feature lacks a cpu_guard row.
Docstring Coverage Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 3 files. (1 skipped: 1 u…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.

A rabbit checks each feature name,
The guard table joins the game.
Unlisted flags raise a warning bright,
Build-time checks keep counts in sight.
The rabbit hops beneath the moon,
And tests confirm the table’s tune.

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T06:12:05.745636Z d8f8305 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d8f830597d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/cpu_guard_drift.rs
// toolchain bump lets LLVM enable a feature the table predates.

/// Features every x86_64 CPU has; the guard does not need rows for them.
const X86_64_BASELINE: &[&str] = &["sse", "sse2", "x87"];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude crt-static from the CPU feature warning

On targets such as x86_64-unknown-linux-musl, CARGO_CFG_TARGET_FEATURE includes crt-static by default, even though it describes the C runtime linkage rather than a CPU capability. Because it is neither in the guard table nor this exclusion list, every such build warns that the CPU guard cannot detect a CPU lacking crt-static and asks for an impossible guard row. Add it to the non-checkable exclusions so static x86_64 targets do not receive this false warning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. rustc --print cfg --target x86_64-unknown-linux-musl lists crt-static, and of the default x86_64 sets (gnu, musl, msvc, darwin) it was the only feature that does not describe the CPU. Fixed in 9869e02 (not yet pushed): it is excluded alongside the baseline. The new test crt_static_is_not_a_cpu_feature was red before the exclusion (6ace106) and checks that a real missing row next to crt-static is still reported.


Generated by Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011BMJnDBAfzcuQh5HeT39Rh
…drift warning

On musl targets crt-static is a default target feature (C-runtime linkage),
so every musl build warned about it. It is now excluded; a feature without a
row beside it is still reported. (codex P2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011BMJnDBAfzcuQh5HeT39Rh
@AdaWorldAPI
AdaWorldAPI merged commit 21948f8 into master Oct 13, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants