Skip to content

startup: lower v_fma_mix* through a precise mad so a zero product keeps its sign - #2818

Merged
tedin7 merged 1 commit into
R0Y00:mainfrom
Not-Saii:gfx2hlsl/fma-mix-precise-mad
Nov 26, 2026
Merged

tedin7 merged 1 commit into
R0Y00:mainfrom
Not-Saii:gfx2hlsl/fma-mix-precise-mad

Conversation

@Not-Saii

Copy link
Copy Markdown
Contributor

Change

startup_gfx2hlsl_fma_mix_dispatch_hardware fails on an AMD Radeon RX 5700 XT (#2729) and an RX 9070 XT (#2746). The case negated inline 0 addend keeps a -0 product reads back 00000000 instead of 80000000, while WARP keeps the sign. The probes in those issues lost the sign only when a binary16 operand widened by f16tof32 fed a non-precise multiply or mad, and kept it under precise. Non-precise HLSL arithmetic does not have to keep the sign of a zero.

Stage.mix() now lowers v_fma_mix_f32, v_fma_mixlo_f16 and v_fma_mixhi_f16 to mix32(a, b, c). This is a helper in Pipeline.common(), next to mul16 from #2779:

float mix32(float a, float b, float c) { precise float result = mad(a, b, c); return result; }
  • Operand order, half selection and modifiers are unchanged, and saturate stays outside the call.
  • mad still lets a backend fuse or round the product, so the existing 1 ULP note for binary32 sources still applies.
  • DXC 1.9.2602.17 emits FMad with !dx.precise on the FMad and on the legacyF16ToF32 that feeds it.

The translated text changes, so the cache revision moves from x2w-startup-shader-27 (and 27cs) to 28 (and 28cs). That covers tools/startup/gfx2hlsl.py, src/startup_stubs/d3d12_draw.inl, docs/startup-runtime.md and the six tests that spell the revision or a known-answer key. git grep x2w-startup-shader-27 is empty at this head.

Refs #2729
Refs #2746

I'm using Refs rather than Fixes because the failing adapters are AMD and I have no AMD card here (see Verification). This is the candidate described in my comment on #2729. That comment gave ColorMeBoom first claim. No PR from them has appeared, so I'm sending this one. If theirs lands first, this one can be closed.

Verification

Commit 9b6f6473, rebased on main at 410f6004. dev preset built from this commit. Environment: Windows 11 10.0.26300.9974, DXC 1.9.2602.17 and FXC from Windows SDK 10.0.26100.0, X2W_TEST_DEBUG_LAYER=1.

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_fma_mix(_fxc|_dispatch_(warp|hardware))?$' -V
1/4 Test  #794: startup_gfx2hlsl_fma_mix .....................   Passed    0.20 sec
2/4 Test  #849: startup_gfx2hlsl_fma_mix_fxc .................   Passed    0.22 sec
3/4 Test #1035: startup_gfx2hlsl_fma_mix_dispatch_warp .......   Passed    0.45 sec
4/4 Test #1036: startup_gfx2hlsl_fma_mix_dispatch_hardware ...   Passed    1.03 sec
100% tests passed, 0 tests failed out of 4
adapter driver readback -0 case mad(4097, 4097, -16785408) sign-clearing control
Microsoft Basic Render Driver (1414:008c) 10.0.26100.9818 17 of 17 80000000 00000000 (unfused) fails only the -0 case
NVIDIA GeForce RTX 4090 (10de:2684) 32.0.16.1088 17 of 17 80000000 3f800000 (fused) fails only the -0 case

The debug layer was enabled and every report showed debug_errors=0 debug_warnings=0.

Controls in the dispatch mode:

  • Fusion probe, information only: it prints mad(4097, 4097, -16785408) per adapter. mix32 allows either result, so it isn't checked.
  • New sign-clearing control: a second shader uses a helper that clears the sign of a -0 result, which is what the RDNA 1 and RDNA 4 drivers did without precise. It must fail exactly negated inline 0 addend keeps a -0 product and no other case.
  • Existing high-half control: it drops the high-half shift and is unchanged.

Revision-dependent tests: at the same build, before committing, a wider run of 43 tests passed on WARP and the RTX 4090. That covered the four above, plus startup_shader_cache_key, startup_shader_cache_rekey, startup_compute_request_*, startup_indirect_draw_indexed_*, startup_resource_retire*, startup_gfx2hlsl_fract*, startup_gfx2hlsl_buffer_store and startup_gfx2hlsl_const_copy_join. The negative-control paths in those tests print FAIL: and debug-layer lines while the tests pass.

Python checks: these pass at this head: startup_gfx2hlsl_fma_mix_check, startup_compute_request_offline_check, startup_gfx2hlsl_buffer_store_check, startup_gfx2hlsl_const_copy_join_check, startup_gfx2hlsl_fract_check, startup_packed_position_provenance_check, startup_shader_cache_key_check, startup_shader_cache_rekey_check, startup_gfx2hlsl_mad_check, docs_links_check and bidi_controls_check.

Not shown: the RTX 4090 also passed before this change, so these runs do not demonstrate the fix on RDNA. They show that mix32 keeps every existing word and the -0 sign on WARP and NVIDIA, and that the control can catch a dropped sign. The RX 5700 XT (#2729) and RX 9070 XT (#2746) retests are still needed:

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_fma_mix_dispatch_(warp|hardware)$' -V

Evidence and follow-up

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted with Claude Code (Claude Opus 5.5). The commands listed were run locally.

🤖 Generated with Claude Code

…ps its sign

On an RX 5700 XT (R0Y00#2729) and an RX 9070 XT (R0Y00#2746) the -0 case of
startup_gfx2hlsl_fma_mix_dispatch_hardware read back 00000000. Their probes
lost the sign only when a binary16 operand widened by f16tof32 fed a
non-precise multiply or mad. Non-precise HLSL arithmetic need not keep a zero's
sign.

All three forms now call mix32(a, b, c), a precise mad. Operand order, half
selection and modifiers are unchanged; saturate stays outside. mad still lets
a backend fuse, so the 1 ULP note stands. The dispatch mode prints a
fused/unfused probe per adapter, and a control helper that clears a -0 result
must fail exactly the -0 case.

The translation changes, so the cache revision moves to x2w-startup-shader-28
(and 28cs) in gfx2hlsl.py, d3d12_draw.inl and the tests that spell it.

Refs R0Y00#2729
Refs R0Y00#2746

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 9b6f6473 (comment only).

Looks sound.

  • Stage.mix() now emits mix32(a, b, c). The helper is precise float result = mad(a, b, c), defined next to mul16 in Pipeline.common(). Operand order, half selection and modifiers are unchanged, and saturate stays outside the call.
  • No overlap: #2779 (mul16) and #2806 (half selection per source position) are merged and touch other lines. #2055 (the original lowering) is merged. No open PR covers this slice.
  • The cache revision moves from 27 to 28 (graphics and cs). git grep x2w-startup-shader-27 is empty at this head, and every test that spells the revision or a known-answer key was updated.
  • Test: startup_gfx2hlsl_fma_mix_dispatch_* reads back from a real D3D12 dispatch and compares with a CPU reference. It includes the -0 product case (80000000). A negative control swaps in a helper that clears the sign of a zero and must fail exactly that case.
  • Limit: the AMD cards from #2729 and #2746 were not available to me, so the fix on those drivers is not verified here. Refs instead of Fixes is right.
  • Real-GPU run not available (RunPod stopped).

Commands run:

  • python3 on startup_gfx2hlsl_fma_mix_check.py, _fract_check.py, _buffer_store_check.py, _const_copy_join_check.py, startup_compute_request_offline_check.py, startup_shader_cache_key_check.py, startup_packed_position_provenance_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py: all rc=0
  • git diff --check origin/main...HEAD: clean
  • Mutation: precise removed from MIX32. The Linux text check still passes, as expected, because the readback needs D3D12. The behavior is guarded by the Windows dispatch test and its sign-clearing control.

Windows Server 2025 (WARP), same head: ctest --preset dev and --preset release with -R on the fma_mix, fract, buffer_store, const_copy, shader_cache_key, compute_request, packed_position, indirect_draw_indexed and resource_retire tests: 100% passed, 42/42 in each preset. This includes startup_gfx2hlsl_fma_mix, _fxc, _dispatch_warp and _dispatch_hardware (on WARP). All Windows Python checks rc=0, including startup_gfx2hlsl_fma_mix_check.py.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

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

  • 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): 24.29s (configure 1.86s)
    • Test (ctest --preset dev -R '^startup_gfx2hlsl_fma_mix|^startup_gfx2hlsl_(fract|buffer_store|const_copy)|shader_cache_key|compute_request|packed_position|indirect_draw_indexed|resource_retire' --parallel 6 --no-tests=error --output-on-failure): 35/42 passed (2.85s: startup_gfx2hlsl_fract_draw_hardware 0.69s, startup_shader_cache_key 0.72s, startup_gfx2hlsl_fract_draw_warp 0.72s, startup_compute_request_offline 0.82s, startup_gfx2hlsl_buffer_store 0.83s, startup_gfx2hlsl_fract 0.26s, startup_compute_request_disabled_control 0.23s, startup_gfx2hlsl_fract_draw_controls 0.24s, startup_resource_retire_hardware_debug0 0.19s, startup_resource_retire_off_warp 0.15s, startup_gfx2hlsl_fma_mix_fxc 0.20s, startup_gfx2hlsl_fma_mix_dispatch_warp 0.57s, startup_gfx2hlsl_fma_mix_dispatch_hardware 0.57s, startup_resource_retire_views_warp_debug0 0.12s, startup_resource_retire_warp_debug0 0.11s, startup_resource_retire_reasons_warp 0.12s, startup_resource_retire_views_keep_warp 0.10s, startup_resource_retire_views_noretire_warp 0.10s, startup_gfx2hlsl_fma_mix 0.18s, startup_resource_retire_title_warp_debug0 0.14s, startup_resource_retire_off_hardware 0.13s, startup_resource_retire_title_hardware_debug0 0.13s, startup_resource_retire_reasons_hardware 0.09s, startup_gfx2hlsl_const_copy_join 0.17s, startup_resource_retire_title_off_warp 0.10s, startup_indirect_draw_indexed_warp 0.17s, startup_resource_retire_reasons_off_hardware 0.08s, startup_resource_retire_views_noretire_hardware 0.09s, startup_resource_retire_views_keep_hardware 0.10s, startup_resource_retire_title_off_hardware 0.10s, startup_resource_retire_reasons_off_warp 0.08s, startup_resource_retire_views_hardware_debug0 0.09s, startup_resource_retire_warp_debug1 0.03s, startup_resource_retire_reasons_no_retire_warp 0.08s, startup_resource_retire_reasons_no_retire_hardware 0.09s, startup_resource_retire_title_warp_debug1 0.03s, startup_resource_retire_hardware_debug1 0.04s, startup_resource_retire_views_warp_debug1 0.03s, startup_resource_retire_title_hardware_debug1 0.03s, startup_resource_retire_views_hardware_debug1 0.03s, startup_indirect_draw_indexed_hardware 0.03s, startup_compute_request_warp 2.70s); skipped (capability, not a pass): startup_resource_retire_warp_debug1, startup_resource_retire_title_warp_debug1, startup_resource_retire_hardware_debug1, startup_resource_retire_views_warp_debug1, startup_resource_retire_title_hardware_debug1, startup_resource_retire_views_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): 50.99s (configure 1.90s)
    • Test (ctest --preset release -R '^startup_gfx2hlsl_fma_mix|^startup_gfx2hlsl_(fract|buffer_store|const_copy)|shader_cache_key|compute_request|packed_position|indirect_draw_indexed|resource_retire' --parallel 6 --no-tests=error --output-on-failure): 35/42 passed (2.89s: startup_gfx2hlsl_fract_draw_warp 0.59s, startup_gfx2hlsl_fract_draw_hardware 0.60s, startup_shader_cache_key 0.65s, startup_resource_retire_views_warp_debug0 0.11s, startup_gfx2hlsl_buffer_store 0.73s, startup_compute_request_offline 0.73s, startup_resource_retire_views_hardware_debug0 0.07s, startup_resource_retire_warp_debug0 0.10s, startup_resource_retire_off_warp 0.10s, startup_resource_retire_views_noretire_warp 0.07s, startup_resource_retire_reasons_no_retire_warp 0.09s, startup_resource_retire_reasons_warp 0.07s, startup_resource_retire_views_keep_warp 0.08s, startup_resource_retire_reasons_off_warp 0.08s, startup_gfx2hlsl_fma_mix_dispatch_warp 0.49s, startup_gfx2hlsl_fract_draw_controls 0.24s, startup_gfx2hlsl_fract 0.26s, startup_gfx2hlsl_fma_mix_dispatch_hardware 0.49s, startup_indirect_draw_indexed_warp 0.11s, startup_resource_retire_views_keep_hardware 0.08s, startup_gfx2hlsl_fma_mix 0.13s, startup_compute_request_disabled_control 0.23s, startup_gfx2hlsl_fma_mix_fxc 0.18s, startup_resource_retire_title_warp_debug0 0.11s, startup_gfx2hlsl_const_copy_join 0.13s, startup_resource_retire_off_hardware 0.09s, startup_resource_retire_hardware_debug0 0.09s, startup_resource_retire_reasons_no_retire_hardware 0.08s, startup_resource_retire_title_hardware_debug0 0.11s, startup_resource_retire_title_off_hardware 0.08s, startup_resource_retire_reasons_hardware 0.08s, startup_resource_retire_views_noretire_hardware 0.08s, startup_resource_retire_warp_debug1 0.03s, startup_resource_retire_hardware_debug1 0.03s, startup_resource_retire_reasons_off_hardware 0.08s, startup_resource_retire_title_warp_debug1 0.03s, startup_resource_retire_title_off_warp 0.10s, startup_resource_retire_title_hardware_debug1 0.03s, startup_resource_retire_views_warp_debug1 0.03s, startup_resource_retire_views_hardware_debug1 0.03s, startup_indirect_draw_indexed_hardware 0.03s, startup_compute_request_warp 2.76s); skipped (capability, not a pass): startup_resource_retire_warp_debug1, startup_resource_retire_hardware_debug1, startup_resource_retire_title_warp_debug1, startup_resource_retire_title_hardware_debug1, startup_resource_retire_views_warp_debug1, startup_resource_retire_views_hardware_debug1, startup_indirect_draw_indexed_hardware
  • Windows Python checks: tests/mesh_heap_sampler_check.py ok (0.6s), tests/startup_buffer_address_check.py ok (0.4s), tests/startup_buffer_format_word3_table_check.py ok (0.1s), tests/startup_compute_global_load_check.py ok (0.2s), tests/startup_compute_request_offline_check.py ok (0.5s), tests/startup_compute_store_format_xyzw_check.py ok (0.2s), tests/startup_compute_translate_check.py ok (0.2s), tests/startup_flat_param_check.py ok (0.1s), tests/startup_gfx2hlsl_add_sub_co_check.py ok (0.1s), tests/startup_gfx2hlsl_add_sub_co_ci_check.py ok (0.1s), tests/startup_gfx2hlsl_bfm_b64_check.py ok (1.1s), tests/startup_gfx2hlsl_bit_alu_check.py ok (0.6s), tests/startup_gfx2hlsl_capture_check.py ok (1.0s), tests/startup_gfx2hlsl_cmp16_check.py ok (0.3s), tests/startup_gfx2hlsl_cmp_class_f16_sdwa_check.py ok (26.5s), tests/startup_gfx2hlsl_compare_dst_check.py ok (0.1s), tests/startup_gfx2hlsl_compute_check.py ok (0.7s), tests/startup_gfx2hlsl_compute_mask_spill_check.py ok (2.2s), tests/startup_gfx2hlsl_compute_spill_check.py ok (1.7s), tests/startup_gfx2hlsl_const_copy_join_check.py ok (0.1s), tests/startup_gfx2hlsl_cselect_mask_check.py ok (0.1s), tests/startup_gfx2hlsl_cvt_alu_check.py ok (0.2s), tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py ok (0.2s), tests/startup_gfx2hlsl_cvt_pk_int16_check.py ok (0.3s), tests/startup_gfx2hlsl_f32_lg_check.py ok (1.1s), tests/startup_gfx2hlsl_f32_nan_check.py ok (4.0s), tests/startup_gfx2hlsl_fma_mix_check.py ok (0.1s), tests/startup_gfx2hlsl_getpc_refusal_check.py ok (0.1s), tests/startup_gfx2hlsl_handler_arity_check.py ok (0.1s), tests/startup_gfx2hlsl_heap_record_check.py ok (0.2s), tests/startup_gfx2hlsl_heap_word_check.py ok (0.2s), tests/startup_gfx2hlsl_image_arity_check.py ok (0.1s), tests/startup_gfx2hlsl_image_get_lod_check.py ok (0.2s), tests/startup_gfx2hlsl_int_med3_check.py ok (0.1s), tests/startup_gfx2hlsl_interp_p2_hi_check.py ok (0.2s), tests/startup_gfx2hlsl_join_symbols_check.py ok (0.2s), tests/startup_gfx2hlsl_loop_constants_check.py ok (0.1s), tests/startup_gfx2hlsl_mask_data_check.py ok (0.1s), tests/startup_gfx2hlsl_mask_overwrite_check.py ok (0.1s), tests/startup_gfx2hlsl_operand_count_check.py ok (0.3s), tests/startup_gfx2hlsl_pack_absdiff_check.py ok (2.0s), tests/startup_gfx2hlsl_pack_b32_f16_check.py ok (0.1s), tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py ok (0.1s), tests/startup_gfx2hlsl_readfirstlane_recompute_check.py ok (0.1s), tests/startup_gfx2hlsl_readfirstlane_uniform_check.py ok (0.1s), tests/startup_gfx2hlsl_refusals_check.py ok (0.5s), tests/startup_gfx2hlsl_root_check.py ok (0.7s), tests/startup_gfx2hlsl_s_abs_check.py ok (0.1s), tests/startup_gfx2hlsl_s_bitset_check.py ok (0.1s), tests/startup_gfx2hlsl_s_ff0_check.py ok (0.1s), tests/startup_gfx2hlsl_s_ff1_check.py ok (0.1s), tests/startup_gfx2hlsl_s_flbit_check.py ok (0.1s), tests/startup_gfx2hlsl_s_quadmask_b64_check.py ok (0.1s), tests/startup_gfx2hlsl_s_quadmask_check.py ok (0.1s), tests/startup_gfx2hlsl_sad_check.py ok (0.3s), tests/startup_gfx2hlsl_sad_hi_msad_check.py ok (0.3s), tests/startup_gfx2hlsl_saveexec_same_reg_check.py ok (0.1s), tests/startup_gfx2hlsl_scalar_arity_check.py ok (0.2s), tests/startup_gfx2hlsl_scalar_brev_fold_check.py ok (0.1s), tests/startup_gfx2hlsl_scalar_m0_check.py ok (0.1s), tests/startup_gfx2hlsl_scalar_null_check.py ok (0.2s), tests/startup_gfx2hlsl_scalar_table_arity_check.py ok (0.3s), tests/startup_gfx2hlsl_sdwa_cndmask_check.py ok (0.2s), tests/startup_gfx2hlsl_sdwa_cvt_check.py ok (0.2s), tests/startup_gfx2hlsl_sgpr_spill_check.py ok (0.1s), tests/startup_gfx2hlsl_shift64_check.py ok (0.2s), tests/startup_gfx2hlsl_sload_const_offset_check.py ok (0.1s), tests/startup_gfx2hlsl_smem_offset_check.py ok (0.1s), tests/startup_gfx2hlsl_sop2_complement_check.py ok (4.7s), tests/startup_gfx2hlsl_sopk_accum_check.py ok (1.0s), tests/startup_gfx2hlsl_sopk_signext_check.py ok (0.1s), tests/startup_gfx2hlsl_spill_branches_check.py ok (0.9s), tests/startup_gfx2hlsl_swap_check.py ok (0.3s), tests/startup_gfx2hlsl_vopc_sdwa_check.py ok (3.8s), tests/startup_gfx2hlsl_waitcnt_depctr_check.py ok (0.1s), tests/startup_gfx2hlsl_watch_check.py ok (2.5s), tests/startup_image_2d_array_check.py ok (0.2s), tests/startup_image_3d_check.py ok (0.1s), tests/startup_image_gather_lz_check.py ok (0.5s), tests/startup_image_load_d16_check.py ok (0.8s), tests/startup_image_sample_a16_check.py ok (0.2s), tests/startup_image_sample_b_check.py ok (0.2s), tests/startup_image_sample_c_check.py ok (0.2s), tests/startup_image_sample_l_check.py ok (0.2s), tests/startup_image_sample_lod_check.py ok (0.5s), tests/startup_inline_sampler_check.py ok (0.1s), tests/startup_mesh57_depth_only_check.py ok (0.2s), tests/startup_mesh57_lane_spill_check.py ok (0.2s), tests/startup_mesh57_loop_check.py ok (0.2s), tests/startup_mesh57_loop_refusal_check.py ok (0.1s), tests/startup_mesh57_skeleton_check.py ok (1.0s), tests/startup_run_ps1_check.py ok (11.6s), tests/startup_run_sh_check.py skipped (0.1s), tests/startup_sampler_heap_check.py ok (0.1s), tests/startup_shader_cache_rekey_check.py ok (2.2s), tests/startup_smem_address_check.py ok (0.1s), tests/startup_stream_fetch_check.py ok (0.2s), tests/startup_stream_interface_check.py ok (0.2s), tests/startup_translator_check.py ok (0.4s)
  • sccache: 4/12 hits; wall 171.4s
  • Real-GPU run not available (RunPod stopped).

AI-assisted; commands listed were executed locally.

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.

2 participants