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

Merged
lcg007xd-c merged 1 commit from upstream-pr/271 into main 2026-11-26 00:15:06 +00:00
lcg007xd-c commented 2026-11-25 22:55:43 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-11-25T22:55:43Z.


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

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:6615436085 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/271). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:55:43Z. --- ## 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)
KainanP commented 2026-11-25 23:42:42 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-25T23:42:42Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7177942924 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/271#issuecomment-7177942924). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-25T23:42:42Z. --- **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._
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LooperSalty/AnyXboxX!271
No description provided.