Skip to content

[AUTOMATED] Print a chain of tests into one no-return call as one if (condfold noreturn) - #1982

Merged
mahaloz merged 6 commits into
mainfrom
feat/fatalchain
Nov 26, 2026
Merged

mahaloz merged 6 commits into
mainfrom
feat/fatalchain

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Code that checks a run of calls and stops in one fatal call, if ((r = put(b, x)) != 0 || (r = put(b, y)) != 0) fatal(r, "first");, prints as a ladder of nested ifs with the continuation innermost and the fatal call after all of them. With this change it prints as one if over the || chain, the fatal call as its body, and the rest of the function after it.

kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/fatalchain_gcc_O2_x86_64 twice

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

Before:

  v1 = put(a0,1);
  v2 = (unsigned int)v3;
  if (!v1) {
    v1 = put(a0,v3 & 0xffffffff);
    if (!v1) {
      sink = v2;
      v1 = put(a0,2);
      if (!v1) {
        v1 = put(a0,*a0);
        if (!v1)
          return *a0;
      }
      fatal(v1,"second"); // no-return
    }
  }
  fatal(v1,"first"); // no-return

After:

  v1 = put(a0,1);
  v2 = (unsigned int)v3;
  if ((v1) || (v1 = put(a0,v3 & 0xffffffff), v1))
    fatal(v1,"first"); // no-return
  sink = v2;
  v1 = put(a0,2);
  if ((v1) || (v1 = put(a0,*a0), v1))
    fatal(v1,"second"); // no-return
  return *a0;
  • The function is structured inside the main loop while each call block still holds the call's stack bookkeeping (on x86 the push of the return address), so BlockBasic::isComplex counts call, store and branch and ruleBlockOr declines every later test. For a chain of two-way blocks whose taken edges all reach one block ending in a call with the no-return note, the later tests are now structured as not complex. A test that then prints more statements before its comparison than condstmts allows goes through that option's existing reanalysis and keeps its ladder, and a structuring that relaxed and prints a goto is replaced by the one that did not.
  • A small print-tree pass after the final structure turns if (chain) { R } f(); (R not falling through) into if (!chain) f(); R, at any depth. Neither form reaches past f(), so the tests run in the same order with the same short-circuit.
  • This is a new value of condfold: noreturn, the new default (on and wide include it). condfold off prints the ladder.
  • On the decbench gcc corpus, 66 more functions at O2 without inlining and 89 more at O0 match the source's control flow exactly; none is lost. Whole-binary analysis time on sshd, bash and ls changes by +1.6%, -0.4% and -1.6%.

Tests: tests/stages/kuna-fatalchain.xml decompiles the -O2 and -O0 fixtures with condfold off (ladders) and the default (one if per chain; a test with four calls before it keeps its ladder). kuna-cli/tests/fatalchain_round_trip.rs compiles the printed functions beside the source with gcc and clang at -O0 and -O2 and runs them with put failing at each call in turn, comparing the result, the stored state, the value fatal receives and the order of the put calls. ghdec-guardarm.xml and kuna-tailform-noreturn.xml now pin condfold off, since they isolate guardarm and tailform. No datatest assertion moves.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 6 commits November 26, 2026 01:57
…ld noreturn)

[AUTOMATED] work in progress: relax the stale mainloop isComplex verdict for the
later tests of a chain into a no-return block, verify at the final position,
keep the structuring with fewer gotos, and orient the folded if so the
no-return call is its body.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…only goto-free structurings

[AUTOMATED] A relaxed chain test that prints too wide is analyzed again without
the relaxation (its own veto set), not seeded complex, so a late re-structure
keeps folding it as main does. A relaxed structuring that prints a goto is
replaced by the unrelaxed one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…; fixture and stage test

[AUTOMATED] A relaxed test may print as many statements before its comparison
as condstmts allows (that option's reanalysis handles a wider one), the
target's call must carry the no-return note, and the fatalchain fixture
(gcc -O2/-O0) with tests/stages/kuna-fatalchain.xml.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] condfold row (values off|noreturn|on|wide, default noreturn), spec
prose in chapter 08, the round-trip test, the regenerated catalog fixtures and
counts, and condfold off pins in two stage tests that isolate guardarm and
tailform.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit 984dbc9 into main Nov 26, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/fatalchain branch November 26, 2026 03:05
mahaloz added a commit that referenced this pull request Nov 26, 2026
…timized code (condfold auto) (#1983)

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 `if`s. #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:

```c
  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:

```c
  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.com/claude-code)

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU


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