Skip to content

Construct a disjoint slice when a kernel builds one - #670

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:fix/disjoint-slice-aggregate-construction
Aug 6, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:fix/disjoint-slice-aggregate-construction

Conversation

@alejandro-soto-franco

Copy link
Copy Markdown
Contributor

Building a DisjointSlice inside a kernel fails device codegen whenever the construction survives optimisation, which it does as soon as the slice crosses a call.

What

DisjointSlice::from_raw_parts fails device codegen when the slice crosses a #[device] boundary:

Unsupported construct: Scalar-lowered ADT expected exactly one runtime field, found 0

The constructor writes a struct literal. That literal usually folds into its use before import, and it survives whenever the slice crosses a call the optimiser keeps. The type translator gives the ADT its own slice type rather than a struct, so the aggregate arm took it for a scalar-lowered newtype, looked for the one field carrying the collapsed scalar type, and found none.

How

mir.construct_disjoint_slice takes the data pointer, the element count and the runtime layout words the index space carries. mir.construct_slice cannot serve: its result is a MirSliceType and its arity is fixed at two, while a runtime row width adds a word after { ptr, len }.

Lowering is the same insert chain one field longer, built through make_disjoint_slice_struct so the field order cannot drift from the layout resolve_aggregate_slots indexes identically.

Field selection in the importer is positional, and the op's verifier checks it against the result type: the data pointer must point to the element type, the length must be an integer, and each remaining operand must match the index space's layout types in order. A reordered or retyped field fails verification rather than writing a row width into the length slot.

Testing

On an RTX 5060 (sm_120):

  • disjoint_from_raw_parts covers both shapes. The two-word form increments 4096 elements, and the three-word form scales 888 elements at a row width of 37, which is neither a power of two nor a multiple of the block width. Every element's expected value depends on its own index, so a constant write or a misaddressed element reports a mismatch. Both exact.
  • Lowering unit tests pin the slot layout for both widths. Writing the row width into any slot but the third fails them.
  • Verifier unit tests cover a missing index-space operand, a surplus one, a mismatched width type and a plain slice result.
  • cargo test --workspace and cargo clippy --workspace --all-targets clean.

Closes #667.

`DisjointSlice::from_raw_parts` writes a struct literal. The literal
usually folds into its use before import, and it survives whenever the
slice crosses a call the optimiser keeps, such as a `#[device]` helper
taking `&mut DisjointSlice`. The type translator gives the ADT its own
slice type rather than a struct, so the aggregate arm took it for a
scalar-lowered newtype, looked for the one field carrying the collapsed
scalar type, found none, and refused the program:

    Scalar-lowered ADT expected exactly one runtime field, found 0

Add `mir.construct_disjoint_slice`, taking the data pointer, the element
count and the runtime layout words the index space carries.
`mir.construct_slice` cannot serve: its result is a `MirSliceType` and
its arity is fixed at two, while a runtime row width adds a word after
`{ ptr, len }`. Lowering is the same insert chain one field longer,
built through `make_disjoint_slice_struct` so the field order cannot
drift from the layout `resolve_aggregate_slots` indexes identically.

Field selection in the importer is positional, and the op's verifier
checks it against the result type: the data pointer must point to the
element type, the length must be an integer, and each remaining operand
must match the index space's layout types in order. A reordered or
retyped field therefore fails verification rather than writing a row
width into the length slot.

Tests: the reported repro as `disjoint_from_raw_parts`, alongside a
runtime row width of 37 so the three-word form runs on GPU; lowering
unit tests pinning both slot layouts; verifier unit tests for a missing
space operand, a surplus one, a mismatched width type and a plain slice
result.

Closes NVIDIA#667.
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 6, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Verified the new op and the fix on hardware:

  • disjoint_from_raw_parts on RTX 5090: both kernels exact match (4096 elements incremented; 888 elements scaled at row width 37), covering the two-word and three-word slice shapes.
  • the mir.construct_disjoint_slice verifier + positional ZST filtering is the right fix per the compiler-gaps-are-bugs rule — the importer arm, not the kernel, was what needed to change.
  • cargo test across dialect-mir / mir-importer / mir-lower green.

Closes #667.

@nihalpasham
nihalpasham merged commit d6fbf02 into NVIDIA:main Aug 6, 2026
34 checks passed
nihalpasham added a commit that referenced this pull request Aug 6, 2026
…test verdict requires

Merging #670 (new example) and #665 (stricter smoketest verdicts) in the
same batch left this example failing the standard-category check: both
kernels verify to exact match but nothing prints SUCCESS/PASS/Complete,
and the stricter harness rightly no longer rescues such a log.
vyncint added a commit to vyncint/cuda-oxide that referenced this pull request Aug 6, 2026
scripts/smoketest.sh assumes two things about the examples that nothing
verified, and neither is reachable without a GPU: the compile-only lane
collapses every category into verdict_compile, so it exercises neither
classify() nor the standard-category success marker.

A `standard` example that verifies its results but prints no
SUCCESS/PASS/Complete marker is reported as FAIL (no success marker). That
landed two days ago: 981b9eb added a marker to disjoint_from_raw_parts on
main after NVIDIA#670 (the new example) and NVIDIA#665 (stricter verdicts) merged in one
batch. Nothing in an example's own directory says a marker is required, and
its author cannot see the consequence without hardware.

Separately, classify() falls through to `standard` for anything its seven
arrays do not claim, so a misspelled or renamed entry does not fail -- it
matches nothing and the example quietly gets the wrong verdict rules. All 45
entries across the 11 arrays are valid today, so that half is preventive.

Add scripts/check-example-smoketest-contract.sh and run it from
status-guard.yml, next to the existing guard that keeps ERROR_EXAMPLES and
STATUS.md in step. It reads sources only: no cargo, no toolkit, no LLVM.

The guard duplicates two of verdict_standard's greps, so it pins that
coupling: if smoketest.sh stops grepping SUCCESS|PASS|Complete, or changes
the skip pattern, the guard says so instead of quietly checking a contract
that no longer exists. Following NVIDIA#643 it also fails closed on a missing
python3 and on a parse self-test, rather than reporting success from a check
that could not run.

Fixes NVIDIA#682

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham pushed a commit that referenced this pull request Aug 7, 2026
…#683)

scripts/smoketest.sh assumes two things about the examples that nothing
verified, and neither is reachable without a GPU: the compile-only lane
collapses every category into verdict_compile, so it exercises neither
classify() nor the standard-category success marker.

A `standard` example that verifies its results but prints no
SUCCESS/PASS/Complete marker is reported as FAIL (no success marker). That
landed two days ago: 981b9eb added a marker to disjoint_from_raw_parts on
main after #670 (the new example) and #665 (stricter verdicts) merged in one
batch. Nothing in an example's own directory says a marker is required, and
its author cannot see the consequence without hardware.

Separately, classify() falls through to `standard` for anything its seven
arrays do not claim, so a misspelled or renamed entry does not fail -- it
matches nothing and the example quietly gets the wrong verdict rules. All 45
entries across the 11 arrays are valid today, so that half is preventive.

Add scripts/check-example-smoketest-contract.sh and run it from
status-guard.yml, next to the existing guard that keeps ERROR_EXAMPLES and
STATUS.md in step. It reads sources only: no cargo, no toolkit, no LLVM.

The guard duplicates two of verdict_standard's greps, so it pins that
coupling: if smoketest.sh stops grepping SUCCESS|PASS|Complete, or changes
the skip pattern, the guard says so instead of quietly checking a contract
that no longer exists. Following #643 it also fails closed on a missing
python3 and on a parse self-test, rather than reporting success from a check
that could not run.

Fixes #682

Signed-off-by: Vyncint Ng <[email protected]>
@alejandro-soto-franco
alejandro-soto-franco deleted the fix/disjoint-slice-aggregate-construction branch September 9, 2026 17:08
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

device codegen error for DisjointSlice::from_raw_parts

2 participants