Skip to content

fix(star-sys): Derive C++ flags from CARGO_CFG_TARGET_FEATURE - #170

Draft
sjackman wants to merge 1 commit into
masterfrom
sj/fix-star-sys-flags
Draft

sjackman wants to merge 1 commit into
masterfrom
sj/fix-star-sys-flags

Conversation

@sjackman

@sjackman sjackman commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The star-sys build script chose C++ compiler flags with cfg!(target_feature = ...). That describes the compilation of the build script itself, not the target being built, and it gave wrong flags:

  • cmpxchg16b produced -mcmpxchg16b. Neither clang nor gcc accepts it; the flag is -mcx16. Building with -C target-cpu=x86-64-v3 (which enables cmpxchg16b) failed with clang++: error: unknown argument: '-mcmpxchg16b', with clang 18, clang 21 and gcc 13.
  • fma produced -mfma4, the AMD FMA4 instructions, which Intel CPUs do not have, instead of -mfma.
  • bmi1 and bmi2 together produced only -mbmi, so -mbmi2 was never passed, and avx2 and f16c were never mapped.

The build script now reads CARGO_CFG_TARGET_FEATURE, which describes the target, and maps each feature to a flag with a table in star-sys/flags.rs: -mcx16 for cmpxchg16b, -mfma for fma, -mbmi and -mbmi2 separately, plus sse3, ssse3, sse4.1, sse4.2, popcnt, avx, avx2, lzcnt, movbe and f16c. Features that are not in the table emit no flag. Every flag was checked against clang 18, clang 21 and gcc 13. The special case for macOS is removed, because -mcx16 is a long-standing clang flag; this is not verified on macOS.

The C++ now gets the target features explicitly, so results may differ in the last digits from before. They are deterministic for a given target-cpu. A build with --target and a build without it now get the same flags, and the script reruns when the features change.

The same table is in bhtsne in 10XDev/scan-rs, which had the same bug (https://github.com/10XDev/scan-rs/pull/828); there is no shared crate, so each copy has its own unit tests. The motivation is the move of Cell Ranger to x86-64-v3 in https://github.com/10XDev/cellranger/pull/21632.

Testing

  • cargo test -p star-sys flags runs three unit tests of the table, and cargo test --locked passes (14 tests in the orbit crate).
  • cargo fmt -- --check and the CI clippy command pass, with Rust 1.99.1. CI pins Rust 1.88.0, which I did not run.
  • Cell Ranger's lib/rust builds with cargo check --all-targets, with and without --target x86_64-unknown-linux-gnu, using this change through a local [patch]; the STAR compile line then contains -mcx16 -mavx2 -mfma -mbmi -mbmi2 -mlzcnt -mmovbe -mf16c.

Not done

  • No benchmark and no comparison of STAR output with the previous flags.
  • CI fails in the cargo deny step on an advisory for libz-sys 1.1.22 in Cargo.lock, which this change does not touch, so the clippy and test steps did not run there.

Jira: CELLRANGER-10331

  • Claude Code (AI assistant), posted on behalf of @sjackman

🤖 Generated with Claude Code

https://claude.ai/code/session_01HbDsntotWVcM9MvHmktSa8

The build script chose compiler flags with `cfg!(target_feature)`, which
describes the build script's own compilation rather than the target. It
also emitted `-mcmpxchg16b`, which clang and gcc both reject (the flag
is `-mcx16`), `-mfma4` (AMD FMA4) for `fma` instead of `-mfma`, and
`-mbmi` alone for BMI2. Building with `-C target-cpu=x86-64-v3` failed
with `clang++: error: unknown argument: '-mcmpxchg16b'`.

Read the target features from `CARGO_CFG_TARGET_FEATURE` and map them
with a table to flags that clang and gcc both accept: `-mcx16` for
`cmpxchg16b`, `-mfma` for `fma`, and `-mbmi` and `-mbmi2` separately.
Features that are not in the table emit no flag. The C++ now gets the
target features explicitly, so results may differ in the last digits
from before and are deterministic for a given `target-cpu`.

The same table is in `bhtsne` in 10XDev/scan-rs; there is no shared
crate, so each copy has its own unit tests.

Jira: CELLRANGER-10331
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HbDsntotWVcM9MvHmktSa8
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.

1 participant