Skip to content

shader: preserve signed zero in plain, packed and SDWA binary16 multiplication - #2779

Merged
tedin7 merged 3 commits into
R0Y00:mainfrom
RavenCurseBurg:shader/keep-half-multiply-zero-sign
Nov 26, 2026
Merged

tedin7 merged 3 commits into
R0Y00:mainfrom
RavenCurseBurg:shader/keep-half-multiply-zero-sign

Conversation

@RavenCurseBurg

@RavenCurseBurg RavenCurseBurg commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Refs #2746. This covers the packed and SDWA half-multiply cases in the RX 9070 XT report. The shared binary16 multiplication expression also serves plain v_mul_f16.

Widening binary16 operands makes their finite product exact in binary32, but an ordinary HLSL multiply lets the compiler disregard the sign of zero. Apply precise to the product before the existing binary16 rounding. The existing per-lane comparisons still cover the selected half, sign modifiers, and the preserved half of the destination. The portable test checks the precision qualifier and the emitted calls for the plain, packed and SDWA paths. After merging main at 96a7b4fb, cached shaders advance to 25/25cs.

The focused SDWA case (set 0, lane 2) read back e000abcd on WARP, Intel UHD 770 and RTX 4060 with both FXC and DXC. The complete draw suite passed on all three at the initial implementation, with 220 calls per adapter including the existing wrong-value controls. For the packed and SDWA pixel fixtures I also compiled a copy with just the qualifier removed. DXC changes the relevant fmul float to fmul fast float; FXC drops [precise] from the corresponding mul.

Repeated the complete FXC/DXC draw suite on 3cc0d4b8 on all three adapters. On 3fdbaa40 the SDWA case again gave e000abcd in all six compiler/adapter combinations. A compiled control that clears the product's sign exactly when its magnitude is zero gave 6000abcd and exited 1 in all six. Locally ran the two binary16 Python checks, LLVM 15 gfx1013 reassembly, both cache-key checks, offline compute request, packed-position, buffer-store, const-copy-join and fract checks. An independently compiled C++ FNV calculation for the final graphics keys gives df78b28cb80d4c5a and 466483d084260802. Windows 11 build 26100.9711, DXC 1.9.2609.5, FXC ps_5_1, UHD 770 driver 32.0.101.5973, RTX 4060 driver 32.0.15.9579, WARP 10.0.26100.9711. Debug layer unavailable.

For the reporter's RDNA 4 retest on this exact candidate:

cmake --build --preset dev --target offscreen_check
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f16_alu_draw_(warp|hardware)$' -V

AI-assisted with Codex (GPT-6.2 Astra, xhigh).

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 3fdbaa4 (0 commits behind main).

Verdict: sound, no blocking findings.

What I checked

  • mul16 (precise float product = a * b) is emitted once in the shared helper prelude (gfx2hlsl.py near fma16o), and all three multiply paths go through it: plain v_mul_f16 (VEC_H, also used by the SDWA path) and v_pk_mul_f16. No other binary16 multiply site was left on a plain *.
  • Binary16 operands widened to binary32 give an exact product, so precise only changes the sign of zero results; the existing f16rn rounding after it is unchanged.
  • Cache key bump 24 -> 25 (graphics and cs) is mirrored in the tests and docs that pin it.
  • Overlap: no other open PR touches this multiply line. Open #2741 (VOPC compares) and #2754 (interp pair) are different slices; merged #2738/#2739 added the packed and SDWA forms this PR fixes. The next PR that edits the same VEC_H row or bumps the key to 25 will need a trivial conflict resolution.
  • Test: the draw check reads back on the GPU (the author reports e000abcd on three adapters with FXC and DXC, and a control that gives 6000abcd); the added Linux controls check the qualifier and call count for the plain, packed and SDWA forms.

Commands run locally (Linux)

  • Python checks, all rc=0: startup_gfx2hlsl_f16_alu_check, startup_gfx2hlsl_f16_alu_draw_check, startup_shader_cache_key_check, startup_gfx2hlsl_fract_check, startup_gfx2hlsl_buffer_store_check, startup_gfx2hlsl_const_copy_join_check, startup_compute_request_offline_check, startup_packed_position_provenance_check
  • tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check origin/main...HEAD: clean
  • Mutation 1: remove precise from mul16 -> startup_gfx2hlsl_f16_alu_draw_check fails (plain, packed and SDWA controls).
  • Mutation 2: revert v_pk_mul_f16 to {0} * {1} -> the same check fails (packed control).

Windows WARP run and the real-GPU run are separate and will be reported in a receipt comment.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Win2025 WARP receipt for head 3fdbaa4 (focused tests: the PR's own f16 ALU, draw, cache-key and sibling checks; WARP draw and hardware draw both ran, not skipped).

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 3fdbaa40fb3c7a99f7bec4eb7ef315e8734bf0ef:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim startup_buffer_format_word3_check startup_indirect_draw_indexed startup_resource_retire_title -- -j 2): 26.55s (configure 1.84s)
    • Test (ctest --preset dev -R 'startup_gfx2hlsl_f16_alu|startup_shader_cache_key|startup_gfx2hlsl_fract|startup_gfx2hlsl_buffer_store|startup_gfx2hlsl_const_copy_join|startup_compute_request_offline|startup_packed_position|startup_indirect_draw_indexed|startup_resource_retire_title' --parallel 6 --no-tests=error --output-on-failure): 17/20 passed (45.84s: startup_shader_cache_key 0.73s, startup_gfx2hlsl_buffer_store 0.75s, startup_compute_request_offline 0.80s, startup_gfx2hlsl_f16_alu_draw_controls 0.58s, startup_gfx2hlsl_fract_draw_warp 0.67s, startup_gfx2hlsl_fract_draw_hardware 0.71s, startup_resource_retire_title_warp_debug0 0.13s, startup_gfx2hlsl_fract 0.25s, startup_gfx2hlsl_fract_draw_controls 0.25s, startup_gfx2hlsl_const_copy_join 0.12s, startup_resource_retire_title_hardware_debug0 0.12s, startup_resource_retire_title_off_warp 0.08s, startup_resource_retire_title_warp_debug1 0.03s, startup_indirect_draw_indexed_warp 0.12s, startup_resource_retire_title_hardware_debug1 0.04s, startup_resource_retire_title_off_hardware 0.10s, startup_indirect_draw_indexed_hardware 0.03s, startup_gfx2hlsl_f16_alu 4.15s, startup_gfx2hlsl_f16_alu_draw_hardware 45.03s, startup_gfx2hlsl_f16_alu_draw_warp 45.69s); skipped (capability, not a pass): startup_resource_retire_title_warp_debug1, startup_resource_retire_title_hardware_debug1, startup_indirect_draw_indexed_hardware
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim startup_buffer_format_word3_check startup_indirect_draw_indexed startup_resource_retire_title -- -j 2): 62.32s (configure 1.83s)
    • Test (ctest --preset release -R 'startup_gfx2hlsl_f16_alu|startup_shader_cache_key|startup_gfx2hlsl_fract|startup_gfx2hlsl_buffer_store|startup_gfx2hlsl_const_copy_join|startup_compute_request_offline|startup_packed_position|startup_indirect_draw_indexed|startup_resource_retire_title' --parallel 6 --no-tests=error --output-on-failure): 17/20 passed (32.15s: startup_gfx2hlsl_fract_draw_warp 0.55s, startup_gfx2hlsl_fract_draw_hardware 0.57s, startup_gfx2hlsl_buffer_store 0.70s, startup_shader_cache_key 0.61s, startup_gfx2hlsl_f16_alu_draw_controls 0.49s, startup_compute_request_offline 0.70s, startup_indirect_draw_indexed_warp 0.08s, startup_gfx2hlsl_fract 0.20s, startup_gfx2hlsl_fract_draw_controls 0.20s, startup_resource_retire_title_warp_debug0 0.10s, startup_resource_retire_title_hardware_debug0 0.09s, startup_gfx2hlsl_const_copy_join 0.12s, startup_resource_retire_title_off_hardware 0.07s, startup_resource_retire_title_warp_debug1 0.02s, startup_resource_retire_title_hardware_debug1 0.03s, startup_indirect_draw_indexed_hardware 0.03s, startup_resource_retire_title_off_warp 0.06s, startup_gfx2hlsl_f16_alu 3.73s, startup_gfx2hlsl_f16_alu_draw_hardware 32.00s, startup_gfx2hlsl_f16_alu_draw_warp 32.02s); skipped (capability, not a pass): startup_resource_retire_title_warp_debug1, startup_resource_retire_title_hardware_debug1, startup_indirect_draw_indexed_hardware
  • Windows Python checks: tests/startup_gfx2hlsl_f16_alu_check.py ok (2.8s), tests/startup_gfx2hlsl_f16_alu_draw_check.py ok (0.4s), tests/startup_shader_cache_key_check.py ok (0.4s)
  • sccache: 0/10 hits; wall 133.5s

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 merged commit 4bbe52e into R0Y00:main Nov 26, 2026
5 checks passed
@thepatrickworld

Copy link
Copy Markdown
Contributor

Post-merge receipt on the merged head 3fdbaa40 (main 4bbe52ee): startup_gfx2hlsl_f16_alu_draw_hardware passes on an RX 9070 XT, where main 96a7b4fb fails eight cases per compiler; keeping the mul16() call and removing only precise restores those same sixteen failures.

Measurements only. The precise experiment below is a local, unpushed edit.

Environment

  • Windows 11 10.0.26200.9974; MSVC 19.44.35230, toolset 14.44.35207; DXC 1.9.2602.17; FXC from the same Windows Kits 10.0.26100.0 directory, ps_5_1 profile.
  • Adapter AMD Radeon RX 9070 XT vendor=1002 device=7550, driver 32.0.32029.2010. warp reports Microsoft Basic Render Driver vendor=1414 device=008c, driver 10.0.26100.9818.
  • Preset dev. Every mode line of this run reports debug_layer=unavailable.
  • Measured tree 3fdbaa40, git status --porcelain empty before the build (recorded in the build log). tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_f16_alu_draw_check.py are byte-identical at 3fdbaa40 and at the merge commit 4bbe52ee (sha256 158324c0a8ecb71ae88dd15e6fc933ce2b1617a3f52ce6a45056420444607918 and 07d07c4556e8de20c41525500cb2dbb50cd77ede1abe2d74edff2e4a3d4a0011, LF-normalised), so this receipt covers the code main carries.

Commands

cmake --build --preset dev --target offscreen_check
ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_f16_alu_draw_(warp|hardware)$" -V
ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_f16_alu" -V

The same hardware entry was also run on main 96a7b4fb — the merge base — in a separate worktree, same machine.

Observed on 3fdbaa40

1/2 Test #997: startup_gfx2hlsl_f16_alu_draw_warp .......   Passed   58.19 sec
2/2 Test #998: startup_gfx2hlsl_f16_alu_draw_hardware ...   Passed   81.82 sec
100% tests passed out of 2
1/4 Test #805: startup_gfx2hlsl_f16_alu .................   Passed    2.56 sec
2/4 Test #996: startup_gfx2hlsl_f16_alu_draw_controls ...   Passed    0.51 sec
3/4 Test #997: startup_gfx2hlsl_f16_alu_draw_warp .......   Passed   57.84 sec
4/4 Test #998: startup_gfx2hlsl_f16_alu_draw_hardware ...   Passed   80.89 sec
100% tests passed out of 4

The renderer's own lines inside that log read adapter=AMD Radeon RX 9070 XT vendor=1002 device=7550, driver=32.0.32029.2010, backend=hardware debug_layer=unavailable. No MISMATCH line appears in either log.

Observed on main 96a7b4fb, before the merge

1/1 Test #998: startup_gfx2hlsl_f16_alu_draw_hardware ...***Failed   79.45 sec
0% tests passed, 1 tests failed out of 1
1/1 Test #997: startup_gfx2hlsl_f16_alu_draw_warp .......   Passed   56.98 sec

Sixteen mismatching draws, eight per compiler: seven v_pk_mul_f16 selectors and the SDWA preserve case. Two of the raw pairs (ctest prefixes each line with the test number; input lists trimmed):

fxc sdwa v_mul_f16_word_1_word_0_word_1_preserve_plain_plain_edge set 0 MISMATCH inputs [...]
startup_flat_param mismatch expected=60001234,60005678,e000abcd,60003456 actual=60001234,60005678,6000abcd,60003456
fxc v_pk_mul_f16:lo:1 set 0 MISMATCH inputs [...]
startup_flat_param mismatch expected=40008000,40008000,40007c00,40007c00 actual=40000000,40000000,40007c00,40007c00

precise, not the call

I kept the mul16() helper and both call sites and removed only the keyword: float mul16(float a, float b) { float product = a * b; return product; }. To let that run reach the draws, the whole if BODY_WORDS: block (the new assertion and the per-op translation checks) was disabled, and both edits are recorded at the head of that run's log; the tree was restored afterwards. Result: the same sixteen case labels and the same expected/actual words as main — I compared the (case, expected, actual) triplets, not just the labels.

Control for the disabled block: the same two edits with precise kept give exit 0 and zero mismatches, so the block is inert for the draws and the qualifier is the variable.

The added checks assert text

Reverting v_mul_f16 to {0} * {1} trips the plain and SDWA labels together (they share the VEC_H row); reverting op_v_pk_mul_f16 trips only packed. One caveat on the first conjunct: 'precise float product = a * b; return product;' in text is already true of a shader that does not multiply (v_add_f16, v_min_f16), because the helper sits in the preamble — it constrains the helper body, while text.count('f16rn(mul16(') is what ties a given path to it.

Regression on the merged head

  • Revision-pinned checks startup_shader_cache_key, startup_gfx2hlsl_const_copy_join, startup_gfx2hlsl_buffer_store, startup_gfx2hlsl_fract, startup_gfx2hlsl_fract_draw_controls, _draw_warp, _draw_hardware, startup_compute_request_offline: 8 pass / 0 skip / 0 fail.
  • startup_indirect_draw_indexed_hardware, startup_indirect_translated_indexed_hardware: 2 pass.
  • startup_resource_retire_title_hardware_debug0, startup_resource_retire_title_off_hardware: 2 pass; startup_resource_retire_title_hardware_debug1 skipped, return code 77 (with X2W_D3D12_DEBUG=1 the fixture returns 77 when D3D12GetDebugInterface fails, and the debug layer is not installed here). A skip is not a pass.
  • tests/startup_packed_position_provenance_check.py has no ctest entry in this preset; run directly, it exits 0.

Still failing on this head

startup_gfx2hlsl_fma_mix_dispatch_hardware: WARP passes, hardware fails on negated inline 0 addend keeps a -0 product readback: MISMATCH, expected 80000000, got 00000000, plus its high-half-shift control. That is the v_fma_mix half of #2746, and this PR does not cover it.

Limits

  • One machine, one adapter, one driver; debug_layer=unavailable, so no API-validation output is part of this result.
  • The precise run is an experiment, not a commit; nothing was pushed.
  • I did not disassemble the emitted DXIL: the measured difference is between two HLSL source forms that differ by one keyword, and nothing finer.
  • The draw cannot separate "mul16 is called" from "mul16 is inlined". What is measured is that the sign of zero survives with the qualifier and is lost without it, on this adapter and driver.
  • Only the binary16 ALU fixture is exercised: no mesh pipeline, no compute path, no title.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants