Skip to content

Branch merger: keep a condition with effects in front of equal exits - #1416

Merged
Frotty merged 1 commit into
masterfrom
branch-merger-keeps-condition
Oct 9, 2026
Merged

Frotty merged 1 commit into
masterfrom
branch-merger-keeps-condition

Conversation

@Frotty

@Frotty Frotty commented Oct 9, 2026

Copy link
Copy Markdown
Member

When both branches of an if start with the same statement, BranchMerger moves it in front of the if. Its check (SideEffectAnalyzer.mightAffect) only asks whether the statement and the condition may be evaluated in the other order. A return, or an exitwhen of a loop around the if, also means the condition is never evaluated, and such a statement uses no variables, so it was always moved:

@noinline function f(int b)
    if eff(b) > 0
        return
    else
        return

compiled to a plain return with -localOptimizations on Jass and Lua, and eff was never called. The same for break in both branches inside a loop. This predates the recent commits (BranchMerger was last changed in #1376 / #1207); the audit's fuzzing of the inliner found it.

A statement which may leave the block (a return anywhere in it, or an exitwhen not inside a loop of its own) is now moved only when the condition has no effect (SideEffectAnalyzer.quickcheckHasSideeffects), so equal returns under a pure condition are still merged.

Tests (OptimizerTests): branchMergerKeepsTheConditionBeforeEqualReturns and ...EqualExits (Jass variants and Lua) fail on master and pass with the change; branchMergerMergesEqualReturnsUnderAConditionWithoutEffects checks the merge still happens. OptimizerTests, BugTests and LuaBackendAuditTests pass. Castle fight (Lua, -inline -localOptimizations) compiles to a byte-identical script.

When both branches of an if start with the same statement, the merger
moves it in front of the if, where it runs before the condition. A return
or an exitwhen of an enclosing loop there means the condition is never
evaluated: if eff(b) > 0 ... return else return became return, and eff
was not called. Such a statement is moved now only when the condition has
no effect.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T15:29:00.558402Z 26ef63f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Frotty
Frotty merged commit 06bbf60 into master Oct 9, 2026
3 checks passed
@Frotty
Frotty deleted the branch-merger-keeps-condition branch November 25, 2026 08:53
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