Skip to content

[AUTOMATED] Drop more rotation guards and test more loops at the top when it is provably the same program (looptest) - #1963

Merged
mahaloz merged 10 commits into
mainfrom
feat/looptest3
Nov 25, 2026
Merged

mahaloz merged 10 commits into
mainfrom
feat/looptest3

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

gcc copies a loop's test into a guard and often gives the skip path its own copy of the code after the loop, so looptest could not prove the guard droppable and printed a guarded do-while with two returns. gcc -O2 for double dsum(const double *a, long n) { double s = 0; for (long i = 0; i < n; i++) s += a[i]; return s; }:

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

Before:

  if (1 <= a1) {
    v1 = &a0[a1];
    v2 = 0.0;
    do {
      v2 = v2 + *a0++;
    } while (a0 != v1);
    return v2;
  }
  return 0.0;

After:

  v1 = &a0[a1];
  v2 = 0.0;
  while (a0 < v1) {
    v2 = v2 + *a0++;
  }
  return v2;

All of this is part of looptest proven; there is no new option or value, and every step is a proof that the printed loop is the same program.

  • Without its guard, the skip path runs the statements before the loop, the (failing) top test and the loop's exit path. The guard drops when that matches what the skip path did, with every carried variable at its entry value, as after zero iterations: two returns (return s against return 0.0), two return blocks whose calls and stores pair up (return report(head, n) against return report(NULL, 0), using head == NULL from the failed guard), or a join where each merged value agrees (an else arm that sets the counter to its start value). This replaces the stricter rule for variables written before the loop. The skip-side code that disappears may not call or store, and exit-side code that now also runs on the skip path may not read memory.
  • A loop whose body prints nothing, do {} while (f());, prints as while (f()) {}: both run f the same number of times.
  • A latch that recomputes a carried variable's next value (&v2[1] beside v1 = &v2[1]) prints that variable at the top.
  • The entry test's normal form folds constant comparisons and compares sums of the same terms by their constants (s + 0x48 != s + 8). A guard's load also counts as reading the loop's memory when it sits in an earlier block that leads only to the guard, or when a store in between writes other bytes of the same base.
  • The rule that keeps a loop whose counter lives in the stack frame now applies only to functions the unoptimized-code detector flags; in optimized code such a counter is address-taken and is tested like any other.

Tests: two new stage tests, kuna-looptest-zerotrip and kuna-looptest-entry, built from gcc -O2 code (one function at -O0), each step with a control that must stay a do-while; the base build fails 19 of their 25 assertions and passes the six controls. Five existing stage tests whose assertions spelled out the old loop form now match either form, and two CLI tests expect the new form (a DOS timer-tick wait prints while (v1 == dat_46c) {}; a dropped guard moves an i386 function's joined warnings up one line). Unit tests cover the new normal-form rules. 0/675 datatest assertions move.

On the decbench O2 corpus 183 more functions match the source's control flow exactly (gained 191, lost 8; the eight are sources that spell an empty polling loop do { r = REG; } while (...) or while (...) continue;); at O0, 87 more (gained 98, lost 11), almost all empty-body polling loops.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 10 commits November 25, 2026 09:47
[AUTOMATED] Extends looptest proven: a guard drops when the skip path equals the
exit path run zero times (returns, return blocks with calls, joins); empty-body
loops; recomputed latch inputs; linear sums and constant comparisons in the
entry normal form; wider fresh loads; the frame-counter rule limited to
unoptimized functions. Stage tests, unit tests, spec prose, catalog row.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
[AUTOMATED] The DOS timer-tick wait has an empty body and now prints
`while (v1 == dat_46c) {}`; the i386 trampoline function loses a dropped
loop guard, so its joined warnings sit one line higher.

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