Repository navigation
feat(cuda-core): derive DeviceCopy - #193
Merged
Merged
Conversation
This was referenced Jun 12, 2026
Signed-off-by: nik <[email protected]>
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
force-pushed
the
prepare-devicecopy-derive
branch
from
June 16, 2026 05:49
e0d8a5b to
6b8a183
Compare
Collaborator
|
Struct/union derive is sound. The enum arm wasn't:
|
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.
meefs
pushed a commit
to meefs/cuda-oxide
that referenced
this pull request
Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #192.
Problem
cuda_core::DeviceCopymarks types that are safe to copy between host and device memory, but users currently have to writeunsafe impl DeviceCopymanually 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 incuda-macrosand re-export it fromcuda-corenext to theDeviceCopytrait. The macro generates theunsafe impl ::cuda_core::DeviceCopyand a hidden type-checking helper that requires each field type to implementDeviceCopy. Generic type parameters also receive aDeviceCopybound on the generated impl.Diligence
This keeps the rule at the marker-trait API boundary: deriving
DeviceCopyis only valid when every stored field is itselfDeviceCopy. 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, souse cuda_core::DeviceCopy;brings both the trait and derive macro into scope.Tests
Added
cuda-coretrybuild coverage for both accepted and rejected derives:DeviceCopytype;DeviceCopy;DeviceCopy.Validation
cargo fmt --all -- --checkcargo test -p cuda-core --all-targetscargo test -p cuda-macros --all-targetscargo clippy --workspace -- -D warnings