Skip to content

[OptimizeInstructions] Fix loop effects in areConsecutiveInputsEqual - #9426

Merged
tlively merged 3 commits into
mainfrom
optimize-instructions-loop-fallthrough-effects
Nov 30, 2026
Merged

tlively merged 3 commits into
mainfrom
optimize-instructions-loop-fallthrough-effects

Conversation

@tlively

@tlively tlively commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

When walking the right-hand input's fallthrough chain in OptimizeInstructions::areConsecutiveInputsEqual, breaking out of the ChildIterator loop upon seeing the immediate fallthrough child skipped any subsequent children (such as the unreachable ifFalse arm of an If, a Break condition, or a RefCast/BrOn descriptor). While those children execute after (or instead of) the fallthrough expression when there is no enclosing loop, once the right-hand traversal enters a named Loop, control flow can cycle back so those skipped children execute on an earlier iteration before a subsequent evaluation of the fallthrough expression. For example, a select whose second arm was a loop containing an if with an unreachable else modifying a local and branching back to the loop falsely compared equal to the first arm and dropped the first arm.

Avoid this problem by bailing out when the right-hand side contains a loop.

When walking the right-hand input's fallthrough chain in `OptimizeInstructions::areConsecutiveInputsEqual`, breaking out of the `ChildIterator` loop upon seeing the immediate fallthrough child skipped any subsequent children (such as the unreachable `ifFalse` arm of an `If`, a `Break` condition, or a `RefCast`/`BrOn` descriptor). While those children execute after (or instead of) the fallthrough expression when there is no enclosing loop, once the right-hand traversal enters a named `Loop`, control flow can cycle back so those skipped children execute on an earlier iteration before a subsequent evaluation of the fallthrough expression. For example, a `select` whose second arm was a `loop` containing an `if` with an unreachable `else` modifying a local and branching back to the loop falsely compared equal to the first arm and dropped the first arm.

Track whether the right-hand fallthrough traversal has entered a named `Loop`, and once inside one, continue walking all remaining non-fallthrough children into `interferingEffects` rather than breaking at the immediate fallthrough child.
Comment thread src/passes/OptimizeInstructions.cpp Outdated
}
if (auto* loop = right->dynCast<Loop>(); loop && loop->name) {
inLoop = true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can just not optimize Loops? That seems simpler and maybe good enough, to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean just return false as soon as we see a loop on the right hand side? We could, but the marginal complexity of handling them like this doesn't seem too high to me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly, but personally I would just return false if any Loop appears, yes.

@tlively
tlively requested a review from kripken November 30, 2026 21:23
@tlively
tlively enabled auto-merge (squash) November 30, 2026 22:17
@tlively
tlively disabled auto-merge November 30, 2026 23:11
@tlively
tlively merged commit 3400881 into main Nov 30, 2026
34 of 37 checks passed
@tlively
tlively deleted the optimize-instructions-loop-fallthrough-effects branch November 30, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants