Skip to content

mirflow: make optimization algorithm non-recursive#1675

Open
zerbina wants to merge 4 commits intonim-works:develfrom
zerbina:mirflow-remove-recursion
Open

mirflow: make optimization algorithm non-recursive#1675
zerbina wants to merge 4 commits intonim-works:develfrom
zerbina:mirflow-remove-recursion

Conversation

@zerbina
Copy link
Copy Markdown
Collaborator

@zerbina zerbina commented Dec 25, 2025

Summary

Change the space optimization applied to the internal, structured
control-flow representation used in the code generator to use a non-
recursive approach.

Details

  • reorder the fields in mirflow.Stmt in order to reduce the type's
    required space (24 byte vs. 32 byte, on a 64-bit architecture)
  • apply the block folding right when emitting a block. This gets rid of
    the recursive post-processing
  • implement a simple optimization pass for inlining some CGIR block
    continuations into dispatcher targets
  • add a dedicated statement kind corresponding to the MIR's continue,
    so that the latter is visible as a terminator to statement analysis

The optimization pass eliminates deep nesting when there are case
statements with many branches, though it's unable to fully optimize the
hash-table lookup generated for case statements over string values.

Processing of the structure control-flow representation in mir2cg
still uses recursion, meaning that its still susceptible to stack
overflow when the structures are very nested.


Notes for Reviewers

  • the first half of fixing thtmlparser.nim not compiling with a debug compiler

Reordering the fields achieves a size of 24 byte on a 64-bit
architecture, compared to the 32 byte the type required previously.
The tree structure the statement list may form can get very deeply
nested, especially when high-level case statements with many branches
are involved, which can lead to stack overflow or exceeding the maximum
recursion depth.

Folding is now done without recursion and as part of constructing the
block structures, which also gets rid of the having to do a separate
pass over the list.
Only breaks in dispatcher targets are considered right now, as those
are the source of the most egregious tree nesting.
The statement is a terminator too, so the block folding and continuation
inlining needs to know also needs to know about it.
@zerbina zerbina added refactor Implementation refactor compiler/backend Related to backend system of the compiler labels Dec 25, 2025
@zerbina
Copy link
Copy Markdown
Collaborator Author

zerbina commented Dec 25, 2025

Changing mir2cg to not use recursion for traversing the control-flow representation isn't as easy, given that there are multiple, mutually recursive routines. It's likely that the test (or other code with large string case statements/expression) won't compile for a long while (until mir2cg is replaced with something better).

As for the CI failure, the C code generator currently emits code that relies on C compiler extensions or C23 (i.e., declarations immediately following a label), and the clang used by the macOS CI seems to be rather strict. I'll make a separate PR for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/backend Related to backend system of the compiler refactor Implementation refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant