Skip to content

feat(cargo-oxide): select extra-rustflags by target triple - #1

Merged
midagedev merged 2 commits into
midagedev:bloomeryfrom
avlp12:cargo-oxide-per-target-extra-rustflags
Oct 11, 2026
Merged

midagedev merged 2 commits into
midagedev:bloomeryfrom
avlp12:cargo-oxide-per-target-extra-rustflags

Conversation

@avlp12

@avlp12 avlp12 commented Oct 11, 2026 •

Copy link
Copy Markdown

Requested by Alis

Summary

extra-rustflags in .cargo/cuda-oxide.toml is one flat list, and the CARGO_ENCODED_RUSTFLAGS cargo-oxide exports hides Cargo's own target.<triple>.rustflags. So a project that wants a CPU flag for its host code gives it to every host.

Bloomery's -C target-cpu=znver3, written for an x86_64 workstation, reaches aarch64 builds on DGX Spark. There rustc warns on every crate that znver3 is not a processor, and on some crates it crashes inside LLVM (midagedev/bloomery#13).

This change lets a [target.<triple>] table hold an extra-rustflags list for one triple. It is the PR asked for in midagedev/bloomery#13. Once it lands, a Bloomery PR moves the [patch] pin and adds the tables.

Changes

crates/cargo-oxide only. No backend or codegen crate changes.

Which flags a build gets. It gets the top-level extra-rustflags, then the table of the triple it builds for. A triple with no table gets the top-level list alone.

How the triple is found, in this order:

  1. A --target among the Cargo arguments. These follow cargo oxide build --, in both the --target X and --target=X forms. A later -- that starts test or program arguments ends the search.
  2. CARGO_BUILD_TARGET. The command's own value comes before the inherited one. This also serves commands with no Cargo arguments, such as cargo oxide run <example>.
  3. The host triple of rustc -vV, run as the build would run it:
    • The compiler is RUSTC, else CARGO_BUILD_RUSTC, else rustc.
    • It runs in the command's directory, with the command's environment changes.

host-tuple in 1 or 2 means the host triple from 3. It counts as the same target as the host's own name.

Configs without tables. They resolve exactly as before and run no extra rustc -vV.

Named errors. A build stops with a named error instead of guessing when:

  • the config has tables but no triple can be found;
  • one command names two different targets;
  • the target is host-tuple and the host probe fails.

Config errors. cfg(...) keys, fields other than extra-rustflags, and wrong types are config errors. cargo oxide doctor reports these too. The target errors above appear only when a build command is constructed.

Known limits. Cargo's [build] target and [build] rustc in a config file are not read, and the host probe does not go through RUSTC_WRAPPER. A cross build passes --target. The README says so.

The second commit addresses review findings on the first: the host-tuple resolution, the probe's compiler and environment, and the README. Both commits are signed off. They can be squashed if you prefer one.

Testing

FAIL-first. per_target_extra_rustflags_reach_only_their_triple fails on 4da6c138: the table was ignored, and aarch64 got []. With this change, aarch64 gets -C target-cpu=native and no znver3, and x86_64 gets the x86 list. flat_extra_rustflags_still_reach_every_triple passes before and after. I ran these on a Mac with a stable toolchain.

New tests. They cover:

  • selection order, unlisted and unknown triples, and the host fallback;
  • --target parsing and the refused shapes;
  • host-tuple on the command line, in the command's env and in the inherited env, including dedup against the host's name, one probe for several mentions, and the error when the probe fails;
  • the probe's compiler choice, directory and environment;
  • a fake compiler whose host: line is read, and one that fails.

Results.

Where Commit Toolchain cargo test -p cargo-oxide fmt and clippy
DGX Spark (GB10, aarch64) both commits nightly-2026-08-28 255 passed, 0 failed cargo fmt -p cargo-oxide -- --check and cargo clippy -p cargo-oxide --all-targets -- -D warnings clean
Two DGX Sparks first commit nightly-2026-08-28 252 passed, 0 failed on each clean
Mac both commits stable 252 passed, 3 failed clean

The first-commit Spark runs used a pre-sign-off copy of the first commit, with the same tree. On the Mac, the same 3 tests also fail on 4da6c138.

End to end in Bloomery. This was on one Spark, inside nvidia/cuda:13.3.1-devel-ubuntu24.04, with the first commit's cargo-oxide installed. cargo oxide build --arch sm_86 -- -p bloomery-gpu-gates --features gpu --release --bin generate_qwen3moe -v builds:

  • The build passes no --target, so the host fallback picks the aarch64 table.
  • All 124 rustc invocations carry -C target-cpu=native, none carries znver3, and nothing crashes. The log has 20 more Running lines, which are build-script runs.
  • On the host's own CUDA 13.0 toolkit the flags are the same, but bloomery-gpu stops on CUDA 13.3 graph APIs. That is why the end-to-end build ran in the container.

x86_64. The flag list this code computes for Bloomery's per-target config with --target x86_64-unknown-linux-gnu is byte-identical to the list the old code computed for the flat config. Both were printed from a test harness, not from an x86 build. The x86 batch that the Bloomery maintainers described in midagedev/bloomery#13 is the real check.

Not run. I did not run just -f cuda-oxide/Justfile check. just and cargo-deny were not installed where I ran, and I ran the cargo-oxide crate's own gates instead.

  • just -f cuda-oxide/Justfile check passes. Not run, see above.
  • cargo oxide run <example> passes. Not run: the checks above cover flag selection and a Bloomery build, not running an example on a GPU.
  • New example added. Not applicable.

Checklist

  • All commits signed off (git commit -s)
  • SPDX headers on new source files. No new source files.

AI help: Claude (Anthropic) wrote the change and the tests, and ran the checks above on my machines at my direction.

🤖 Generated with Claude Code

avlp12 and others added 2 commits October 11, 2026 10:21
`extra-rustflags` in `.cargo/cuda-oxide.toml` was one flat list, and the
`CARGO_ENCODED_RUSTFLAGS` cargo-oxide exports hides Cargo's own
`target.<triple>.rustflags`. A project whose host code wants a CPU flag
therefore gave it to every host: `-C target-cpu=znver3` written for an
x86_64 workstation reached an aarch64 build (DGX Spark), where rustc warns
that znver3 is not a processor on every crate and crashes inside LLVM on
some.

A `[target.<triple>]` table now holds an `extra-rustflags` list for one
triple. A build gets the top-level list, then the table of the triple it
builds for: the command's `--target` (before any `--`), else
`CARGO_BUILD_TARGET`, else the host triple of `rustc -vV` run where Cargo
runs. A triple with no table gets the top-level list alone. With tables
present and no triple known, or two different `--target` values in one
command, the build stops with a named error instead of guessing. A config
without tables resolves exactly as before and runs no extra `rustc -vV`.
`cfg(...)` keys and fields other than `extra-rustflags` are refused by
name, so a table cargo-oxide cannot honor never builds without its flags;
`doctor` reports them through the existing config check.

Tests: a per-target config built with `--target aarch64-unknown-linux-gnu`
gets `-C target-cpu=native` and no znver3, and with `--target
x86_64-unknown-linux-gnu` the x86 list (red before this change: the table
was ignored and aarch64 got no flags); a flat list still reaches every
triple in the same position; selection order, unlisted and unknown
triples, the host fallback, `--target` parsing and the refused shapes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Alis Volat Propriis <[email protected]>
Review of the per-target `extra-rustflags` selection found three gaps:

- `--target host-tuple` (or `CARGO_BUILD_TARGET=host-tuple`), which
  Cargo accepts as the host triple, was looked up as a table named
  `host-tuple`, so the host's table was silently skipped. It now resolves
  through the host probe, asked at most once, and counts as the same
  target as the host's own name.
- The host probe ran `rustc` from PATH with the process environment. It
  now runs the compiler Cargo would run for the command (`RUSTC`, else
  `CARGO_BUILD_RUSTC`, the command's own value before the inherited one,
  else `rustc`) with the command's directory and environment changes, so
  a project `RUSTUP_TOOLCHAIN` reaches it too. The probe lives next to
  its one caller, and `backend::active_host_triple_in` goes.
- The README placed `--target` before `--`; Cargo arguments follow the
  first `--`. It now says so, names `CARGO_BUILD_TARGET` for commands
  without Cargo arguments, and lists `host-tuple`, the unread
  `[build] target` / `[build] rustc`, and that the probe does not go
  through `RUSTC_WRAPPER`.

The error for an unknown triple now also covers a probe that could not
run.

Tests: `host-tuple` on the command line and in both env sources, its
dedup against the host's name, one probe for several mentions, and the
named error when the probe fails; the probe's compiler choice, directory
and environment; and a fake compiler whose `host:` line is read, and one
that fails.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Alis Volat Propriis <[email protected]>
@midagedev
midagedev merged commit 87bc951 into midagedev:bloomery Oct 11, 2026
@midagedev

Copy link
Copy Markdown
Owner

Merged as 87bc951, with your authorship. Thank you, @avlp12. The old pin is tagged pin/4da6c138.

Next, please open the bloomery PR that moves the [patch] rev to 87bc951 and adds the [target.<triple>] tables. We will run the x86 identity check on our side.

This change is useful beyond bloomery. Upstream (NVIDIA/cuda-rust) main still has the flat list, and we found no open issue or PR for it. If you are willing, please send the same patch there under your name. When it merges upstream, we will move our pin to their version.

@midagedev

Copy link
Copy Markdown
Owner

@avlp12 Your upstream issue NVIDIA#1481 has had no reply for 10 days, and no PR is open for it. Would you open the PR there now, under your name, and link NVIDIA#1481? The patch is 87bc951 here. A PR gives the maintainers code to review. When it merges, we will move our pin to the upstream version.

midagedev pushed a commit that referenced this pull request Nov 24, 2026
…handshakes (NVIDIA#1716)

## What This PR Does

Replaces fixed 30ms `thread::sleep` delays with deterministic
synchronization in two concurrency/retry unit tests:

1. **`ETXTBSY` Version-Probe Retry Test (`cuda-artifact-finalizer`)**
2. **Single-Flight LTOIR Cache Lock Test (`cuda-host`)**

---

### 1. Deterministic Retry & Lock Contention Hooks

Previously, both tests slept for 30ms to approximate thread ordering,
which could either fail spuriously on loaded runners or pass without
exercising the retry/lock path:

```text
BEFORE:
  ptx.rs:         background thread sleeps 30ms, then drops write fd
  ltoir_cache.rs: winner sleeps 30ms hoping 7 other threads contend on lock

AFTER:
  ptx.rs:         `BUSY_RETRY_WAIT` hook drops write fd inside retry #1
                  (plus a second test asserting all 8 doubling delays: 2ms..256ms)
  ltoir_cache.rs: `KEY_LOCK_ARRIVALS` condvar holds the build until all 8 threads
                  reach `acquire_key_lock`
```

* Guarantees that `PtxAssembler::from_path` observes
`ExecutableFileBusy` at least once before succeeding.
* Guarantees that all 8 threads miss the cache fast path and contend on
the per-key lockfile before the build publishes.

---

## Verification

* **Tests & CI Gates**:
  * `cargo test -p cuda-artifact-finalizer -p cuda-host --lib`: **PASS**
* `cargo clippy -p cuda-artifact-finalizer -p cuda-host --all-targets --
-D warnings`: **PASS**
  * `cargo oxide fmt --check`: **PASS**

Part of NVIDIA#1680

Signed-off-by: Vyncint Ng <[email protected]>
midagedev pushed a commit that referenced this pull request Nov 24, 2026
## What This PR Does

Fixes `cargo oxide setup` in standalone projects so it builds the
project's `cuda-oxide` dependency once instead of attempting to compile
the user's crate as the codegen backend:

1. **Build-Free Setup Context & Standalone Refresh
(`crates/cargo-oxide/src/commands/`)**
2. **Cache-Preserving Refresh Planning
(`crates/cargo-oxide/src/backend.rs`)**

---

### 1. Standalone `cargo oxide setup` Resolution and Refresh

Previously, `Commands::Setup` called `resolve_context()`, which eagerly
built the backend once and set `ctx.codegen_crate = cwd` outside the
workspace. `setup()` then tried to build `ctx.codegen_crate` as
`rustc-codegen-cuda`, compiling the user's project and failing:

```text
BEFORE (standalone project):
  cargo oxide setup
  ──► resolve_context() builds backend #1, sets codegen_crate = <user-project>
  ──► setup() runs build_backend_from_source(<user-project>) ──► fails ("no library targets found")

AFTER:
  cargo oxide setup
  ──► resolve_setup_context() locates backend without building
  ──► workspace:  SetupPlan::BuildAndPublish (builds local backend once, publishes to cache)
  ──► standalone: SetupPlan::RefreshFromDependency (rebuilds from project's cuda-oxide dependency)
  ──► pinned:     SetupPlan::RefusePinned (refuses when CUDA_OXIDE_BACKEND or config pin is set)
```

* `refresh_plan` resolves `Cargo.lock` and checks toolchain
compatibility before clearing `~/.cargo/cuda-oxide/`, so lockless path
dependencies and refused toolchains never wipe a valid shared cache.

---

## Verification

* **Tests & CI Gates**:
  * `cargo test -p cargo-oxide`: **PASS** (287 passed)
  * `cargo clippy -p cargo-oxide --all-targets -- -D warnings`: **PASS**
* `RUSTDOCFLAGS="-D warnings" cargo doc -p cargo-oxide --no-deps`:
**PASS**

Closes NVIDIA#1688

---------

Signed-off-by: Praveen Perera <[email protected]>
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