Skip to content

An 8-bit bswap emits an invalid bitcast when its result type is an aggregate #767

Closed

Description

@alejandro-soto-franco

Device codegen emits an invalid bitcast for an 8-bit bswap when the call's result type is not an 8-bit integer. LLVM rejects the module when it is read back, so the failure arrives as an llc diagnostic against a generated .ll rather than as an error against the source.

error: rustlantis_smoke.ll:342:17: error: invalid cast opcode for cast from 'i8' to '{ double, i8, [7 x i8] }'
  %v4 = bitcast i8 7 to { double, i8, [7 x i8] }
                ^

Reproducer

#[custom_mir(dialect = "runtime", phase = "initial")]
fn fn1(mut _1: isize) -> (f64, u8) {
    mir! {
        type RET = (f64, u8);
        let __rl_dump0: (isize, isize);
        let _7: ();
        {
            Call(RET.1 = core::intrinsics::bswap(7_u8), ReturnTo(bb1), UnwindUnreachable())
        }
        bb1 = {
            Call(_7 = dump_var(Move(__rl_dump0)), ReturnTo(bb2), UnwindUnreachable())
        }
        bb2 = {
            Return()
        }
    }
}

{ double, i8, [7 x i8] } is the layout of the (f64, u8) return, and i8 7 is the intrinsic's operand. The second call is load bearing: with nothing keeping the function alive the whole body is eliminated and the case compiles.

Why this arm and not the others

bswap on a single byte is identity, and LLVM has no useful intrinsic for it, so convert_rust_bit_intrinsic handles that width with an early bitcast and returns:

if matches!(intrinsic, RustBitIntrinsic::Bswap) && value_width == 8 {
    let bitcast = llvm::BitcastOp::new(ctx, value, result_type);
    ...
    return Ok(());
}

Every other arm falls through to cast_integer_value_to_type, which asks integer_bit_width for the target width and refuses a type that has none. The early return skips that, and bitcast requires both types to be non-aggregate and of one size, so an aggregate result produces IR that cannot be parsed.

Where the wrong type comes from

crates/mir-importer/src/translator/terminator/intrinsics/bitops.rs:107 types the call from the destination's local:

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

destination.projection is dropped, so RET.1 yields the type of RET. cargo oxide pipeline shows the result:

(_0.1: u8) = std::intrinsics::bswap::<u8>(const 7_u8) -> [return: bb1, unwind unreachable]

v325 = mir.call (v324) [callee: "__cuda_oxide_rust_intrinsic_bswap"]:
       <(builtin.integer ui8) -> (mir.tuple <[builtin.fp64 , builtin.integer ui8],[0, 1],[0, 8],16,8>)>

helpers::emit_function_call then stores the result with value_map.store_local(ctx, destination.local, ..), which drops the projection on the store as well.

This is the same root cause as #765, reached through a field projection rather than a deref. The two differ only in which check catches it: a deref result is a pointer, which has no width and is refused; a field result on an 8-bit bswap reaches the early bitcast, which checks nothing.

Ordinary calls are unaffected 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 only custom_mir reaches this.

Suggested split

Refusing the invalid bitcast in mir-lower keeps the backend from emitting unparseable IR and stands on its own. Teaching the importer to type and store through a projected destination is the fix for the underlying fault and touches the 920-line projected-assignment path in translator/statement.rs, which emit_function_call does not currently share.

Provenance

Seed 1792 of a rustlantis sweep with composite_count raised, reduced from 25 statements to the 3 above by delta debugging.

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