Skip to content

dedupitetail: merge duplicated if/else tails (true_1804 usage) - #104

Merged
mahaloz merged 2 commits into
mainfrom
feat/dedup-ite-tail
Jun 26, 2026
Merged

mahaloz merged 2 commits into
mainfrom
feat/dedup-ite-tail

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

dedupitetail — merge duplicated if/else tails (angr ITE region-dedup)

Implements proposal #53. A new S8 structuring pass ActionDedupIteTail
(option dedupitetail, ElementId 4109, default-OFF) — the inverse of the
SAILR duplication passes (gotoreduce/crossjumprevert/taildup).

The gap

kuna inherits Ghidra's collapse-based structurer, which, when a basic block is
reachable from both arms of an if/else, clones it into each arm instead of
converging the two predecessors onto one copy. angr emits each shared continuation
once. So kuna over-duplicates a shared if/else tail and the readability win is to
merge the identical copies.

What it does

For every 3-component BlockIf, find the maximal common prefix or suffix of
leaf blocks that emit identical C and hoist that shared run out of the if (prefix
before it, suffix after), emitting one copy instead of two:

  • Equivalence is exact-or-decline: same printed-op sequence; constants matched by
    value; named storage by location; call targets by resolved callee
    (get_call_specs_index, not the per-call-site fspec index); block-local unique
    temps paired positionally. STORE and any unresolvable operand decline.
  • Soundness: a pure print-tree edit (no p-code cloned, SSA/def-use untouched). A
    prefix hoist is unconditionally sound (both arms run it first); a suffix hoist is
    gated on both arms' divergent middles falling through to it.
  • Bounded (<=8 leaves, <=64 ops/leaf); only flat unlabelled leaves are merged
    (never a nested if/loop or a goto/label block). Surgery helpers
    kuna_hoist_ite_{prefix,suffix} in block.rs, with unwrap_singleton_clause to
    avoid the degenerate 1-element BlockList the PrintC::emitBlockLs path
    double-emits.

Before / after (the stage-test reproducer g)

The two arms each run a divergent guard then call the SAME shared epilogue
shared(y) in its own block (the ISC-condensed shared-tail shape):

// option dedupitetail OFF (default)        // option dedupitetail ON
if (a0 == 0) {                              if (a0 == 0) {
  v1 = cond_b(); if (v1 != 0) body_b();       v1 = cond_b(); if (v1 != 0) body_b();
  shared(a1);          // copy 1            }
} else {                                    else {
  v1 = cond_a(); if (v1 != 0) body_a();       v1 = cond_a(); if (v1 != 0) body_a();
  shared(a1);          // copy 2 (DUP)      }
}                                           shared(a1);          // merged: one copy
post(a1);                                   post(a1);

shared(a1): 2 copies -> 1; one fewer cloned call.

Scope note (the original target)

The proposal's target true_ubuntu1804::usage has its duplication
sub-block-entangled — the online-help __printf_chk shares a basic block with
the setlocale condition test, so the shared code is the condition block of an if,
not a hoistable leaf. Fully matching angr there needs the deeper label-convergence
transform (angr threads LABEL_4018e8/LABEL_401822), which is genuinely LARGE and
beyond a sound bounded leaf hoist. This PR ships the sound, general subset (prefix +
suffix leaf hoisting) the proposal's plan step 1 sanctions, with the stage test on a
minimised x86-64 reproducer — exactly the who.o/morton SAILR-family convention.

Registration / tests

Registered everywhere: stages.toml (settable -> 64), options.rs
KUNA_OPTION_NAMES, architecture flag/ctor/reset/parse/seam-copy, seams.rs,
universalaction (after crossjumprevert). All count fixtures bumped
(catalog_bytecompat 64, kuna_stages settable_count_is_64, verify_w8 265
leaves + order, stage_catalog.json regenerated, list_action oracle, corpus count
154, kuna-catalog.xml #5/#6/#7/#9). docs/assertions.md regenerated;
kuna catalog --check OK.

Two-pass stage test
tests/stages/ghangr-true-1804-dedup-ite-tail-8a690c.xml: off clones the epilogue
into both arms (2), on merges to 1 and logs the dedup.

Gates

  • make test — PARITY OK 675/675 (byte-identical; the option is default-OFF).
  • make test-stages — PARITY OK 233/233 (incl. the 3 new assertions).
  • make rust-test — green (count fixtures updated).
  • Speed: no measurable change (~0.20s on usage, off vs on) — the pass is
    O(structured-nodes x bounded-leaf-eq).

docs/baseline.json is untouched.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac

mahaloz and others added 2 commits June 26, 2026 22:37
New S8 structuring pass `ActionDedupIteTail` (option `dedupitetail`, ElementId
4109, default-OFF) — the inverse of the SAILR duplication passes
(gotoreduce/crossjumprevert/taildup). kuna inherits Ghidra's collapse-based
structurer, which clones a basic block reachable from both arms of an if/else
into each arm instead of converging the two predecessors onto one copy. This
pass detects when both arms of a 3-component BlockIf share a maximal common
PREFIX or SUFFIX of leaf blocks that emit identical C and hoists that shared run
out of the if (prefix before, suffix after), emitting one copy instead of two.

Equivalence is exact-or-decline: same printed-op sequence, constants matched by
value, named storage by location, call targets by resolved callee (op-wise via
get_call_specs_index), block-local unique temps paired positionally; STORE and
unresolvable operands decline. The transform is a pure print-tree edit (no p-code
cloned, SSA/def-use untouched): a prefix hoist is unconditionally sound (both
arms run it first); a suffix hoist is gated on both arms' divergent middles
falling through to it. Bounded (<=8 leaves, <=64 ops/leaf) and only flat
unlabelled leaves are merged. Surgery helpers kuna_hoist_ite_{prefix,suffix} in
block.rs, with unwrap_singleton_clause to avoid the degenerate 1-element BlockList
the PrintC emitBlockLs path double-emits.

Registered everywhere: stages.toml (settable -> 64), options.rs
KUNA_OPTION_NAMES, architecture flag/ctor/reset/parse/seam-copy, seams.rs,
universalaction (after crossjumprevert). Catalog/count fixtures bumped
(catalog_bytecompat 64, kuna_stages settable_count_is_64, verify_w8 265 leaves +
order, stage_catalog.json regenerated, list_action oracle, kuna-catalog.xml
#5/#6/#7/#9). docs/assertions.md regenerated; `kuna catalog --check` OK.

Two-pass stage test tests/stages/ghangr-true-1804-dedup-ite-tail-8a690c.xml
(minimised x86-64 reproducer, the who.o/morton convention): off clones the shared
epilogue into both arms (2 copies), on merges to 1 and logs the dedup.

Gates: `make test` PARITY OK 675/675 (byte-identical, default-OFF);
`make test-stages` PARITY OK 233/233. Speed: no measurable change (~0.20s on
usage off vs on). NOTE: the target true_1804::usage tail is sub-block-entangled
(the online-help printf shares a bblock with the setlocale condition test), so it
needs the full label-convergence transform and is out of scope for this bounded
leaf hoist — the pass is sound/general and the stage test uses the reproducer.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
The documentary count comment lagged the actual table (40 stage-model
knobs after dedupitetail; settable=64). No asserted value changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz merged commit e3b9279 into main Jun 26, 2026
mahaloz added a commit that referenced this pull request Jun 26, 2026
Union both opt-in settables atop main: dedupitetail (4109, #104) +
unrolledguard (no ElementId, #105). Resolved by UNION (no registration
dropped):
- stages.toml: 65 settables, 98 surfaces; header counts updated
- kuna_stages/tests.rs: settable_count_is_65, surface_count_is_98, 64
  trailing commas in the static catalog
- catalog_bytecompat.rs: 65 "option" rows
- xml.rs corpus count: 155 (83 datatests + 72 stage testcases)
- kuna-catalog.xml provenance: #6 source_decompiler angr=24, #7
  structure-recovery=13 (+dedupitetail), #8 opt-in-tool=12 (+unrolledguard),
  #5/#9 use_when/change_kind max bumped 65->66 (65 settables + returnpair dup)
- baseline-stages.json + assertions.md regenerated (235/235)

Gates: catalog --check OK; make test PARITY OK 675/675; make test-stages
PARITY OK 235/235; make rust-test green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz deleted the feat/dedup-ite-tail 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