Skip to content

mir-importer: index slice data pointers by element - #210

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-slice-array-index
Jun 16, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-slice-array-index

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #209.

Problem

The place-address walker extracts field 0 from fat slice values so it can continue indexing through the original backing storage. When the slice element type is itself an array, such as &mut [[f32; 2]], that extracted data pointer has type ptr<[f32; 2]>.

The compiler rule is that this pointer addresses slice elements. Therefore the first Index or forward ConstantIndex after the fat-slice deref must use pointer arithmetic over [f32; 2] rows. It must not be reclassified as a pointer to one array object and indexed inside row 0.

Fix

Track when the current address is the data pointer just extracted from a fat slice. For the immediately following Index or forward ConstantIndex, force the address walker to use the direct pointer-offset path. Clear the marker after consuming that indexed projection, and also clear it for ordinary sized field access.

This keeps ordinary array-object indexing unchanged: a true pointer to [T; N] still uses mir.array_element_addr; only a pointer known to originate as fat-slice data uses element-sized mir.ptr_offset for the first index.

Diligence

I checked the sibling lowering paths that decide between pointer offset and array-element address:

  • translate_place_addr_from_slot, which serves Rvalue::Ref, Rvalue::AddressOf, and projected assignment destinations;
  • emit_indexed_element_addr, the shared dispatch point for runtime and forward constant indices;
  • MirPtrOffsetOp lowering, which scales by the pointer pointee type;
  • MirArrayElementAddrOp lowering, which is correct for indexing inside one array object.

Tests

Extended crates/rustc-codegen-cuda/examples/slice_get_mut with three nested-array fat-slice cases:

  • write_nested_slice_row_ref: takes let elem = &mut rows[row][0] and writes through the reference;
  • write_nested_slice_const_row_ref: takes let elem = &mut rows[1][0] and writes through the reference;
  • write_nested_slice_row_assign: writes through direct projected assignment rows[row][0] = value.

The runtime-row cases prove the Index path, the literal-row case proves the forward ConstantIndex path, and the assignment case proves the statement-level destination path that reuses the same address walker.
I also checked the raw Rust MIR dump for the literal-row kernel; the relevant projection is emitted as (*_9)[1 of 2][0 of 1], so this is covering the forward constant-index arm rather than another runtime-index lowering.

All three cases initialize row 0 column 1 to a sentinel and row 1 column 0 to zero. On upstream/main with only the regression applied, they write the sentinel column in row 0:

write_nested_slice_row_ref     FAIL    (elem[0] = [[10.0, 42.0], [0.0, 99.0]])
write_nested_slice_const_row_ref FAIL    (elem[0] = [[10.0, 62.0], [0.0, 99.0]])
write_nested_slice_row_assign  FAIL    (elem[0] = [[10.0, 52.0], [0.0, 99.0]])

On this branch, they write row 1 column 0:

write_nested_slice_row_ref     PASS    (elem[0] = [[10.0, 77.0], [42.0, 99.0]])
write_nested_slice_const_row_ref PASS    (elem[0] = [[10.0, 77.0], [62.0, 99.0]])
write_nested_slice_row_assign  PASS    (elem[0] = [[10.0, 77.0], [52.0, 99.0]])

Validation

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

niklebedenko and others added 2 commits June 16, 2026 11:22
…esting

Two additions on top of the slice-data element-indexing fix:

1. Localize the current_is_slice_data invariant. Capture-and-clear the
   flag at the top of the address-walk loop and set it only in the Deref
   continuation that hands off a slice DATA pointer. This removes the
   per-arm resets and makes it structurally impossible for a future
   projection arm to leak the flag forward. Proven codegen-neutral: the
   slice_get_mut .ll is byte-identical before and after.

2. Add write_nested_slice_wide_nonzero_col, covering a wider nested
   element ([f32; 3]) written at a NON-ZERO inner column (col 2). The
   existing nested kernels all used column 0, so the 'inner index reverts
   to array-aware' half was exercised only at offset 0. The generated .ll
   confirms the row index strides by a whole [3 x float] and the column
   index lands at element 2.
@nihalpasham
nihalpasham force-pushed the upstream-slice-array-index branch from 94a59a9 to adaed3b Compare June 16, 2026 06:03
@nihalpasham nihalpasham added bug Something isn't working miscompile Produces incorrect PTX/IR (wrong generated code) codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Jun 16, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

I was able to reproduce the wrong store address for &mut [[T;N]][i][j] on main and the fix on the branch. So this LGTM 👍🏾 i.e. GPU-verified.

  • Layered: localized the slice-data flag (capture-and-clear at loop top; generated .ll byte-identical) + a wider-element, non-zero-column kernel (rows[row][2] over &mut [[f32;3]]).
  • cargo oxide run slice_get_mut --arch sm_90 → SUCCESS, all kernels pass.

@nihalpasham
nihalpasham merged commit 779e194 into NVIDIA:main Jun 16, 2026
24 checks passed
@niklebedenko
niklebedenko deleted the upstream-slice-array-index branch June 24, 2026 11:40
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
NVIDIA#236)

* refactor: resolve check-placement policy once per compile

The bounds-check gates were scattered environment reads:
check_hoisting_disabled() and force_device_checks() consulted process
globals at seven sites across classification, relocation, and proof
discharge. They become one CheckOptimizations policy — three
independent axes (hoist_to_preheaders, relocate_to_launch,
discharge_proofs) with named constructors: release() (all on),
device_debug() (checks stay where the source puts them; proofs still
discharge), and disabled() (the differential harness's semantic
reference, which inherits no proofs; 2026-08-12 review, S2).

The policy is resolved from the environment once at compiler
construction and carried on the function compiler, so it cannot
change between classification and emission within a compile. The
ablation env vars keep their names and semantics; behavior is
unchanged under every existing switch combination (differential
placement harness green). This is groundwork for NVIDIA#210, where
--device-debug selects device_debug() per compile and joins the
cache keys.

Signed-off-by: Melih Elibol <[email protected]>

* feat: device-debug, lineinfo, sanitizer, and opt-level compile options

CompileOptions gains opt_level, device_debug, lineinfo, and
sanitize_memcheck; a TileirasOptions carrier resolves them once and
threads through run_tileiras (--opt-level / --device-debug /
--lineinfo / --sanitize=memcheck), the disk-cache path, and both cache
keys. device_debug implies --opt-level 0 unless a level is set
explicitly, and selects the device_debug check policy: no preheader
hoisting, so every device-resident bounds check sits at the source
line that wrote it and inlined regions stay contiguous for the
rangeless -O0 -G DWARF contract. Proof discharge and launch relocation
are deliberately untouched — both remove a check from device code
entirely, and the bounded family's launch checks are load-bearing
(without relocation its undischarged checks are compile errors).

Cache correctness: the flags join the L1 key via CompileOptions and
the L2 key as new key material (existing disk entries recompile once),
and cache entries validate the flags through a formerly-padding header
byte — old release entries carry 0 there, which is exactly the release
encoding, so they keep validating without a format bump.

Tests: placement pins under device_debug (a hoistable check falls back
in place; discharge and launch checks undisturbed), key separation per
flag, entry-validation flag mismatches with legacy-zero acceptance,
option resolution, and GPU end-to-end launches of debug, lineinfo, and
memcheck-instrumented cubins through the real tileiras.

Closes NVIDIA#210. Closes NVIDIA#219 (opt_level reaches KernelCompiler via
.options(CompileOptions::new().opt_level(n)) and both cache keys).

Signed-off-by: Melih Elibol <[email protected]>

* docs: align device_debug and cache-entry comments with the implementation

The CompileOptions doc claimed device_debug disables launch relocation;
it only disables hoisting (relocation is load-bearing for the bounded
family). And three comments claimed old cache entries keep validating
via the flags byte — they cannot be reached at all, since the flags
also joined the key material and orphaned pre-flags entries; the byte
is defense in depth, now described as such, with a note on why the
entry format version was deliberately not bumped.

Signed-off-by: Melih Elibol <[email protected]>

---------

Signed-off-by: Melih Elibol <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) miscompile Produces incorrect PTX/IR (wrong generated code)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index fat-slice data pointers by slice element

2 participants