Skip to content

feat(cuda-macros): put the generated host surface behind a default-on host feature - #702

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
nh13:draft/device-only-macros
Aug 8, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
nh13:draft/device-only-macros

Conversation

@nh13

@nh13 nh13 commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Partially addresses #701 (the non-generic surface; generic #[kernel]s remain host-coupled and are tracked in the issue).

Problem

#[cuda_module] and #[kernel] emit host-only code into the consumer crate: LoadedModule, load/load_named/from_module, the launch and constant methods, the nested-module loader, and the CudaKernel marker impl. All of it names ::cuda_host or ::cuda_core, so a crate that only compiles kernels still drags in cuda-host -> cuda-core -> cuda-bindings -> cuda.h at build time.

        #[cuda_module] mod kernels { #[kernel] fn scale(...) {...} }
                            | expands to
   TODAY (always)           |    WITH default-features = false
   kernels        -> PTX    |    kernels        -> PTX
   PTX-merge markers        |    PTX-merge markers
   LoadedModule + load()    |
   launch()/prepare_..()    |         (nothing else)
   impl CudaKernel ...      |
        |                   |
   needs cuda.h + toolkit   |    needs nothing host-side

Fix

  • New host feature on cuda-macros, in default, following the async feature precedent in the same file. Existing consumers expand byte-identically.
  • Gates exactly the host surface; kernel bodies and the PTX-merge markers the codegen collector consumes are never gated.
  • The decision is threaded as an emit_host: bool parameter rather than a cfg! read, because the dev-dep cycle on cuda-host re-unifies the feature under cargo test --no-default-features; the parameter makes both settings testable in one build (five new unit tests).
  • Workspace: the cuda-macros entry takes default-features = false; cuda-host names features = ["host"] explicitly.

Maintainer commit on top (67c1c12)

  • Documented the mixed-graph pattern in the Cargo.toml feature comment and a new crate-level doc: a device-only kernel crate consumed by a host application must forward the feature itself (host = ["dep:cuda-host", "cuda-macros/host"]), because proc-macro features unify globally per build graph.
  • One-line note beside the generic-kernel emitters: generics stay host-coupled by design for now (TypeId machinery) and are deliberately not gated.
  • Verified with a scratch crate outside the repo: cargo check with default-features = false passes with zero cuda-host/cuda-core/cuda-bindings in cargo tree; adding --features cuda-macros/host fails with the documented E0433.

… `host` feature

`#[cuda_module]` and `#[kernel]` emit host-only code into the consumer
crate: the `LoadedModule` type, `load`/`load_named`/`from_module`, the
launch, prepare-launch and constant methods, the nested-module loader,
and the `CudaKernel` marker impl. All of it names `::cuda_host` or
`::cuda_core`, so a crate that only compiles kernels -- and never loads
or launches a module -- still has to depend on cuda-host -> cuda-core ->
cuda-bindings -> `cuda.h`.

Gate that surface on a new `host` feature, following the `async` feature
already in this file. `host` is in `default`, so nothing changes for an
existing consumer. A kernel-only crate takes cuda-macros with
`default-features = false` and drops the host stack entirely.

Measured on a crate depending only on cuda-device and cuda-macros, whose
dependency tree contains no cuda-host, cuda-core or cuda-bindings:

  cargo check                              -> 0
  cargo check --features cuda-macros/host  -> 101,
      "cannot find module or crate `cuda_host`"

The feature is only useful if it survives unification, which is why the
workspace entry sets `default-features = false` and `cuda-host` asks for
`features = ["host"]`. cuda-device sits in every kernel crate's graph, so
requesting `host` at that edge would turn it back on everywhere.

The decision is threaded as an `emit_host` parameter rather than read
from `cfg!` inside each emitter. Without that, the off path could not be
tested at all: this crate dev-depends on cuda-host, which unifies `host`
back on even under `cargo test --no-default-features`. Five new tests
cover both settings in one build.

Not covered: generic kernels. `generate_generic_cuda_kernel_impl` also
emits `GenericCudaKernel`, and its `<kernel>_ptx_name` helper is called
by the instantiation path, so gating it is a larger change with a
different risk profile. A crate with generic kernels still needs
cuda-host.

Signed-off-by: Nils Homer <[email protected]>
@nihalpasham nihalpasham added enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) build-related Build scripts, toolchain discovery, or compilation setup labels Aug 8, 2026
…and the generic-kernel exception

Proc-macro features unify globally per build graph, so a device-only
kernel crate consumed by a host application must forward the feature
itself (host = ["dep:cuda-host", "cuda-macros/host"]); otherwise any
crate enabling cuda-macros/host makes the kernel crate's expansion name
cuda_host it cannot resolve (E0433). State that where users will look:
the feature comment in Cargo.toml and a new crate-level doc in lib.rs.

Also note on generate_generic_cuda_kernel_impl that generic kernels
remain host-coupled by design for now (TypeId naming machinery lives in
cuda_host) and are deliberately not gated by the host feature.

Verified with a scratch crate outside the repo depending on cuda-device
and cuda-macros with default-features = false: cargo check passes with
zero cuda-host/cuda-core/cuda-bindings in cargo tree -e normal, and
cargo check --features cuda-macros/host fails with the documented E0433.
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: The right split at the right boundary: everything the macros generate that mentions the host crates now sits behind a host feature that is on by default, so existing users see zero change, while a crate that only defines kernels can turn it off and build with no CUDA installation at all. Kernel bodies and the markers the compiler collects are never gated. Threading the on/off decision as a parameter instead of reading the feature flag inline is what makes both settings testable in one build.

I pushed one commit on top:

  • documented the one sharp edge where users will look: cargo features are global per build, so a kernel-only crate used inside a host application must forward the feature itself (host = ["dep:cuda-host", "cuda-macros/host"]) or it hits a confusing build error.
  • a one-line note in code that generic kernels intentionally stay coupled to the host crates for now.

Verified with a scratch crate outside the repo:

  • a kernel-only build has zero host crates in its dependency tree, and turning the feature on fails as documented.

On your two questions:

Merging.

@nihalpasham
nihalpasham merged commit 7975811 into NVIDIA:main Aug 8, 2026
36 checks passed
nihalpasham pushed a commit that referenced this pull request Aug 13, 2026
#702 put the generated host surface behind a default-on `host` feature, so a
crate that only compiles kernels can take
`cuda-macros = { default-features = false }` and stop pulling
cuda-host -> cuda-core -> cuda-bindings -> `cuda.h`. That is what #701 asked
for, and it works today -- but nothing exercises it. No example takes that
configuration, and every CI lane that builds a kernel crate installs the
toolkit, so the whole point of the feature was reachable only by a user who
tried it.

The regression direction is quiet as well. Proc-macro features unify per build
graph, so one crate enabling `cuda-macros/host` makes every crate expanding
these macros get the host-emitting expansion -- including one that asked for
`default-features = false` and has no cuda-host to resolve the generated
`cuda_host::` paths. cuda-macros' manifest documents that trap; nothing catches
it, and it surfaces downstream as E0433 on a path the user never wrote.

Adds a fixture crate that exercises both macro entry points which emit host
items -- a bare `#[kernel]` (the `CudaKernel` impl) and a `#[cuda_module]` (the
loader and launchers) -- and a guard asserting the two properties #701 named:

  * cuda-host, cuda-core and cuda-bindings are absent from the resolved graph,
    read from `cargo metadata` rather than the manifest so an indirect
    reacquisition counts;
  * it type-checks with the CUDA environment variables unset.

The CI job deliberately installs no toolkit, since a toolkit on the runner would
mask the dependency the job exists to keep out.

The fixture carries its own `[workspace]` and is not a root member, so the root
`cargo metadata` still resolves 25 packages and no existing lane builds it.

Verified: guard passes on a clean tree (8-package host-free graph) and exits 1
under four mutations -- `default-features` dropped, cuda-host added back,
`#[cuda_module]` removed from the fixture, and the fixture made not to compile.
All eight fast guards still pass.

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham pushed a commit that referenced this pull request Aug 19, 2026
`crates/cuda-macros/tests/device-only` is a Cargo workspace of its own, and it
falls outside every formatting scope this job has:

  * `cargo fmt --all` at the repository root stops at the workspace boundary;
  * the `crates/rustc-codegen-cuda` step is a different workspace;
  * the examples step globs `crates/rustc-codegen-cuda/examples/**/Cargo.toml`,
    and the fixture lives under `crates/cuda-macros/tests/`.

`check-device-only-build.sh` runs `cargo check --locked` on it, so it compiles,
but nothing has ever run rustfmt over it.

That matters slightly more than for an ordinary fixture: this is the crate that
proves the #701/#702 property -- a kernel-only crate taking
`cuda-macros = { default-features = false }` needs no host surface and no
`cuda.h` -- so it is a file contributors will edit when that gating changes, and
the one file where a formatting drift would not be caught.

It is formatted correctly today, so this step fails nothing now. Verified both
directions:

  clean tree              new step passes
  fixture deliberately
  unformatted             new step fails; `cargo fmt --all --check` at the root
                          still passes, which is the gap in one line

clippy has the same blind spot on this fixture plus three nested example
workspaces; that is a separate change to `clippy.yml` and does not touch this
file.

`fmt.yml` still parses, 5 steps.

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham added a commit that referenced this pull request Aug 19, 2026
…fixture (#1026)

* ci: clippy skips three nested example workspaces and the device-only fixture

`clippy.yml`'s per-example loop is one level deep:

    for ex in crates/rustc-codegen-cuda/examples/*/; do

Three manifests sit below that and are not reachable from their parent either,
because the parent does not list them in `[workspace] members`:

  cutile_inter_kernel/simt          own [workspace]
  interop_cubin_identity/device     own [workspace]
  mathdx_ffi_test/libmathdx-sys     plain path dependency

cargo *builds* the third as a dependency of its parent but never lints it, and
the first two are separate workspace roots the parent run stops at. So none of
the three has ever been linted, while `fmt.yml` reaches all three through its
`**/Cargo.toml` glob -- the asymmetry is what makes this a gap rather than a
policy.

Membership is the test rather than a hardcoded list of three, so a future
nesting is picked up without editing this file again.

A fourth workspace is outside `examples/` entirely and outside every existing
scope: `crates/cuda-macros/tests/device-only`. It is its own workspace, so the
root `cargo clippy --workspace` stops at it, and the examples loop never sees
it. It is also the only thing proving the #701/#702 property that a kernel-only
crate needs no host surface, and `check-device-only-build.sh` runs
`cargo check` on it but never a lint. Added explicitly.

All four are clean today, so this fails nothing now -- verified by running
`cargo clippy --all-targets -- -D warnings` in each:

  cutile_inter_kernel/simt        Finished, no warnings (needs CUDA_TOOLKIT_PATH)
  interop_cubin_identity/device   Finished, no warnings
  mathdx_ffi_test/libmathdx-sys   Finished, no warnings
  cuda-macros/tests/device-only   Finished, no warnings

Verified the step itself by extracting its `run` block from the parsed YAML and
executing it with `cargo clippy` stubbed to `echo`: it visits 219 directories --
the 215 top-level examples it visited before, plus the three nested manifests
and the fixture -- and the three parents are still visited separately, so
nothing is lost. `clippy.yml` still parses.

Signed-off-by: Vyncint Ng <[email protected]>

* ci: pass --locked to the new clippy invocations and note the grep's assumptions

All four directories the new step visits resolve against tracked
lockfiles: cutile_inter_kernel/simt, interop_cubin_identity/device and
the device-only fixture carry their own Cargo.lock, and
mathdx_ffi_test/libmathdx-sys resolves through its parent's lock as a
path-dependency member. check-device-only-build.sh already runs the
fixture with --locked because a check that can rewrite its own input is
not a check; the lint invocations now hold the same line. Verified with
cargo metadata --locked in all four directories.

Also record the two assumptions behind the members-list grep: skipping
a member is only safe because the parent's own clippy run lints it, and
a [workspace] exclude entry would match the pattern the same way and
wrongly skip that directory (no example uses exclude today).

---------

Signed-off-by: Vyncint Ng <[email protected]>
Co-authored-by: nihalpasham <[email protected]>
nihalpasham pushed a commit that referenced this pull request Aug 19, 2026
…1043)

`cargo deny check` stops at a `[workspace]` boundary, which is why this
workflow already runs it twice: once at the root and once for
crates/rustc-codegen-cuda. There is a third such root under version control
that resolves third-party crates, and nothing judges it.

`crates/cuda-macros/tests/device-only` declares its own `[workspace]` on
purpose -- the fixture exists to prove a graph that does *not* contain
cuda-host (#701/#702), so it must resolve independently rather than share the
root lock. That same boundary puts it outside all three existing runs: the
root `cargo deny check` stops at it, the backend run names a different
manifest, and `check-example-license-policy.sh` walks
`crates/rustc-codegen-cuda/examples` only.

The gap is not theoretical. Comparing its lock against both governed locks:

    proc-macro2   fixture 1.0.107   root 1.0.106   backend 1.0.106
    quote         fixture 1.0.47    root 1.0.45    backend 1.0.44
    syn           fixture 2.0.119   root 2.0.117   backend 2.0.114
    unicode-ident fixture 1.0.24    root 1.0.24    backend 1.0.22

Three of its four third-party `(name, version)` pairs appear in neither
governed lock, so no advisory, license, ban or source policy has ever been
applied to them. `(name, version, source)` is this repo's own unit of policy
-- it is the grouping key `check-example-license-policy.sh` uses to argue its
26-runs-for-187-workspaces equivalence -- so a version present nowhere else
is a distinct policy question, not a duplicate.

Add the third run, with `--locked` like the other two: a check that can
rewrite its own input is not a check, and verified here, deleting a package
block from that lock makes the run stop with "cannot update the lock file"
rather than silently re-resolving.

`crates/fuzzer/rustlantis` is the one remaining `[workspace]` root and is
deliberately left out: it is a vendored upstream project rather than
first-party code, so bringing its graph under deny.toml is a policy call for
a maintainer, not a gap to close silently.

Two present-tense counts in this file had also drifted and are corrected
against the current tree: the backend workspace resolves 88 third-party
crates, not 82 (`cargo metadata --manifest-path
crates/rustc-codegen-cuda/Cargo.toml`, packages with a non-null source), and
the example-policy script is 31 runs over 217 lock files, not 26 over 187.

Verified: the new invocation reports `advisories ok, bans ok, licenses ok,
sources ok` and exits 0 on today's tree, so main stays green; every workflow
still parses; and `just check-guards` carries the same third run so the local
mirror keeps predicting CI.

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham pushed a commit that referenced this pull request Aug 21, 2026
… roots (#1114)

`check-dependency-licenses.sh` builds its required set from two manifests:

    required="$(
        {
            declared_crates Cargo.toml
            declared_crates crates/rustc-codegen-cuda/Cargo.toml
        } | LC_ALL=C sort -u
    )"

and the comment above it says "Both first-party workspaces, not just the root
one". There are three. `cargo metadata` stops at a `[workspace]` boundary, so
one pass per root is the only way to reach them all, and
`crates/cuda-macros/tests/device-only` declares its own `[workspace]` on
purpose: the fixture exists to prove a graph that does *not* contain
`cuda-host` (#701/#702), so it must resolve independently rather than share the
root lock. That boundary puts it outside both passes.

The gap is live, not theoretical. Its member `device-only-kernels` has no row
in `dependency-licenses.csv`, and adding the third pass says so immediately:

    error: dependency-licenses.csv is missing a row for:
      device-only-kernels

`publish = false` is not what excluded it -- `cuda-intrinsics-gen` and
`reserved-oxide-symbols` are both unpublished root members and both have rows.
The only reason it was absent is that no pass ever looked at that workspace.

This is #1043's gap in the sibling guard. That fix taught `cargo deny check`
the third root, which governs the license *policy* over the resolved graph.
This guard covers the other half, stated in its own header: "that the
human-readable inventory does not silently fall behind what the workspace
declares." Both halves need all three roots, and until now only one had them.

What it protects going forward: the fixture currently declares nothing but two
first-party path dependencies, so the one missing row is the whole of today's
drift. But a third-party dependency added there would have been recorded
nowhere -- cargo-deny would judge its policy and the inventory would stay
silent, which is exactly the asymmetry this pair of guards exists to prevent.

Changes:

* the third `declared_crates` pass, plus a rewritten comment that enumerates
  all three roots and says why each is separate, so the next reader does not
  have to rediscover that `cargo metadata` stops at the boundary;
* the `device-only-kernels` row, matching the column layout of the other
  first-party rows and inserted in sort position.

Verified, both directions:

* drop the CSV row, keep the third pass -> fails naming `device-only-kernels`;
* keep the row, drop the third pass -> reports "OK: ... all 58 declared
  crates" while blind to that workspace, which is the state being fixed;
* restored -> "OK: dependency-licenses.csv records all 59 declared crates" and
  the example half unchanged.

`scripts/check-example-license-policy.sh`, the other consumer of the CSV, still
reports "OK: deny.toml holds over every example workspace". `bash -n` clean.

Signed-off-by: Vyncint Ng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-related Build scripts, toolchain discovery, or compilation setup enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants