Skip to content

Naga can't produce spans for references to local variables #7498

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

Open
jimblandy opened this issue Apr 9, 2025 · 0 comments
Open

Naga can't produce spans for references to local variables #7498

jimblandy opened this issue Apr 9, 2025 · 0 comments
Labels
area: naga middle-end Intermediate representation kind: diagnostics Error message should be better naga Shader Translator

Comments

@jimblandy
Copy link
Member

Naga can't produce spans for expressions that are simply uses of local variables.

Given the following WGSL:

fn f() {
  var i: i32 = 0;
  _ = sin(i);
}

Naga's error message is:

Could not parse WGSL:
error: wrong type passed as argument #1 to `sin`
  ┌─ in.wgsl:3:7
  │
3 │   _ = sin(i);
  │       ^^^
  │
  = note: `sin` accepts the following types for argument #1:
...

If we instead pass a literal 0i, then Naga can actually point at the argument and tell us what its type is:

Could not parse WGSL:
error: wrong type passed as argument #1 to `sin`
  ┌─ in.wgsl:3:7
  │
3 │   _ = sin(0i);
  │       ^^^ ^^ argument #1 has type `i32`
  │

The root of the problem is that Naga IR says that each function body should only have a single LocalVariable expression, which produces a pointer to the variable, and then all accesses, loads, and stores should point to that. Because all references to the local variable are actually the same ir::Expression, the arena can only associate a single span with all of them. Pointing at the definition would be better than this, but for goodness' sake, this is not rocket science - Naga should just point at the expression proper.

I think this was kind of a SPIR-V inspired idea, but it just doesn't make sense for Naga. To the best of my knowledge, we never take advantage of the uniqueness of LocalVariable expressions. It would be completely fine for us to just emit a separate Expression::LocalVariable for each use. Then the expression arena could store a separate span for each use.

@jimblandy jimblandy added naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language kind: diagnostics Error message should be better area: naga middle-end Intermediate representation and removed area: naga front-end lang: WGSL WebGPU Shading Language labels Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga middle-end Intermediate representation kind: diagnostics Error message should be better naga Shader Translator
Projects
Status: Todo
Development

No branches or pull requests

1 participant