Skip to content

feat(cuda-macros): make cuda_launch\! a caller-unsafe API - #171

Merged
nihalpasham merged 2 commits into
mainfrom
feat/cuda-launch-caller-unsafe
Jun 12, 2026
Merged

nihalpasham merged 2 commits into
mainfrom
feat/cuda-launch-caller-unsafe

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

cuda_launch\! now requires the caller's own unsafe { } block; a bare invocation no longer compiles.

Before: cuda_launch\! { kernel: k, ..., args: [...] }          compiles;
        the macro hides `unsafe` inside its expansion
After:  unsafe {                                              caller owns
            // SAFETY: args match k's signature ...           the promise
            cuda_launch\! { kernel: k, ..., args: [...] }
        }

Why unsafe rather than checked

Issue #29: the macro validates neither argument count nor types — a forgotten argument sends the driver past the end of the args array (host UB), and &struct marshals a host stack address the device dereferences. For embedded kernels, the checked answer already exists: #[cuda_module]'s generated methods make these mistakes compile errors, and the prerequisites landed earlier (#169 cooperative launch, #170 example ports). What remains for the macro is launching kernels loaded at runtime by name — external PTX, LTOIR-linked artifacts, fuzzer output — where no compile-time signature exists to check against. For that niche, safety cannot be built, only labeled honestly.

How

  • The expansion's four internal unsafe blocks are removed, surfacing the obligation to the caller (E0133 otherwise); the safety contract is documented on the macro in plain language.
  • A trybuild compile-fail test (launch_requires_unsafe) pins the rejection with expected stderr.
  • All surviving in-tree call sites wrapped with one-line SAFETY comments: manual_launch_generic, manual_launch_libdevice (the deliberate low-level API coverage), rustlantis-smoke, host_closure's secondary variant, coop_groups_demo's non-cooperative calls.
  • README and book sections now lead with #[cuda_module] and present the macro as the unsafe escape hatch. Not deprecated/removed — separate decision.

Evidence

  • All five touched examples execute on sm_120 (quiet machine, run serially).
  • cuda-macros/cuda-host test suites green including the new compile-fail case; clippy/fmt (root + per-example, edition-aware) clean; doc gate clean; full compile-only smoketest 86/86.

References #29

The legacy launch macro checks neither argument count nor types
(issue #29): a forgotten argument makes the driver read past the end
of the args array, and a `&struct` argument marshals a host stack
address the device then dereferences. For kernels embedded at compile
time the checked #[cuda_module] path is the replacement (cooperative
launch and the example ports landed earlier). The macro's remaining
legitimate niche is kernels loaded at runtime by name, where no
compile-time signature exists to check against, so safety cannot be
built there, only labeled.

This is the labeling: the expansion no longer wraps its calls to the
unsafe cuda_core launch functions, so every use must sit inside the
caller's own `unsafe { }` block, with the contract documented on the
macro (argument count, order, and types must match the named kernel's
real signature; pointer arguments must be device-accessible). A
trybuild compile-fail test pins that a bare cuda_launch\! is rejected
with the missing-unsafe error.

All remaining in-tree call sites (the two deliberate low-level API
examples, the fuzzer harness, host_closure's secondary variant, and
coop_groups_demo's non-cooperative calls) are wrapped with one-line
SAFETY comments. README and book sections now present #[cuda_module]
first and the macro as the unsafe escape hatch for runtime-loaded
modules. The macro is not deprecated or removed; that is a separate
decision.

References #29 (the typed-path half landed as the cooperative-launch
and example-port PRs).
@nihalpasham nihalpasham added host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) safety Memory safety, soundness, or undefined behavior labels Jun 12, 2026
The launch_requires_unsafe trybuild fixture dev-depends on cuda-host
and cuda-core so the real cuda_launch\! expansion type-checks, which
pulls cuda-bindings into the test build; its build script needs cuda.h.
Install the toolkit for the cuda-macros matrix row, the same pattern
as cuda-host/cargo-oxide/cuda-async.
@nihalpasham
nihalpasham merged commit 0412fd7 into main Jun 12, 2026
24 checks passed
nihalpasham added a commit to goog00/cuda-oxide that referenced this pull request Jun 12, 2026
…odule]

Main made bare cuda_launch! a caller-unsafe API (NVIDIA#171) after this
example was written, so its host side no longer compiles (E0133). The
typed #[cuda_module] launch path is the safe replacement the other
math examples already use; the 16-value x 2-width host comparison and
the SUCCESS marker are unchanged.
@nihalpasham
nihalpasham deleted the feat/cuda-launch-caller-unsafe branch June 12, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant