Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ jobs:
tests/startup_compute_request_offline_check.py \
tests/startup_gfx2hlsl_scalar_m0_check.py \
tests/startup_gfx2hlsl_readfirstlane_uniform_check.py \
tests/startup_gfx2hlsl_readfirstlane_recompute_check.py \
tests/startup_triage_run_check.py \
tests/startup_run_ps1_check.py \
tests/startup_flat_param_check.py \
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ python3 -m venv out/pe-env
./out/pe-env/bin/python -B tests/startup_compute_request_offline_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_scalar_m0_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sop2_complement_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sdwa_cvt_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sopk_accum_check.py
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,9 @@ if(EXISTS "${INTAKE_PYTHON}")
# 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")
# v_readfirstlane_b32 past the entry block of a branched program: recomputed definition, joins and EXEC refused.
add_test(NAME startup_gfx2hlsl_readfirstlane_recompute COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py")
# Startup shader translator: read-only census of every refusal site (--refusals).
add_test(NAME startup_gfx2hlsl_refusals COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_refusals_check.py")
Expand Down
15 changes: 12 additions & 3 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -12184,12 +12184,21 @@ 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` 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.
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 takes no modifiers. In straight-line code and in the entry block of a program with branches (before the first branch target and before the first terminator), the proof and the read require known `exec` and the read copies the VGPR; past the entry block the read recomputes the definition instead (below). Every path reaches those instructions through the same instructions, so the program-order tracking is exact there. A branch back to the first instruction ends the entry block at the first instruction, because a loop re-enters it. 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 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.
Past the entry block of a program with branches, the read does not copy the VGPR. It recomputes the uniform expression that defined the VGPR, for example `s9 = U(F(s3) * 2.0)` after `v_mul_f32 v7, s3, 2.0`. The value read is the first active lane's, and that lane ran the definition when nothing between the definition and the read could change which lanes ran it or what they computed:

- no branch target lies between them (a join, a loop head or a back edge): a branch target clears every VGPR proof;
- no instruction between them writes `exec`, explicitly or implicitly (`saveexec`, `wrexec`, `v_cmpx`, WQM, `s_movreld`); narrowing is refused as well as widening;
- the VGPR is not written in between, and no SGPR that the expression reads is written in between (VOP3b carry-out destinations count);
- every SGPR source was proven uniform since the last branch target, so a value from another path cannot reach it; an SCC from before the last branch target is not trusted.

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.

```powershell
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_uniform$' -V
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_(uniform|recompute)$' -V
```

## Desktop audio render sink
Expand Down
128 changes: 128 additions & 0 deletions tests/startup_gfx2hlsl_readfirstlane_recompute_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""Check v_readfirstlane_b32 past the entry block of a program with branches, without shader machine code.

There the read recomputes the uniform expression that defined its VGPR. It is accepted only when no branch
target (a join or a loop head), EXEC write, overwrite of the VGPR, or write of a scalar the expression reads
lies between the definition and the read, and every scalar source was itself written since the last join.
"""
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tools', 'startup'))
import gfx2hlsl as G # noqa: E402

failures = []


def check(label, condition, detail=''):
print(f"{label}: {'ok' if condition else 'MISMATCH ' + detail}")
if not condition:
failures.append(label)


class Pipe:
def resource(self, key, kind):
return 'r0'


def run(*instructions):
"""Translate disassembly text through the same dispatch and block split as a real stage."""
s = G.Stage.__new__(G.Stage)
s.pipe, s.lines, s.attrs = Pipe(), [], {}
s.sym = {4 + i: ('desc', ('table', 0), 0, i) for i in range(4)}
s.used_s, s.used_v, s.masks = set(), set(), set()
s.exec_known = True
s.ins = [(4 * i, 4, asm.split()[0], asm) for i, asm in enumerate(instructions)]
try:
s.translate()
except G.Unsupported as error:
return str(error)
return [line for _, lines, _ in s.blocks for line in lines]


REFUSAL = 'v_readfirstlane_b32 requires a proven uniform source'
READ = 'v_readfirstlane_b32 s9, v7'
# Offset 0x8 is a join: the branch at 0x0 skips the instruction at 0x4.
JOIN = ('s_cbranch_scc0 label_0008', 's_nop 0')
LOAD = 's_buffer_load_dword s3, s[4:7], 0x10' # a uniform scalar written after the join
DEFINE = 'v_mul_f32_e64 v7, s3, 2.0'
RECOMPUTED = 's9 = U(F(s3) * 2.0);'


def read_line(lines):
return lines[-1] if isinstance(lines, list) and lines else lines


ACCEPTED = [
('definition and read after a join', JOIN + (LOAD, DEFINE, READ)),
# The read does not copy this lane's VGPR, so an EXEC write before the definition does not matter.
('EXEC narrowed before the definition', JOIN + ('s_and_saveexec_b32 s8, s2', LOAD, DEFINE, READ)),
('EXEC restored before the definition', JOIN + ('s_mov_b32 exec_lo, s8', LOAD, DEFINE, READ)),
# A conditional branch between them is a fall-through, not a join: one path reaches the read.
('fall-through between definition and read', JOIN + (LOAD, DEFINE, 's_cbranch_scc1 label_0020', READ,
'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)),
]
EXPECTED = {'chain through a second VGPR': 's9 = U(F((s3)) * 2.0);'}
for label, instructions in ACCEPTED:
got = run(*instructions)
check(f'{label} accepted', read_line(got) == EXPECTED.get(label, RECOMPUTED), repr(got))

REFUSED = [
# The first active lane at the read may not have run the definition.
('EXEC widened between definition and read', JOIN + (LOAD, 's_and_saveexec_b32 s8, s2', DEFINE,
's_mov_b32 exec_lo, s8', READ)),
('EXEC narrowed between definition and read', JOIN + (LOAD, DEFINE, 's_and_saveexec_b32 s8, s2', READ)),
('compare writes EXEC between definition and read', JOIN + (LOAD, DEFINE, 'v_cmpx_lt_f32_e32 v0, v1', READ)),
('WQM between definition and read', JOIN + (LOAD, DEFINE, 's_wqm_b32 exec_lo, exec_lo', READ)),
# The recomputed expression would read a newer scalar.
('scalar input rewritten in between', JOIN + (LOAD, DEFINE, 's_mov_b32 s3, 0', READ)),
('scalar input reloaded in between', JOIN + (LOAD, DEFINE, 's_buffer_load_dword s3, s[4:7], 0x20', READ)),
('scalar input pair rewritten in between', JOIN + (LOAD, DEFINE, 's_mov_b64 s[2:3], 0', READ)),
# A loop head between them: a later iteration reaches the read without the definition.
('loop back edge between definition and read', JOIN + (LOAD, DEFINE, 's_nop 0', READ,
's_cbranch_scc0 label_0014', 's_endpgm')),
# A join where two definitions reach.
('join of two definitions', ('s_cbranch_scc0 label_0014', LOAD, DEFINE, 's_branch label_0014',
'v_mov_b32_e32 v7, 0', READ, 's_endpgm')),
# A scalar written before the last join can hold another path's value.
('scalar written before the join', (LOAD,) + JOIN + (DEFINE, READ)),
# A second join (here a loop head at the definition) resets the since-join marks again: on the next iteration
# s3 holds a lane mask.
('scalar written before the second join', JOIN + (LOAD, DEFINE, READ, 's_mov_b32 s3, vcc_lo',
's_cbranch_scc0 label_000C', 's_endpgm')),
('lane-varying source', JOIN + ('v_mul_f32_e64 v7, v0, 2.0', READ)),
('lane mask source', JOIN + ('v_cmp_lt_f32_e64 s3, v0, v1', DEFINE, READ)),
('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)),
('select on an SCC from before the join', JOIN + ('s_cselect_b32 s3, 1, 0', DEFINE, READ)),
]
for label, instructions in REFUSED:
got = run(*instructions)
check(f'{label} refused', got == REFUSAL, repr(got))

# Writes the translator does not route through the first operand.
probe = G.Stage.__new__(G.Stage)
for name, ops, regs in (('v_add_co_u32_e64', ['v1', 's3', 'v2', 'v3'], {3}),
('v_div_scale_f32', ['v1', 's3', 'v2', 'v3', 'v4'], {3}),
('s_movreld_b32', ['s0', 's2'], {3, 2}),
('s_cmp_eq_u32', ['s3', '0'], set())):
probe.uniform_expr, probe.since_join = {7: 'U(F(s3) * 2.0)', 8: 'U(F(s2) * 2.0)'}, {2, 3}
probe.forget_writes(name, ops)
check(f'{name} forgets s{sorted(regs)}', probe.since_join == {2, 3} - regs and
set(probe.uniform_expr) == {7, 8} - {7 if 3 in regs else 0, 8 if 2 in regs else 0},
repr((probe.since_join, probe.uniform_expr)))
for name, ops, expected in (('s_mov_b64', ['exec', 's[2:3]'], True), ('s_mov_b32', ['exec_hi', 's2'], True),
('s_load_dwordx2', ['s[126:127]', 's[2:3]', '0x0'], True),
('s_mov_b32', ['s125', 's2'], False), ('s_mov_b32', ['s126', 's2'], True),
('s_mov_b64', ['s[124:125]', 's[2:3]'], False),
('s_or_saveexec_b64', ['s[2:3]', 's[4:5]'], True),
('s_andn2_wrexec_b32', ['s2', 's4'], True), ('v_cmpx_lt_f32_e32', ['v0', 'v1'], True),
('s_movreld_b32', ['s0', 's2'], True), ('s_mov_b32', ['s2', 'exec_lo'], False),
('v_add_co_u32_e64', ['v1', 'exec_lo', 'v2', 'v3'], True),
('v_mul_f32_e64', ['v1', 'exec_lo', 'v2'], False), ('s_cmp_eq_u32', ['exec_lo', '0'], False)):
check(f'{name} {ops[0]} writes EXEC: {expected}', G.writes_exec(name, ops) == expected)

print('Readfirstlane recompute checks: %d failed' % len(failures))
sys.exit(1 if failures else 0)
5 changes: 4 additions & 1 deletion tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def run(*instructions, exec_known=True):
'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',
Expand All @@ -165,6 +164,10 @@ def run(*instructions, exec_known=True):
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))
# Past the entry block the read recomputes the definition instead of copying the VGPR
# (tests/startup_gfx2hlsl_readfirstlane_recompute_check.py covers that rule).
got = run(MOVE, 's_cbranch_scc0 label_0010', READ, 's_endpgm', 's_endpgm')
check('read after the first terminator recomputed', got[:2] == ['v7 = s2;', 's9 = s2;'], 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:
Expand Down
Loading
Loading