Skip to content

PTX Codegen Failure: StepBy struct lowering (invalid cast opcode) exposed by missing Pat type translation #21

Closed

Description

@JonatanLindh

Description
While experimenting with CUDA Oxide, I attempted to use a standard (start..end).step_by(step) iterator inside a kernel. This initially failed due to a missing translation arm for TyKind::Pat in the MIR importer.

After adding a local patch to strip the pattern metadata and translate the base type, the frontend passed, but it revealed a deeper LLVM PTX code generation failure related to how the StepBy struct is lowered.

1. The "Hidden" Bug (Pat missing)
Initially, compiling a step_by loop fails because the MIR importer doesn't handle the Pat types used by the iterator's internal state. I patched this locally in crates/mir-importer/src/translator/types.rs:

// ...
rustc_public::ty::TyKind::RigidTy(rustc_public::ty::RigidTy::Array(elem_ty, len_const)) => { /* ... */ }

// ADDED THIS ARM
rustc_public::ty::TyKind::RigidTy(rustc_public::ty::RigidTy::Pat(base_ty, _pat)) => {
    // Patterns don't have a distinct runtime type, so we translate to the base type.
    translate_type(ctx, &base_ty)
}

rustc_public::ty::TyKind::RigidTy(rustc_public::ty::RigidTy::Slice(elem_ty)) => { /* ... */ }
// ...

(Note: I have the frontend Pat translation patch ready locally. Should I open a PR for that, or do you prefer to keep the Unsupported construct frontend error in place as a guardrail until the backend StepBy lowering is fully implemented?)

2. The Revealed Error (PTX Codegen Failure)
With the frontend satisfied, the compiler proceeds to LLVM IR generation, where llc crashes. It appears the optimizer fails to lower the StepBy state machine struct correctly, resulting in an illegal bitcast between a scalar i64 and an aggregate struct:

error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: PTX generation failed: CUDA_OXIDE_LLC=/nix/store/.../bin/llc failed:
/nix/store/.../bin/llc: error: /nix/store/.../bin/llc: [...].ll:126:18: error: invalid cast opcode for cast from 'i64' to '{ i8, { { i64 } } }'
         %v8 = bitcast i64 %v7 to { i8, { { i64 } } }

Minimal Reproducible Example
With the Pat arm patched, the following simple loop triggers the LLVM crash:

#[kernel]
fn fail() {
    // This high-level iterator causes the bitcast failure
    for i in (thread::index_1d().get()..1000).step_by(thread::blockDim_x() as usize) {
        // dummy payload
        let _ = i;
    }
}

Note: Rewriting this to a standard while loop bypasses the issue completely, confirming it's specific to the StepBy iterator struct's layout in the backend

#[kernel]
fn success() {
    // This is fine
    let mut i = thread::index_1d().get();
    while i < 1000 {
        // dummy payload
        let _ = i;
        i += thread::blockDim_x() as usize;
    }
}

Activity

  1. JonatanLindh commented on May 11, 2026

    @JonatanLindh
    ContributorAuthor

    Regarding Pat lowering: I'm doing the bare minimum to lower here, we likely want to use the _pat info for optimizations

  2. nihalpasham commented on May 13, 2026

    @nihalpasham
    Collaborator

    Hey, thanks for flagging this and for the minimal repro. The while loop workaround is useful context, but this should be fixed in the compiler path rather than documented as a source workaround.

    I think there are two gaps here:

    1. Proper lowering for RigidTy::Pat
    2. Better verification before we hand generated LLVM IR to llc

    For the first one, I think the fix would require checking the layout story. Pat(base_ty, pattern) carries extra type information over the base type, and even if the storage often lowers like the base type, we need the aggregate layout to remain consistent with what rustc expects.

    For the second gap, cuda-oxide should ideally catch malformed LLVM before PTX generation. Today this escapes as an llc failure, but the underlying issue is that we emitted an invalid scalar-to-aggregate bitcast.

    If you’re interested in taking this on, a PR would be very welcome. I’d suggest:

    1. Add a minimal step_by regression example that reproduces the current failure.
    2. Add RigidTy::Pat handling in the MIR type translator, with a comment explaining the layout assumption.
    3. Fix the revealed aggregate/cast lowering bug so StepBy no longer emits invalid LLVM.
    4. Add or keep verification so the repro either builds correctly or fails loudly with a cuda-oxide diagnostic, not an opaque llc error.
  3. added
    bug
    codegen
    on May 13, 2026
  4. g-ampo commented on May 14, 2026

    @g-ampo
    Contributor

    Taking this on if thats alright. Fix is done locally and verified end-to-end (step_by kernel now produces correct sums vs the while-loop control). PR shortly.

  5. JonatanLindh commented on May 15, 2026

    @JonatanLindh
    ContributorAuthor

    Thanks for the detailed breakdown of the required fixes, @nihalpasham !

    @g-ampo Yes of course, nice with a quick fix :)

  6. added a commit that references this issue on May 18, 2026
    7505c7a
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

    bugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions