Skip to content

[PROPOSAL] angr-dd-argmatch-to-argument-noea-9e6e8b: SAILR goto-reduction by return-tail duplication - #57

Merged
mahaloz merged 3 commits into
mainfrom
feat/angr-dd-argmatch-to-argument-noea-9e6e8b
Jun 25, 2026
Merged

mahaloz merged 3 commits into
mainfrom
feat/angr-dd-argmatch-to-argument-noea-9e6e8b

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

dd-argmatch-to-argument-noea-9e6e8b: SAILR goto-reduction by return-tail duplication (option gotoreduce)

Closes the one structural gap on angr testcase
test_decompiling_dd_argmatch_to_argument_noeagerreturns::argmatch_to_argument
(coreutils dd, x86-64 @ 0x40a640). See
docs/features/dd-argmatch-to-argument-noea-9e6e8b/analysis.md
for the full side-by-side.

Why angr was better

The only difference was a single residual goto label_40a6a3; + label_40a6a3:. A shared
return tail (v2 = 0; falling through to the single return v2;) has two predecessors —
the entry if (v2 == 0) true-arm and an in-loop if (v2 == 0) edge — so Ghidra's
CollapseStructure (the verbatim port of CollapseStructure/TraceDAG/BlockTrace) must
express the second edge as an unstructured goto. It has no schema that duplicates a tail to
avoid a goto. angr (SAILR/Phoenix ReturnDuplicator) emits 0 gotos by duplicating the
constant return tail into the in-loop edge.

Mechanism

New S8 pass kuna_gotoreduce.rs (ActionGotoReduce), registered after ActionFinalStructure.
It finds every BlockIf that renders as if (cond) goto T whose target T is a small
single-successor basic-block chain ending in return (bounded: ≤3 blocks / ≤8 ops, no
CALL/STORE), and rewrites it to if (cond) { <tail> } by minting fresh BlockCopy leaves
over the same underlying basic blocks (a print-tree duplication — the C printer re-emits
their ops; CPUI_RETURN carries the returns flag, not branch, so it survives the if-body
NO_BRANCH). No p-code is cloned and SSA/def-use is untouched. When the converted goto was
the only unstructured edge into T, T's label is cleared. The only genuine graph surgery is
one (kuna) method BlockGraph::kuna_inline_return_tail in block.rs; everything else is the
new module plus the standard option anchors.

The option

option gotoreduce on|off (architecture/seam flag reduce_return_gotos, ElementId 4100),
default-OFF opt-in — flip per decompilation:

kuna decompile ./dd argmatch_to_argument --option gotoreduce on

Ablation / parity / speed

  • Ablation: clean — 0/675 upstream datatest assertions change with the feature default-ON.
  • Speed: within the +5% budget on the target (off≈148.7ms, on≈105.1ms, Δ≈-29% — the
    extra post-structuring scan is negligible; the spread is measurement noise).
  • Default decision: kept default-OFF opt-in despite the clean ablation. This is the
    first S8 structured-tree mutation on the verbatim-ported collapse engine and is rated
    high-risk in the approved proposal, so it ships opt-in pending broader validation (no DIV
    entry; default output stays byte-identical). A reviewer can flip it default-ON later given
    the clean ablation.
  • Gates: kuna catalog --check OK · datatest parity PARITY OK (675/675) · stage suite
    PARITY OK (162/162, incl. the new tests/stages/ghangr-dd-argmatch-to-argument-noea-9e6e8b.xml).

This is the same SAILR goto-reduction family as the parked morton/newburry/tr-build
proposals; dd::argmatch_to_argument is the minimal return <const> variant of that transform.

🤖 Generated with Claude Code


Before / after — argmatch_to_argument in dd

Real captured kuna output on the function this feature was built for, with option gotoreduce flipped. Ported from angr — test_decompiling_dd_argmatch_to_argument_noeagerreturns; SAILR/Phoenix goto-reduction (return-tail duplication / ReturnDuplicator); argmatch_to_argument.

metric gotoreduce off (default) gotoreduce on
gotos 1 0 ⬇️
labels 1 0 ⬇️
loc 21 23 ⬆️
switches 0 0
speed 148.66 ms 105.11 ms — Δ -29.3% (budget 5.0%)
kuna — before (option gotoreduce off)
int8 argmatch_to_argument(unsigned long a0,int8 *a1,int8 a2,int8 a3)

{
  int4 v1; // eax
  int8 v2;
  
  v2 = *a1;
  if (v2 == 0) {
label_40a6a3:
    v2 = 0;
  }
  else {
    v1 = bcmp(a0,a2,a3);
    while (v1 != 0) {
      a2 = a2 + a3;
      a1 = &a1[1];
      v2 = *a1;
      if (v2 == 0) goto label_40a6a3;
      v1 = bcmp(a0,a2,a3);
    }
  }
  return v2;
}
kuna — after (option gotoreduce on)
int8 argmatch_to_argument(unsigned long a0,int8 *a1,int8 a2,int8 a3)

{
  int4 v1; // eax
  int8 v2;
  
  v2 = *a1;
  if (v2 == 0) {
    v2 = 0;
  }
  else {
    v1 = bcmp(a0,a2,a3);
    while (v1 != 0) {
      a2 = a2 + a3;
      a1 = &a1[1];
      v2 = *a1;
      if (v2 == 0) {
        v2 = 0;
        return v2;
      }
      v1 = bcmp(a0,a2,a3);
    }
  }
  return v2;
}
angr reference (the goal, v9.2.213)
unsigned long long argmatch_to_argument(void* a0, unsigned long long *a1, void* a2, unsigned long a3)
{
    void* v1;  // r15
    unsigned long long v2[2];  // rbx
    unsigned long long v3[2];  // rbx
    void* v4;  // r15

    if (!*(a1))
        return 0;
    if (bcmp(a0, a2, a3))
    {
        v1 = a2 + a3;
        v2 = a1 + 1;
        while (true)
        {
            v3 = v2;
            v4 = v1;
            if (!v3[0])
                break;
            v1 = v4 + a3;
            v2 = &v3[1];
            if (!bcmp(a0, v4, a3))
                return v3[0];
        }
    }
    else
    {
        return *(a1);
    }
    return 0;
}

…(return-tail duplication)

angr emits 0 gotos on dd::argmatch_to_argument where kuna emits 1 goto + 1 label to a
shared `v2 = 0; return v2;` tail. Closing this needs the proposed S8 goto-reduction pass
(kuna_gotoreduce.rs) doing return-tail duplication into the verbatim CollapseStructure
engine — large per Hard rule 7 and the recorded decider verdict. This is the simpler
return-constant variant of the existing morton `gotoreduce` proposal; file as an
additional motivating testcase, not a competing point fix. Analysis + proposal + partial
record only; no implementation.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz

mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

APPROVE

…ail duplication (option gotoreduce)

New S8 pass `kuna_gotoreduce.rs` (`ActionGotoReduce`, registered after
`ActionFinalStructure`, option `gotoreduce`, default-OFF) implementing angr's
SAILR/Phoenix goto-reduction (ReturnDuplicator) for the return-constant variant.

On `dd::argmatch_to_argument` the only structural diff vs angr was a single
residual `goto label_40a6a3;` + `label_40a6a3:` to a shared `v2 = 0; return v2;`
return tail with two predecessors, which Ghidra's verbatim CollapseStructure must
express as an unstructured edge. The pass finds every `BlockIf` that renders as
`if (cond) goto T` whose target T is a small single-successor basic-block chain
ending in `return` (<=3 blocks / <=8 ops, no call/store) and rewrites it to
`if (cond) { <tail> }` by minting fresh BlockCopy leaves over the same underlying
basic blocks (a print-tree duplication; no p-code cloned, SSA/def-use untouched),
then clears T's now-unused label.

Only genuine graph surgery is one `(kuna)` method
`BlockGraph::kuna_inline_return_tail` in block.rs; the rest is the new module plus
the standard option anchors (architecture/seam flag `reduce_return_gotos`,
options.rs, universalaction.rs, stages.toml). ElementId 4100.

Ablation clean (0/675 datatest assertions change if default-ON) and speed within
budget, but kept default-OFF opt-in: first S8 structured-tree mutation on the
verbatim collapse engine, rated high-risk in the approved proposal. No DIV entry
(default output byte-identical). Stage test
tests/stages/ghangr-dd-argmatch-to-argument-noea-9e6e8b.xml. Snapshot/count test
fixtures (action listing, catalog, settable counts, corpus count) updated for the
new option/action/test.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz
mahaloz marked this pull request as ready for review June 25, 2026 16:06
…57)

Union-resolve the registrations for the gotoreduce opt-in option (angr SAILR
ReturnDuplicator goto-reduction) against main, which had advanced with the #55
(loopbreak_recovery/DIV-10) and #67 (macho-arm64e) settables:
- architecture.rs / seams.rs / options.rs: union reduce_return_gotos with
  recover_loop_break/fold_call_returns fields, ctors, resets, set_kuna_option
  arms, KUNA_OPTION_NAMES, seam-copies
- s8_structure/mod.rs: union kuna_gotoreduce + kuna_loopbreak_recovery modules
- stages.toml: union the gotoreduce settable; 45 settables now
  (27 stage-model + 15 analysis + 3 loader caps), surface=91
- gotoreduce ElementId 4100 (no collision: dedup 4091, loopbreak 4092)
- catalog provenance counts: angr 10, structure-recovery 6 (gotoreduce both)
- count assertions (kuna_stages/tests.rs, catalog_bytecompat.rs, xml.rs corpus
  137 files) bumped; regenerate stage_catalog fixture, assertions.md,
  baseline-stages.json (168/168)
- gotoreduce is opt-in (default-off) -> no DIV entry

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (168/168).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz merged commit 85ea072 into main Jun 25, 2026
mahaloz added a commit that referenced this pull request Jun 25, 2026
Region-structurer foundation (Inc 0) — analysis-only, no new --option. Main had
advanced with #55 (loopbreak_recovery/DIV-10), #57 (gotoreduce), #67 (macho-arm64e);
the only conflict was docs/baseline-stages.json (data_footer counts), all other
region code (s7_regions/kuna_regiongraph.rs, kuna_regionid.rs, kuna_console.rs) and
the kuna-regions.xml assertions merged cleanly. Regenerated assertions.md (45
settables, unchanged) and baseline-stages.json (171/171, incl. the 8 KUNA-REGIONS
assertions plus main's gotoreduce/loopbreak/catalog ones).

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (171/171).
Region unit tests: regionid 6/6, regiongraph 18/18 (incl. new build_from_block_graph_*).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz deleted the feat/angr-dd-argmatch-to-argument-noea-9e6e8b branch July 29, 2026 08:01
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