From 6854aa059a44421a61a94c8b1c50f8a9e68b929d Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 03:54:37 +0100 Subject: [PATCH] startup: prove v_med3_f32 results uniform for v_readfirstlane_b32 The uniformity proof behind v_readfirstlane_b32 did not follow v_med3_f32, so a scalar clamped with a median of three (for example v_med3_f32 v14, s44, 0, 0.5) never reached a proven read. The median is a per-lane function of its three inputs, like the multiply and fused multiply-add already in the set. The check accepts the clamp form and a form with negated, absolute and clamped scalar sources. It refuses a median with a lane-varying source or a lane-mask source. Co-Authored-By: Claude Fable 5.2 --- docs/experiments.md | 4 ++-- tests/startup_gfx2hlsl_readfirstlane_uniform_check.py | 7 +++++++ tools/startup/gfx2hlsl.py | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index ac23ae63db..01c36816da 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -12045,13 +12045,13 @@ python -B tests/startup_gfx2hlsl_scalar_m0_check.py binary llvm-mc-15 The startup shaders are compiled with FXC (shader model 5), which has no wave intrinsics. The translator lowers `v_readfirstlane_b32 sD, vS` to `sD = vS` only when it proves that `vS` holds the same value in every lane. The previous proof covered only a `v_cvt_i32_f32` of a fixed buffer word. An unproven source still produces `v_readfirstlane_b32 requires a proven uniform source` (see [issue #1723](https://github.com/R0Y00/Xbox2Windows/issues/1723)). -In straight-line code with a known `exec`, the proof follows `v_cvt_u32_f32`, `v_cvt_i32_f32`, `v_cvt_f32_u32`, `v_cvt_f32_ubyte0`, `v_mul_f32`, `v_fma_f32`, `v_fmaak_f32`, `v_rcp_f32`, and `v_mov_b32`, including e32 and e64 forms. Every source must be uniform. The float operations that lower operands as floats accept negation and absolute-value source modifiers. The float-result forms accept the modeled output modifiers `clamp`, `mul:2`, `mul:4`, and `div:2`. +In straight-line code with a known `exec`, the proof follows `v_cvt_u32_f32`, `v_cvt_i32_f32`, `v_cvt_f32_u32`, `v_cvt_f32_ubyte0`, `v_mul_f32`, `v_fma_f32`, `v_fmaak_f32`, `v_rcp_f32`, `v_med3_f32`, and `v_mov_b32`, including e32 and e64 forms. Every source must be uniform. The float operations that lower operands as floats accept negation and absolute-value source modifiers. The float-result forms accept the modeled output modifiers `clamp`, `mul:2`, `mul:4`, and `div:2`. Uniform sources include inline constants and literals, SGPRs holding constants, user data or fixed buffer words, other SGPRs tracked as uniform, and VGPRs with an existing proof. A scalar result is tracked as varying when its expression depends on `vcc`, `exec`, `m0`, a varying scalar register, a VGPR without a proof, or a varying `scc`. Descriptor, pointer, and sampler symbols, and SGPRs holding a lane mask (a compare result, even from uniform operands), are treated as non-uniform sources. A 64-bit move proves its high word only from the source high word as it was before the move, never when the destination overlaps it, and a descriptor-path move carries a high-word symbol only when that symbol does not depend on the register's run-time contents. SCC follows the same dependency tracking, with saveexec and wrexec marking it varying. A scalar write to `m0` that also sets SCC judges SCC from the write's sources, so `s_and_b32 m0, vcc_lo, -1` makes it varying while a write from uniform scalars makes it uniform. Instructions whose SCC result is not modelled (`s_add`, `s_sub`, `s_min` and `s_max`, `s_wqm`, and scalar ops on symbolic descriptor words, which emit nothing) mark SCC varying. Scalar buffer loads track the original offset across every destination word, including when the emitted code saves it in a temporary. An overwrite clears the destination's old proof unless the new value is proven uniform. This applies to SDWA forms and all three interpolation forms (`v_interp_p1_f32`, `v_interp_p2_f32`, and `v_interp_mov_f32`). Either WQM form, and restoring `exec` from a saved copy, clears all existing VGPR proofs because they can activate helper lanes. The read requires known `exec`, a branch-free program, and no modifiers. It now explicitly checks for a single SGPR destination and a single VGPR source. -`tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` translates short sequences through the stage's dispatch without shader machine code. It checks emitted assignments for the listed operations, supported modifiers, and in-place and cross-register chains, including the unsigned-division reciprocal estimate (`v_cvt_f32_u32`, `v_rcp_f32`, `v_mul_f32` by `0x4f7ffffe`, `v_cvt_u32_f32`) that compiled shaders read back with `v_readfirstlane_b32`. That chain stays refused from a lane-varying source or a lane mask, across an `exec` write or an overwrite, and in a program with branches. Refusal cases cover varying sources and offsets, overwrites that invalidate a proof, WQM, a restored `exec` copy and other exec writes, lane masks, overlapping and descriptor-path 64-bit moves, branch and operand restrictions, and constructor-less probes with incomplete tracking state. The negative control empties the allowlist and requires every accepted sequence to refuse. +`tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` translates short sequences through the stage's dispatch without shader machine code. It checks emitted assignments for the listed operations, supported modifiers, and in-place and cross-register chains, the median of three that clamps a scalar to `[0, 0.5]`, including the unsigned-division reciprocal estimate (`v_cvt_f32_u32`, `v_rcp_f32`, `v_mul_f32` by `0x4f7ffffe`, `v_cvt_u32_f32`) that compiled shaders read back with `v_readfirstlane_b32`. That chain stays refused from a lane-varying source or a lane mask, across an `exec` write or an overwrite, and in a program with branches. Refusal cases cover varying sources and offsets, overwrites that invalidate a proof, WQM, a restored `exec` copy and other exec writes, lane masks, overlapping and descriptor-path 64-bit moves, branch and operand restrictions, and constructor-less probes with incomplete tracking state. The negative control empties the allowlist and requires every accepted sequence to refuse. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_uniform$' -V diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index b9fd8b974a..1e800d9180 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -79,6 +79,11 @@ def run(*instructions, exec_known=True): ['v7 = U(F(s2) * 2.0 + F(0x3f800000u));']), ('reciprocal', ('v_rcp_f32_e32 v7, s2',), ['v7 = U(1.0 / F(s2));']), + # The clamp to [0, 0.5] of a scalar, as compiled shaders write it. + ('median of three', ('v_med3_f32 v7, s2, 0, 0.5',), + ['v7 = U(med3(F(s2), F(0x00000000u), 0.5));']), + ('median of three scalar sources', ('v_med3_f32 v7, s2, -s3, |s2| clamp',), + ['v7 = U(saturate(med3(F(s2), (-F(s3)), abs(F(s2)))));']), ('move', ('v_mov_b32_e32 v7, s2',), ['v7 = s2;']), ('literal move', ('v_mov_b32_e32 v7, 0x1234',), ['v7 = 0x00001234u;']), ('chain with an in-place write', ('v_mul_f32_e64 v7, s2, 2.0', 'v_cvt_u32_f32_e32 v7, v7'), @@ -114,6 +119,8 @@ def run(*instructions, exec_known=True): ('varying scalar buffer offset', (COMPARE, 's_buffer_load_dword s3, s[4:7], s2', 'v_mov_b32_e32 v7, s3', READ)), ('listed op with varying source', (MOVE, 'v_mul_f32_e64 v7, v0, 2.0', READ)), + ('median of three with a varying source', (MOVE, 'v_med3_f32 v7, s2, v0, 0.5', READ)), + ('median of three of a lane mask', (COMPARE, 'v_med3_f32 v7, s2, 0, 0.5', READ)), ('op_sel overwrite', (MOVE, 'v_mul_f32_e64 v7, s2, 2.0 op_sel:[0,0]', READ)), ('modified move overwrite', (MOVE, 'v_mov_b32_e32 v7, s2 clamp', READ)), ('multi-register read destination', (MOVE, 'v_readfirstlane_b32 s[8:9], v7')), diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 1df448f66b..9aa3d47dcb 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -420,11 +420,11 @@ def smem_mods(mods): # Each conversion uses only its input bits (including byte extraction for ubyte0). -# Multiply, fused multiply-add (including the literal form), and reciprocal use only -# local arithmetic. Move copies its input bits. None reads a lane ID or another lane. +# Multiply, fused multiply-add (including the literal form), reciprocal and median of three use +# only local arithmetic. Move copies its input bits. None reads a lane ID or another lane. UNIFORM_VALU = frozenset(( 'v_cvt_u32_f32', 'v_cvt_i32_f32', 'v_cvt_f32_u32', 'v_cvt_f32_ubyte0', 'v_mul_f32', - 'v_fma_f32', 'v_fmaak_f32', 'v_rcp_f32', 'v_mov_b32', + 'v_fma_f32', 'v_fmaak_f32', 'v_rcp_f32', 'v_med3_f32', 'v_mov_b32', ))