diff --git a/CMakeLists.txt b/CMakeLists.txt index df192b9fc..44f03f543 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2682,7 +2682,7 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: the SMEM offset 'off' is the literal 0; m0 and other operands keep their handling (#1674). add_test(NAME startup_gfx2hlsl_smem_offset COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_smem_offset_check.py") - # Startup shader translator: uniform sources of v_readfirstlane_b32 in straight-line code (#1723). + # Startup shader translator: uniform sources of v_readfirstlane_b32 in straight-line code and entry blocks (#1723). add_test(NAME startup_gfx2hlsl_readfirstlane_uniform COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py") # Startup shader translator: read-only census of every refusal site (--refusals). diff --git a/docs/experiments.md b/docs/experiments.md index ac23ae63d..8c38206fd 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -12049,9 +12049,9 @@ In straight-line code with a known `exec`, the proof follows `v_cvt_u32_f32`, `v 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. +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` and no modifiers. In a program with branches, the proof and the read are accepted only in the entry block: before the first branch target and before the first terminator. Every path reaches those instructions through the same instructions, so the program-order tracking is exact there. A branch back to the first instruction makes the whole program refuse, because a loop re-enters the entry block. 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, 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, after the first terminator, or in an entry block re-entered by a loop), 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 b9fd8b974..b5b7dd242 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -1,4 +1,4 @@ -"""Check straight-line v_readfirstlane_b32 lowering without shader machine code.""" +"""Check v_readfirstlane_b32 lowering in straight-line code and entry blocks, without shader machine code.""" import os import sys @@ -130,20 +130,41 @@ def run(*instructions, exec_known=True): ('interpolant overwrite', ('v_mov_b32_e32 v7, s2', 'v_interp_mov_f32 v7, p0, attr0.x', READ)), ('interpolation p1 overwrite', (MOVE, 'v_interp_p1_f32_e32 v7, v0, attr0.x', READ)), ('VGPR buffer address', (MOVE, 'buffer_load_dword v7, v0, s[4:7], 0 offen', READ)), - ('branch after read', ('v_mov_b32_e32 v7, s2', READ, 's_branch label_000C', 's_endpgm')), # The unsigned-to-float conversion proves nothing from a lane-varying source, across an exec - # write or an overwrite, or in a program with branches (a definition in another block included). + # write or an overwrite, or from a definition in another block. ('unsigned to float conversion of an entry VGPR', ('v_cvt_f32_u32_e32 v7, v0', READ)), ('unsigned to float conversion of a mask', (COMPARE, 'v_cvt_f32_u32_e32 v7, s2', READ)), ('exec write inside the chain', ('v_cvt_f32_u32_e32 v7, s3', 's_and_saveexec_b32 s8, s2', 'v_rcp_f32_e32 v7, v7', READ)), ('overwrite inside the chain', ('v_cvt_f32_u32_e32 v7, s3', 'v_add_f32_e64 v7, v0, 1.0', 'v_rcp_f32_e32 v7, v7', READ)), - ('chain in a program with branches', ('v_cvt_f32_u32_e32 v7, s3', 'v_rcp_f32_e32 v7, v7', READ, - 's_cbranch_scc0 label_0010', 's_endpgm')), ('definition in another block', ('v_cvt_f32_u32_e32 v7, s3', 's_cbranch_scc0 label_0008', 'v_rcp_f32_e32 v7, v7', READ, 's_endpgm')), + # A program with branches proves only before its first branch target or terminator (the entry block). + ('read at the first branch target', (MOVE, 's_cbranch_scc0 label_000C', 's_nop 0', READ, 's_endpgm')), + ('read after the first terminator', (MOVE, 's_cbranch_scc0 label_0010', READ, 's_endpgm', 's_endpgm')), + ('entry block re-entered by a loop', (MOVE, READ, 's_mov_b32 s2, vcc_lo', 's_cbranch_scc0 label_0000', + 's_endpgm')), + ('exec write in the entry block', (MOVE, 's_and_saveexec_b32 s8, s2', READ, 's_branch label_0010', + 's_endpgm')), + ('mask in the entry block', (COMPARE, MOVE, READ, 's_branch label_0010', 's_endpgm')), + # A branch target ends the entry block even before any terminator: a later branch can come back to it. + ('read in a loop whose header is in the entry block', + (MOVE, READ, 'v_mov_b32_e32 v7, v0', 's_cbranch_scc0 label_0004', 's_endpgm')), + ('read at a backward branch target', (MOVE, 's_nop 0', READ, 'v_mov_b32_e32 v7, v0', 's_branch label_0008', + 's_endpgm')), + ('read after s_endpgm in a branch-free program', (MOVE, 's_endpgm', READ)), ] +# The entry block of a program with branches: every path reaches it through the same instructions, so a read +# there is proven like straight-line code, up to the instruction just before the first branch target. +for label, instructions in (('branch after read', (MOVE, READ, 's_branch label_000C', 's_endpgm')), + ('read before the first branch target', + (MOVE, READ, 's_nop 0', 's_cbranch_scc0 label_0008', 's_endpgm'))): + got = run(*instructions) + check(f'{label} accepted', got[:2] == ['v7 = s2;', 's9 = v7;'], repr(got)) +got = run('v_cvt_f32_u32_e32 v7, s3', 'v_rcp_f32_e32 v7, v7', READ, 's_cbranch_scc0 label_0010', 's_endpgm') +check('chain in the entry block accepted', got[:3] == ['v7 = U(float(s3));', 'v7 = U(1.0 / F(v7));', 's9 = v7;'], + repr(got)) # Since #1755 the float lowering itself refuses an op_sel it does not model, before the read. EARLIER = {'op_sel overwrite': 'modifier op_sel:[0,0]'} for label, instructions in REFUSED: diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 1df448f66..3b7f97841 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -702,6 +702,10 @@ def translate(self): self.blocks.append((start, self.lines, None)) self.lines, start = [], off self.pc = off + # Before the first branch target or terminator every path has run exactly the instructions above, so + # the program-order tracking is exact there even in a program with branches. Code after s_endpgm in a + # branch-free program is past it too (dead code). + self.past_entry = bool(self.blocks) or 0 in targets try: term = self.one(name, asm) except Unsupported as reason: @@ -752,7 +756,7 @@ def one(self, name, asm): dst = None # Unit probes build a Stage without __init__: without the tracking state, prove nothing. if (base in UNIFORM_VALU and ops and self.tracks_uniformity() and - not getattr(self, 'has_branches', True) and getattr(self, 'exec_known', False) and + not getattr(self, 'past_entry', True) and getattr(self, 'exec_known', False) and (not mods or (base in VEC_F or base in VEC_U_TO_F) and all(m in ('clamp', 'mul:2', 'mul:4', 'div:2') for m in mods))): candidate = reg_range(ops[0]) @@ -1730,7 +1734,7 @@ def op_v_cvt_i32_f32(self, ops, mods): def op_v_readfirstlane_b32(self, ops, mods): src, dst = reg_range(ops[1]), reg_range(ops[0]) - if (mods or not self.tracks_uniformity() or getattr(self, 'has_branches', True) or + if (mods or not self.tracks_uniformity() or getattr(self, 'past_entry', True) or not getattr(self, 'exec_known', False) or not dst or dst[0] != 's' or dst[1] != dst[2] or not src or src[0] != 'v' or src[1] != src[2] or src[1] not in self.uniform_v): raise Unsupported('v_readfirstlane_b32 requires a proven uniform source')