Skip to content

device codegen fails if you iterate over an array.iter() #138

Closed

Description

@mutantbob

The for x in a construct causes codegen to fail. here is an example kernel:

use cuda_device::cuda_module;

#[cuda_module]
mod kernels {
    use cuda_device::{DisjointSlice, kernel, thread};

    #[kernel]
    pub fn asplode(mut dst: DisjointSlice<u32>) {
        let idx = thread::index_1d();

        let mut sum = 0;
        let a: [u32; 4] = [1, 2, 3, 4];
        for x in a {
            sum += x;
        }
        /*  for i in 0..4 {
            sum += a[i];
        }*/
    }
}

The error this produces is

error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Translation failed: asplode: [name: "Span { id: 23, repr: "codegen-explosion-5/src/main.rs:15:9: 15:10" }", loc: (?)] Compilation error: invalid input program.
       Unsupported construct: drop of `RigidTy(Adt(AdtDef(DefId { id: 3, name: "std::array::IntoIter" }), GenericArgs([Type(Ty { id: 1, kind: RigidTy(Uint(U32)) }), Const(TyConst { kind: Value(Ty { id: 5, kind: RigidTy(Uint(Usize)) }, Allocation { bytes: [Some(4), Some(0), Some(0), Some(0), Some(0), Some(0), Some(0), Some(0)], provenance: ProvenanceMap { ptrs: [] }, align: 8, mutability: Mut }), id: TyConstId(0, ThreadLocalIndex) })])))` is not supported on the device; cuda-oxide does not yet emit device-side `drop_in_place` calls. Restructure the kernel to use only `Copy` types, or wrap the value in `core::mem::ManuallyDrop` to suppress drop glue.

It is possible to work around this problem by using for i in 0..4 and indexing the array instead. This code is slightly less idiomatic and can expend significant porting effort. I do not know if it would be easier to solve the problem, or adjust the error message to guide the programmer to adjust the troublesome source lines until the problem can be fully solved.

Activity

  1. added
    bug
    codegen
    on Jun 8, 2026
  2. mutantbob commented on Jun 8, 2026

    @mutantbob
    Author

    The for i in 0..a.len() also triggers the warning for clippy::needless_range_loop so I am adding
    #![allow(clippy::needless_range_loop)] to the module for now.

  3. added 2 commits that reference this issue on Jun 11, 2026
    fb5f20d
    d84af09
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