Repository navigation
fix: Allow break inside a switch inside an unrolled loop - #3152
Open
dchaudhari7177 wants to merge 1 commit into
Open
dchaudhari7177 wants to merge 1 commit into
dchaudhari7177 wants to merge 1 commit into
Conversation
A `break` in a switch case exits the switch, which survives unrolling, so it is not a `break` of the unrolled loop. Track whether the innermost `break` target is a switch, reset it for each unrolled iteration, and only reject a `break` that would exit the unrolled loop itself. `continue` inside such a switch still targets the loop and still throws. Closes software-mansion#3068
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
breakin aswitchinside an unrolled loop — added a#breakExitsSwitchflag toWgslGeneratorthat is set while generating aswitchcase consequent and reset per unrolled iteration, sobreakis only rejected when it would actually exit the unrolled loop.- Tests — four new cases in
unroll.test.tscovering the runtime-discriminant switch, the comptime-folded switch,continuestill throwing, and abreakin an unrolled loop nested inside aswitchcase still throwing.
I traced the flag through nested switches, switches in unrolled loops, unrolled loops in switch cases, and runtime for/while loops nested in either (these reset #unrollingChain, so they need no flag handling). Save/restore via finally is correct in every path. I also confirmed the two positive tests fail with the fix condition reverted to the base form, so they genuinely pin the change. The full packages/typegpu suite (2735 tests) and packages/typegpu-gl (118 tests) pass; GlslGenerator inherits _statement, so it picks up the fix.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
This branch has not been deployed
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.

Fixes #3068.
Cause
WgslGeneratorrejects everybreakwhile#unrollingChainis non-empty ("Cannot unroll loop containingbreak"). Abreakinside aswitchexits the switch, not the loop, and the switch is still there after unrolling. So this threw:Fix
A new
#breakExitsSwitchflag says whether the innermost statement abreakwould exit is aswitch.switch's case consequents are generated, it istrue.false. An unrolled loop nested inside aswitchcase therefore still rejects abreakof its own, which would otherwise end up exiting the switch after unrolling.breakis rejected only when unrolling and the flag isfalse.continueis unchanged: inside a switch it still targets the unrolled loop, so it still throws.Ordinary
for/whileloops already reset#unrollingChain, sobreakinside them was never affected.Tests (
tests/unroll.test.ts)breakin a runtimeswitchinsidetgpu.unroll: unrolls, oneswitchper iteration, and the trailingbreaks are pruned as usual.defaultswitch).continuein aswitchinside the unrolled loop still throws "Cannot unroll loop containingcontinue".breaknested inside aswitchcase still throws "Cannot unroll loop containingbreak".On
mainthe first two fail. On this branch,packages/typegpugives 192 files, 2735 passed, andpackages/typegpu-glpasses. oxfmt is clean.