diff --git a/docs/experiments.md b/docs/experiments.md index 3831986964..0bb90d7e01 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -12305,7 +12305,7 @@ 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. @@ -12320,7 +12320,7 @@ Past the entry block of a program with branches, the read does not copy the VGPR A lane that was inactive at the definition keeps an older VGPR value; the recomputed expression gives every lane the first active lane's value instead. `exec` need not be known at the definition. A conditional branch between the definition and the read is a fall-through, not a join, and is accepted. The one case where hardware can differ is a read with no active lane: it returns lane 0's VGPR, which the program cannot rely on. `tests/startup_gfx2hlsl_readfirstlane_recompute_check.py` checks the accepted forms and refuses each condition above, including a definition under a narrower `exec` than the read, a rewritten scalar input, a loop back edge and a join where two definitions reach. -`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 from a definition in another block. 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 (a read at the first branch target, in a loop whose header is in the entry block, at a backward branch target, in an entry block re-entered by a loop, or after `s_endpgm` in a branch-free program; a read after the first terminator is recomputed), 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 from a definition in another block. 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 (a read at the first branch target, in a loop whose header is in the entry block, at a backward branch target, in an entry block re-entered by a loop, or after `s_endpgm` in a branch-free program; a read after the first terminator is recomputed), 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|recompute)$' -V diff --git a/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py b/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py index 9572b8885e..095ffa50ee 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py @@ -62,8 +62,10 @@ def read_line(lines): 's_endpgm')), ('chain through a second VGPR', JOIN + (LOAD, 'v_mov_b32_e32 v6, s3', 'v_mul_f32_e64 v7, v6, 2.0', READ)), ('unrelated scalar write in between', JOIN + (LOAD, DEFINE, 's_mov_b32 s2, 0', READ)), + ('median of three after a join', JOIN + (LOAD, 'v_med3_f32 v7, s3, 0, 0.5', READ)), ] -EXPECTED = {'chain through a second VGPR': 's9 = U(F((s3)) * 2.0);'} +EXPECTED = {'chain through a second VGPR': 's9 = U(F((s3)) * 2.0);', + 'median of three after a join': 's9 = U(med3(F(s3), F(0x00000000u), 0.5));'} for label, instructions in ACCEPTED: got = run(*instructions) check(f'{label} accepted', read_line(got) == EXPECTED.get(label, RECOMPUTED), repr(got)) @@ -96,6 +98,8 @@ def read_line(lines): ('VGPR overwritten in between', JOIN + (LOAD, DEFINE, 'v_add_f32_e64 v7, v0, 1.0', READ)), ('chain across an EXEC write', JOIN + (LOAD, 'v_mov_b32_e32 v6, s3', 's_and_saveexec_b32 s8, s2', 'v_mul_f32_e64 v7, v6, 2.0', READ)), + ('median of three across an EXEC write', JOIN + (LOAD, 'v_med3_f32 v7, s3, 0, 0.5', 's_and_saveexec_b32 s8, s2', + READ)), ('select on an SCC from before the join', JOIN + ('s_cselect_b32 s3, 1, 0', DEFINE, READ)), ] for label, instructions in REFUSED: diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index 674bc35b6d..eef9354837 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 51e647d8f2..2df86f31ed 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -435,11 +435,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', ))