Skip to content

[AUTOMATED] Print a rotated loop with its test at the top when that is provably the same program (option looptest) - #1899

Merged
mahaloz merged 4 commits into
mainfrom
feat/looptest
Nov 23, 2026
Merged

mahaloz merged 4 commits into
mainfrom
feat/looptest

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

An optimizing compiler rotates while (c) body into if (c) do body while (c);, and kuna prints the rotated form. For this gcc -O2 for (p = a; p < a + n; p++) *p = v;:

printf '4885f67411488d04f74889174883c7084839c775f4c3c3' | xxd -r -p > fill.bin
kuna decompile fill.bin 0x100000 --raw-image --target x86:LE:64:default:gcc --base 0x100000

Before:

  if (a1) {
    v1 = &a0[a1];
    do {
      *a0++ = a2;
    } while (a0 != v1);
    return;
  }

After:

  v1 = &a0[a1];
  while (a0 != v1) {
    *a0++ = a2;
  }
  • New option looptest (P8, binary|proven|assume, default proven, C output only). It plans each do … while on the final structured tree and prints it as while (c) { body } only when that is the same program; the p-code is not changed.
  • The test can move to the top when every loop-variant leaf of the printed condition is the value a header MULTIEQUAL carries into the next iteration (a COPY source prints as the carried variable, so do { q = *p; p = q; } while (q) becomes while (p)) and the condition calls nothing.
  • The first test must agree with the binary: the condition with each carried value replaced by its entry value has to fold to true, equal a dominating branch condition, or equal the guard in front of the loop. The comparison is a small normal form over p-code (0 < n and n != 0 for unsigned n, p == &p[n] and n == 0, constant folding, LOADs only when nothing writes memory in between). Implication is not accepted: with a signed n, a guard n > 0 in front of p != &a[n] keeps the do-while.
  • The guard is left out only when the statements between it and the loop are pure and write nothing the skip path reads, and the skip path reaches the loop's exit or an equal bare return. If the guard has to stay, or the loop variable lives in the stack frame (unoptimized code), the loop stays a do-while.
  • binary restores the old output; assume also moves the test of loops with no entry proof (speculative, off by default).

Tests: new stage test kuna-looptest (six rotated loops; three lose their guard, one has a constant entry test, two must stay do-while; option looptest binary restores all of them) and unit tests for the condition normal form. 0/675 datatest assertions move; six stage tests that pin a do-while with a constant entry test now set option looptest binary (one checks the loop in either spelling).

On the decbench O2 corpus 756 more functions match the source's control flow exactly (gained 760, lost 4, all four real do … while loops whose first test is constant); at O0, +9 (gained 9, lost 0).

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 4 commits November 23, 2026 10:00
…he same program (option looptest)

gcc's loop header copying turns `while (c) body` into `if (c) do body while (c);`
and drops the `if` when it proves `c` on entry, so kuna printed do-while loops
the source tests at the top. New option looptest (P8, values
binary|proven|assume, default proven, C output only) plans each BlockDoWhile
on the final structured tree at the top of body emission. The test moves to
the top when every loop-variant leaf of the printed condition is a value a
header MULTIEQUAL carries into the next iteration (a COPY source prints as the
carried variable) and the condition calls nothing, and the entry test (each
carried value replaced by its entry value, compared in a small normal form)
folds to true, equals a dominating edge's condition, or equals the guard in
front of the loop. That guard is left out when the blocks between it and the
loop are pure and write nothing the skip path reads, and the skip path reaches
the loop's exit or an equal bare return. A guard that must stay, or a carried
variable in the stack frame, keeps the do-while. 0/675 datatest assertions
move. New stage test kuna-looptest.

[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…age tests to the bottom test

The catalog counts, the stage corpus count and the phase-catalog fixture move
by one for looptest; docs/options.md is regenerated. Six existing stage tests
pin a do-while whose entry test folds to true and now prints top-tested: five
add `option looptest binary` (gh9203, endptrbound, repstring x2,
stackguard-tailcall, tiedphitrim), and REPFRAMECOPY #9 checks the loop in
either spelling. The stages baseline gains the twelve LOOPTEST assertions and
loses none.

[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
Both probes check what endptrbound does to a loop bound; under looptest the
counted loop and the buffer walk now print `while (c) {` (their entry tests
fold to true), so the bound regexes accept the test at the top or the bottom.

[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit d7aeaef into main Nov 23, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/looptest branch November 23, 2026 10:54
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