Repository navigation
Conversation
The transmute lowering rejected any cast involving a shared-memory pointer (addrspace(3)) because its physical width is target-dependent: legacy NVPTX/PTX uses 64-bit pointers, while modern NVVM (libNVVM) uses 32-bit p3:32. With no target mode available at this stage, the previous code bailed out before choosing a size. Add `shared_address_space_pointer_width` to `LoweringOptions` and use it in `llvm_type_size_align` and `scalar_bit_width` so the correct width (4 or 8 bytes) is reported for addrspace(3) pointers. The general transmute paths then handle shared-pointer casts correctly, and a width-mismatched cast still fails with a clear size-mismatch error. `cuda-oxide-codegen` derives the width from `IntrinsicBackend` (LibNvvm -> 32, LlvmNvptx -> 64). Fixes NVIDIA#874.
16472cc to
7d2fc05
Compare
|
So as I understand it, this implements issue #874's proposed fix. The problem is with that proposal, not with the execution. The approach isn't right for three independent reasons: 1. The width isn't knowable at the point the PR reads it. The pipeline decides between the two NVVM dialects only after lowering has already run: So the PR guesses from 2. Even with the right width, it's the wrong ruler. Rust defines transmute legality by rustc's layout, and rustc sizes every nvptx64 pointer at 8 bytes, shared or not: // valid Rust: rustc checks 8 == 8, so this must compile on every target
let p: *mut u32 = transmute(bits as usize);
// this PR in p3:32 mode: re-measures the shared pointer at 4 bytes,
// rejects with "size mismatch: 8 vs 4"That rejected round trip is the very thing #874's acceptance criteria ask for on the p3:32 target. Meanwhile the added unit test transmutes 3. It turns a loud error into silently wrong bytes. This is the disqualifying one.
Two casts Rust considers identical return different integers, and one of them manufactures null-looking values from valid pointers. This is the hazard Two further notes:
The sound fix needs no width at all:
That version is implemented and verified:
It lands as its own PR shortly; closing this one with |
…address space (#884) * fix(mir-lower): lower scalar shared-pointer transmutes through the generic address space A Transmute touching a shared-memory (addrspace(3)) pointer was rejected wholesale because the pointer's physical width is target-mode dependent (64-bit PTX/legacy, 32-bit modern NVVM p3:32) and lowering runs before a data layout is chosen. The scalar forms never needed the physical width: Rust transmute legality is defined by rustc layout, where every nvptx64 pointer is 8 bytes, and the codebase's own emit_ptr_to_int invariant already makes a shared pointer's Rust-visible integer form its 64-bit GENERIC address, never a raw space-local offset. Narrow the rejection to the transmutes that would actually observe physical pointer bytes, and let the scalar arms handle the rest through the generic-address bridge: - shared ptr -> integer: addrspacecast to generic, then ptrtoint (already handled by the early emit_ptr_to_int return; unchanged) - integer -> shared ptr: inttoptr to generic, then addrspacecast into the shared space (emit_int_to_ptr already implemented this; the blanket guard just never let it run) - ptr <-> ptr across address spaces: direct addrspacecast - ptr -> non-integer scalar: the intermediate ptrtoint now also goes through emit_ptr_to_int, so the invariant holds on every arm Aggregate transmutes containing shared pointers (including transparent wrappers, which stay llvm.struct at this stage) remain intentionally unsupported: their memory round-trip would store the pointer's physical bytes at an unknowable width and leak raw shared-local offsets into Rust-visible bytes. The rejection now sits on the memory-round-trip dispatch points and names the types with an actionable diagnostic. Behavior is identical under both NVVM dialects and the llc path: no LoweringOptions changes, no width plumbing. New GPU example shared_ptr_transmute round-trips a shared pointer through usize and through a generic pointer alias, then writes and reads shared memory through the reconstructed pointers; verified on sm_120 through the default llc path, CUDA_OXIDE_NO_OPT=1, and the modern NVVM path (--emit-nvvm-ir --arch=sm_120, p3:32 datalayout). Unit tests cover usize -> shared, shared <-> generic, and the aggregate rejection. Closes #874. Supersedes #879. * style(shared_ptr_transmute): allow the ptr-cast clippy lint on the four transmute helpers The as-cast form clippy suggests lowers through the pointer-cast path; Transmute lowering is the path this example exists to regression-test.
Summary
mir-lowerrejected everyTransmuteinvolving a shared-memory pointer(
addrspace(3)) with a target-mode-dependent-width error. The physical widthis target-dependent: legacy NVPTX/PTX uses 64-bit pointers, while modern NVVM
(libNVVM) uses 32-bit
p3:32. With no target mode available at this stage,the previous code bailed out before choosing a size.
This adds
shared_address_space_pointer_widthtoLoweringOptionsand uses itin
llvm_type_size_alignandscalar_bit_widthso the correct width (4 or 8bytes) is reported for
addrspace(3)pointers. The existing transmute pathsthen handle shared-pointer casts correctly, and a width-mismatched cast still
fails with a clear size-mismatch error.
cuda-oxide-codegen::lower_to_llvmderives the width fromIntrinsicBackend(
LibNvvm-> 32,LlvmNvptx-> 64), so the plumbing reaches the real codegenpath automatically.
rustc-codegen-cudadoes not constructLoweringOptionsdirectly, so it needs no source change.
Fixes #874.
Verification
crate::mir-lower:cargo test -p mir-lowergreen (146 integration + 76 lib tests, 0 failed).crates/mir-lower/src/convert/ops/cast.rs:transmute_involving_shared_pointer_uses_target_dependent_width(both 32- and 64-bit modes).transmute_shared_pointer_to_wrong_width_integer_is_rejected.cargo test -p cuda-oxide-codegengreen except for one pre-existing,environment-specific failure (
ptx::tests::legacy_opt_failure_warns_but_experimental_mode_fails,which invokes
/bin/falseand fails identically onmainwithout this change).Scope / caveat (partially verified)
The 32-vs-64-bit selection is keyed off
IntrinsicBackend::LibNvvm== modernNVVM (
p3:32). This matches the existing data-layout assumptions inconvert/types.rs, but the final PTX/object correctness of shared-pointertransmutes on real hardware still needs validation in CI with the CUDA toolkit
and a GPU, which is unavailable in this dev environment.