Skip to content

HighByteZero trusts only the bytes the selection keeps clean - #98

Merged
skerkewitz-agent[bot] merged 1 commit into
developfrom
fix-seeds-20787
Nov 30, 2026
Merged

skerkewitz-agent[bot] merged 1 commit into
developfrom
fix-seeds-20787

Conversation

@skerkewitz-agent

Copy link
Copy Markdown
Contributor

What

Fixes the miscompile behind today's two failing random loop programs (seeds 20787012 and 20787028). They block CI for every pull request today, #97 included.

HighByteZero (from #90, eight-bit stretches) takes 16-bit code to 8 bits where the high byte of A (B) is known to be 0. To know that, it assumed every byte's place in memory holds a high byte of 0. But the selection masks a byte only where one of its readers needs a clean high byte (Selection.maskResult). A byte that only byte-wise code reads keeps whatever high byte its code left.

In the failing programs that byte was a[c] >> 15, truncated to a byte: $FFFF in its place for a negative a[c]. A 16-bit EOR with it was taken to leave B at 0. The loop counter's c += 2 right after went to 8 bits, was stored in 16 bits with that high byte, and the loop then indexed and ended wrong.

How

MirCodeGen.zeroExtended becomes per function. It now takes a byte's place as clean only for:

  • truth values, which are always masked;
  • byte parameters, which are clean by convention;
  • the bytes in Selection.clean: masked, or clean by themselves where a reader needs them clean.

The selection already relies on that set being right. It is rebuilt for each function during selection, while the LIR passes run after all functions are selected, so a copy is kept per function (cleanBytes).

Seed 20787012 is the same bug, although the pass bisect named Widths. When Widths changes a function, MirCodeGen also finishes the version without it, to compare worst cases, and that finish runs with no pass limit (PassLimit.none()). HighByteZero therefore ran unlimited there, and dev/bisect_passes.sh can't see it. I confirmed it by switching HighByteZero off on develop: seed 20787012 then passes.

Verified

  • ./gradlew build: 1,275 tests, 0 failed, 13 skipped, today's random programs included.
  • The new exec/regress_byte_high_byte.c is seed 20787028 cut down to 20 lines. It fails on develop and passes with the fix.
  • dev/bench_examples.sh, develop against this branch: geometric mean 1.000. Mode 7: 10,401 to 10,418 master cycles of work per tick (+0.2 %). Schach: 359,956 to 360,013 (+0.02 %). The others are unchanged within a few cycles.

What follows

  • Generated code: backend-output.txt changes for every example. In cost-baseline.txt one static worst case moves: Schach's _unmake_move, 3,114 to 3,140 master cycles.
  • The game: its code could have been hit wherever a byte that only byte-wise code reads sits next to 8-bit stretches, since 8-bit stretches across blocks, and the high byte of A kept at zero #90. Rebuilding with the fixed dampf is enough; no entry in docs/game-findings.md.
  • The C guide, StyleGuideTest, documents, the book: unchanged.

Open

  • The pass bisect can't see changes made under PassLimit.none(). That's worth a look on its own.

🤖 Generated with Claude Code

HighByteZero (#90) took every byte's place as zero-extended to 16 bits. The
selection masks a byte only where a reader needs it clean
(Selection.maskResult): one that only byte-wise code reads keeps whatever
high byte its code left, such as a 16-bit shift's truncation ($FFFF for
a >> 15 of a negative a). A 16-bit EOR with such a place was then believed
to leave B at 0, and a byte counter's increment after it went to 8 bits and
was stored with that high byte.

The predicate is now per function: truth values, byte parameters, and the
bytes in Selection.clean (masked, or clean by themselves where a reader
needs it), kept per function since the LIR passes run after the selection
of all of them. Found by the random loop programs, seeds 20787012 and
20787028; exec/regress_byte_high_byte.c is the program cut down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skerkewitz-agent
skerkewitz-agent Bot merged commit b2f5d89 into develop Nov 30, 2026
1 check passed
@skerkewitz-agent
skerkewitz-agent Bot deleted the fix-seeds-20787 branch November 30, 2026 13:08
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