Skip to content

Mutable array element borrow in kernel #58

Closed

Description

@DanilSerd
  #[kernel]
    pub fn life(mut out: DisjointSlice<[f32; SIZE]>) {
        let idx = thread::index_1d();
        let a = match out.get_mut(idx) {
            Some(a) => a,
            None => return,
        };
        for i in 0..SIZE {
            a.get_mut(i).map(|e| *e = 42.);
        }

        // This works as expected.
        //
        // for e in a {
        //     *e = 42.;
        // }
    }

Write through get_mut() doesn't end up being persisted in out. Out remains all zeroes. Compiles fine.

Doctor:

Rust nightly toolchain... ✓ rustc 1.96.0-nightly (55e86c996 2026-04-02)
rust-toolchain.toml... ✓ present
Codegen backend... ✓ ~/.cargo/cuda-oxide/librustc_codegen_cuda.so
CUDA toolkit (nvcc)... ✓ Cuda compilation tools, release 13.1, V13.1.115
libNVVM (libnvvm.so)... ✓ libNVVM 2.0
nvJitLink (libnvJitLink.so)... ✓ nvJitLink 13.1
libdevice (libdevice.10.bc)... ✓ /usr/local/cuda/nvvm/libdevice/libdevice.10.bc
llc (LLVM)... ✓ Ubuntu LLVM version 21.1.8 (llc-21)
clang / libclang resource dir... ✓ /usr/lib/llvm-21/lib/clang/21
cuda-gdb (optional)... ✓ exec: /usr/local/cuda/bin/cuda-gdb-python3.12-tui --version

✅ Environment looks good!

Activity

  1. mutantbob commented on May 15, 2026

    @mutantbob

    What do you see in the out that comes out of the kernel?

    When I try to use get_mut(i) I get a compile error since the API changed to take a ThreadIndex a little while ago. If you do end up upgrading your cuda-oxide install, beware of #49 .

    I don't know enough about the SIMT model to know if it is safe to access a destination cell that is not assigned to your thread. The APi I most recently checked out does not have a safe route to do it. I modified my app to include

            for i in 0..5000 {
                unsafe {
                    *dst.as_mut_ptr().add(i) = 77;
                }
            }
    

    and it behaved as I expected.

    Under normal circumstances I would be worried about a race condition, since I hypothesize that multiple SIMT threads would be trying to perform the same iteration and I am not sure if there is a defined order in which each thread would write 42.0 to any particular cell. Since they all write 42.0 the effect would be the same, but if the computation were more complex it may become a problem.

  2. DanilSerd commented on May 15, 2026

    @DanilSerd
    Author

    I should have been clearer on which get_mut I meant. It's this one:

            for i in 0..SIZE {
                a.get_mut(i).map(|e| *e = 42.);
            }

    Each a reference is unique to each thread as per the ThreadIndex guard from the lines above the loop. out remains unchanged (all arrays are filled with zeroes as it was initialized to zeroes).

    This variant results in the same bug:

            for i in 0..SIZE {
                let e = &mut a[i];
                *e = 42.;
            }

    This variant results in a compile time error:

            for i in 0..SIZE {
                a[i] = 42.;
            }
    error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Translation failed: life: [name: "Span { id: 21, repr: "src/
    main.rs:24:13: 24:23" }", loc: (?)] Compilation error: invalid input program.
           Unsupported construct: 2-level projection Deref -> Index(10) not yet implemented for assignment
    

    Which is good.

  3. added
    bug
    codegen
    on Jun 6, 2026
  4. added a commit that references this issue on Jun 11, 2026
    ab417b5
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