Skip to content

fix(mir-importer): write an intrinsic result through its projected destination - #769

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
alejandro-soto-franco:fix/intrinsic-call-projected-destination
Aug 10, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
alejandro-soto-franco:fix/intrinsic-call-projected-destination

Conversation

@alejandro-soto-franco

@alejandro-soto-franco alejandro-soto-franco commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #765. Closes #767.

An intrinsic call took its result type from the destination's local and stored the result into that local, discarding any projection on the place.

Summary

destination_type asks Place::ty for the projected type, and store_result_to_place writes through the projection instead of over the local.

Changes

  • crates/mir-importer/src/translator/terminator/helpers.rs: destination_type and store_result_to_place; emit_function_call uses the latter; emit_store_result_and_goto refuses a projected destination.
  • crates/mir-importer/src/translator/terminator/intrinsics/{bitops,saturating,bigint,exact_div}.rs: take the result type from destination_type.
  • crates/mir-importer/src/translator/statement.rs: pointer_is_mutable, pointer_address_space, reject_raw_field_index_on_enum_pointee, slot_array_element_ty and emit_array_element_store become pub(crate) so the store path can reuse them rather than restate them.
  • crates/rustc-codegen-cuda/examples/mir_projected_call_destination: new example covering the shapes on the device against a host oracle.

Rationale

RET.1 = bswap(x) on a (f64, u8) return carried the whole tuple as the call's result type and aimed its store at the tuple rather than at the field. (*RET) = bswap(x) carried the pointer and stored over the pointer rather than through it.

Ordinary calls never showed this, because rustc lowers a projected call destination into a call to a temporary followed by a store:

_9 = compute_stage1_trace(const 10_i32) -> [return: bb3, unwind continue]
(*_8) = move _9

An intrinsic keeps its projected destination through to codegen, so custom_mir is the only way to reach it.

Both symptoms were compile failures rather than wrong answers, each caught by a different check. A pointer result has no width, so it was refused where the lowering asks for one (#765). A tuple result reached the 8-bit bswap arm, which bitcasts without checking, and produced IR that LLVM will not parse (#767).

Place::ty is the existing idiom for this question: intrinsics/atomic.rs:1348 already uses it to type a cmpxchg destination. The store mirrors what translator/statement.rs does for an ordinary assignment to a projected place, taking the field's address through the local's slot with mir.field_addr, the pointer through the base place for a deref, and the element through emit_array_element_store for an index.

emit_store_result_and_goto is the other epilogue with the same fault. It has no Body, so it cannot compute the address of a place, and 600 of its 634 callers sit in the generated intrinsics/generated.rs, so widening its signature would mean changing the factory template. It now refuses a projected destination instead, which is the alternative to aiming a store at the wrong address.

Four single-element projections are modelled: a deref, a field, a runtime index, and a constant index. Each takes the address the ordinary assignment path takes for the same shape, and a constant index from the end is refused there and here alike. Anything longer than one element is refused rather than guessed at.

Two of those four came from running the work rather than from reading the code. Re-running the seeds that found these bugs left seed 1987 refused with projection [Index(2)], and the example below then hit ConstantIndex on its first compile, rustc having folded a literal index into one.

Validation

RTX 5060 Laptop GPU, compute capability 12.0, CUDA 13.3, driver 610.43.03.

The new example fails to compile on main with expected integer type for Rust bit intrinsic, and here reports:

host  deref case: 0x0000000060a0e21a
host  field case: 0x3ff8000000000007
host  index case: 0x00000060a0c31a0b

PASS: device and host agree on all three projections

Each value is checkable by hand. bswap(0x1AE2A060) is 0x60A0E21A; the field case is the bit pattern of 1.5f64 against the byte 7, which a byte swap leaves alone; and the index case keeps 11 and 33 in the elements the call did not name, which a store over the whole array would not.

The two reduced cases from the issues behave the same way, main against this branch:

(*RET) = bswap(x), RET: *mut i32     main: refused          branch: traces match
RET.1 = bswap(7_u8), RET: (f64, u8)  main: invalid bitcast  branch: traces match

The seven fuzzer seeds that surfaced both issues, re-run with composite_count raised so they generate the same programs:

seed 1170  PASS      seed 1792  PASS      seed 1967  PASS
seed 1202  PASS      seed 1825  PASS      seed 1987  PASS
seed 1420  PASS

All seven failed to compile on main: six with #765 and one with #767.

cargo test --workspace                              exit 0, no failing test
scripts/smoketest.sh --compile-only                 192 / 192
cargo clippy -p mir-importer --all-targets          clean
cargo clippy -- -D warnings (the new example)       clean
cargo fmt --check                                   clean

…stination

An intrinsic call took its result type from the destination's local and
stored the result into that local, discarding any projection on the place.
RET.1 = bswap(x) on a (f64, u8) return therefore carried the whole tuple as
the call's result type and aimed its store at the tuple rather than at the
field, and (*RET) = bswap(x) carried the pointer and stored over the
pointer rather than through it.

Ordinary calls never showed this. rustc lowers a projected call destination
into a call to a temporary followed by a store, so the importer never sees
the projection. An intrinsic keeps its projected destination through to
codegen, which is why custom_mir is the only way to reach it.

Both symptoms were compile failures rather than wrong answers, each caught
by a different check. A pointer result has no width and was refused where
the lowering asks for one. A tuple result reached the 8-bit bswap arm,
which bitcasts without checking, and produced IR that LLVM will not parse.

destination_type asks Place::ty for the projected type, the idiom
intrinsics/atomic.rs already uses to type a cmpxchg destination.
store_result_to_place takes the address of the place, through the local's
slot for a field and through the base place for a deref, mirroring what
statement.rs does for an ordinary assignment.

emit_store_result_and_goto carries the same fault and cannot be fixed the
same way: it has no Body to compute an address from, and 600 of its 634
callers are generated. It refuses a projected destination instead, which is
the alternative to aiming a store at the wrong address.
Re-running the seven fuzzer seeds that found NVIDIA#765 and NVIDIA#767 against the
previous commit left one, seed 1987, refused by the new projection check:

    call destination with projection [Index(2)] is not supported

An array element is the third shape a call destination carries in practice,
so it is modelled rather than refused. The arm mirrors what statement.rs
does for `arr[i] = value`, reading the index through its own place and
handing the array slot, the index and the value to the same
emit_array_element_store the ordinary assignment path uses.

slot_array_element_ty and emit_array_element_store become pub(crate) for
this, alongside the three helpers the previous commit exposed.

All seven seeds now pass.
…n all four shapes

Adds the example that covers the projected shapes end to end, and the
ConstantIndex arm the example turned up on its first compile: a literal
index in `RET[1] = bswap(x)` reaches the importer folded into
ConstantIndex rather than Index, so the runtime-index arm did not catch it.
A constant index from the end stays refused, as it is for an ordinary
assignment.

The example writes an intrinsic result through a dereferenced pointer, a
tuple field and an array element, runs the same bodies on the device and on
the host, and compares. A store aimed at the local rather than the place
shows up as a disagreement: the array case keeps 11 and 33 in the elements
the call did not name, which a store over the whole array would not.

Surface Rust cannot express any of this, since rustc lowers an ordinary
call with a projected destination into a call to a temporary followed by a
store. All three bodies are custom MIR for that reason.
…c result typing

The new helpers::destination_type duplicated types::translate_destination_type
verbatim while dropping the source location from its error. Delete the
duplicate and call the existing helper from the bitops, saturating, bigint,
and exact_div sites, keeping the located error message.

Also note the known eval-order fidelity gap on store_result_to_place: rustc
computes the call destination address before the call, this path materializes
it after; observable only from custom MIR where the callee mutates the
destination's base local through a &mut argument.
@nihalpasham

Copy link
Copy Markdown
Collaborator

Validated on 2x RTX 5090:

  • mir_projected_call_destination prints PASS on device,
  • smoketest --compile-only is 192/192, workspace suite clean.

Pushed one fixup:

  • the new helpers::destination_type duplicated types::translate_destination_type while dropping the source location from its error, so the four intrinsic sites now use the existing helper, and store_result_to_place documents the known eval-order gap.

Filed a follow-up for the remaining bare-local typing sites.

@nihalpasham
nihalpasham merged commit a134e7a into NVIDIA:main Aug 10, 2026
36 checks passed
vyncint added a commit to vyncint/cuda-oxide that referenced this pull request Aug 10, 2026
…ination

NVIDIA#769 taught `store_result_to_place` to write an intrinsic result through a
projected call destination and switched the bitops/saturating/bigint/exact_div
sites to type the result from `Place::ty`. Six sites still typed it from
`body.locals()[destination.local].ty`, i.e. the bare local, so with a projected
destination the call op was created with the whole aggregate's type (or the
pointer's, for `(*p)`) and `store_result_to_place` then stored that mistyped
value through a correctly typed projected address.

Converted, matching what the four fixed sites already do:

  float_math.rs   placeholder calls, and the sincos tuple-result path
  terminator/mod.rs  select_unpredictable
  memory.rs       DynamicSharedArray::get / ::offset
  generated.rs    elect_sync, via its render.rs template

The two memory.rs sites match on the destination's `RawPtr` kind to pull out a
pointee, so they need the projected *rustc* type rather than the dialect one:
they take `destination.ty(body.locals())` before the match, with the same
diagnostic `translate_destination_type` emits when the place cannot be resolved.
The generated.rs hunk is regeneration output, not a hand edit;
`cuda-intrinsics-gen -- check` passes.

No test accompanies this, and that is deliberate rather than an omission. I
tried to build a `#[custom_mir]` reproducer for the `select_unpredictable` site
-- `RET.1 = select_unpredictable(..)` on a `(u64, u32)` -- and could not make it
fail on main: with a literal condition rustc folds the call before the importer
sees it, and with the condition derived from an earlier custom-MIR result it
still compiles clean both before and after. A case that passes either way would
sit in the suite guarding nothing, so I left it out rather than claim coverage
it does not provide. The change is the same mechanical alignment NVIDIA#769 applied to
its four siblings, and the four unit-test suites above stay green.

Closes NVIDIA#773

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham pushed a commit to vyncint/cuda-oxide that referenced this pull request Aug 10, 2026
…ination

NVIDIA#769 taught `store_result_to_place` to write an intrinsic result through a
projected call destination and switched the bitops/saturating/bigint/exact_div
sites to type the result from `Place::ty`. Six sites still typed it from
`body.locals()[destination.local].ty`, i.e. the bare local, so with a projected
destination the call op was created with the whole aggregate's type (or the
pointer's, for `(*p)`) and `store_result_to_place` then stored that mistyped
value through a correctly typed projected address.

Converted, matching what the four fixed sites already do:

  float_math.rs   placeholder calls, and the sincos tuple-result path
  terminator/mod.rs  select_unpredictable
  memory.rs       DynamicSharedArray::get / ::offset
  generated.rs    elect_sync, via its render.rs template

The two memory.rs sites match on the destination's `RawPtr` kind to pull out a
pointee, so they need the projected *rustc* type rather than the dialect one:
they take `destination.ty(body.locals())` before the match, with the same
diagnostic `translate_destination_type` emits when the place cannot be resolved.
The generated.rs hunk is regeneration output, not a hand edit;
`cuda-intrinsics-gen -- check` passes.

No test accompanies this, and that is deliberate rather than an omission. I
tried to build a `#[custom_mir]` reproducer for the `select_unpredictable` site
-- `RET.1 = select_unpredictable(..)` on a `(u64, u32)` -- and could not make it
fail on main: with a literal condition rustc folds the call before the importer
sees it, and with the condition derived from an earlier custom-MIR result it
still compiles clean both before and after. A case that passes either way would
sit in the suite guarding nothing, so I left it out rather than claim coverage
it does not provide. The change is the same mechanical alignment NVIDIA#769 applied to
its four siblings, and the four unit-test suites above stay green.

Closes NVIDIA#773

Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham added a commit that referenced this pull request Aug 10, 2026
…ination (#780)

* fix(mir-importer): type six intrinsic results from the projected destination

#769 taught `store_result_to_place` to write an intrinsic result through a
projected call destination and switched the bitops/saturating/bigint/exact_div
sites to type the result from `Place::ty`. Six sites still typed it from
`body.locals()[destination.local].ty`, i.e. the bare local, so with a projected
destination the call op was created with the whole aggregate's type (or the
pointer's, for `(*p)`) and `store_result_to_place` then stored that mistyped
value through a correctly typed projected address.

Converted, matching what the four fixed sites already do:

  float_math.rs   placeholder calls, and the sincos tuple-result path
  terminator/mod.rs  select_unpredictable
  memory.rs       DynamicSharedArray::get / ::offset
  generated.rs    elect_sync, via its render.rs template

The two memory.rs sites match on the destination's `RawPtr` kind to pull out a
pointee, so they need the projected *rustc* type rather than the dialect one:
they take `destination.ty(body.locals())` before the match, with the same
diagnostic `translate_destination_type` emits when the place cannot be resolved.
The generated.rs hunk is regeneration output, not a hand edit;
`cuda-intrinsics-gen -- check` passes.

No test accompanies this, and that is deliberate rather than an omission. I
tried to build a `#[custom_mir]` reproducer for the `select_unpredictable` site
-- `RET.1 = select_unpredictable(..)` on a `(u64, u32)` -- and could not make it
fail on main: with a literal condition rustc folds the call before the importer
sees it, and with the condition derived from an earlier custom-MIR result it
still compiles clean both before and after. A case that passes either way would
sit in the suite guarding nothing, so I left it out rather than claim coverage
it does not provide. The change is the same mechanical alignment #769 applied to
its four siblings, and the four unit-test suites above stay green.

Closes #773

Signed-off-by: Vyncint Ng <[email protected]>

* fix(mir-importer): store call results through the projected place

Three translation paths typed their result from the projected
destination but still stored it with value_map.store_local on the bare
local: the sincos emitter (whose (sin, cos) tuple the parent commit
types from the projection), the function-item call path, and the
closure call path. A projected destination therefore kept failing at
each of them, and worse than before at sincos: (*p) = sincosf(x) went
from a clean "libm::sincos destination is not a tuple" diagnostic to
a MirStoreOp verifier abort, because the value was typed from the
pointee while the store aimed at the pointer's slot. For a
pointer-typed function return with a (*p) destination the bare-local
store was even silent: maybe_ptr_coerce papered over the type
mismatch with a PtrToPtr cast and overwrote the pointer instead of
the pointee. All three now route through store_result_to_place, the
same projection-aware helper every emit_function_call result already
uses; unsupported projections keep failing closed with a diagnostic.

mir_projected_call_destination gains four cases covering the paths
with their own store sites: RET.1 = sqrtf32(x) and RET[i] = sqrtf32(x)
for the float-math placeholder (typed from the projected place, not
the whole local), RET.1 = double_it(x) for the function-item path,
and (*p) = sincosf(x) for the tuple-pack emitter. Writing shapes that
actually reach the importer under -O took care: inlining rewrites a
projected call destination into a temporary plus a store, and GVN
rewrites a deref of a locally-taken pointer back to the plain local,
so the new bodies are inline(never) and the sincos pointer arrives as
an opaque argument. Inputs (sqrt(2), angle 0, integer doubling) are
exact on host and device, keeping the bit-exact comparison safe.

Verified both ways on an RTX 5090: against this branch all seven
projections run and agree; against main's importer the device build
hard-fails with a verification error on the ConstantIndex sqrtf32
copy the inliner leaves inside case_results.

---------

Signed-off-by: Vyncint Ng <[email protected]>
Co-authored-by: nik <[email protected]>
@alejandro-soto-franco
alejandro-soto-franco deleted the fix/intrinsic-call-projected-destination 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

2 participants