MTBUF d16: RDNA2-measured conversions replace the ISA-only lowering #41
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!41
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/41/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?
@SchmashinPrunes on GitHub · 2026-11-21 18:58 UTC · original
SchmashinPrunes:mtbuf-d16→main· +1665 −167 · 16 files · 84 commits · closed without mergingLibrary / area: tombox-xshader (RDNA2 shader translator), MTBUF
tbuffer_{load,store}_format_d16_*, plus a newtools/rdna2-hwprobe. Completes #17 (the LDS-atomics half landed on main in1159c96).Rebased onto
0fc7295. Main now carries an ISA-text-only d16 lowering (1159c96). This PR replaces it with the hardware-measured rules below. Compared with main it:The 8 d16 mnemonics are already counted as verified on main, so
gen.pyoutput is unchanged. Theevidencecaveat inprogress.json("tbuffer d16 refuses UNORM/SNORM and 32-bit-component formats") no longer applies after this PR; I left that text for you to edit.Exports changed (exports.txt rows): none
Source of the behaviour: measurements on real RDNA2 hardware, the gfx1036 iGPU of a Ryzen 5 9600X under ROCm. I used the new
tools/rdna2-hwprobe, which runs hand-written kernels; no game or console files are involved. The public ISA text only says "convert to packed 16-bit". It doesn't specify the rounding, the int narrowing, or what happens to the unused register half, so I measured those. Packed layout per LLVM's AMDGPU backend (HasPackedD16VMemon GFX9+).Measured semantics (77 formats × 65536 elements, every 8/16-bit value)
vdatalo/hi, c2/c3 invdata+1._x/_xyzkeep the upper half of their last VGPR0x3bff, fp32-then-fp16 would give0x3c00sign|0x7c00|((mant>>13) or 1). Payload truncated, not quietedA missing w on d16 loads reads 1 (0x3c00 / 1), matching the existing 32-bit loads. On hardware that 1 comes from the descriptor's dst_sel. The probe used an identity swizzle, so it saw 0. Scaled and 11/10-bit-float stores didn't follow any simple rule and stay unlowered, the same as the 32-bit forms.
Translator
src/hlsl/mtbuf.rs: loads reuse the existing fetch and field extraction, then narrow each component withD16_HELPERS. Stores unpack the halves and pack them per format. All conversions are integer-only:uint64_t.(M·max) >> sh.A first version used float/double math, and WARP got it wrong twice: DXC folded
x != x, and a double product was off by one.hlsl.rsgains auses_d16flag that emits the helpers.Tests (
tests/exec_mem/mtbuf.rs)mtbuf_d16_model_matches_rdna2pins it to 571 recorded vectors (mtbuf_hw.rs, regenerated bytools/rdna2-hwprobe/vectors.py), weighted to the cases where naive rules differ.mtbuf_d16_load_formats: 539 cases on WARP, each rerun with runtime inputs.mtbuf_d16_store_formats: 330 cases, output pre-filled with 0xAA._xzeroing the upper half;mtbuf_d16_load_addressing(308 cases) /mtbuf_d16_store_addressing(660 cases): immediate offset, SGPR offset and unaligned element addresses, adapted from the d16 tests in1159c96(thanks @QuentinDanblon) and checked against the hardware-matched model.mtbuf_refuses_unlowered: drops the now-lowered d16 rows and adds d16 scaled/f11 stores.python tools/progress/gen.py: no change (main's 769/779 already counts the d16 rows).Full
cargo test -p tombox-xshader --release(Windows 11 Pro, WARP)All green on the rebased branch: 0 failures in every target (
exec73 pass / 1 ignored; dump-fixture tests ignored as on main).Also found
The 32-bit store path disagrees with hardware on partial stores and snorm ties. That's a separate issue: #42. This PR doesn't change any 32-bit behaviour.
cargo test -p tombox-xshaderpasses for the changed area🤖 Generated with Claude Code
Replaces the d16 tbuffer lowering with conversion rules measured on RDNA2 hardware (gfx1036 via ROCm, tools/rdna2-hwprobe): - loads: UNORM/SNORM/USCALED/SSCALED round once to nearest-even fp16, saturating at +-65504; 32-bit float rounds toward zero (overflow -> +-65504, inf kept, NaN payload kept); 32-bit ints clamp. These were refused before. - stores: fp16 widened exactly (NaN payload kept), unorm rounds to nearest-even, snorm rounds ties away from zero, ints extend then clamp; the whole element is written with missing components 0. mtbuf_d16_model_matches_rdna2 checks the Rust reference model against the recorded hardware vectors (mtbuf_hw.rs); the WARP tests compare the translator with that model. The address-mode cases (imm/soffset/ unaligned) from the previous d16 tests are kept as mtbuf_d16_{load,store}_addressing. Co-Authored-By: Claude Opus 5.5 <[email protected]>@QuentinDanblon on GitHub · 2026-11-21 21:44 UTC · original
Note for review: the d16 MTBUF code now on main (
1159c96,hlsl/mtbuf.rs+ themtbuf_d16_*groups) is my version, written from the ISA text only. It refuses UNORM/SNORM, 32-bit components and 16-bit scaled formats, because the ISA gives no rounding/clamp rule for them. This PR measured those rules on real RDNA2 (gfx1036): single round-to-nearest-even to fp16 saturating at ±65504, 32-bit float to fp16 rounding toward zero, 32-bit int clamped,_x/_xyzkeeping the upper half. That's strictly better evidence than mine, so where they differ I'd take this PR's semantics over main's refusals. The upper-half rule matches what main already does.@SchmashinPrunes on GitHub · 2026-11-21 22:38 UTC · original
Thanks! Rebased onto
1159c96. Since main now has your d16 lowering, this PR replaces it with the hardware-measured semantics: UNORM/SNORM/32-bit/16-bit scaled now lowered, and d16 stores write the whole element (missing components 0) with snorm ties rounding away from zero, matching gfx1036. I kept your address-mode cases (imm/soffset/unaligned) asmtbuf_d16_*_addressingagainst the hardware-matched model.@QuentinDanblon on GitHub · 2026-11-21 22:55 UTC · original
Your
rdna2-hwprobeharness can answer more than MTBUF. Several open questions in other PRs come down to "what does RDNA2 actually return", and nobody else here seems to have the hardware. I wrote a second generator on top of your runner and kernel preamble: branchprobe/valu-open-questionson my fork (one commit on top of this PR,tools/rdna2-hwprobe/valu_probe.py+ a README section, nothing else touched).It generates 33 kernels:
fma16v_fmac/fmamk/fmaak_f16, LLVM's GFX10 names) or multiply-then-add (v_mac/madmk/madak_f16, what main implements)? Each kernel also runsv_fma_f16andv_mul_f16+v_add_f16on the same inputs, so both references come from the hardware. About 28,000 of the 65,536 inputs per kernel separate the two.sbfes_bfe_{u,i}{32,64}with a width field of 32 or more: 7-bit width with sign-fill past the top bit (main since8491769), 7-bit width with the literal ISA formula (zero-fill), or width modulo the operand size (the older test oracle). 152 cases.nanv_fma_f32,v_mul_f32,v_add_f32,v_sub_f32,v_fmac_f32,v_min_f32,v_max_f32return, IEEE mode off and on, over every combination of quiet/signalling NaN, infinity, zero and denormal.tb_fmaon main, #61normv_cvt_pknorm_{i,u}16_{f32,f16}andv_cvt_norm_{i,u}16_f16(f16 forms exhaustive).tb_nrmhelper, #63showprints, per question, how many outputs match each candidate rule.What I could and could not check: I have no AMD GPU, so none of this has run on hardware. The kernels assemble with LLVM 18 and disassemble identically for gfx1036 and gfx1013. I tested
showagainst simulated outputs for each possible answer and it tells them apart. If a kernel faults or the preamble assumptions are wrong for a non-MTBUF kernel, that is my mistake, tell me and I'll fix it.If you have ten minutes with the 9600X, the
showoutput pasted here would settle three disagreements with measurements, the same way this PR did for d16. Feel free to pull the commit into this PR if you want it to live with the tool.@QuentinDanblon on GitHub · 2026-11-21 23:15 UTC · original
Tested your rebased head
2515839, merged locally onto main8491769(no conflict), on two backends you may not have at hand:--test exec exec_mem::mtbufTOMBOX_EXEC_HW=1)That includes
mtbuf_d16_model_matches_rdna2and bothmtbuf_d16_*_addressingtests. I did not run the rest of the suite on that merge.The probe branch from my previous comment is rebased onto
2515839as well (probe/valu-open-questions, still one commit on top of this PR, kernels re-assembled with LLVM 18). Still not run on hardware.@SchmashinPrunes on GitHub · 2026-11-21 23:54 UTC · original · edited 2026-11-22 00:01 UTC
Ran
valu_probe.py(efffbc0, unmodified) on the gfx1036 iGPU of a Ryzen 5 9600X: Mint, kernel 7.0.0-42, ROCm/HIP 5.7.1, clang/ld.lld 18.1.3. All 33 kernels ran, and no kernel had outputs that differ between threads.Results
fma16v_fma_f16on every discriminating input and never matchv_mul_f16+v_add_f16: 254,891 inputs over the three K values, fused on all, unfused on 0, neither on 0. Hardwarev_fma_f16matches the exact single-rounding model, 0 mismatches. The upper half of D is preserved (0x36 keeps D's old upper half, 0x37 / 0x38 keep the preset).sbfewide: 152 / 152 (lit 114, mod 100). The 7-bit width is used, and a signed field that reaches the top bit sign-fills. Junk in the unused S1 bits is ignored, and offset bit 5 (32-bit) or bit 6 (64-bit) is ignored. SCC = result != 0 in every case. This matches main since8491769.normv_cvt_pknorm_{i,u}16_{f32,f16},v_cvt_norm_{i,u}16_f16), with NaN → 0.halfupfails on i16 (51 / 2 / 1), so it's not floor(x+0.5). Thev_cvt_norm_*upper half is preserved.nanNaN rules (fitted, 0 mismatches)
The
showcandidate is off in three ways: sNaN isn't quieted in IEEE mode 0,v_subnegates src1, and the default NaN is0xffc00000. I fitted these rules against the raw outputs. They reproduce every NaN result (fma 65265, mul 43824, add 43886, fmac 65265, sub 43886, min 43736, max 43736 cases, in each IEEE mode). No input outside the rules produced a NaN.Arithmetic (fma / fmac / mul / add / sub), both IEEE modes:
v_sub_f32negates src1 first, so a NaN src1 comes out with its sign flipped:(0, 0x7fc00001)→0xffc00001.(0, inf, 0x7fc00001)→0xffc00000.(0x7f800003, 0x7fc00001)→0x7f800003). IEEE mode 1: quieted (| 0x400000).0xffc00000(negative quiet NaN) in both modes.v_min_f32/v_max_f32:Since this seems to be the only RDNA2 hardware the repo has at the moment, I'm happy to run probes or exec tests from other PRs on it too. Just mention me on the PR with what you'd like run.
The full
showoutput and the fitting script are below.valu_probe.py show (gfx1036)
nanfit.py (reads nan.in / nan_ieee{0,1}.out from the gen dir)
@QuentinDanblon on GitHub · 2026-11-22 00:07 UTC · original
Thank you, that settles three questions with measurements. I recorded your results in the tool's README on #72 (commit
ae3a658, credited to you, with a link to your comment), and reported them where they apply:v_fmac/fmamk/fmaak_f16); main still decodes them asv_mac/madmk/madak_f16and multiplies then adds.s_bfe: 7-bit width, sign-fill. Main since8491769is right, and the oracles in #70 now have a hardware source.For @omonkeyz, the NaN part is the one that does not match the translator.
tb_fmaon main (and the CPU reference in #73) returns the first NaN operand quieted, and0x7fc00000for an invalid operation. On gfx1036:0xffc00000, in both IEEE modes;0 * inf + NaNreturns that default NaN, not the NaN in c;I have not changed anything for this. Which IEEE mode the console's shaders run in is something I can't establish from here, and the measurement is gfx1036, not gfx1013.
I did not update
showto check your fitted rules, since I can't run it against real outputs. If you want your fitting script in the tool, it is yours to add; I'll rebase #72 on whatever #41 becomes.@omonkeyz on GitHub · 2026-11-22 13:12 UTC · original
Main's history was rewritten, which auto-closed this PR (sorry for the noise). Please rebase on main's current mtbuf code and strip comment lines. The probe README is .md under tools/, which is not published, so put that text in the PR description or a .txt. To resubmit:
git fetchthis repo, branch from the currentmain, cherry-pick only your own commits (the old branch is based on the previous history and cannot be merged), and open a new PR.Pull request closed