Skip to content

[AUTOMATED] Fold a chain of tests into one ordinary error block in optimized code (condfold auto) - #1983

Merged
mahaloz merged 7 commits into
mainfrom
feat/chainfold
Nov 26, 2026
Merged

mahaloz merged 7 commits into
mainfrom
feat/chainfold

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

In optimized code, a run of calls whose failures all go to one error block, if ((r = put(b, x)) != 0 || (r = put(b, 2 * x)) != 0) { note(r); return; }, prints as a ladder of nested ifs. #1982 fixed this when the error block ends in a call that does not return; this does the same when it is an ordinary block (a return, the target of a goto out), in functions that are not compiled without optimization.

kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/chainfold_gcc_O2_x86_64 store

(the fixture is added here; it is chainfold.c built with gcc 11.4 -O2 -fno-reorder-blocks-and-partition)

Before:

  v1 = put(a0,a1);
  if (!v1) {
    v1 = put(a0,(int)a1 * 2);
    if (!v1) {
      sink = *a0;
      note(0); // tail-call
      return;
    }
  }
  note(v1); // tail-call

After:

  v1 = put(a0,a1);
  if ((!v1) && (v1 = put(a0,(int)a1 * 2), !v1)) {
    sink = *a0;
    note(0); // tail-call
    return;
  }
  note(v1); // tail-call
  • condfold gets a value auto, the new default: everything noreturn does, plus, in a function the unoptimized detector does not flag, a chain into any block only the chain enters. noreturn keeps its meaning, so the old print is --option condfold noreturn. In unoptimized code authors nest these tests often enough that the fold loses there, so it stays off.
  • The no-return case's guards all apply: the condstmts width cap, the comment check, and replacing a structuring that prints a goto. A later print-tree pass can still leave a goto (taildup did on one function), so a function whose fold into an ordinary block prints a goto at the end is decompiled again without it, and the copy with fewer gotos wins (the unfolded one on a tie).
  • On the decbench gcc corpus, against noreturn in the same build: 73 more functions at O2 without inlining match the source's control flow exactly (102 gained, 29 lost), positive on both project halves; 14 more at O0 from functions the detector does not flag; no function gains a goto. The losses are sources that really nest the second test, mostly because they use its value afterwards.
  • Whole-binary analysis time against noreturn on sshd, bash and ls: +1.0%, +2.4%, +3.1%. To keep it there, the goto fallback only re-structures when a relaxed test actually folded, and a large function (64 blocks or more) stops relaxing once such a fold has fallen back.

Tests: tests/stages/kuna-chainfold.xml decompiles the fixture with condfold noreturn (the -O2 ladder) and the default (one condition), and checks that the -O0 build keeps its ladder under both. The round-trip test gains a second case that compiles the printed functions beside the source with gcc and clang at -O0 and -O2, with put failing at each call in turn, and compares the results, stored values and call order. gh6904-ppc-e500-concat44.xml, structsynth-locals.xml and the ELFv1 callmechanisms CLI test now pin condfold noreturn, since they check other features (under callmechanisms on the chain now folds as it does by default: the TOC store it keeps after each call does not print). No datatest assertion moves.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 7 commits November 26, 2026 03:11
[AUTOMATED] Under the default auto the callmechanisms-on chain folds too: the
store kept after each call does not print, it only made the first structuring
call the test complex.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit 27f65c9 into main Nov 26, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/chainfold branch November 26, 2026 04:47
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