Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hlsl-out] "continue cannot be used in a switch" within loop #4485

Closed
hasali19 opened this issue Mar 19, 2023 · 1 comment · Fixed by #5654
Closed

[hlsl-out] "continue cannot be used in a switch" within loop #4485

hasali19 opened this issue Mar 19, 2023 · 1 comment · Fixed by #5654
Labels
area: naga back-end Outputs of naga shader conversion FXC bug lang: HLSL D3D Shading Language naga Shader Translator type: bug Something isn't working

Comments

@hasali19
Copy link
Contributor

WGSL:

@compute
@workgroup_size(1)
fn main() {
    loop {
        switch (0) {
            default: {
                continue;
            }
        }
    }
}

HLSL:

[numthreads(1, 1, 1)]
void main()
{
    while(true) {
        switch(0) {
            default: {
                continue;
            }
        }
    }
    return;
}

The generated HLSL is rejected by FXC with the following error:

error X3708: continue cannot be used in a switch

I guess the continue would need to be compiled down to e.g. a flag that conditionally executes the remainder of the loop.

@teoxoy teoxoy added kind: bug area: naga back-end Outputs of naga shader conversion lang: HLSL D3D Shading Language labels Mar 20, 2023
@cwfitzgerald cwfitzgerald transferred this issue from gfx-rs/naga Oct 25, 2023
@cwfitzgerald cwfitzgerald added naga Shader Translator type: bug Something isn't working and removed kind: bug labels Oct 25, 2023
@teoxoy teoxoy added this to the WebGPU Specification V1 milestone Nov 3, 2023
@teoxoy teoxoy added the FXC bug label Jan 11, 2024
@Imberflur
Copy link
Contributor

Imberflur commented Apr 30, 2024

Another variant of this.

WGSL:

    var i = 0;
    loop {
        if i > 4 { break; }
        i = i + 1;
        switch 0 {
            case 0, default {
                continue;
            }
        }
    }

HLSL:

    int i = 0;

    while(true) {
        int _expr2 = i;
        if ((_expr2 > 4)) {
            break;
        }
        int _expr5 = i;
        i = (_expr5 + 1);
        switch(0) {
            case 0:
            default: {
                continue;
            }
        }
    }

will have this error:

 error X3533: non-empty case statements must have break or return

jimblandy pushed a commit to Imberflur/wgpu that referenced this issue Jul 24, 2024
Introduce a new module, `naga::back::continue_forward`, containing
shared code for rendering Naga `Continue` statements as backend
`break` statements and assignments to introduced `bool` locals.
See the module's documentation for details.

- [hlsl-out] Transform degenerate single body switches into `do-while`
  loops. Properly render `Continue` statements enclosed by
  `Switch` statements enclosed by `Loop` statements.

- [glsl-out] Transform degenerate single body switches into `do-while`
  loops.

Improve `naga xtask validate spv` error message.

Fixes gfx-rs#4485.
Fixes gfx-rs#4514.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga back-end Outputs of naga shader conversion FXC bug lang: HLSL D3D Shading Language naga Shader Translator type: bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants