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

WGSL: const declarations in function scope cannot be used as const expressions #6720

Open
ErichDonGubler opened this issue Dec 13, 2024 · 4 comments · May be fixed by #6935
Open

WGSL: const declarations in function scope cannot be used as const expressions #6720

ErichDonGubler opened this issue Dec 13, 2024 · 4 comments · May be fixed by #6935
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working

Comments

@ErichDonGubler
Copy link
Member

Description

const-declared values in function scope (i.e., "local" const mySize = 2 inside a fn declaration) cannot be used in contexts where values must be constant-evaluatable, like array sizes.

Repro steps

  1. Observe that while this passes validation in WGSL:

    const myArraySize = 2;
    @compute @workgroup_size(1, 1)
    fn main() {
    	var myArray: array<f32, myArraySize>;
    }

    …this does not:

    @compute @workgroup_size(1, 1)
    fn main() {
    	const myArraySize = 2; // Hello, I moved here!
    	var myArray: array<f32, myArraySize>;
    }

    …which returns an error from naga-cli:

    error: this operation is not supported in a const context
      ┌─ ../in.wgsl:4:26
      │
    4 │     var myArray: array<f32, myArraySize>;
      │                             ^^^^^^^^^^^ operation not supported here
    

Expected vs observed behavior

I expect that values bound to const declarations should be usable as a const expression in the same contexts, independent of where they are declared (lexical scoping notwithstanding).

@ErichDonGubler ErichDonGubler added the type: bug Something isn't working label Dec 13, 2024
@cwfitzgerald cwfitzgerald added naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language labels Dec 13, 2024
@sagudev
Copy link
Contributor

sagudev commented Dec 13, 2024

Indeed, I will check whats wrong.

@sagudev
Copy link
Contributor

sagudev commented Dec 13, 2024

The problem is that we pass global ctx when resolving type:

v.ty.map(|ast| self.resolve_ast_type(ast, &mut ctx.as_global()))

@sagudev
Copy link
Contributor

sagudev commented Jan 17, 2025

I am working on this.

@sagudev sagudev linked a pull request Jan 17, 2025 that will close this issue
7 tasks
@sagudev
Copy link
Contributor

sagudev commented Jan 18, 2025

not just in type resolution, most of naga still assumes const are only global and uses global const ctx instead of local one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

3 participants