Skip to content

A bit intrinsic writing through a dereferenced pointer is typed from the pointer rather than the pointee #765

Closed

Description

@alejandro-soto-franco

A Rust bit intrinsic whose call destination is a dereferenced raw pointer is refused, though the same intrinsic writing to a plain local compiles and runs.

error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Lowering failed:
       ["src/generated_case.rs": line: 21, column: 13] Compilation error: invalid input program.
       expected integer type for Rust bit intrinsic

Reproducer

#[custom_mir(dialect = "runtime", phase = "initial")]
fn fn1(mut _1: i32) -> *mut i32 {
    mir! {
        let _2: ();
        {
            RET = core::ptr::addr_of_mut!(_1);
            Call((*RET) = core::intrinsics::bswap(451059808_i32), ReturnTo(bb1), UnwindUnreachable())
        }
        bb1 = {
            Return()
        }
    }
}

The operand is an i32 literal, so the type the message complains about is not the one the intrinsic was given.

What varies

Four runs on an RTX 5060, compute capability 12.0, CUDA 13.3, changing one thing at a time:

call destination callee result
plain local _3: i32 bswap compiles, CPU and GPU traces match
(*_2) where _2: *mut i32 ordinary fn compiles, CPU and GPU traces match
(*RET) where RET: *mut i32 ordinary fn compiles, CPU and GPU traces match
(*RET) where RET: *mut i32 bswap rejected

So neither the deref destination nor the intrinsic is refused on its own. Only their combination is. bitreverse reproduces identically.

Mechanism

cargo oxide pipeline shows the importer giving the intrinsic call a pointer result type:

v327 = mir.constant () [] [value: builtin.integer <451059808: si32>]: <() -> (builtin.integer si32)>;
v328 = mir.call (v327) [] [callee: builtin.string "__cuda_oxide_rust_intrinsic_bswap"]:
       <(builtin.integer si32) -> (mir.ptr <builtin.integer si32,mutable:true,addrspace:0>)>;
mir.store (v323, v328) [] []:
       <(mir.ptr <mir.ptr <builtin.integer si32,mutable:true,addrspace:0>,mutable:true,addrspace:0>,
         mir.ptr <builtin.integer si32,mutable:true,addrspace:0>) -> ()>;

The operand is si32 and the result is mir.ptr<si32>, so the result carries the type of the destination local RET rather than the type of the place (*RET) the call writes. The store that follows then treats the swapped value as a *mut i32 and writes it into RET itself, which models RET = bswap(..) rather than (*RET) = bswap(..).

From there the refusal follows the lowering:

  • crates/mir-lower/src/convert/ops/call.rs:809 reads the result type off the call op.
  • :889 casts the LLVM call result to it through cast_integer_value_to_type.
  • :1474 asks integer_bit_width for the width of that target type.
  • :1404 reports expected integer type for Rust bit intrinsic, the pointer having no width.

Every bit intrinsic runs through the same cast at :889, which is why bitreverse behaves the same way.

The type check is what turns this into a compile error. Given the store the dump shows, an intrinsic result that happened to convert to the destination's type would be written to the pointer rather than through it.

Provenance

Surfaced by seeds 1170, 1202 and 1420 of a rustlantis sweep run with composite_count raised, then reduced to the twelve lines above by hand. Deref destinations are generated in that configuration; the checked-in configuration sets composite_count = 0.

Activity

  1. alejandro-soto-franco commented on Aug 9, 2026

    @alejandro-soto-franco
    ContributorAuthor

    Minimising a second fuzzer finding, #767, landed on the same root cause as this one, so the two are one fault with two symptoms.

    The importer types an intrinsic call from its destination's local rather than from the projected place it writes, at crates/mir-importer/src/translator/terminator/intrinsics/bitops.rs:107:

    let return_type = types::translate_type(ctx, &body.locals()[destination.local].ty)?;

    destination.projection is dropped, so a deref destination yields the pointer type and a field destination yields the whole aggregate. helpers::emit_function_call then stores the result with value_map.store_local(ctx, destination.local, ..), dropping the projection on the store too, which is the store this issue's dump shows writing into RET itself.

    What differs between the two reports is only which check catches it. A deref result is a pointer, which has no width, so it reaches integer_bit_width and is refused. A field result on an 8-bit bswap takes the early bitcast path, which checks nothing, and emits IR that LLVM cannot parse.

    Ordinary calls escape this because 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.

    The same body.locals()[destination.local].ty pattern appears at nine further sites under translator/terminator/, including saturating.rs:66, bigint.rs:72, exact_div.rs:69 and float_math.rs:626. I have reproducers for the bit intrinsics only, so I have not claimed the others are reachable.

    #768 refuses the invalid bitcast in mir-lower, which stops the backend emitting unparseable IR and leaves this fault in place. #769 fixes the fault itself: destination_type takes the projected place type from Place::ty, and store_result_to_place writes through the projection, reusing the address helpers in translator/statement.rs rather than restating them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions