Skip to content

mir-importer: support pointer distance intrinsics - #206

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-ptr-offset-unsigned
Jun 16, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-ptr-offset-unsigned

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #205.

Problem

Rust raw pointer distance methods lower to compiler intrinsics such as ptr_offset_from and ptr_offset_from_unsigned. cuda-oxide did not handle those intrinsics, so device code using offset_from, offset_from_unsigned, or their byte-oriented wrappers failed during MIR import.

The compiler rule is that pointer distance is an address difference divided by the pointee size, with signed and unsigned result types preserved.

Fix

Add shared lowering for core/std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned}.

The lowering:

  • exposes both pointer operands as integer addresses;
  • subtracts this - other;
  • divides by the rustc-reported pointee size;
  • returns isize for the signed intrinsic and usize for the unsigned intrinsic.

The importer now errors explicitly when the pointer operand type cannot be identified, when layout lookup fails, or when the pointee is zero-sized.

Diligence

The signed and unsigned intrinsic paths share one helper so the address-difference rule cannot drift. The helper uses rustc layout data for pointee size rather than deriving size from surface syntax or falling back to 1.

The regression covers stable Rust pointer methods instead of calling compiler intrinsics directly, so it exercises the user-visible API shape. It includes both element-distance and byte-distance wrappers.

Tests

Added crates/rustc-codegen-cuda/examples/ptr_offset_from with a standalone Cargo.toml and Cargo.lock.

The example computes four distances on device:

  • positive offset_from_unsigned;
  • negative signed offset_from;
  • signed byte_offset_from;
  • unsigned byte_offset_from_unsigned.

The host asserts exact results for a u32 slice with lo = 3 and hi = 9: [6, -6, 24, 24].

The regression fails on upstream/main without the lowering and passes on this branch.

Validation

cargo fmt --all -- --check
cargo check -p mir-importer
cargo clippy -p mir-importer -- -D warnings
(cd crates/rustc-codegen-cuda/examples/ptr_offset_from && cargo clippy --all-targets -- -D warnings)
scripts/smoketest.sh --compile-only --only '^ptr_offset_from$' --no-color
cargo oxide run ptr_offset_from

@nihalpasham nihalpasham added enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) intrinsics Device intrinsics and libdevice math mappings labels Jun 16, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Lowering looks right and it checks out on GPU ([6, -6, 24, 24], the negative slot exercises the signed path). Layered one thing on top:

  • added a supported-features.md row for the pointer-distance intrinsics

Same types.rs insertion point as #212, so one of the two will want a quick rebase at merge. Otherwise good to go.

@nihalpasham
nihalpasham force-pushed the upstream-ptr-offset-unsigned branch from 4368f60 to 316cb67 Compare June 16, 2026 09:49
@nihalpasham
nihalpasham merged commit 16672c2 into NVIDIA:main Jun 16, 2026
24 checks passed
@niklebedenko
niklebedenko deleted the upstream-ptr-offset-unsigned 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

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support raw pointer distance intrinsics

2 participants