Skip to content

"Cant have a mutable operation on a const variable" when initializing variables with a expression #1432

Description

@jyi2ya

reproduction, this works well if we change the 256 / 2 into 128:

use std::mem::size_of;

use cubecl::{Runtime, prelude::*};

#[cube(launch)]
fn const_mutation(output: &mut Array<u32>) {
    let thread_idx = UNIT_POS as usize;
    let num_threads = CUBE_DIM as usize;
    let mut step = 256 / 2;

    while step > 0 {
        for pos in range_stepped(thread_idx, step, num_threads) {
            output[pos] = 0;
        }
        step /= 2;
    }
}

fn main() {
    let client = cubecl::wgpu::WgpuRuntime::client(&Default::default());
    let output = client.empty(128 * size_of::<u32>());

    unsafe {
        const_mutation::launch::<cubecl::wgpu::WgpuRuntime>(
            &client,
            CubeCount::new_1d(1),
            CubeDim::new_1d(1),
            ArrayArg::from_raw_parts(output.clone(), 128),
        );
    }

    let _ = client.read(vec![output]);
}

result:

hepnode0 15:28 (master) ~/dev/gpu-zhenhaowan/repros
0 cargo run --bin const-mutation
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/const-mutation`
WARNING: radv is not a conformant Vulkan implementation, testing use only.

thread 'DSD-4-0' (1852029) panicked at /home/jyi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/cubecl-core-0.10.0/src/frontend/operation/base.rs:180:9:
Can't have a mutable operation on a const variable. Try to use `RuntimeCell`.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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