Skip to content

feat(cuda-device): expose raw shared-array pointer - #624

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
niklebedenko:upstreaming/raw-shared-array-pointer
Aug 1, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
niklebedenko:upstreaming/raw-shared-array-pointer

Conversation

@niklebedenko

@niklebedenko niklebedenko commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #623.

Summary

SharedArray::as_mut_ptr forces callers to create a whole-allocation mutable
reference before multiple CUDA threads can derive disjoint element pointers.
This adds an unsafe as_raw_mut_ptr(*mut Self) entry point and lowers it through
the same shared-to-generic conversion as the borrowed pointer methods. The
caller retains responsibility for disjoint accesses and CUDA synchronization,
while the API no longer requires an overlapping &mut SharedArray.

What changed

Raw receiver and lowering rule

  • Added SharedArray::as_raw_mut_ptr, with safety documentation centered on
    raw static mut provenance, per-thread disjointness, kernel lifetime, and
    synchronization.
  • Kept the returned pointer in generic address space 0, matching as_ptr and
    as_mut_ptr after their explicit conversion from shared address space 3.
  • Generalized the existing pointer-conversion emitter diagnostics so all three
    public entry points share the same lowering implementation.

Coverage and related paths

  • Replaced duplicated substring dispatch with one typed recognizer used by
    both intrinsic dispatch and call-result address-space classification.
  • Anchored recognition to the cuda_device crate, an exact SharedArray path
    component, and exact method names; unit tests cover canonical names and
    nearby false matches.
  • Extended the existing addressof_sharedarray regression without introducing
    another artifact: one 32-thread kernel writes distinct shared elements from
    the raw receiver, synchronizes, and checks their sum while retaining the
    original issue-54 behavior.

Known separate limitations

  • This is an unsafe escape hatch, not a safe ownership witness. Callers must
    prove that concurrent accesses are disjoint and place the required CUDA
    barriers. A higher-level safe partitioning API is separate work.
  • The focused LTOIR smoke on current upstream/main is independently blocked
    on CUDA 12.8 by cubin validation requiring generic ELF e_version == 1;
    nvJitLink emits the CUDA format value 0x80. The raw-pointer branch remains
    independent of that validator fix. Its PTX route passes directly, and the
    LTOIR route passes when tested with only that validator correction stacked
    transiently.
  • Dynamic shared-memory APIs and multi-artifact module lookup are unchanged.

Testing

  • Upstream-red: with only the GPU regression added to
    bead880cd7461c67a14bc69d10fa7def538f3394,
    scripts/smoketest.sh --only '^addressof_sharedarray$' --no-color failed
    during device compilation with Rust error E0599 because
    SharedArray::as_raw_mut_ptr did not exist.
  • Patched-green on an RTX 3080 (sm_86):
    cargo oxide run addressof_sharedarray --arch=sm_86 preserved the original
    result (21) and produced the 32-thread raw-pointer sum (528).
  • LTOIR integration: with only the separate CUDA-ELF validator condition
    applied transiently,
    scripts/smoketest.sh --only '^addressof_sharedarray$' --no-color passed
    1/1. Removing that unrelated condition reproduces Finalizer(InvalidCubin)
    after nvJitLink has produced a valid 5,152-byte CUDA ELF.
  • cargo test -p mir-importer -p cuda-device --all-targets passed
    (892 mir-importer tests, 76 cuda-device unit tests, the config extension
    test, and all 25 thread-index trybuild cases).
  • cargo clippy -p mir-importer -p cuda-device --all-targets -- -D warnings
    passed.
  • Example cargo clippy --all-targets -- -D warnings, workspace and example
    formatting checks, and git diff --check passed.

Checklist

  • The branch is based on current upstream main, or its dependency is explicit.
  • The regression fails on upstream main and passes on this branch.
  • Relevant sibling paths and edge cases are covered or called out above.
  • Formatting, focused tests, and relevant broader checks pass.
  • Commits include DCO signoff and new files have required headers.

@nihalpasham nihalpasham added enhancement New feature or request safety Memory safety, soundness, or undefined behavior codegen Device code-generation pipeline (Rust MIR to IR to PTX) intrinsics Device intrinsics and libdevice math mappings labels Aug 1, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

LGTM.

Every thread deriving a pointer into one static mut SharedArray had to create overlapping &mut references — aliasing UB even with disjoint accesses. The *mut Self raw receiver is the std escape hatch

Verified on hardware:

  • addressof_sharedarray passes on GPU: both PASS lines (21 and the 32-thread disjoint-write sum 528).
  • 977 tests across mir-importer and cuda-device.

One item for the toolchain-bump checklist: recognition depends on rustc's def-path rendering, so a future pretty-printing change would silently un-intercept these methods — worth a canary test eventually.

Merging;

closes #623.

@nihalpasham
nihalpasham merged commit 88b21fa into NVIDIA:main Aug 1, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared arrays lack a raw-pointer escape hatch for disjoint thread writes

2 participants