Skip to content

[AUTOMATED] Print a compiler-merged range test as its two comparisons (option rangetest) - #1979

Merged
mahaloz merged 7 commits into
mainfrom
feat/rangetest
Nov 26, 2026
Merged

mahaloz merged 7 commits into
mainfrom
feat/rangetest

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Optimizing compilers fold n < 1 || n > 30 and c >= '0' && c <= '9' into one unsigned comparison of a subtraction, and kuna printed that merged form. This prints the two comparisons the test stands for, where they are exact:

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/rangetest_gcc_O2_x86_64 reject --option rangetest merged
void reject(int a0)
{
  if (0x1e <= (unsigned int)(a0 - 1U))
    knob = a0;
}
$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/rangetest_gcc_O2_x86_64 reject
void reject(int a0)
{
  if (a0 < 1 || 0x1e < a0)
    knob = a0;
}
$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/rangetest_gcc_O2_x86_64 isdig
bool isdig(int a0)
{
  return 0x30 <= a0 && a0 <= 0x39;
}

(the fixture is gcc -O2 -fno-inline of rangetest.c beside it.)

  • New option rangetest merged|split|auto, default auto, in a new P9 module p9_emit/kuna_rangetest.rs. merged is the old output.
  • The split happens only where it means the same as the merged test for every input: the range does not wrap, and x prints in an unsigned type of the test's width or the upper bound is below that width's sign bit (casts and extensions that keep the range one interval are looked through). x prints twice, so it must be a variable or a side-effect-free in-place expression. Anything else keeps the merged form, as does the bound check before a recovered switch (ARM tbb/tbh prints one).
  • auto keeps the merged form in loop conditions and in stand-alone if conditions whose range holds at most 16 values, which are more often a switch case list or an isdigit-style macro in the source. Returned or assigned tests and &&/|| operands split at any range size.
  • A split || heading an && chain that is itself the second clause of another chain prints in its own parentheses, since that clause gets none.

On the decbench corpus 50 more optimized functions (54 gained, 4 lost) and 12 more unoptimized ones (16 gained, 4 lost) match the source's control flow exactly; most losses are switch case lists or an RTA_PAYLOAD(x) < n macro whose subtraction the source wrote itself.

Tests: tests/stages/kuna-rangetest.xml (gcc -O2 x86-64, all three values: inside and outside ranges, a wrapping range, a loop condition, a ten-value test, the nested chain) and kuna-rangetest-thumb.xml (a clang tbb switch guard that stays merged). kuna-cli/tests/rangetest_round_trip.rs compiles the printed C of every value with gcc and clang at -O0 and -O2 and compares each function with its source at the range boundaries, 0 and the type extremes. No datatest expectation moved.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 7 commits November 25, 2026 22:45
…rd) and option counts

[AUTOMATED] kuna-rangetest.xml pins the split inside/outside/chain/returned
tests, and the wrapping range, the loop condition and the ten-value
stand-alone test that stay merged; kuna-rangetest-thumb.xml pins a tbb
switch's bound check that keeps its one comparison under every value.
Stages baseline re-recorded (5,489), settable count 245.

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

[AUTOMATED] The first clause of an &&/|| chain prints its branch with an
empty operator stack, and gets no parenthesis of its own when the chain is
the second (comma-separated) clause of another chain. A split
`x < A || C < x` there bound to the following `&&`
(openssh vasnmprintf/strnvis). emit_block_condition now names the chain's
operator while its first clause prints, and the split wraps itself when that
operator binds tighter. Fixture function ctl, round-trip inputs 5/10/11 and
stage assertions #13/#14 pin it (the old spelling returns 45 for ctl(9, 5),
the source 5).

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

[AUTOMATED] The phases.toml summary/use_when and the docs/spec/09-emission.md
prose now describe the shipped auto (loop conditions and stand-alone
conditions of at most 16 values stay merged; no unoptimized-code rule), the
chain-head parenthesis rule and the measured corpus effect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] xml.rs corpus count 930 (main 928 + kuna-rangetest,
kuna-rangetest-thumb); docs/baseline-stages.json synthesized with
mergebaseline.py (main 5,480 + 17 = 5,497).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] docs/options.md and tests/fixtures/phase_catalog.json from the
merged build (245 settables, rangetest row).

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