Skip to content

[AUTOMATED] Print one || guard and a return in each arm where optimized code shares a return block (returndup auto) - #1965

Merged
mahaloz merged 14 commits into
mainfrom
feat/retshape
Nov 25, 2026
Merged

mahaloz merged 14 commits into
mainfrom
feat/retshape

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

In optimized code the binary's return layout, not the author, decides two return shapes kuna prints: tests that share one return block print as one guard each, and a return block the arms share prints as one return v; fed by assignments. Sources rarely wrote either. The new fixture shows both (--option returndup record prints the old form):

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/retshape_gcc_O2_x86_64 mutable_ptr --option returndup record
  if (sane(a0))
    return 0;
  if (a0->field_0x10)
    return 0;
  if (2 <= a0->field_0x14)
    return 0;
  return a0->field_0x8 + a0->field_0x0;

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/retshape_gcc_O2_x86_64 mutable_ptr
  if (((sane(a0)) || (a0->field_0x10)) || (2 <= a0->field_0x14))
    return 0;
  return a0->field_0x8 + a0->field_0x0;

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/retshape_gcc_O2_x86_64 describe --option returndup record
  if (prepare(a0,v2) != -1) // branch-flip
    v1 = measure(a1,v2,a3 & 0xffffffff);
  else {
    __snprintf_chk(a2,a3,1,0xffffffffffffffff,"cannot prepare %s",a1);
    v1 = 0xffffffff;
  }
  return v1;

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/retshape_gcc_O2_x86_64 describe
  if (prepare(a0,v1) != -1)
    return measure(a1,v1,a3 & 0xffffffff);
  __snprintf_chk(a2,a3,1,0xffffffffffffffff,"cannot prepare %s",a1);
  return 0xffffffff;
  • returndup takes a new value record (the previous default). The default auto keeps it and adds both re-spellings, only in functions the unoptimized detector does not flag and that do not address their frame through a frame pointer.
  • Guards (p8_structure/kuna_retshape.rs, ActionRetShape, a print-tree edit before tailform): adjacent guards with the same bare return fold into one || guard when the later test prints nothing before it. Each test keeps its polarity and order, so it is the same program. Two tests that both call something stay apart when the return is void or a constant, as sources usually check one call per guard.
  • Merged returns (arm_returns, handed to returndup): a return block whose value is a phi of its in-edges, perhaps cast or extended, and nothing else is split per arm with the existing node_split. A block that only runs into the return (the join a stripped stack-protector check leaves) is spliced first. Every in-edge must end an arm entered from one place, one if or one short-circuit chain; otherwise the split arm would need a goto. A one-block if splits only when the arm does not read the carried value, so v += k updates keep one return.

On the decbench corpus, against record in the same build, 305 more O2-noinline functions match the source's control flow exactly and 23 fewer (+282; +204 and +78 on the two project halves). At O0, 61 more and 8 fewer, all in functions the detector misses. One function gains a goto, through loopcondhoist's per-structuring check.

Tests: tests/stages/kuna-retshape-guards.xml and kuna-retshape-arms.xml (default, then record) on the new gcc -O2 fixture. They also cover the cases that must not change: two call tests and a one-armed update. kuna-cli/tests/retshape_round_trip.rs compiles and runs the printed C under gcc and clang at -O0/-O2 in both modes. Tests that cover something else pin option returndup record: 4 vendored datatests and 17 stage files, with docs/baseline.json unchanged.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 14 commits November 25, 2026 12:55
…default re-spells

[AUTOMATED] Four vendored datatests (13 assertions) and 17 stage tests (30
assertions) test something other than return shapes; each pins the structured
form after its image loads. docs/baseline.json is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] Stage corpus count 920, stage baseline re-recorded with the
nine retshape assertions, phase catalog and options.md regenerated.

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

[AUTOMATED] The action listing snapshot and schedule count gain retshape;
the boolless parity tests and two kuna-cli renaming tests pin the structured
returns; the bejoin round trip declares each function without the
signature line's trailing annotation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] The returndup symptoms no longer contain `||` (the catalog
splits symptoms on `|`); use_when and the spec chapter carry the numbers
measured against `record` on main fb88f58, the one goto gainer and the
speed measurement.

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