Skip to content

feat(cuda-core): derive DeviceCopy - #193

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:prepare-devicecopy-derive
Jun 16, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:prepare-devicecopy-derive

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #192.

Problem

cuda_core::DeviceCopy marks types that are safe to copy between host and device memory, but users currently have to write unsafe impl DeviceCopy manually for application data structs. That is an ergonomics gap and an API safety footgun: a manual impl can omit the field-level check that every contained type is also device-copy-safe.

Fix

Add a #[derive(DeviceCopy)] proc macro in cuda-macros and re-export it from cuda-core next to the DeviceCopy trait. The macro generates the unsafe impl ::cuda_core::DeviceCopy and a hidden type-checking helper that requires each field type to implement DeviceCopy. Generic type parameters also receive a DeviceCopy bound on the generated impl.

Diligence

This keeps the rule at the marker-trait API boundary: deriving DeviceCopy is only valid when every stored field is itself DeviceCopy. The derive handles structs, enums, and unions through the same field-check helper, so named and unnamed fields share one enforcement path. The public re-export follows the established trait-plus-derive pattern, so use cuda_core::DeviceCopy; brings both the trait and derive macro into scope.

Tests

Added cuda-core trybuild coverage for both accepted and rejected derives:

  • compile-pass coverage for named structs with scalar and array fields;
  • compile-pass coverage for tuple structs containing another derived DeviceCopy type;
  • compile-pass coverage for generic structs with the generated bound;
  • compile-pass coverage for enum and union forms whose fields are DeviceCopy;
  • compile-fail coverage proving a copyable host reference field is rejected because the field type is not DeviceCopy.

Validation

  • cargo fmt --all -- --check
  • cargo test -p cuda-core --all-targets
  • cargo test -p cuda-macros --all-targets
  • cargo clippy --workspace -- -D warnings

niklebedenko and others added 2 commits June 16, 2026 11:11
The DeviceCopy safety contract requires every bit pattern (including the
all-zero pattern from DeviceBuffer::zeroed, and arbitrary device-written
bytes read back by to_host_vec) to be a valid value of the type. That
holds for product types (structs/unions) when every field is DeviceCopy,
but not for enums: a discriminant leaves most byte patterns invalid, so a
zeroed or device-written buffer can materialize an out-of-range
discriminant (UB). This is the same reason bool/char/NonZeroU32 are not
DeviceCopy.

Reject enums in the derive with a clear diagnostic pointing at a manual
unsafe impl; keep struct and union support. Also use the type identifier
verbatim (not lowercased) for the hidden verifier fn name so types that
differ only in case no longer collide. Replace the enum pass-test with a
union-only pass-test and add an enum compile-fail test.
@nihalpasham
nihalpasham force-pushed the prepare-devicecopy-derive branch from e0d8a5b to 6b8a183 Compare June 16, 2026 05:49
@nihalpasham nihalpasham added enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) safety Memory safety, soundness, or undefined behavior labels Jun 16, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Struct/union derive is sound. The enum arm wasn't: DeviceCopy requires the all-zero pattern (DeviceBuffer::zeroed) to be valid, which an enum discriminant can't guarantee (#[repr(u8)] enum E { A=1, B=2 } → UB).

@nihalpasham
nihalpasham merged commit 784b4b6 into NVIDIA:main Jun 16, 2026
24 checks passed
nihalpasham added a commit to niklebedenko/cuda-oxide that referenced this pull request Jun 16, 2026
bool and char have validity holes (only 0/1 for bool, only valid Unicode
scalars for char). DeviceCopy promises that ANY value copied back from
the device has a valid bit pattern for Self, and to_host_vec does
set_len over the raw device bytes with no validation, so a kernel that
writes a non-0/1 byte into a DeviceBuffer<bool> (or a surrogate/out-of-
range u32 into DeviceBuffer<char>) is UB on readback. The trait's own
# Safety docs name bool and char as the canonical reason DeviceCopy must
be stronger than Copy, and the merged derive(DeviceCopy) macro (NVIDIA#193)
documents the same. Admitting them would contradict both.

Keep the representation-preserving parity additions, which are sound:
PhantomData<T> (zero-sized), MaybeUninit<T> (any bit pattern valid by
design), Wrapping<T> (repr(transparent) over T). Restore the # Safety
doc and the type-level test to reject bool/char.
@niklebedenko
niklebedenko deleted the prepare-devicecopy-derive branch June 24, 2026 11:40
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request 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.

Add #[derive(DeviceCopy)] for CUDA transfer-safe data types

2 participants