Skip to content

MLIR: rotate counted loops and inline trivial accessors - #538

Merged
godofecht merged 1 commit into
mainfrom
feat/mlir-counted-loops-and-accessor-inlining
Aug 18, 2026
Merged

godofecht merged 1 commit into
mainfrom
feat/mlir-counted-loops-and-accessor-inlining

Conversation

@godofecht

@godofecht godofecht commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #473. Closes #474.

Both rewrites came out of compiling Doom through the MLIR backend, and both are the blockers the issues describe for -O3 producing useful code. They live in a new src/flow/mlir_canonicalize.py and 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 } becomes P; 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 P of 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), a defer (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 a let would land in two scopes).

Before, on the fixture's hot loop:

cf.br ^bb0
^bb0:
  ... body ...
  %c = arith.cmpi eq, %count, %zero : i32
  cf.cond_br %c, ^exit, ^continue
^continue:
  ... rest of body ...
  cf.br ^bb0

After:

... peeled prefix ...
cf.br ^bb0
^bb0:
  %c = arith.cmpi ne, %count, %zero : i32
  cf.cond_br %c, ^bb1, ^bb2
^bb1:
  ... body ...
  cf.br ^bb0

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 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.

%17 = llvm.mlir.addressof @dc_iscale : !llvm.ptr   # was func.call @dc_iscale_value
%18 = llvm.load %17 : !llvm.ptr -> i32

Validation

tests/integration/test_counted_loop_rotation.py compiles 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.py pins the rotated AST, the emitted MLIR, and every case where a rewrite must decline.

  • Unit suite: 1411 passed, 2 skipped
  • Integration: 44 passed, 17 pre-existing failures unchanged from main
  • Tier 2: ./flow test --tier2 --strict — 677/677
  • ruff check src clean
  • MLIR generation swept over 1593 corpus files: no new failures, and byte-identical output where neither pattern occurs

Neither pattern appears anywhere in-tree today, which is why the sweep shows no diff. tests/fixtures/counted_loop_rotation.flow adds 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 -O3 codegen improvement itself is unverified here. The semantics of both rewrites are verified by execution through the C backend.

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]>
@godofecht
godofecht merged commit 539ab70 into main Aug 18, 2026
21 checks passed
@godofecht
godofecht deleted the feat/mlir-counted-loops-and-accessor-inlining branch August 18, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant