Skip to content

Two wrong-code fixes (index-half parameters, word +1 with flags observed) + eZ80 loop data movement, gap-example benchmarks - #380

Merged
oisee merged 15 commits into
mainfrom
perf/ez80-loop-regs
Dec 2, 2026
Merged

oisee merged 15 commits into
mainfrom
perf/ez80-loop-regs

Conversation

@oisee

@oisee oisee commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Two wrong-code fixes that affect every Z80-family target, then a first cut at data movement in eZ80 loops.

Wrong code fixed

  1. Index-half parameters arrived swapped. @z80_ixh / @z80_ixl / @z80_iyh / @z80_iyl all mapped to one class that any of the four halves satisfied. The callee's allocator could therefore put an @z80_ixl parameter in IXH, and inline asm reading IXL got the other argument. Z80, Z80N and eZ80 were all affected.
    • Fix: four hard-pin classes (ClassRegIXH/IXL/IYH/IYL), wired into parsing, asm aliases, clobber facts, the extern ABI and the eZ80 cost table.
    • Test: TestAsmIXYHalfParamsArriveInTheirHalf.
    • New scripts/fuzz_regargs.py: 4–6 annotated register arguments, index halves included, passed through a forwarding function. On main 150 of the first 200 seeds fail. Now 2000/2000 pass on z80, ez80 and z80n.
  2. Loop increment emitted as doubling. genBinOp skipped the operand swap for an in-place word +1, expecting the INC rr peephole. That peephole also requires the add's flags to be unobserved. When they were observed, the ADD path loaded i over the constant and emitted i = i + i.

eZ80 data movement

Each commit was measured; the large eZ80 benchmark program stays byte-exact.

  • A word compare leaves a live A to genCmp16's own save.
  • An i16 store through HL needs no HL save.
  • ptr_add of a constant 1..3 becomes INC rr, and the constant gets no home. This is eZ80 only: on Z80 it made a pinned profile-guided measurement obsolete.
  • ADL pair copies:
    • LEA y,IX/IY+0 takes 3 cycles instead of PUSH/POP's 9;
    • HL→IX/IY goes through DE when the upper byte is dead;
    • the same rewrites apply to a non-adjacent PUSH x … POP y when x is not written in between.
  • A Z80 miscompile caught by assert_matrix during this work is fixed: a dead-constant offset in the generic ptr_add path is now loaded as an immediate (TestPtrAddDeadOffsetConstantLoadedOnZ80).

Benchmarks

  • New TestEZ80GapExamples with six neutral reduced examples (testdata/ez80_gap_examples/). Each has a cycle budget, with the hand-written cycle count recorded beside it. They are currently 1.14–3.53× the hand versions.
  • eZ80 C benchmark geomean: 0.991 (default) / 0.992 (size). No case is slower; the best cases are fusion_two_ptr_xor 0.908 and fat12_decode 0.926.
  • Large eZ80 benchmark program, cycles per picture: natural source 2,728,746 → 2,654,688 (−2.7 %); restructured source 1,785,616 → 1,705,702 (−4.5 %).

Gates

  • Z80:
    • compile_sweep: 13 files change, all smaller, with no exit changes;
    • assert_matrix: 0 new failures;
    • C benchmark unchanged;
    • fast profile PASS;
    • fuzz: direct-call 0–19999 20000/20000, --signed 4000/4000, folded 3000/3000, byte-shifts 3000/3000.
  • Z80N:
    • benchmark products unchanged;
    • fuzz_diff --target z80n 1000 + 1000.
  • eZ80:
    • corpus identical to main (1071/1071);
    • fuzz_diff 2000 + 2000, fuzz_ptr 2000, fuzz_nowrap 2000, fuzz_regargs 2000;
    • Agon player byte-exact.

Left for later

  • Loop counters still land in A.
  • Loop-carried words sit in HL/DE under address arithmetic.
  • Words in IX/IY feeding ADD HL.
  • Exact clobber sets are lost in large programs.
  • Static spill slots in hot loops.

🤖 Generated with Claude Code

https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS

oisee and others added 14 commits December 1, 2026 18:54
The four index-half annotations all became ClassIXY8, a class any of
IXH/IXL/IYH/IYL satisfies. The callee's allocation was free to put an
@z80_ixl parameter in IXH, the caller followed that contract, and an
inline-asm body reading IXL got the other argument: with five register
arguments the call site emitted LD IXL,IXH / LD IXH,A and the two bytes
arrived swapped. The physical-alias form of an asm (in IXL, IXH) list
matched both names to the first ClassIXY8 parameter for the same reason.
Not a parallel-move problem: the copy was correct for the contract it
was given. Z80, Z80N and eZ80 were all affected.

Add hard-pin classes ClassRegIXH/IXL/IYH/IYL (appended after ClassFlag,
so existing class numbers and MIR dumps are unchanged), priced 0 only on
their half, and map the annotations and asm aliases to them.

scripts/fuzz_regargs.py: fuzz_diff only generates calls with up to three
u16 arguments and no annotations, so it cannot reach this. The new
generator builds 4-6-argument calls in annotated registers (index halves
included) through a forwarding function, so the parallel copy permutes
and breaks cycles; an asm leaf folds the bytes in order. On main 150 of
the first 200 seeds fail on Z80 and on eZ80; with the fix 2000/2000 pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
When A holds a live value and no byte register is free, genCmp saved A on
the stack before every comparison (PUSH BC / PUSH AF ... POP BC / LD A,B /
POP BC, 17 cycles on eZ80). For a word comparison of two pairs that save
is redundant: genCmp16's general SBC HL,rr path runs under
saveWrittenScratch, which already saves each live register its body
writes, and SBC HL,rr does not write A. Skip the outer save for exactly
that path (not for a constant-zero right operand, whose BIT/OR forms
write A outside the wrapper).

Measured (eZ80 cycles): gap-map example x1 11,502 -> 9,802; large program
v0 2,728,746 -> 2,726,577, v4 1,785,616 -> 1,784,815 (both byte-exact);
eZ80 and Z80 C benchmarks unchanged in both profiles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
saveWrittenScratch exempts the canonical word store LD (HL),lo / INC HL /
LD (HL),hi / DEC HL from saving a live HL, since the sequence ends with HL
unchanged. The exemption tested inst.Ty == TyU16, so the same sequence for
an i16 store pushed and popped the pointer around itself (8 cycles on
eZ80 per store through a loop-carried pointer). Take i16 too; the shape
check is unchanged.

Measured (eZ80 cycles): x1 9,802 -> 9,242; v4 1,784,815 -> 1,776,863,
v0 unchanged (both byte-exact); eZ80 and Z80 C benchmarks unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
`p = p.bytes(2)` lowers to ptr_add p, 2, not ptr_bump, and the generic
ptr_add form loaded the constant into a scratch pair and added it
(LD BC,2 / ADD HL,BC, with saves of a live scratch pair). When the result
and the base share a pair, emit INC rr per byte as OpPtrBump does: no
flags change, and in ADL INC rr carries into the upper byte like the
24-bit ADD it replaces (positive offsets only, so zero and sign extension
of the offset agree).

Measured (eZ80 cycles): x1 9,242 -> 8,942; v4 1,776,863 -> 1,772,628,
v0 unchanged (both byte-exact); eZ80 and Z80 C benchmarks unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
computeDeadConsts did not know that a ptr_add advancing a base in its own
pair by 1..3 is now INC rr, so the constant kept a home through the
allocator's trial. In x1 that home was BC, live exactly where the fraction
byte t needed the last free byte register, and t went to a static spill
slot stored and reloaded every iteration. Count that use as folded, with
the same conditions as tryPtrAddSmallConst (non-constant base, base and
result in the same pair).

Measured (eZ80 cycles): x1 8,942 -> 7,342 (no spill slot left in the
loop); v0, v4 unchanged (byte-exact); eZ80 and Z80 C benchmarks unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
Six reduced loop shapes where natural Nanz loses to hand code on the
eZ80 (register allocation in a leaf loop and around a call, loop bound /
index work, invariants and induction variables, inlining a multiply
helper, guard-range products). TestEZ80GapExamples compiles each with a
small harness for the bare eZ80 shape, runs it with poisoned registers,
checks the shown results and fails when the region between OUT ($40) and
OUT ($41) exceeds its budget (today's cycles; lower a budget when a change
improves it). The hand-code cycles are recorded next to each budget as
the target, in the style of TestEZ80ADLVertexBench.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
The ADL emitter copies one pair to another with PUSH x / POP y (all 24
bits, flags kept), and adlLocalRewrites turns that into byte loads when
y's upper byte is dead. Copies out of IX/IY into HL, the other index
register, or a pair whose upper byte is live stayed on the stack: PUSH IX
/ POP HL is 9 cycles, and the scene runs such copies on every hot path.

- PUSH IX|IY / POP y -> LEA y, IX|IY+0 (3 cycles, exact 24-bit copy, no
  flags). The byte copy is kept when one of y's halves is dead, since
  that half's load is deleted afterwards (2 cycles).
- PUSH HL / POP IX|IY with the index register's upper byte dead ->
  EX DE,HL / LD IXH,D / LD IXL,E / EX DE,HL (6 cycles, flags kept).
- The upper-byte fact of IX/IY travels through LEA rr,IX+0 as it did
  through the stack, so widenings it made redundant stay removed.

Measured (eZ80 cycles): v0 2,726,577 -> 2,667,858 (-2.2%), v4 1,772,628
-> 1,732,267 (-2.3%), both byte-exact; x1 7,342 -> 7,242, x2 10,769 ->
10,313, x3 10,577 -> 10,492, x4 19,242 -> 18,212, x5 4,710 -> 4,542, x6
4,516 -> 4,452; eZ80 C benchmark geomean 0.9913 (default) / 0.9923
(size), no case slower. ADL-only pass: Z80/Z80N output unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
…tween

The emitter's parallel copies sequence a cycle as PUSH x / moves / POP y
(e.g. PUSH IY / LD B,D / LD C,E / POP DE for BC <- DE, DE <- IY), and the
pair-copy rewrites only matched an adjacent PUSH/POP. When nothing
between writes x, x still holds the pushed value at the POP, and the slot
is the POP's alone (adlMatchingPop admits no SP-relative access, branch
or label in between), so the same copy forms apply there: LEA y,IX|IY+0,
the byte loads, or HL -> IX|IY through DE. The EX DE,HL form, which
changes the source, still needs the two to be adjacent.

Measured (eZ80 cycles): v4 1,732,267 -> 1,705,702 (-1.5%), v0 2,667,858
-> 2,654,688, both byte-exact; x2 10,313 -> 9,857, x3 10,492 -> 10,186,
other examples unchanged; eZ80 C benchmark unchanged in both profiles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
x1 7,342 -> 7,242, x2 10,769 -> 9,857, x3 10,577 -> 10,186, x4 19,242 ->
18,212, x5 4,710 -> 4,542, x6 4,516 -> 4,452 (the ADL pair-copy rewrites).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
a96461a lets the dead-constant inventory drop the home of a ptr_add
offset 1..3 when the allocation puts base and result in one pair, since
tryPtrAddSmallConst emits INC rr. The inventory reads the allocation; the
emitter tests its current homes, which a relocation can change, and then
falls to the generic form, which read the dropped home. On Z80 that
miscompiled two C corpus files (assert_matrix: dowhile_break.c
chain_length, fatfs_lowlevel.c test_ld_word / test_fat12_entry, 9
asserts). The generic form now loads such an offset with LD rr,n.

Examples and gap budgets unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
genBinOp swaps a word ADD's operands when the right one lives in HL,
because the ADD path loads the left one into HL first. It skipped the
swap for an in-place step of 1 (lhs and result in one pair), expecting
the INC rr peephole further down, but that peephole also requires the
add's flags to be unobserved (and a register result). When they are
observed the ADD path ran, loaded lhs over the constant and emitted
i = i + i (PUSH BC / POP HL / ADD HL,HL): a loop counting from 0 never
ended. Gate the skip on the same conditions as the peephole.

The defect is in the target-independent word ADD path (older than the
eZ80 work; not introduced by #376). The reduced example is correct up to
#367 (fbb7172) and wrong from #368 (b4a013e, loop- and call-aware
eZ80 allocation), whose allocation of the shape (a conditional call
before the loop, a callee with its own loop, an index base + i) first
puts the constant in HL with the flags observed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
run_stable_pipeline.py refuses unclassified tests. The eight new tests
(IX-half parameters, word compare / store / pointer-step / dead-offset
checks, the gap-map example budgets, the word unit-step fix) run in a
few seconds together.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
On the Z80 the INC rr form of a small constant pointer step (217b285,
a96461a) made the plain byte path of the FAT12 word workload faster
than its profile-guided word fusion (TestFATSequentialWordFusionNative:
unfused 92,928 -> 76,800 T against fused 89,088) and moved the pinned
deltas of TestFAT12BytePackingRetainsNativeAccessesAndWrap and
TestProfileGuidedSumVMZ80. Those are measurements of other passes, so
keep the Z80 product as it was: emit the form, and drop the offset's
home in the dead-constant inventory, only for 24-bit pairs (eZ80). The
eZ80 numbers are unchanged (gap budgets hold); the immediate fallback
of b50e4a8 stays for any disagreement between the two gates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
…Z80 output

The fresh CI run shows every stage boundary still equal to the original Ruby;
only the compiled MIX.COM bytes changed (hash taken from that run's evidence).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR tests: 🟢 pass

Commit: f56ca001 · Full test log

Check Result
Complete fresh Ruby/Crystal native pipeline 🟢 pass
Inherited compiler and complete frozen native gates 🟢 pass
Build mz, mza, mze, mzv 🟢 pass
Fast compiler regressions (deep suites run separately) 🟢 pass
Fibonacci compile + assemble 🟢 pass
Nanz language regressions 🟢 pass
Z3 West of House demo 🟢 pass
Z3 small conformance fixtures 🟢 pass

Deep pipeline domains and the full assertion corpus run on release tags, nightly and manual audits.
The external CZECH and full Zork I stories are not part of this PR gate.

…80 output

Regenerated with freeze.py from this PR's fresh CI run. Only compiled-image
hashes, image sizes and T-state counts change; every case result is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS
@oisee
oisee merged commit 9d5ae4d into main Dec 2, 2026
12 of 13 checks passed
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