Skip to content

[glsl-in] lost statement of multi part loop-expression #6208

@Vipitis

Description

@Vipitis

Description
If the third expression of a for loop (loop-expression according to the spec Chapter 6.3) contains multiple statements... naga just takes the last one and forgets the the first. I haven't looked at more than two statements.

this leads to infinite loops and potential crashes. Likely root cause to finding gfx-rs/wgpu-native#416, maybe related to #4558

Repro steps
I have two semantically equivalent code snippets in glsl

for (int i = 0; i < 50; i+=1, b+=0.01) {
        a -= 0.01;
    }
for (int i = 0; i < 50; b+=0.01, i+=1) {
        a -= 0.01;
    }

the third expression contains the usual incrementor, as well as a completely unrelated statement.

translating them to wgls using naga gives me the following output for this loop

loop {
        let _e22 = i;
        if !((_e22 < 50i)) {
            break;
        }
        {
            let _e29 = a;
            a = (_e29 - 0.01f);
        }
        continuing {
            let _e26 = b;
            b = (_e26 + 0.01f);
        }
    }
loop {
        let _e22 = i;
        if !((_e22 < 50i)) {
            break;
        }
        {
            let _e29 = a;
            a = (_e29 - 0.01f);
        }
        continuing {
            let _e26 = i;
            i = (_e26 + 1i);
        }
    }

the difference is which statement is part of the continuing block. The other statement is completely lost. It is dependant on the order.

Expected vs observed behavior
A reference for this being valid in a shadertoy. Perhaps the continuing block could just include both statements?

Platform
naga 22.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions