diff --git a/docs/experiments/startup-gfx2hlsl-cselect-descnum.md b/docs/experiments/startup-gfx2hlsl-cselect-descnum.md new file mode 100644 index 0000000000..1e33a3f2fc --- /dev/null +++ b/docs/experiments/startup-gfx2hlsl-cselect-descnum.md @@ -0,0 +1,87 @@ +# Scalar select on descriptor-derived data in gfx2hlsl compute stages (#2618, the C500 rows) + +In 250 compute rows of the C500-01 and C500-02 batches, the recorded refusal is `numeric use of sX holding descnum` +at `s_cselect_b32 sX, 0, sX`. Before this change, `Stage.scalar_select` in `tools/startup/gfx2hlsl.py` read both +sources as run-time values. A number computed from a buffer descriptor (`descnum`) has none, so the select refused. + +## Contract + +A select between descriptor data and zero can disable a buffer by zeroing its size. On the console such a buffer +returns zeros. A lowering that keeps only the descriptor arm would read real data instead. So in a compute stage, +`s_cselect_b32 sD, a, b` into an SGPR, where one source holds `descnum` and each other source is a constant, emits +no HLSL. sD holds `('descnum', 'cselect', offset, arm a, arm b)`: both arms and the offset of the select, whose +SCC picks one. + +Nothing reads that symbol as a value: + +- A numeric read still refuses with `numeric use of sD holding descnum`. This covers VALU and SALU sources, compares, + m0, store data and lane writes. +- `s_mov_b32` and `s_mov_b64` copy the symbol. Any other scalar ALU instruction with it in any register of a source + (the high word of a 64-bit pair too) refuses with ` on a select of descriptor data` + (`Stage.refuse_select_source`). It would otherwise become a plain `descnum` or an in-place edit of a descriptor + word, which loses the arm. This also covers the structured-to-raw conversion when its stride comes from such a + select. +- An access whose descriptor operand has the symbol in any word refuses with + `descriptor operand s[N:M]: sK holds a select of descriptor data` (`Stage.select_word`, called by + `Stage.descriptor`). On main b505c32b a buffer load ignores edits to words 1–3 of its descriptor (#2901), so + without this check a copied select would reach the load with the zero arm dropped. +- In compute, an access whose descriptor operand holds a word joined at a branch target after its first word + refuses with `descriptor operand s[N:M]: sK holds a word joined at a branch target` (`Stage.joined_word`). A join + of different symbols leaves only that mark, so a select that reaches a descriptor word on one path, or through a + loop back edge, would otherwise be dropped. This also refuses compute joins that bring two different non-select + words there. Main translates those, ignoring the difference, as in #2901. +- The select still refuses with `scc from descriptor data in s_cselect_b32` when its SCC came from an ALU + instruction on descriptor data, because that SCC is not emitted. + +Unchanged: `s_cselect_b64`, an m0, exec or vcc destination, a raw descriptor word or a pointer as a source, a +run-time register next to the `descnum` source, and every non-compute stage. + +A row only gets past the select if sX is dead afterwards or is only copied. Where sX is read later, the refusal moves +to that read. The packets record only the first refusal, so how often each case occurs is not known yet. + +## Checks + +`tests/startup_gfx2hlsl_cselect_descnum_check.py` splices synthetic instructions into case A of +`tests/startup_gfx2hlsl_compute_check.py`. The decoder must print the expected text for each one. + +- Accepted: the select with its result dead, in both source orders, with a literal source and on an extracted + stride. The HLSL may differ from case A only by the compare's `scc` line, and sD must hold both arms and the offset. +- Refused, each checked by its reason: every later use listed above, a copy into word 1, 2 or 3 of the load + descriptor (also through a second copy), a copy into the store descriptor, and SCC from descriptor data. +- Branches: the select writes no HLSL variable, so a join must not turn sD back into a run-time register. A read + after a forward join where one path skips the select, and a read at a loop head reached by a back edge after the + select, both refuse with `numeric use of s21 holding joined at a branch target`. The same forward join with a + run-time write in place of the select translates. +- Descriptor words through a join, from the #2903 review: a load after a join where one path selects into word 2 + of its descriptor, and the same select at the bottom of a loop whose head is the load, refuse as joined words. + The same join with the loaded word on both paths, and the same loop without the select, translate. A select in + the high word of `s_or_b64 s[14:15], s[20:21], 0` refuses. +- Only buffer load and store operands are exercised for the descriptor-operand refusal. Image, sampler and + `s_buffer_load` operands go through the same `Stage.descriptor` check but have no control here. +- Still refused as before: the unchanged forms above, in the compute stage and in the vertex and pixel probe stages. +- Negative controls, each a mutated translator that must fail a named check: + - `zero-arm-dropped`: the select keeps only its `descnum` source. The structured-to-raw conversion with the + selected stride then translates. + - `load-check-removed`: `select_word` finds nothing. A load through a copied select then translates. + - `alu-check-removed`: `refuse_select_source` accepts everything. An ALU copy into word 2 then reaches the load. + - `join-check-removed`: `joined_word` finds nothing. The load after the join, and the load at the loop head, then + translate. + - `high-word-unchecked`: `refuse_select_source` sees only the low word of each source. The `s_or_b64` case then + translates. + +```powershell +python -B tests/startup_gfx2hlsl_cselect_descnum_check.py +ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_cselect_descnum_(controls|warp)$" -V +``` + +The `warp` and `hardware` tests translate case A with the dead select, compile it with DXC `cs_6_0`, dispatch it +and compare all 320 words with case A's reference. + +Measured on Windows 11 build 26300 with Python 3.14 at the commit that adds this note (base b505c32b): the controls +pass (41 first-pass checks, and each of the five controls fails its named check). On b505c32b the same file fails +20 of its 34 first-pass checks. The four accepted cases are refused at the select, and 16 later-use, branch and +review checks get that refusal instead of their own reason. The mutation phase cannot run there, because the helpers +don't exist yet. + +Still a hypothesis: which later use of sX matters in the C500 rows. The packets don't show the instructions that +produce sX, write SCC or read sX afterwards. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index dcad1222ea..4aca7e1cbb 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2233,6 +2233,13 @@ These are measured on one executable; treat them as hypotheses for others. and any other read of it is refused ([the note](experiments/startup-gfx2hlsl-pixel-first-lane.md)). Run `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_pixel_first_lane"`. [#2618](https://github.com/R0Y00/Xbox2Windows/issues/2618) +- **Select on descriptor data.** In a compute stage, `s_cselect_b32 sD, a, b` with one source a number computed + from a descriptor (`descnum`) and the others constants, such as `sX, 0, sX`, emits no HLSL. sD keeps both arms as + a symbol with no run-time value. A numeric read, scalar ALU other than a move, or a descriptor operand holding it + (also as a word joined at a branch target) is refused, so an arm that zeroes a buffer's size is never dropped + ([the note](experiments/startup-gfx2hlsl-cselect-descnum.md)). Run + `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_cselect_descnum"`. + [#2618](https://github.com/R0Y00/Xbox2Windows/issues/2618) - **Soft asserts.** Retail code keeps `int3` instructions followed by a continuation. Without a debugger the title's handler steps over them; under the startup debugger use `-ContinueAsserts N`. One of them fires about ten times a second in the texture-streaming path diff --git a/tests/checks/python-linux/startup_gfx2hlsl_cselect_descnum.txt b/tests/checks/python-linux/startup_gfx2hlsl_cselect_descnum.txt new file mode 100644 index 0000000000..e8d9138028 --- /dev/null +++ b/tests/checks/python-linux/startup_gfx2hlsl_cselect_descnum.txt @@ -0,0 +1 @@ +tests/startup_gfx2hlsl_cselect_descnum_check.py diff --git a/tests/cmake/startup_gfx2hlsl_cselect_descnum.cmake b/tests/cmake/startup_gfx2hlsl_cselect_descnum.cmake new file mode 100644 index 0000000000..f4a1648b2a --- /dev/null +++ b/tests/cmake/startup_gfx2hlsl_cselect_descnum.cmake @@ -0,0 +1,13 @@ +# gfx2hlsl compute s_cselect_b32 on descriptor-derived data (#2618, the C500 rows): the select keeps both arms as an +# opaque symbol, every later use that needs its value is refused by name, and three mutated translators must fail; +# then case A of the compute check with the dead select is dispatched and all 320 words compared. +if(EXISTS "${INTAKE_PYTHON}" AND TARGET offscreen_check) + add_test(NAME startup_gfx2hlsl_cselect_descnum_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cselect_descnum_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_cselect_descnum_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cselect_descnum_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_cselect_descnum_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/startup_gfx2hlsl_cselect_descnum_check.py b/tests/startup_gfx2hlsl_cselect_descnum_check.py new file mode 100644 index 0000000000..40cedf16c5 --- /dev/null +++ b/tests/startup_gfx2hlsl_cselect_descnum_check.py @@ -0,0 +1,363 @@ +"""s_cselect_b32 on descriptor-derived data in the compute stage of tools/startup/gfx2hlsl.py (#2618, C500 rows). + +usage: startup_gfx2hlsl_cselect_descnum_check.py [controls] + startup_gfx2hlsl_cselect_descnum_check.py dispatch + +The C500 compute rows stop at `s_cselect_b32 sX, 0, sX` where sX holds a number computed from a buffer descriptor +(descnum). Such a select can zero a buffer's size, which disables the buffer, so a translation that drops the zero +arm would read real data where the console reads zeros. The select now records both arms and its own offset (the +SCC it read) as an opaque symbol, and every later use that would need its value refuses. + +The fixture is case A of startup_gfx2hlsl_compute_check.py (a descriptor table load s[12:19], a buffer load +through s[12:15], a buffer store through s[16:19]) with synthetic instructions spliced in. Their bytes come from +the RDNA 2 SOP1/SOP2/SOPC/VOP1/VOPC layouts, and the decoder must print the expected text for each. + +Checked (controls): +- accepted: the select with its result dead, in both arm orders, with a literal arm, and on an extracted stride; + the HLSL differs from case A only by the compare's scc line, and sX holds both arms and the select's offset; +- refused, each with its reason: numeric reads (v_mov, s_cmp, v_cmp, m0), scalar ALU on the result, the + structured-to-raw conversion with the selected stride, a copy into the load descriptor's word 1, 2 or 3 (also + through a second copy), a copy into the store descriptor, SCC from an ALU on descriptor data, a read after a + forward join or at a loop head that a path brings the select to, a load whose word 2 is joined after a select on + one path or on a loop back edge, and a select in the high word of a 64-bit ALU source (the same joins and loop + without the select translate); +- still refused as before: s_cselect_b64, m0 and vcc_lo destinations, a raw descriptor word, a pointer or run-time + register arm, and the vertex and pixel stages. +Negative controls, each a mutated translator that must fail a named check: +- zero-arm-dropped: the select keeps only its descnum arm, so the conversion with the selected stride translates; +- load-check-removed: Stage.select_word finds nothing, so a load through a copied select translates; +- alu-check-removed: Stage.refuse_select_source accepts everything, so an ALU copy into word 2 reaches the load; +- join-check-removed: Stage.joined_word finds nothing, so a load after a join with a select on one path translates; +- high-word-unchecked: refuse_select_source sees only the low word of each source, so s_or_b64 of a pair whose high + word holds the select translates. +The dispatch mode runs case A with the dead select on D3D12 and compares all 320 words with case A's reference. +""" +import struct +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools')) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +import startup_gfx2hlsl_compute_check as C # noqa: E402 +import startup_gfx2hlsl_scalar_m0_check as M # noqa: E402 + +ZERO, LIT = 128, 255 + + +def sop1(op, d, a): + return struct.pack('= 106: if getattr(self, 'has_branches', False): @@ -2832,6 +2846,23 @@ def descriptor(self, tok): return s[1], None raise Unsupported('descriptor operand %s holds %s' % (tok, s)) + def select_word(self, tok): + """The first SGPR of operand tok that holds a select of descriptor data (descriptor_select), or None.""" + r = reg_range(tok) + if not r or r[0] != 's': + return None + return next((n for n in range(r[1], min(r[2], 105) + 1) if descriptor_select(self.sym.get(n))), None) + + def joined_word(self, tok): + """In compute, the first SGPR after the first word of operand tok that holds a value joined at a branch + target, or None. The paths bring different words there, a select of descriptor data possibly among them, + and the access names its resource by the first word only. A loop head joins the same way.""" + r = reg_range(tok) + if getattr(self, 'kind', None) != 'cs' or not r or r[0] != 's': + return None + return next((n for n in range(r[1] + 1, min(r[2], 105) + 1) + if (self.sym.get(n) or ('',))[0] == 'joined at a branch target'), None) + def heap_sampler(self, tok): """'qh[...]' when the four sampler words of tok came from one run-time sampler heap load.""" r = reg_range(tok) @@ -5381,6 +5412,7 @@ def src_sym(t): self.set_s(dst[1], '0u', ('const', 0), scc='false') return None raise Unsupported('TG_SIZE register s%d read outside s_and_b32 with 0xfc0' % tg_srcs[0]) + self.refuse_select_source(base, ops[1:]) # whole ranges: srcs names only the low word of a pair if any(s and s[0] in ('desc', 'descnum', 'ptr', 'ptrhi') for s in syms): if 'src_scc' in srcs and not getattr(self, 'scc_verified', False): raise Unsupported('src_scc with unverified SCC') @@ -5870,6 +5902,16 @@ def scalar_select(self, ops, pair): low = [('s%d' % reg_range(t)[1]) if reg_range(t) and reg_range(t)[0] == 's' else t for t in ops] if pair and 'm0' in low: raise Unsupported('64-bit scalar operand m0') + r = reg_range(low[0]) + if not pair and getattr(self, 'kind', None) == 'cs' and r and r[0] == 's' and r[1] == r[2] <= 105: + # A select between descriptor-derived data (descnum, no run-time value) and constants, such as + # s_cselect_b32 sX, 0, sX, which can zero a buffer's size: record both arms and the offset whose SCC + # picks one. Nothing reads that symbol as a value (S, refuse_select_source, select_word), so neither + # arm is dropped. + arms = [self.select_arm(t) for t in low[1:]] + if None not in arms and any(arm[0] == 'descnum' for arm in arms): + self.set_s(r[1], None, ('descnum', 'cselect', self.pc, *arms)) + return expr = 'scc ? %s : %s' % (self.u(low[1]), self.u(low[2])) if low[0] == 'm0': self.sym.pop('m0', None) @@ -5891,6 +5933,28 @@ def scalar_select(self, ops, pair): self.masks.add(d) self.set_s(d + 1, '0u') + def select_arm(self, tok): + """An s_cselect_b32 source as a symbol: the descnum a register holds, ('const', value) for a literal or a + register holding a known constant, otherwise None.""" + r = reg_range(tok) + if r: + sym = self.sym.get(r[1]) if r[0] == 's' and r[1] == r[2] and r[1] <= 105 else None + return sym if sym and (sym[0] == 'descnum' or sym[0] == 'const' and len(sym) == 2) else None + value = self.const_u32(tok) + return None if value is None else ('const', value) + + def refuse_select_source(self, base, srcs): + """A scalar ALU source holding a select of descriptor data in any of its registers (the high word of a pair + too): a move copies the symbol, anything else would turn it into an unrelated descnum or descriptor edit, + losing the arm that SCC picks, so it refuses.""" + if base in ('s_mov_b32', 's_mov_b64'): + return + for tok in srcs: + r = reg_range(tok) + regs = ['m0'] if tok == 'm0' else range(r[1], r[2] + 1) if r and r[0] == 's' else () + if any(descriptor_select(self.sym.get(n)) for n in regs): + raise Unsupported('%s on a select of descriptor data' % base) + def smem_soffset(self, tok, record_use=True): """Runtime expression for an SMEM scalar offset operand (s0..s105 or m0).""" if tok == 'm0':