fix(#740): Thumb-2 B<cond>.W (T3) halved the branch offset — wide conditional branches landed mid-shape - #748
Merged
Merged
Conversation
… 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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 incrates/synth-backend/src/arm_encoder.rs, which packedhalfword_offset >> 1intoimm6:imm11. Per ARMv7-M the branch byte offset isSignExtend(S:J2:J1:imm6:imm11:'0')— the 20-bit fieldS:J2:J1:imm6:imm11is 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 firsttransitioncall instead of at the block-@2 join, producing the spuriousstate+0x34write plus spurioustransition/mixcalls 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 otherbr_if @2in 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
scripts/repro/brif_outer_740_differential.py+brif_outer_740.wat: minimal loop-inside-block shape — loop-headbr_ifto the outer block end + a two-levelbr_ifexit, padded past the 254-byte span so both encode wide; non-tailreturnkeeps it on the DIRECT path (optimized declines, Optimized path: br_if to a forward block end still unresolved for if/else and sequential-block shapes (#483 class, broader) #500 — same path as gust_poll). Return value and a linear-memory window vs wasmtime, 6 cases. RED pre-fix (5/6 FAIL), GREEN with the fix (6/6). Anti-vacuous: hard-fails ifpollever stops containing a 32-bit T3 conditional branch. CI-wired into the emulation-differential job.gust_spill_fwd_390_differential.pyfired (FIXD) the moment the divergence disappeared, as designed; the two zero-state rows are promoted to strict — return AND full state window match wasmtime in all three lever configs (default,SYNTH_NO_STACK_FWD=1,SYNTH_SPILL_REALLOC=0). Full harness PASS.test_encode_thumb_bcond_wide_t3_halfword_offset_740: forward + backward wide offsets, narrow encoding byte-identical, out-of-range → loudErr.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