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

[naga wgsl-out] letdeclarations are wrongly detected as const #6207

Open
sagudev opened this issue Sep 3, 2024 · 11 comments · May be fixed by #7304
Open

[naga wgsl-out] letdeclarations are wrongly detected as const #6207

sagudev opened this issue Sep 3, 2024 · 11 comments · May be fixed by #7304
Labels
area: correctness We're behaving incorrectly area: naga back-end Outputs of naga shader conversion lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working

Comments

@sagudev
Copy link
Contributor

sagudev commented Sep 3, 2024

Description
In wgsl-in there is

// The WGSL spec says that any expression that refers to a
// `let`-bound variable is not a const expression. This
// affects when errors must be reported, so we can't even
// treat suitable `let` bindings as constant as an
// optimization.
ctx.local_expression_kind_tracker.force_non_const(value);

but in wgsl-out when we create expression tracker from arena this information is lost:

expr_kind_tracker: ExpressionKindTracker::from_arena(&function.expressions),

expr_kind_tracker: ExpressionKindTracker::from_arena(&ep.function.expressions),

same problem is also in validator

let local_expr_kind = crate::proc::ExpressionKindTracker::from_arena(&fun.expressions);

which causes #4390 (such checks should only be done in const but let expr should not be treated as const).

@sagudev
Copy link
Contributor Author

sagudev commented Sep 3, 2024

which causes #4390

I tested

diff --git a/naga/src/valid/function.rs b/naga/src/valid/function.rs
index 23e6204cc..c60f3a7e9 100644
--- a/naga/src/valid/function.rs
+++ b/naga/src/valid/function.rs
@@ -1365,7 +1365,11 @@ impl super::Validator {
     ) -> Result<FunctionInfo, WithSpan<FunctionError>> {
         let mut info = mod_info.process_function(fun, module, self.flags, self.capabilities)?;
 
-        let local_expr_kind = crate::proc::ExpressionKindTracker::from_arena(&fun.expressions);
+        let mut local_expr_kind = crate::proc::ExpressionKindTracker::from_arena(&fun.expressions);
+
+        for (handle, _) in fun.expressions.iter() {
+            local_expr_kind.force_non_const(handle);
+        }
 
         for (var_handle, var) in fun.local_variables.iter() {
             self.validate_local_var(var, module.to_ctx(), &info, &local_expr_kind)

and it doesn't fix, but we should still need correct constness information for fixing the issue.

@teoxoy
Copy link
Member

teoxoy commented Sep 4, 2024

I thought this should be fine for wgsl-out and the validator. The reason it's not ok in the frontend is because treating them as const changes semantics in a non-spec compliant way. What would be an example where this is an issue?

@sagudev
Copy link
Contributor Author

sagudev commented Sep 4, 2024

I do not have concrete example but

fn start_named_expr(
&mut self,
module: &Module,
handle: Handle<crate::Expression>,
func_ctx: &back::FunctionCtx,
name: &str,
) -> BackendResult {
// Some functions are marked as const, but are not yet implemented as constant expression
let quantifier = if func_ctx.expr_kind_tracker.is_impl_const(handle) {
"const"
} else {
"let"
};
could upgrade let expressions to const, which might change behavior (for example when overflows are reported, if we did that correctly).

@teoxoy
Copy link
Member

teoxoy commented Sep 4, 2024

I see, that would be a problem indeed. I think the wgsl backend should just not emit function-local consts at all (that would mean reverting 6125ab4 & 8e967db).
As for the validator, I think it should be fine as we aren't evaluating those expressions and we already check constness in the frontend.

@cwfitzgerald cwfitzgerald added type: bug Something isn't working area: correctness We're behaving incorrectly area: naga back-end Outputs of naga shader conversion naga Shader Translator lang: WGSL WebGPU Shading Language labels Jan 11, 2025
@sagudev
Copy link
Contributor Author

sagudev commented Feb 25, 2025

The problem is that both let a = 5; and const a = 5 will create same result in IR (named expr), but there is no way to now if expr was defined as (local) const (or is just const-able). I think we will need to refactor

Constant(Handle<Constant>),
to also account for local const or smth like that.

@sagudev
Copy link
Contributor Author

sagudev commented Feb 26, 2025

Or alternatively do the reverts and remove const from named_expressions as we use local_table for lookup anyway (and let backends inline local const like it's done in #7213):

ctx.local_table
.insert(c.handle, Declared::Const(Typed::Plain(init)));
ctx.named_expressions
.insert(init, (c.name.name.to_string(), c.name.span));

@teoxoy
Copy link
Member

teoxoy commented Feb 27, 2025

Not having them in named_expressions sounds fine to me.

@cwfitzgerald
Copy link
Member

Will this have negative consequences on our ability to generate debug information?

@sagudev
Copy link
Contributor Author

sagudev commented Feb 27, 2025

Will this have negative consequences on our ability to generate debug information?

What exactly is considered as debug info in naga?

@cwfitzgerald
Copy link
Member

I don't think this has a strict definition yet, nor is it set in stone. I think currently the spirv backend uses the variable name in the named_expressions to add label the name of certain values to spirv when debug information is enabled. I would be willing to accept this is the wrong place to do it, and I don't know the specifics, but there are spirv-out tests with debug information in the snapshots.

@jimblandy
Copy link
Member

Leaving local const declarations out of named_expressions entirely would be fine with me. When our constant expression and abstract type handling gets better reconciled with validation, we should be able to keep them without any difficulty.

@sagudev sagudev linked a pull request Mar 9, 2025 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: correctness We're behaving incorrectly area: naga back-end Outputs of naga shader conversion 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.

4 participants