Repository navigation
codegen: accept char in device-extern signatures - #1049
NgoQuocViet2001 wants to merge 2 commits into
Conversation
A `#[device]` extern taking or returning Rust `char` was rejected outright, with the message pointing at a u32 wrapper. `char` is a 32-bit Unicode scalar value: it already fills a parameter slot and needs no extension attribute in any position, so it lowers exactly as `u32` does and the C side declares it `unsigned int`. Route TyKind::Char through the same helper u32 uses. Nothing else moves -- plain i32 as a parameter, as a result, and behind a pointer. The device_ffi_test example gains char_to_upper on both sides and a kernel that round-trips 'q' and 'Z' through it, and its README documents the mapping along with the two caveats: a value outside the Unicode scalar range is UB once it reaches Rust, and rustc's improper_ctypes lint still fires. Fixes NVIDIA#875
nihalpasham
left a comment
There was a problem hiding this comment.
Verified this end to end on an RTX 5090: scripts/smoketest.sh device_ffi_test passes (LTOIR, sm_120), the round-trip prints 'q' -> 'Q' with 'Z' unchanged, and the generated .ll shows declare i32 @char_to_upper(i32) with no extension attribute in any position. The compiler change matches the #875 spec and closes it.
Two things block the merge, both in the example, patch below:
- The runner doesn't compile:
stream.alloc_zeros::<u32>(2)doesn't exist in this repo's cuda-core (that's cudarc's API). The sibling runners useDeviceBuffer::<u32>::zeroed(&stream, 2). - The new extern needs
#[allow(improper_ctypes)]. The per-example clippy gate runs-D warnings, and the warn-by-defaultimproper_ctypeslint fires oncharin a foreign declaration. The attribute survives the#[device]re-emission, so it can sit right on the item.
// Rust `char` is a 32-bit Unicode scalar, so it lowers to a plain i32
// parameter slot with no extension attribute -- the C side declares it
- // `unsigned int`.
+ // `unsigned int`. `improper_ctypes` still flags `char` (no C equivalent);
+ // the u32 mapping above is the supported ABI, so allow it here.
+ #[allow(improper_ctypes)]
fn char_to_upper(c: char) -> char;- let mut d_output = stream.alloc_zeros::<u32>(2).unwrap();
+ let d_output = DeviceBuffer::<u32>::zeroed(&stream, 2).unwrap();To land it:
- Apply the patch and push, or tick "Allow edits by maintainers" on this PR and we'll push the commit ourselves; it's already written and validated (fmt, clippy, GPU smoketest all green with it).
- Keep the two rustfmt reflow hunks in device_codegen.rs. Deleting the long string literal is what unlocks them, and rustfmt demands them.
Once the branch compiles clean we'll approve CI and merge.
Two review fixes from @nihalpasham, both in the example rather than the compiler change: - The runner called stream.alloc_zeros, which is cudarc's API and does not exist in this repo's cuda-core. Use DeviceBuffer::<u32>::zeroed like every sibling runner in this file. - The per-example clippy gate runs -D warnings, and the warn-by-default improper_ctypes lint fires on char in a foreign declaration. The attribute survives the #[device] re-emission, so it sits on the item.
|
Both applied exactly as written, pushed. Thank you for taking it to a 5090 and for writing the patch out — that saved a round trip.
The two rustfmt reflow hunks in
Ready for CI approval whenever you are. |
|
Thanks, @NgoQuocViet2001 — the core I could not repair this branch directly because maintainer edits are disabled, and its commits do not carry the required DCO trailers/signatures. I therefore landed the change independently, with explicit credit to this PR, in #1179. The maintainer version keeps the parameter/return example and adds the pointer form plus exact All exact-head hosted checks on #1179 are green. I am closing this PR only because #1179 supersedes its landing path, not because the compiler idea was declined. #1179 will close #875 when merged. |
* codegen: accept char in device extern signatures Map Rust char to a plain i32 across parameter, result, and pointee positions. Extend device_ffi_test with a char parameter/return/pointer case and a dual sm_90/sm_100 IR gate that rejects extension attributes. This independently lands and credits the sound compiler change proposed by @NgoQuocViet2001 in #1049. Closes #875 Signed-off-by: nihalp <[email protected]> * style(codegen): apply the backend rustfmt Signed-off-by: nihalp <[email protected]> * style(device-ffi): apply the example rustfmt Signed-off-by: nihalp <[email protected]> --------- Signed-off-by: nihalp <[email protected]>
Fixes #875, following @nihalpasham's spec in this comment.
The change
TyKind::Charinrustc_ty_to_device_extern_typenow routes through the sameunsigned_integerhelperu32uses:Not the
boolshape and notZeroExtInteger(32). Extension attributes widen sub-32-bit values into a 32-bit slot;charis already 32 bits and fills the slot as it stands, so it lowers to a plaini32in the parameter, result and pointee positions alike.Test surface
Per the updated acceptance criteria,
device_ffi_testis the surface rather than aTyCtxtunit test:extern-libs/external_device_funcs.cugainsunsigned int char_to_upper(unsigned int)— ASCII uppercase only, so its return value can never leave the Unicode scalar range.src/main.rsdeclares it asfn char_to_upper(c: char) -> char;in the existing#[device]extern block, exercisingcharin both the parameter and the result position, and adds atest_char_ffikernel that round-trips'q'(expects'Q') and'Z'(expects'Z'), with a host runner asserting both exactly..ll/.ptxshape, and both caveats: a value outside the Unicode scalar range (above0x10FFFF, or a surrogate in0xD800..=0xDFFF) is UB once it reaches Rust, and rustc'simproper_ctypeslint still fires oncharexterns.Test plan
cargo checkincrates/rustc-codegen-cuda— clean.cargo clippyincrates/rustc-codegen-cuda— no new errors or warnings.cargo fmt --check— clean (see the note below).rustfmt --edition 2024 --checkon the example'smain.rs— clean.scripts/smoketest.sh device_ffi_test— no NVIDIA device on this machine. The expected values in the new runner are the exact'q' -> 'Q','Z' -> 'Z'round trip, so a mismatch fails loudly rather than silently.One incidental hunk
cargo fmtreflows two calls in the samematch(rustc_ty_to_device_extern_typein theRawPtrandArrayarms) that it had previously left alone. The oldTyKind::Chararm held an over-long string literal, which made rustfmt give up on formatting the enclosing expression; with that line gone it formats the arm bodies it could not reach before. The tree iscargo fmt --checkclean before and after, so I took rustfmt's output rather than fighting it — happy to drop those two hunks if you would rather keep the diff to the one arm.