Repository navigation
fix(mir-importer): write an intrinsic result through its projected destination - #769
Merged
nihalpasham merged 4 commits intoAug 10, 2026
Conversation
…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.
Collaborator
|
Validated on 2x RTX 5090:
Pushed one fixup:
Filed a follow-up for the remaining bare-local typing sites. |
nihalpasham
approved these changes
Aug 10, 2026
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
deleted the
fix/intrinsic-call-projected-destination
branch
September 9, 2026 17:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_typeasksPlace::tyfor the projected type, andstore_result_to_placewrites through the projection instead of over the local.Changes
crates/mir-importer/src/translator/terminator/helpers.rs:destination_typeandstore_result_to_place;emit_function_calluses the latter;emit_store_result_and_gotorefuses a projected destination.crates/mir-importer/src/translator/terminator/intrinsics/{bitops,saturating,bigint,exact_div}.rs: take the result type fromdestination_type.crates/mir-importer/src/translator/statement.rs:pointer_is_mutable,pointer_address_space,reject_raw_field_index_on_enum_pointee,slot_array_element_tyandemit_array_element_storebecomepub(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:
An intrinsic keeps its projected destination through to codegen, so
custom_miris 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
bswaparm, which bitcasts without checking, and produced IR that LLVM will not parse (#767).Place::tyis the existing idiom for this question:intrinsics/atomic.rs:1348already uses it to type a cmpxchg destination. The store mirrors whattranslator/statement.rsdoes for an ordinary assignment to a projected place, taking the field's address through the local's slot withmir.field_addr, the pointer through the base place for a deref, and the element throughemit_array_element_storefor an index.emit_store_result_and_gotois the other epilogue with the same fault. It has noBody, so it cannot compute the address of a place, and 600 of its 634 callers sit in the generatedintrinsics/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 hitConstantIndexon 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
mainwithexpected integer type for Rust bit intrinsic, and here reports:Each value is checkable by hand.
bswap(0x1AE2A060)is0x60A0E21A; the field case is the bit pattern of1.5f64against the byte7, which a byte swap leaves alone; and the index case keeps11and33in 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,
mainagainst this branch:The seven fuzzer seeds that surfaced both issues, re-run with
composite_countraised so they generate the same programs:All seven failed to compile on
main: six with #765 and one with #767.