Rule 11.27
Chapter 11, Targets and the oracle Test
A function of an emitted module must hold no constant expression that its target refuses to evaluate, or evaluates to another value than the operation has at run time: the pass that settles constant expressions binds an operand of such an operation to a let, and the operation is evaluated when the shader runs.
The rule text and the parts under it are the compiler's own English, as the design document writes them.
Rationale
WGSL evaluates a constant expression when it creates the shader and makes one outside a builtin’s domain or past the range of its type an error; the compiler can give such an operation its run-time value by evaluating it when the shader runs, so the form is not refused (OD-9), whether the author wrote the constants in the call or passed them through a helper, and the same source does not compile or fail by optimization level. A WebGL2 driver accepts such an expression and folds it to a value of its own (pow(-2.0, 2.0) to 0).
Derives from
How it is verified
Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.
Where the rule says the compiler enforces it:
settleConstExprs (src/core/passes/const-expr.ts) and constFold (src/core/passes/opt/const-fold.ts), pinned by src/core/passes/const-bind.test.ts (each class of the scope table, and what stays unbound), src/core/passes/const-bind-emit.test.ts (each emit path) and src/core/backends/glsl-const-bind.test.ts (the operand in a local on GLSL ES 3.00); both halves read the helper forms and the twelve forms of #1876 in src/compiler/ts/const-bind-forms.test.ts, and the refusals that stay in src/compiler/ts/const-bind-refusals.test.ts; the const-bind leg of scripts/compile-gate.ts judges the programs on Tint and WebGL2 and hands Tint the text that the baseline emitted for each class (Rule 13.3).
The files that verify it at commit af54ddad5, each at the first line that names the rule:
-
scripts/compile-gate.ts:134 -
src/compiler/ts/const-bind-forms.test.ts:1 -
src/compiler/ts/const-bind-refusals.test.ts:1 -
src/core/backends/glsl-const-bind.test.ts:1 -
src/core/passes/const-bind-emit.test.ts:1 -
src/core/passes/const-bind.test.ts:1 -
src/core/passes/const-bind.ts:1 -
src/core/passes/const-expr.ts:1 -
src/core/passes/opt/const-fold.ts:1
See also
Source
The rule at commit af54ddad5:
docs/language-design.md:1709(the design document)reqs/rules/RULE-1127.md(its traceability item)