rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

Malformed SPIR-V when compiling a simple loop

Open Patryk27 opened this issue 2 years ago • 3 comments

Hi,

Compiling this code:

#[spirv(compute(threads(8, 8)))]
pub fn main(#[spirv(descriptor_set = 0, binding = 0)] out: TexRgba32f) {
    let mut value = vec3(0.1, 0.2, 0.3);
    let mut i = 0;

    loop {
        if i >= 32 {
            break;
        }

        i += 1;
    }

    unsafe {
        out.write(uvec2(0, 0), value.extend(1.0));
    }
}

... will work, but adding a seemingly no-op value = value into that loop:

loop {
    value = value;

    if i >= 32 {
        break;
    }

    i += 1;
}

... will panic the compiler, saying:

error: malformed SPIR-V (in OpCompositeExtract: undefined ID %4547)

Meta:

rustc 1.71.0-nightly (1a5f8bce7 2023-05-26)
SPIRV-Tools v2023.2 unknown hash, 1980-01-01T00:00:00

Patryk27 avatar Jun 16 '23 15:06 Patryk27

fwiw, an assignment after (or before) the loop triggers this error as well:

#[spirv(compute(threads(8, 8)))]
pub fn main(#[spirv(descriptor_set = 0, binding = 0)] out: TexRgba32f) {
    let mut val = vec3(0.1, 0.2, 0.3);
    let mut i = 0;

    while i < 10 {
        i += 1;
    }

    val = val;

    unsafe {
        out.write(uvec2(0, 0), val.extend(1.0));
    }
}

Patryk27 avatar Jun 17 '23 08:06 Patryk27

@Patryk27 I think there's something up with while loops and loops with loop { if ... { break; } } because I have a number of shaders that work with for i in range that won't work with any other looping mechanism.

schell avatar Jul 29 '23 22:07 schell