Skip to content

Stop merging a long run of assignments into one deep expression - #1490

Merged
Frotty merged 3 commits into
masterfrom
bound-merged-assignments
Oct 17, 2026
Merged

Frotty merged 3 commits into
masterfrom
bound-merged-assignments

Conversation

@Frotty

@Frotty Frotty commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

With -localOptimizations, SimpleRewrites merges x = e1; x = x ⊕ e2 into x = e1 ⊕ e2, and one walk merges a whole run of such statements. A generated run of sum += g0 ... sum += g3299 therefore became a single expression 3300 levels deep. The flatten at the end of the same pass then overflowed the Java stack (Flatten.flattenExprs -> ImOperatorCall.flatten -> Flatten.flatten). On the default 1 MB stack this happens from about 850 statements with SimpleRewrites cold, and between 1200 and 1500 statements in a full compile.

The same growth also breaks Lua output well before the stack. When the operators alternate (h *= a, h += b), each level is a parenthesis in the Lua output, and luac rejects 150 pairs with "too many C levels (limit is 200)".

The deep expression exists only because this pass builds it from flat statements, so I fixed it there: the merge now stops once the left operands already hold 64 operators (SimpleRewrites.MAX_MERGED_OPERATORS). I chose this over making the walkers independent of the Java stack because the Lua case is a limit in the emitted script, not in the compiler, and because the IM walkers (generated copy, visitors, attributes, flatten, translators, printers) all recurse. A long run still merges into one assignment per 64 statements. Output is unchanged unless an expression already has 64 operators down its left operands.

Tests (OptimizerTests):

  • longRunOfAccumulatingStatementsCompiles: 3300 statements, all Jass variants plus the interpreter. StackOverflowError before the fix, passes after it.
  • longRunOfAlternatingOperatorsLoadsInLua: luac fails before the fix, loads and runs after it.
  • mergingConsecutiveAssignmentsNestsBoundedly: runs the pass on its own and checks that runs still merge and stay within the bound.

OptimizerTests, LuaBackendAuditTests and DeterministicChecks pass. Compiling the repros twice gives byte-identical Jass and Lua, and pjass parses all five Jass variants. This is an IM-only change with no host assumptions; I checked it on Windows only.

Measured on the 3.0.0 client (Lua, -inline -localOptimizations, 150 pairs of h *= a and h += b in one function): built by master before this change, the script nests 300 parentheses and the map never started; the client stayed at the main menu and exited without running it. Built with this change, the deepest line nests 64, the map loads and mix(1, 1) returns 151. The same function in a Jass build loads and returns 151 as well.

With -localOptimizations, SimpleRewrites merges `x = e1; x = x + e2` into
`x = e1 + e2`, and one walk merges a whole run, so a generated run of
`sum += gN` became one expression as deep as the run. The flatten after
it overflowed the Java stack from about a thousand statements, and with
alternating operators the Lua output nested a parenthesis per level,
which luac rejects beyond 200. A merge now stops once the left operands
hold 64 operators.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 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-17T19:16:39.425667Z 2947b2a Manual request
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f5f1f7a9ec

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The bound counted only operators down the left operands, so a first value nested deep on its right operands could still be merged past what luac loads.
@Frotty

Frotty commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 2947b2a30d

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Frotty
Frotty merged commit 2cadc6a into master Oct 17, 2026
2 checks passed
@Frotty
Frotty deleted the bound-merged-assignments branch October 17, 2026 19:25
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