Skip to content

The value of a signed comparison with a constant reads C again, not N - #94

Merged
skerkewitz merged 1 commit into
developfrom
fix-seed-20786007
Nov 29, 2026
Merged

skerkewitz merged 1 commit into
developfrom
fix-seed-20786007

Conversation

@skerkewitz-agent

@skerkewitz-agent skerkewitz-agent Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes a miscompile that today's random loop program (seed 20786007) found on develop. The truth value of a signed 16-bit comparison against a constant, stored as a value, could come out wrong whenever the value ranges rule out an overflow:

  • x < 0 and x >= 0 gave 1 for every 16-bit x (a comparison with 0 never overflows);
  • ((uint16_t)a - (uint8_t)b) <= 232 of two bytes was computed as the unsigned a - b >= 233, so every positive difference gave 0.

Comparisons used as branches (if, loop conditions) were right. It came in with #89 (signed comparisons by one subtraction). exec/enums.c had the bug and passed by luck: there dir - 1 < 0 always gave 1, and the test makes dir - 1 negative.

The tests for the gap that let it through (exec/signed_compare.c with comparisons against constants as values, exec/enums.c with both outcomes) were pushed after this pull request was merged, and are in #96.

How

Since #89, BranchSelection.orderedFlags decides a signed i16 comparison by N after CMP when the value ranges rule out an overflow, and that includes comparisons against a constant. The value form in cmp meant to leave comparisons with a constant to the biased form, but it went through orderedFlags and then read C. Now it calls the biased form, split out as carryFlags, directly. Branches still use orderedFlags and N, which was right all along.

The pass bisect (dev/bisect_passes.sh) named Narrow, because that pass gives the difference the value range that enables the subtraction form. The optimized MIR was right; only the code selected from it was wrong.

Verified

  • ./gradlew build: 1,272 tests, 0 failed, 13 skipped. That includes the random programs with today's seed, so 20786007 is among them, and ExamplesTest for every example.
  • The new exec/regress_signed_compare_value.c (<=, <, >=, > of a byte difference against constants) fails without the fix and passes with it. The x < 0 and x >= 0 cases were found with the tests of Comparisons with constants as values in signed_compare.c, both outcomes in enums.c #96.
  • RandomProgramTest with -PrandomSeed=20786007 -Prandom=1 passes.

What follows

  • Generated code: in backend-output.txt only exec/enums.c changed (+30 master cycles on the line that was wrong) and the new test was added. Every example's output is byte-identical, so the benchmark can't move and I didn't rerun it. cost-baseline.txt is unchanged.
  • The game: no entry in docs/game-findings.md. A comparison of this form stored as a value (x < 0 is the likely one) would have given a wrong value since Signed comparisons by one subtraction, and the absolute difference as one instruction #89. Rebuilding with the fixed dampf is enough, and checking that is the game project's job.
  • C guide, StyleGuideTest, documents, book: unchanged.

Open

🤖 Generated with Claude Code

Since the signed comparisons by subtraction (#89), orderedFlags decides a
comparison of an i16 value with a constant by N after CMP where the value
ranges rule out an overflow. The value form in BranchSelection.cmp left
comparisons with a constant to the biased form, but called orderedFlags and
read C after it: (a - b) <= 232 of two bytes became the unsigned
a - b >= 233, and in exec/enums.c, dir - 1 < 0 was always 1. The value form
now calls the biased form (carryFlags) directly; branches keep N.

Found by the random loop programs (seed 20786007), and
exec/regress_signed_compare_value.c covers the four predicates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skerkewitz
skerkewitz merged commit 60269e7 into develop Nov 29, 2026
1 check passed
@skerkewitz-agent

Copy link
Copy Markdown
Contributor Author

🤖 Tests for the gap that let the bug through

Problem: the existing tests should have caught this. exec/signed_compare.c, added with #89, took comparisons as values only between two variables, and against constants only as branches. exec/enums.c had the broken case, but on one input whose right answer (1) was what the broken code always gave. Found by asking why no test caught it.
Fix: signed_compare.c now also takes all four relations as values against constants, of any 16-bit value and of a difference of bytes (no overflow possible). enums.c runs each comparison on an input that makes it false as well. Without the fix, the new values in signed_compare.c fail, and that showed the bug was wider: x < 0 and x >= 0 as values gave 1 for every 16-bit x. The description now says so.
Commits: 48169e3 Comparisons with constants as values in signed_compare.c, both outcomes in enums.c
Verified: both tests fail without the fix and pass with it; ./gradlew build: 1,272 tests, 0 failed, 13 skipped; backend-output.txt changes only for the two tests.

@skerkewitz-agent

Copy link
Copy Markdown
Contributor Author

🤖 The test commit came after the merge

Problem: 48169e3, announced in the comment above, reached the branch after this pull request had been merged, so it is not in develop.
Fix: the same commit is in #96, on a branch of its own from develop. The description here now says what was merged, and points to #96.
Verified: ./gradlew build on develop with the commit: 1,272 tests, 0 failed, 13 skipped.

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