Skip to content

fix(tail-dup): copy only a straight-line tail to more than one place - #902

Open
sjkim1127 wants to merge 1 commit into
mainfrom
fix/tail-dup-straight-line-tails
Open

sjkim1127 wants to merge 1 commit into
mainfrom
fix/tail-dup-straight-line-tails

Conversation

@sjkim1127

Copy link
Copy Markdown
Contributor

What

tail_dup copies a function's shared tail into each place that reaches it. It admitted any tail of at most six statements, including one that contains an if or a loop, so a decision the program makes once was printed at every site:

  • BSD RB_REMOVE (openssh, 16 rows): if (!color) REMOVE_COLOR(); return old; four times. The source has it once, under color:.
  • bash ifs_firstchar: if (lenp) *lenp = len; return ret; at the end of both arms. The source has it once, after the if/else.
  • libopencm3 sdram_command (-O2): a do { } while (busy) wait, a store and the return eight times. The source has it once, after the switch.

A reader who meets the same if four times has to compare the copies to learn they are one piece of code. A compiler merges a function's return; it does not merge four separately written ifs. angr's ReturnDuplicatorHigh, which the module cites as its reference, duplicates only a return-only block.

Change

Both admission points (the gotoless split and the goto-driven clone) require the region to be straight-line when it would end up in more than one place. A single-reference region that is moved, not copied, is exempt.

One branch does not count: the stack-protector check. It is the compiler's, it precedes every return of a protected function, and the last cleanup pass removes it. stack_protector_pass_arm recognises it in the two-armed shape it has when tail_dup runs. Without this exemption the same rule loses 25-27 perfects per config, all of them canary epilogues (measured; table in the proposal).

Measurement

Full local DecBench configs against main (1c10edab0), same rows, caches off:

-O0 main -O0 this PR -O2 main -O2 this PR
total structure distance 394,148 392,704 621,358 619,683
structure perfect 12,976 12,994 7,378 7,379
Union / type / byte perfect 15,131 / 3,383 / 780 15,146 / 3,383 / 780 7,827 / 624 / 500 7,828 / 624 / 500
rows better / worse 112 (-1,796) / 40 (+352) 178 (-1,946) / 27 (+271)
new / lost perfect 18 / 0 1 / 0
  • Recompile-and-compare bytes, which does not see CFG shape: total byte distance 2,162,792 -> 2,160,155 (-O0) and 1,404,115 -> 1,404,001 (-O2); compilable count unchanged.
  • Sample-set gate (the scored 233 rows): perfect 67 -> 67, distance 7,603 -> 7,611. One row moves (gnutls print_list, 105 -> 113). On the scored rows this is a small loss; the gain is on rows the benchmark does not score.
  • What the score does not show, in the changed functions (166 at -O0, 228 at -O2): lines 39,192 -> 37,701 and 32,727 -> 31,052; gotos 1,429 -> 1,695 and 1,732 -> 2,063. Each added goto replaces a copy of a tail that decides something.

Rows that got worse

Read, not just counted. Four kinds:

  1. The source reaches the tail by goto and the output now does too, but the distance rises: sshauthopt_merge (+24; the output now has the source's alloc_fail: / fail: pair), get_token, globextend.
  2. The source falls into the tail from an if the structurer did not recover, so the single copy sits behind a goto: f_open, echo::main, realpath::main.
  3. The source repeats the tail through a macro (BZ_SETERR in BZ2_bzWrite, +52). The old copies matched it.
  4. The canary check in a shape neither this PR nor the stack-protector pass recognises, if (!diff) { return v; } __stack_chk_fail();, is printed once behind a goto instead of at every return (zlib inflate, start_header, prime2_p, print_list). At -O2 the same shape improves 47 rows and worsens 12.

Not in this PR

Checks

  • cargo nextest run -p fission-midend-structuring -p fission-pcode, debug and --release: pass.
  • Eleven new tests; each was checked by breaking the one line it guards and confirming that it, and only the intended tests, fail (table in the proposal).
  • cargo clippy --workspace --locked -- -D warnings -A clippy::style -A clippy::complexity, cargo fmt --all --check, scripts/check/owner_boundaries.sh: pass.

Proposal: docs/proposals/2026-11-16-tail-dup-straight-line-tails.md

🤖 Generated with Claude Code

tail_dup admitted any shared tail of at most six statements, including one
that contains an `if` or a loop, so a decision the program makes once was
printed at every place that reaches it: `if (!color) REMOVE_COLOR(); return`
four times in every BSD `RB_REMOVE`, a `do { } while (busy)` wait eight times
in libopencm3's `sdram_command`. angr's ReturnDuplicatorHigh, which the module
cites, duplicates only a return-only block.

Both admission points -- the gotoless split and the goto-driven clone -- now
require the region to be straight-line when it would end up in more than one
place. A single-reference region that is moved rather than copied is exempt.

The stack-protector check is the one branch that does not count: it is the
compiler's, it precedes every return of a protected function, and the last
cleanup pass removes it. `stack_protector_pass_arm` recognises it in the
two-armed shape it has when tail_dup runs; the existing predicate matches
only the settled one-armed guard. Without this exemption the same rule loses
25-27 perfects per config, all of them canary epilogues.

Measured on the full local DecBench configs against main, same rows:
unoptimized distance 394,148 -> 392,704, perfect 12,976 -> 12,994 (18 new,
0 lost); -O2 621,358 -> 619,683, perfect 7,378 -> 7,379 (1 new, 0 lost).
Sample-set gate: perfect 67 -> 67, distance 7,603 -> 7,611 (one row).
In the changed functions gotos rise (1,429 -> 1,695 and 1,732 -> 2,063) and
lines fall (39,192 -> 37,701 and 32,727 -> 31,052).

Proposal: docs/proposals/2026-11-16-tail-dup-straight-line-tails.md

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 17:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

This branch has not been deployed

No deployments
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.

2 participants