Repository navigation
Description
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
JonatanLindh commentedon May 11, 2026
Regarding Pat lowering: I'm doing the bare minimum to lower here, we likely want to use the _pat info for optimizations
docs: qualify Asus GX10 (GB10, sm_121) + add Ubuntu 24.04 bring-up re…
nihalpasham commentedon May 13, 2026
Hey, thanks for flagging this and for the minimal repro. The
whileloop 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:
RigidTy::PatllcFor 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
llcfailure, 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:
step_byregression example that reproduces the current failure.RigidTy::Pathandling in the MIR type translator, with a comment explaining the layout assumption.StepByno longer emits invalid LLVM.llcerror.g-ampo commentedon May 14, 2026
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.
examples: add step_by regression test for issue NVIDIA#21.
JonatanLindh commentedon May 15, 2026
Thanks for the detailed breakdown of the required fixes, @nihalpasham !
@g-ampo Yes of course, nice with a quick fix :)
examples: add step_by regression test for issue NVIDIA#21.
examples: add option_ref_transmute (pointer-source niche Transmute)
Merge pull request #64 from g-ampo/fix/issue-21-step-by-codegen
Fix documentation links and commands (NVIDIA#21)
refactor: split workspaces and scope nightly toolchain (NVIDIA#21)
refactor: split workspaces and scope nightly toolchain (NVIDIA#21)
refactor: split workspaces and scope nightly toolchain (NVIDIA#21)