Repository navigation
MLIR: rotate counted loops and inline trivial accessors - #538
Merged
Merged
Conversation
Two rewrites run on the AST before MLIR generation, both found while compiling Doom through the MLIR backend. Counted-loop rotation (#473): `while true { P; if c == 0 { break }; S }` becomes `P; while c != 0 { S; P }`. The exit test moves from the middle of the body to the loop latch, which is the form LLVM's vectorizer and unroller need to recover a trip count. Both shapes evaluate the test at the same program points, so the iteration count is unchanged. The rewrite is declined when the body holds another break/continue, a defer, a second write to the counter, or a declaration in the peeled prefix. Trivial accessor inlining (#474): a parameterless function whose entire body is `return &g` or `return g` for a module-scope `g` is substituted at its call sites. R_DrawColumn makes eight such calls per iteration; each becomes a plain load instead of a call+load pair. The definitions are still emitted, so external linkage is unchanged. Integration coverage compiles six loop shapes plus a Doom-shaped fixture through the C backend twice, once from the parsed AST and once after rotation, and compares what each binary returns. Unit tests pin the MLIR shape and the cases where each rewrite must decline. Unit 1411 passed, tier-2 677/677, ruff clean. Closes #473 Closes #474 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.
Closes #473. Closes #474.
Both rewrites came out of compiling Doom through the MLIR backend, and both are the blockers the issues describe for
-O3producing useful code. They live in a newsrc/flow/mlir_canonicalize.pyand run on the AST just before MLIR generation. The C backend is untouched.Counted-loop rotation (#473)
while true { P; if c == 0 { break }; S }becomesP; while c != 0 { S; P }.The exit test moves from the middle of the body onto the loop latch, which is the form LLVM's vectorizer and unroller need in order to recover a trip count. The original evaluates the test after
Pof every iteration; the rotated loop evaluates it at exactly the same points, so the iteration count is unchanged.The rewrite declines when the body holds another
break/continue(rotation would retarget it), adefer(scope-sensitive), a second write to the counter (then it is not a countdown), or a declaration in the peeled prefix (the prefix is duplicated and aletwould land in two scopes).Before, on the fixture's hot loop:
After:
Trivial accessor inlining (#474)
A parameterless function whose entire body is
return &gorreturn gfor a module-scopegis substituted at its call sites.R_DrawColumnmakes eight such calls per iteration; each becomes a plain load rather than a call+load pair. The definitions are still emitted, so external linkage is unchanged. The substitution is skipped when a local has shadowed the global at the call site.Validation
tests/integration/test_counted_loop_rotation.pycompiles six loop shapes plus a Doom-shaped fixture through the C backend twice, once from the parsed AST and once after rotation, and asserts the two binaries return the same value. Shapes covered: plain countdown, zero-trip, break-first, statements after the decrement, a nested if in the prefix, and a step of two. A separate case pins the expected iteration counts so both forms cannot be wrong the same way.tests/unit/test_mlir_canonicalize.pypins the rotated AST, the emitted MLIR, and every case where a rewrite must decline.main./flow test --tier2 --strict— 677/677ruff check srccleanNeither pattern appears anywhere in-tree today, which is why the sweep shows no diff.
tests/fixtures/counted_loop_rotation.flowadds a Doom-shaped case so the repo covers both.I have no local MLIR toolchain and no access to the doom-flow CRC32 parity samples, so the
-O3codegen improvement itself is unverified here. The semantics of both rewrites are verified by execution through the C backend.