fix(ir): Fix vertex index detection inside control flow - #8
Merged
Conversation
- vertexIndex reads nested inside if/for/assign/return were invisible to UsesVertexIndex, so backends emitted shaders referencing vertexIndex without the @Builtin(vertex_index)/[[vertex_id]]/gl_VertexID declaration the reference needed — naga rejects the resulting WGSL outright - replace type-switch walkers in stmtMatches/exprMatches with interface methods (StmtCF.exprs/nestedStmts, Expr.children) so a new CF or Expr variant missing these methods is a compile error instead of a silently-false walker result - remove the hand-rolled vertexIndex walker in lower/lower_vertex.go, replacing it with ir.StageUsesVertexIndexBuiltin which routes through the exhaustive shared walker the same way UsesDerivatives already does for dpdx/dpdy/fwidth - add regression tests: IR walker unit tests for the walker, an end-to-end lowering test through the compiler, a new conformance material with golden outputs for all six backends, and a validation test proving naga rejects the old invalid WGSL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The previous vertex-index detection logic only scanned top-level IR statements, missing reads nested inside control flow (e.g., an
ifreassignment). This caused emitted shaders to omit the per-backend vertex-index binding while still referencing it, resulting in invalid code. Refactored the IR's usage walkers to dispatch through interface methods, making exhaustive control-flow cases enforceable at compile time.Changes
ExprandStmtCFinterfaces to requirechildren()andexprs()/nestedStmts()methods, replacing brittle type switches in usage walkers with safe interface dispatch.irStageUsesVertexIndexvertex-index walker with the new exhaustiveir.StageUsesVertexIndexBuiltin, correctly detecting references insideif/for/assignstatements.ir/uses_test.goandlower/lower_test.goto verify derivatives and vertex-index are found inside control flow.validate/validate_test.gotest to prove the previous output failed Naga validation.Testing
go test ./ir ./lower ./validate -run 'TestUsesDerivativesFindsCallInsideReturnCF|TestStageUsesVertexIndexBuiltinFindsRefInsideIf|TestLowerMeshAuthoredVertexUsesVertexIndexInsideControlFlow'