Skip to content

test(encoder): pin AND-immediate byte-range encoding + document fold bound (VCR-RA-001)#249

Closed
avrabe wants to merge 1 commit into
mainfrom
test/encoder-and-imm-fold-bound
Closed

test(encoder): pin AND-immediate byte-range encoding + document fold bound (VCR-RA-001)#249
avrabe wants to merge 1 commit into
mainfrom
test/encoder-and-imm-fold-bound

Conversation

@avrabe

@avrabe avrabe commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Investigating immediate folding (the biggest win per #248's evidence) surfaced an encoder limitation.

Finding

The And { Operand2::Imm } encoder (arm_encoder.rs:1886) packs the low 12 bits straight into the i:imm3:imm8 field without applying ThumbExpandImm (modified-immediate expansion):

  • imm <= 0xFF (gale's clamps #0x7e/#0x7f): correctand r2,r0,#0x7e00 f0 7e 02 (pinned by this test).
  • imm >= 0x100: needs a rotation/replication pattern that isn't implemented → would silently encode a different value.

This path is currently dead (the selector never emits And-imm), so there's no live bug — but it sets the precondition for the immediate-folding transform: fold only imm <= 0xFF until the encoder is hardened to ThumbExpandImm / Ok-or-Err (the "encoder must be Ok-or-Err, never silently wrong" principle, #180/#185). That bound covers the measured flat_flight waste.

Pins the safe-range encoding as a regression guard. No codegen change.

Part of #242.

🤖 Generated with Claude Code

…old bound (VCR-RA-001)

Investigating immediate folding (the biggest win the #248 evidence pointed to)
surfaced an encoder limitation: the `And { Operand2::Imm }` path packs the low
12 bits straight into the `i:imm3:imm8` field WITHOUT ThumbExpandImm (the
modified-immediate expansion). For imm <= 0xFF (gale's int8 clamps #0x7e/#0x7f)
that is correct — `and r2,r0,#0x7e` encodes to the canonical `00 f0 7e 02`. For
imm >= 0x100 the field needs a true rotation/replication pattern that is not
implemented, so it would silently encode a different value.

This path is currently DEAD (the selector never emits And-imm), so no live bug —
but it sets the precondition for the immediate-folding transform: **fold only
imm <= 0xFF** until the encoder is hardened to ThumbExpandImm / Ok-or-Err (the
"encoder must be Ok-or-Err, never silently wrong" principle, #180/#185). That
bound covers the measured flat_flight waste.

Pins the safe-range encoding as a regression guard; no codegen change.

Part of #242.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@avrabe

avrabe commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Subsumed into #250 — the encoder-bound test is included there alongside the immediate-folding transform that uses it.

@avrabe avrabe closed this Jun 4, 2026
@avrabe avrabe deleted the test/encoder-and-imm-fold-bound branch June 4, 2026 20:27
avrabe added a commit that referenced this pull request Jun 4, 2026
…r bound (VCR-RA-001) (#250)

* test(encoder): pin AND-immediate byte-range encoding + document the fold bound (VCR-RA-001)

Investigating immediate folding (the biggest win the #248 evidence pointed to)
surfaced an encoder limitation: the `And { Operand2::Imm }` path packs the low
12 bits straight into the `i:imm3:imm8` field WITHOUT ThumbExpandImm (the
modified-immediate expansion). For imm <= 0xFF (gale's int8 clamps #0x7e/#0x7f)
that is correct — `and r2,r0,#0x7e` encodes to the canonical `00 f0 7e 02`. For
imm >= 0x100 the field needs a true rotation/replication pattern that is not
implemented, so it would silently encode a different value.

This path is currently DEAD (the selector never emits And-imm), so no live bug —
but it sets the precondition for the immediate-folding transform: **fold only
imm <= 0xFF** until the encoder is hardened to ThumbExpandImm / Ok-or-Err (the
"encoder must be Ok-or-Err, never silently wrong" principle, #180/#185). That
bound covers the measured flat_flight waste.

Pins the safe-range encoding as a regression guard; no codegen change.

Part of #242.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(selector): fold small constants into i32.and immediates (VCR-RA-001)

The FIRST delta-emitting codegen transform on the allocator track. Evidence
(#248) showed the dominant flat_flight-shape waste is redundant const
materialization: `i32.const C; i32.and` lowered to `movw rN,#C; and rD,rA,rN`
even when C is a small constant the AND instruction can take as an immediate.

Fix: when the operand pushed immediately before `i32.and` is `i32.const C` with
C in 0..=0xFF AND its `movw` is cleanly at the instruction tail (not spilled),
fold to `and rD, rA, #C` and drop the materialization (foldable_bitwise_imm +
drop_prev_const_materialization, mirroring the const-divisor pattern).

Bounded to 0..=0xFF: the encoder's AND-immediate path is not yet ThumbExpandImm-
complete (#249) — larger modified immediates await an encoder hardening to
Ok-or-Err. 0..=0xFF covers gale's int8 clamps.

Measured delta: the (p & 0x7e) + (p & 0x7e) pattern drops 8 → 6 instructions
(both `movw #126` eliminated; each AND uses the immediate). Better than const-CSE
— no materialization at all.

GATE (full, codegen change): 282 lib tests + 20 integration suites pass; the
three frozen differential fixtures stay RESULT-identical (control_step
0x00210A55, flight_seam 0x07FDF307, div_const 338/338); tests
i32_and_folds_small_const_into_immediate (folded shape) +
i32_and_does_not_fold_out_of_range_const (0x140 stays a register operand — the
encoder safety bound). CI fuzz adds totality.

Part of #242. Supersedes the #248 evidence (the redundancy it pinned is now folded away).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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