fix: v_bfe_i32 reads copies of the sign bit when the field runs past bit 31 #271

Manually merged
github merged 1 commit from gh-pr/271/head into gh-pr/271/base 2026-11-26 05:41:23 +00:00
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:55 UTC · original
lcg007xd-c:fix/rdna2-bfe-i32-sign → main · +72 −4 · 3 files · 1 commits · merged 2026-11-26 00:15 UTC

Change

v_bfe_i32 (interpreter _bitfield and HLSL BfeI32) shifted S0 as an unsigned value before masking and sign-extending the field. When the field runs past bit 31 (offset + width > 32), the bits above bit 31 came in as zeros instead of copies of the sign bit:

S0 offset width main (e921268) ISA
939108F5 16 31 00009391 FFFF9391
D992A382 31 31 00000001 FFFFFFFF
80000000 28 8 00000008 FFFFFFF8
EC3B1D4F 16 24 0000EC3B FFFFEC3B

The ISA pseudo-code shifts the signed S0: V_BFE_I32, D.i = (S0.i >> S1.u[4:0]) & ((1 << S2.u[4:0]) - 1) (section 12.12, p. 169), and the field is then sign-extended, as for S_BFE_I32, D.i = signext((S0.i >> S1.u[4:0]) & ...) (section 12.1, p. 105). The interpreter's s_bfe_i32 (rdna2_exec_bits.bfe) and the HLSL SBfeI32 already shift arithmetically, so only the vector form was wrong. Both the interpreter and BfeI32 now shift arithmetically. Fields that fit inside S0 (offset + width ≤ 32) and width 0 give the same results as before.

Linked issue: none.

Verification

Linux, Python 3.14, no C++ runtime, no GPU (the runtime and GPU tests skip):

python -m pytest → 1092 passed, 326 skipped
python -m pytest tests/test_rdna2_exec_bfe.py → 2 passed, 1 skipped (on main without the fix: 2 failed)
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!

Not run here: test_bfe_i32_matches_interpreter_on_gpu (no dxc or GPU on this machine). It runs in the Windows CI job on WARP. The HLSL text was checked offline: rdna2_hlsl.translate of the parity program succeeds, and a Python model of the new BfeI32 (32-bit shl and ashr with 5-bit shift amounts, as in DXIL) equals the interpreter on 200,000 random (S0, offset, width), with 0 differences.

Oracle and test data

  • How it was found: differential testing of the interpreter against LLVM's constant folding of llvm.amdgcn.sbfe.i32 (opt -passes=instcombine -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1030, LLVM 23.1.5, which turns a field past bit 31 into ashr S0, offset). On main, 23 of 256 random triples differed. With the fix, 0 differ. v_bfe_u32 matched before and still does.

  • New tests/test_rdna2_exec_bfe.py:

    • 11 (S0, offset, width) cases, including offsets and widths above 31 (S1[4:0], S2[4:0]), fields inside S0, and width 0;
    • all 32 offsets with width 31 on a negative S0;
    • a GPU parity program: offset, width, or both taken from the lane number, on a negative and a positive S0, so 6 × 32 results are compared byte for byte.

    The expected values are worked out from the ISA pseudo-code and agree with LLVM's folding. The data is built in the test.

  • Failure case: without the fix, the two interpreter tests fail.

Checklist

  • The PR covers a single reviewable behaviour.
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.

AI assistance: written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:55 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/271) `lcg007xd-c:fix/rdna2-bfe-i32-sign` → `main` · +72 −4 · 3 files · 1 commits · merged 2026-11-26 00:15 UTC ## Change `v_bfe_i32` (interpreter `_bitfield` and HLSL `BfeI32`) shifted S0 as an unsigned value before masking and sign-extending the field. When the field runs past bit 31 (offset + width > 32), the bits above bit 31 came in as zeros instead of copies of the sign bit: | S0 | offset | width | `main` (`e921268`) | ISA | | --- | --- | --- | --- | --- | | `939108F5` | 16 | 31 | `00009391` | `FFFF9391` | | `D992A382` | 31 | 31 | `00000001` | `FFFFFFFF` | | `80000000` | 28 | 8 | `00000008` | `FFFFFFF8` | | `EC3B1D4F` | 16 | 24 | `0000EC3B` | `FFFFEC3B` | The ISA pseudo-code shifts the **signed** S0: V_BFE_I32, `D.i = (S0.i >> S1.u[4:0]) & ((1 << S2.u[4:0]) - 1)` (section 12.12, p. 169), and the field is then sign-extended, as for S_BFE_I32, `D.i = signext((S0.i >> S1.u[4:0]) & ...)` (section 12.1, p. 105). The interpreter's `s_bfe_i32` (`rdna2_exec_bits.bfe`) and the HLSL `SBfeI32` already shift arithmetically, so only the vector form was wrong. Both the interpreter and `BfeI32` now shift arithmetically. Fields that fit inside S0 (offset + width ≤ 32) and width 0 give the same results as before. Linked issue: none. ## Verification Linux, Python 3.14, no C++ runtime, no GPU (the runtime and GPU tests skip): ``` python -m pytest → 1092 passed, 326 skipped python -m pytest tests/test_rdna2_exec_bfe.py → 2 passed, 1 skipped (on main without the fix: 2 failed) python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` Not run here: `test_bfe_i32_matches_interpreter_on_gpu` (no dxc or GPU on this machine). It runs in the Windows CI job on WARP. The HLSL text was checked offline: `rdna2_hlsl.translate` of the parity program succeeds, and a Python model of the new `BfeI32` (32-bit `shl` and `ashr` with 5-bit shift amounts, as in DXIL) equals the interpreter on 200,000 random (S0, offset, width), with 0 differences. ## Oracle and test data - How it was found: differential testing of the interpreter against LLVM's constant folding of `llvm.amdgcn.sbfe.i32` (`opt -passes=instcombine -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1030`, LLVM 23.1.5, which turns a field past bit 31 into `ashr S0, offset`). On `main`, 23 of 256 random triples differed. With the fix, 0 differ. `v_bfe_u32` matched before and still does. - New `tests/test_rdna2_exec_bfe.py`: - 11 (S0, offset, width) cases, including offsets and widths above 31 (`S1[4:0]`, `S2[4:0]`), fields inside S0, and width 0; - all 32 offsets with width 31 on a negative S0; - a GPU parity program: offset, width, or both taken from the lane number, on a negative and a positive S0, so 6 × 32 results are compared byte for byte. The expected values are worked out from the ISA pseudo-code and agree with LLVM's folding. The data is built in the test. - Failure case: without the fix, the two interpreter tests fail. ## Checklist - [x] The PR covers a single reviewable behaviour. - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. AI assistance: written with Claude Code (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The interpreter and the HLSL BfeI32 helper shifted S0 as unsigned before
masking and sign-extending the field. With offset + width > 32 the bits
above bit 31 were zeros instead of copies of the sign bit: 0x939108F5,
offset 16, width 31 gave 0x00009391 instead of 0xFFFF9391. The ISA
pseudo-code shifts the signed S0 ("D.i = (S0.i >> S1.u[4:0]) & ...",
section 12.12, p. 169), as s_bfe_i32 already does in this interpreter
(section 12.1, p. 105). Both now shift arithmetically; fields inside S0
(offset + width <= 32) are unchanged.

Found by differential testing against LLVM's constant folding of
llvm.amdgcn.sbfe: 23 of 256 random triples differed, 0 now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:41:22 +00:00
github manually merged commit 36a2dbcf52 into gh-pr/271/base 2026-11-26 05:41:23 +00:00
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 23:42 UTC · original

Setup: Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from main 3e258c8. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, ANYXBOXX_GPU=Intel, 16 lanes, no FP64), WARP 10.0.26100.9818 (ANYXBOXX_GPU=warp). This PR was merged onto main 3e258c8 (clean merge).

main this PR
RTX 4060, python -m pytest 1404 passed, 17 skipped 1407 passed, 17 skipped
WARP, tests/test_rdna2*.py 996 passed, 2 skipped 999 passed, 2 skipped
Intel UHD, tests/test_rdna2*.py 994 passed, 4 skipped 997 passed, 4 skipped
tests/test_rdna2_exec_bfe.py on main code, each adapter 2 failed, 1 passed 3 passed
  • test_bfe_i32_matches_interpreter_on_gpu (the item you couldn't run) passes with this PR on the RTX 4060, Intel UHD (16 lanes) and WARP. On main it also passes on all three, because the interpreter and BfeI32 shared the bug. It guards parity, and the two interpreter tests guard the fix.
  • Semantics: the interpreter and BfeI32 now compute the same thing: an arithmetic shift by S1[4:0], then sign extension from bit w - 1. Width 0 still gives 0, and v_bfe_u32 is unchanged. No rule-3 or rule-4 issue: there is no refusal change, and both sides change together.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

**@⁠KainanP** on GitHub · 2026-11-25 23:42 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/271#issuecomment-7177942924) **Setup:** Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from `main` `3e258c8`. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, `ANYXBOXX_GPU=Intel`, 16 lanes, no FP64), WARP 10.0.26100.9818 (`ANYXBOXX_GPU=warp`). This PR was merged onto `main` `3e258c8` (clean merge). | | `main` | this PR | |---|---|---| | RTX 4060, `python -m pytest` | 1404 passed, 17 skipped | 1407 passed, 17 skipped | | WARP, `tests/test_rdna2*.py` | 996 passed, 2 skipped | 999 passed, 2 skipped | | Intel UHD, `tests/test_rdna2*.py` | 994 passed, 4 skipped | 997 passed, 4 skipped | | `tests/test_rdna2_exec_bfe.py` on `main` code, each adapter | 2 failed, 1 passed | 3 passed | - `test_bfe_i32_matches_interpreter_on_gpu` (the item you couldn't run) passes with this PR on the RTX 4060, Intel UHD (16 lanes) and WARP. On `main` it also passes on all three, because the interpreter and `BfeI32` shared the bug. It guards parity, and the two interpreter tests guard the fix. - Semantics: the interpreter and `BfeI32` now compute the same thing: an arithmetic shift by `S1[4:0]`, then sign extension from bit `w - 1`. Width 0 still gives 0, and `v_bfe_u32` is unchanged. No rule-3 or rule-4 issue: there is no refusal change, and both sides change together. _AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this._
This discussion has been locked. Commenting is limited to contributors.
No description provided.