Skip to content

fix(#740): Thumb-2 B<cond>.W (T3) halved the branch offset — wide conditional branches landed mid-shape - #748

Merged
avrabe merged 3 commits into
mainfrom
fix/740-brif-outer-block
Jul 15, 2026
Merged

fix(#740): Thumb-2 B<cond>.W (T3) halved the branch offset — wide conditional branches landed mid-shape#748
avrabe merged 3 commits into
mainfrom
fix/740-brif-outer-block

Conversation

@avrabe

@avrabe avrabe commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Closes #740. Part of the #242 Track-C validation lane (v0.43.0 hub, Lane E).

Root cause

The encoder, not the selector. The direct selector's label/branch resolution computes the halfword displacement correctly for the loop-inside-block shape; the bug was in the Thumb-2 encoder's 32-bit B<cond>.W (encoding T3) arm in crates/synth-backend/src/arm_encoder.rs, which packed halfword_offset >> 1 into imm6:imm11. Per ARMv7-M the branch byte offset is SignExtend(S:J2:J1:imm6:imm11:'0') — the 20-bit field S:J2:J1:imm6:imm11 is the signed halfword offset — so every conditional branch spanning > 254 bytes jumped to half its intended displacement.

On gust_poll (#740) the loop-head empty-budget br_if 1 (;@2;) was the only wide conditional in the function: it landed after the first transition call instead of at the block-@2 join, producing the spurious state+0x34 write plus spurious transition/mix calls on a round that must not touch memory. Narrow (16-bit) B<cond> encodings were unaffected — which is exactly why the short-range CF differentials (#483/#497/#500/#508/#509) never caught it, and why every other br_if @2 in gust_poll (all narrow) landed correctly.

Fix

Pack the halfword offset directly into S:J2:J1:imm6:imm11 (mirroring the correct T4 unconditional arm), and loud-decline offsets outside the signed 20-bit T3 range instead of emitting a truncated branch. Bytes cross-checked against the llvm disassembler (bne.w #0x224 = f040 8112).

Red → green

Frozen status

Frozen anchors 10/10 bit-identical (frozen_codegen_bytes) — the fixtures contain no wide conditional branches, so no refreeze. All existing CF differentials green: block_brif_483, br_table_507, br_table_value_509, cf_shapes_500, control_step (13/13).

cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test -p synth-cli -p synth-synthesis (61/61 targets) all green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YJK5LZZEkV5smCY1jKn18L

avrabe and others added 3 commits July 15, 2026 12:20
… here)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…itional branches landed mid-shape

Root cause (NOT the direct selector's label resolution, which computes the
halfword displacement correctly): the Thumb-2 encoder's 32-bit B<cond>.W
(encoding T3) arm packed `halfword_offset >> 1` into imm6:imm11. Per ARMv7-M
the byte offset is SignExtend(S:J2:J1:imm6:imm11:'0') — the 20-bit field
S:J2:J1:imm6:imm11 IS the signed halfword offset — so every conditional
branch spanning > 254 bytes jumped to HALF its intended displacement.
gust_poll's loop-head empty-budget `br_if 1 (;@2;)` (the only wide
conditional in the function) landed after the first transition call:
spurious state+0x34 write + spurious calls on a round that must not touch
memory. Narrow (16-bit) B<cond> was unaffected, which is why all
short-range CF differentials (#483/#497/#500/#508/#509) stayed green.

Fix packs the halfword offset directly (mirroring the T4 unconditional arm)
and loud-declines offsets outside the signed 20-bit T3 range instead of
truncating. Bytes cross-checked against llvm (`bne.w #0x224` = f040 8112).

Evidence:
- scripts/repro/brif_outer_740_differential.py — minimal loop-inside-block
  shape with wide exits on the direct path, RED pre-fix (5/6), GREEN now;
  anti-vacuous (fails if `poll` stops containing a T3 conditional); CI-wired.
- gust_spill_fwd_390_differential.py zero-state rows: the anti-vacuous #740
  xfail fired (FIXD) and is PROMOTED to strict — return AND state window
  now match wasmtime in all three lever configs.
- encoder unit test test_encode_thumb_bcond_wide_t3_halfword_offset_740
  (forward/backward wide, narrow byte-identical, out-of-range loud Err).
- Frozen anchors 10/10 bit-identical (no wide conditionals in fixtures);
  block_brif_483 / br_table_507 / br_table_value_509 / cf_shapes_500 /
  control_step differentials all PASS.

Closes #740. Part of the #242 Track-C validation lane.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YJK5LZZEkV5smCY1jKn18L
@avrabe
avrabe merged commit bc3a2c5 into main Jul 15, 2026
36 checks passed
@avrabe
avrabe deleted the fix/740-brif-outer-block branch July 15, 2026 10:42
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

direct path: br_if exiting an outer block from inside a loop lands mid-shape — gust_poll empty-budget round mutates state it must never touch

1 participant