gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.77k stars 937 forks source link

WGSL: Const. eval. short-circuiting #6302

Open ErichDonGubler opened 2 months ago

ErichDonGubler commented 2 months ago

Description

Naga currently rejects programs with constant expressions such as this one:

const asdf = false && (123 / 0 > 0);

An uninformed reader might assume that dividing by zero yielding an error is expected. Indeed, Naga will happily report an error here:

Could not parse WGSL:
error: Division by zero
  ┌─ in.wgsl:1:24
  │
1 │ const asdf = false && (123 / 0 > 0);
  │                        ^^^^^^^ see msg

And yet, this is not standard behavior! The WGSL spec. clearly states that we should only be attempting const. eval. on the RHS of expressions when it cannot short-circuit. In the current WGSL spec.'s section 8.1.1:

The evaluation rule implies that short-circuiting operators && and || guard evaluation of their right-hand side subexpressions unless there is a subexpression that requires evaluation to determine a [static type](https://www.w3.org/TR/WGSL/#static-type).

Repro steps

Validate the above shader via naga as a file, i.e., in.wgsl. One can do this with a repo. checkout as follows:

$ cargo run -p naga-cli -- in.wgsl

Expected vs observed behavior

Already described, I hope!

Extra materials

-

Platform

-

sagudev commented 2 months ago

This will be complicated as evaluator already evals them to bool when doing logical and: https://github.com/gfx-rs/wgpu/blob/14abdd47547e0cd364e8431dec46c3d57be3e160/naga/src/proc/constant_evaluator.rs#L1925-L1929

teoxoy commented 1 month ago

Related: https://github.com/gfx-rs/wgpu/issues/4394