shader: lower SDWA binary16 add, sub, subrev and mul in gfx2hlsl #2731
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2739"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T04:43:29Z.
Part of #1723
What this unblocks:
v_add_f16_sdwa,v_sub_f16_sdwa,v_subrev_f16_sdwaandv_mul_f16_sdwastop being refused ingfx2hlsl.py, so shaders that add or multiply a selected half of a register and write it into one half of another (the packed-half pattern) translate instead of stopping at the first SDWA binary16 instruction.What changed
Stage.sdwa_half(called fromsdwa()): each source is the half chosen bysrc0_sel/src1_sel(WORD_0orWORD_1;DWORDreads the low half, as the existing SDWAv_cmp_class_f16does), widened exactly withf16tof32, combined with the same expression the plainVEC_Hentry uses, rounded once withf16rn, and written intodst_selWORD_0orWORD_1through the existingsdwa_dest(UNUSED_PADzeroes the other half,UNUSED_PRESERVEkeeps the old one).abs/negsource modifiers (-v,|v|,-|v|) flip or clear the half's sign bit, the same as on the plain form.WORD_0write withUNUSED_PRESERVEinto a register whose upper half is still undefined (after a d16 load) keeps it tagged as low-16-only, and aWORD_1source read of such a register is refused (u()already refuses it).What stays refused
dst_sel:DWORD: the upper half of a 16-bit result is not claimed here (same reason the low-16-only contract of #732 exists; no new lowering depends on it).UNUSED_SEXT,sext(),clamp, output modifiers, inline-constant or scalar sources,v_min_f16/v_max_f16/v_fmac_f16in SDWA form.v_pk_*) operations are separate slices and are not touched.Why it is correct
The arithmetic is the one already merged for the plain forms (binary32 operation on exactly widened halves, one rounding to binary16; the 11-bit significand argument of #2073 applies unchanged). SDWA only changes which 16 bits are read and where the result lands; both are plain bit placement.
Tests
startup_gfx2hlsl_f16_alu_check.py: the emitted statement is evaluated for every lane of the draw fixtures (zeros of both signs, smallest and largest denormal, smallest normal, 65504, infinities, quiet/signalling NaN, NaN payload, tie-to-even cases,inf - inf,0 * inf) against the exact-rational reference; the refusals above; the low-16-only tag cases.startup_gfx2hlsl_f16_alu_draw_check.py(existing draw suite, extended): 22 configurations (3 x 3 source selections, the four destination placements with PAD and PRESERVE, all four operations, the abs/neg spellings, edge values) drawn on D3D12 and compared with the CPU reference word by word. Every source word carries a different non-NaN value in its unselected half, and the old destination word is a known value so PRESERVE is observable. NaN halves are canonicalised to0x7e00on both sides (no portable sign or payload). A wrong-value control (one bit changed in the expected word) must be reported as a mismatch, and the Python controls require that a swapped source half, the other destination half and PAD/PRESERVE exchanged each change at least one expected lane of every configuration. Thestartup_gfx2hlsl_f16_alu_draw_*CTest timeout goes from 300 s to 900 s for the larger set.Verification:
python3 -I tests/startup_gfx2hlsl_f16_alu_check.py->f16 alu okpython3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py-> controls all okpython3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py binary llvm-mc-19->fixture words match llvm-mc(gfx1013 encodings of all 22 bodies and vertex fixtures)tests/startup_*check.pysweep compared with main: 0 scripts differ.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by RonLogic on 2026-11-26T04:47:10Z.
Independent Windows GPU blocker on
4145d0803ecf77de448543ab1bf0be82b935cc91. The pure-Python check passes. The complete D3D12 flow check reported identical failures on in-box WARP10.0.26100.10068and RTX 407032.0.16.2124: ten FXCps_5_1WORD_0/PAD cases read back all four zero words. The first isv_add_f16_dword_dword_word_0_pad_plain_plain: expected00004200,00004000,00003666,0000644c, actual00000000,00000000,00000000,00000000. Both backends pass the DXCps_6_0cases. D3D12 debug layer enabled, Windows26300.10075, SDK10.0.26100.0. I am isolating whether the zeroing comes from the draw's NaN canonicalization or the translated arithmetic.Isolated to the FLOAT test output. Each expected WORD_0/PAD result fits in 16 bits, so the existing
asfloatexport represents it as binary32 zero or a subnormal. In a local probe I changed only those fixture exports toF(nanc(vN) ^ 0x3f800000u)and XORed the expected words with the same constant. This keeps the expected output normal and does not hide a destination bit as an OR tag would. All ten original FXC failures then matched on each adapter, and a one-bit reference mutation was rejected for every case. The translated arithmetic and placement stayed unchanged. Please use this export encoding for the WORD_0/PAD draw cases or use an integer render target.Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:22:54Z.
Review: COMMENTED
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:
ef44ab4b01ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:31:24Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T05:53:37Z.
Review: APPROVED
Rechecked the combined suite after the #2738 merge. Both the existing packed and new SDWA assertions passed, then FXC and DXC readback passed on Windows 11 build 26200 on WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656. The output tag carries the raw SDWA word through the float target, and the wrong-value control still fails on both compilers and adapters.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:14:21Z.
Thank you, RonLogic: your isolation was exactly right. A result that fits in 16 bits reads back from the float target as binary32 zero or a subnormal, and FXC drops it. The translated arithmetic was fine; the fixture export was not.
Fix pushed (
startup_gfx2hlsl_f16_alu_draw_check.py): the drawn export is nowF(nanc(vN) ^ TAG)and every expected word is XORed with the same tag.One change from the suggestion: with
0x3f800000some lanes (upper halves of the0x4000class, from the WORD_1 cases) turn into infinity or NaN as binary32. I use0x60000000instead, and a new control requires that every expected word of every configuration stays a finite normal float after the XOR.Checks run locally (Linux only):
python3 -I tests/startup_gfx2hlsl_f16_alu_check.py->f16 alu ok; draw check controls all ok;binary llvm-mc-19words still match; main merged in (no conflicts), Linux sweep compared with main: 0 differ.The D3D12 draw itself is still for the Stage-2 run; if you can re-run your WARP / RTX flow on the new head, that would confirm the ten FXC cases.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:16:34Z.
Independent gate on head
ef44ab4b0passed, promoting to Ready.Verification:
cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim startup_buffer_format_word3_checkthenctest --preset dev -R '^startup_gfx2hlsl_f16_alu' --no-tests=error --output-on-failureon Windows Server 2025 (D3D12 WARP): 4/4 passed.--preset release: 4/4 passed. The WARP draw test compares GPU readback with the CPU reference word by word, including zeros of both signs, denormals, infinities, NaN and a wrong-value control.python3 -I tests/startup_gfx2hlsl_f16_alu_check.pyandpython3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py: ok on Linux and Windows.@MasterDomino @R0Y00 this is ready for your review.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:16:41Z.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
ef44ab4ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T05:22:55Z.
Code review location:
tools/startup/gfx2hlsl.py; line 4141AGENTS.mdrequires a README ordocs/experiments.mdupdate whenever a public capability changes, but this exposes four previously refused instructions without changing either document. For these f16 arithmetic forms, the retained statement indocs/startup-runtime.md:2065-2067that floating-point SDWA stops translation is now incorrect. Document the accepted selectors, PAD/PRESERVE behavior, remaining refusals, and reproduction commands, and correct the blanket refusal statement.AGENTS.md reference: AGENTS.md:L317-L318
Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:31:24Z.
Code review location:
tools/startup/gfx2hlsl.py; line 4141Fixed in
f3764680b(docs/startup-runtime.md): the SDWA section no longer says all float SDWA stops translation. It now documents the binary16 add, sub, subrev and mul forms: accepted src0_sel/src1_sel (WORD_0, WORD_1, DWORD), dst_sel WORD_0/WORD_1 with UNUSED_PAD (default) or UNUSED_PRESERVE, the remaining refusals (dst_sel DWORD, byte selects, sext, clamp, output modifiers, UNUSED_SEXT, non-VGPR sources), and the ctest reproduction command.Verification:
python3 -B tests/docs_links_check.py: passed (Linux).python3 -B tests/startup_gfx2hlsl_f16_alu_check.py:f16 alu ok(Linux).f3764680b(x2w_pipe.py win2025 2739 --regex startup_gfx2hlsl_f16_alu): dev 4/4 passed, release 4/4 passed (includes the WARP/hardware readback draws).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-26T06:50:15Z.
Post-merge check on an AMD Radeon RX 9070 XT (driver 32.0.32029.2010) and WARP 10.0.26100.9818, Windows 11 build 26300.
What passes:
startup_gfx2hlsl_f16_alu,_draw_controlsand_draw_warp.What fails:
startup_gfx2hlsl_f16_alu_draw_hardwarefails on one SDWA case, with both FXC and DXC.v_mul_f16_word_1_word_0_word_1_preserve_plain_plain_edge, set 0, lane 2, which multiplies -0 by +0.e000abcd, read back6000abcd, so the product loses its sign of zero.v_pk_mul_f16cases from #2738.Everything else checks out: source selection, placement, operand order (including
v_subrev) and refusals. Six one-line mutations each failstartup_gfx2hlsl_f16_alu_check.py: source half swapped, PRESERVE treated as PAD, abs dropped, operands swapped,dst_sel:DWORDaccepted, and the low-16 tag dropped. No cache bump was needed, since these forms were refused before.Could the -0 product case be folded into the #2746 fix (for example a
precisemultiply), so that the AMD hardware variant goes green?Commands:
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f16_alu' --output-on-failure;python -B tests/startup_gfx2hlsl_f16_alu_check.py.AI-assisted; commands listed were executed locally.