fix: v_bfe_i32 reads copies of the sign bit when the field runs past bit 31 #271
No reviewers
Labels
No labels
accessibility
bloqué : binaire console
bug
chargeur
console-dll
cpu
documentation
duplicate
enhancement
good first issue
gpu
help wanted
invalid
performance
question
runtime-gdk
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/LooperSalty--AnyXboxX!271
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/271/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCChange
v_bfe_i32(interpreter_bitfieldand HLSLBfeI32) 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:main(e921268)939108F500009391FFFF9391D992A38200000001FFFFFFFF8000000000000008FFFFFFF8EC3B1D4F0000EC3BFFFFEC3BThe 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'ss_bfe_i32(rdna2_exec_bits.bfe) and the HLSLSBfeI32already shift arithmetically, so only the vector form was wrong. Both the interpreter andBfeI32now 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):
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.translateof the parity program succeeds, and a Python model of the newBfeI32(32-bitshlandashrwith 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 intoashr S0, offset). Onmain, 23 of 256 random triples differed. With the fix, 0 differ.v_bfe_u32matched before and still does.New
tests/test_rdna2_exec_bfe.py:S1[4:0],S2[4:0]), fields inside S0, and width 0;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
E_NOTIMPL), never a fake success.AI assistance: written with Claude Code (Claude Opus 5.5).
🤖 Generated with 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]>@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
main3e258c8. 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 ontomain3e258c8(clean merge).mainpython -m pytesttests/test_rdna2*.pytests/test_rdna2*.pytests/test_rdna2_exec_bfe.pyonmaincode, each adaptertest_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. Onmainit also passes on all three, because the interpreter andBfeI32shared the bug. It guards parity, and the two interpreter tests guard the fix.BfeI32now compute the same thing: an arithmetic shift byS1[4:0], then sign extension from bitw - 1. Width 0 still gives 0, andv_bfe_u32is 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.