Skip to content

add(compiler): support early return in if/for bodies - #4

Merged
odvcencio merged 1 commit into
mainfrom
feat/early-return
Jul 27, 2026
Merged

add(compiler): support early return in if/for bodies#4
odvcencio merged 1 commit into
mainfrom
feat/early-return

Conversation

@odvcencio

Copy link
Copy Markdown
Contributor

Summary

Enable return statements inside if and for blocks in surface and feedback bodies, allowing conditional early exits from shaders. The parser accepts returns at any nesting depth, emitting a new ReturnCF IR node, while backend emitters use a pluggable ReturnFn to handle void-returning entry functions (GLSL/GLES fragment shaders, feedback compute kernels). Vertex bodies retain the restriction to prevent undefined varying assignments.

Changes

  • Parser: convert trailing returns in if/for sub-blocks to hir.Return statements instead of rejecting them; retain the block-level guard that nothing may follow a return.
  • HIR/IR: introduce hir.Return and ir.ReturnCF types to carry early-exit values through lowering and emission.
  • Lowering: lower early returns, widen vec3/color to vec4 with alpha 1.0, and reject them in authored vertex() bodies; updated super.surface inlining diagnostics to reference control flow instead of the retired B2a label.
  • Backend emit: add Resolver.ReturnFn to the emitter; GLSL/GLES write the output global (gl_FragColor/fragColor) before return;, and feedback kernels write outState[cellIndex] before returning.

@odvcencio
odvcencio force-pushed the fix/return-defects branch from 542b9ea to d4a6812 Compare July 27, 2026 04:53
@odvcencio
odvcencio changed the base branch from fix/return-defects to main July 27, 2026 04:54
- Allow `return` nested inside if/for bodies in surface() and feedback() (Phase 1); vertex() continues to reject early returns since assigned varyings would be undefined
- Add hir.Return and ir.ReturnCF nodes; parser's subBlock appends a Return instead of erroring on a trailing return statement
- Wire per-backend ReturnFn so void-returning entries (GLSL/GLES fragments, WGSL/Metal compute) write the output global then bare-return, while typed entries emit plain `return val;`
- Retire the 'B2a limitation' wording with a descriptive diagnostic naming the unsupported control-flow shape for super.surface() parents
- Extend collectGeoStmt and derivative detection to cover early returns so geo-only usages and dpdx-in-return still resolve correctly
@odvcencio
odvcencio force-pushed the feat/early-return branch from 26e012e to 4fc68c4 Compare July 27, 2026 04:58
@odvcencio
odvcencio merged commit ebfff72 into main Jul 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant