diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index b7a494ddaa..7e383a04e7 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -304,6 +304,7 @@ jobs: tests/startup_gfx2hlsl_s_abs_check.py \ tests/startup_gfx2hlsl_s_bitset_check.py \ tests/startup_gfx2hlsl_s_flbit_check.py \ + tests/startup_gfx2hlsl_s_ff1_check.py \ tests/startup_gfx2hlsl_fma_mix_check.py \ tests/startup_gfx2hlsl_shift64_check.py \ tests/startup_gfx2hlsl_mul_mad_i24_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 11f41b9b5a..1c9d01c42c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -176,6 +176,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_s_abs_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_s_bitset_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_s_flbit_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_s_ff1_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_fma_mix_check.py ./out/pe-env/bin/python -B tests/startup_image_2d_array_check.py ./out/pe-env/bin/python -B tests/startup_image_sample_a16_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f22ae5ec7..6edcb0b930 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3355,6 +3355,17 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(startup_gfx2hlsl_s_flbit_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) endforeach() + # gfx2hlsl lowers s_ff1_i32_b32 of a general scalar and s_ff1_i32_b64 of a zero-extended + # immediate (#2022 Item F); live SGPR-pair b64 is owned by open #2072. CPU evaluation, then + # a D3D12 UAV readback on WARP/hardware (same dispatch pattern as pack_absdiff). + add_test(NAME startup_gfx2hlsl_s_ff1 COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_ff1_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_s_ff1_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_ff1_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_s_ff1_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() # gfx2hlsl lowers v_cube{id,sc,tc,ma}_f32: device-free translation controls. add_test(NAME startup_gfx2hlsl_cube_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cube_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 7e69d617fa..42bd2e56a6 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8115,6 +8115,20 @@ destination with a known bit index folds. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_bitset$' -V` (or `python3 -B tests/startup_gfx2hlsl_s_bitset_check.py`). The fixture is CPU-only; Windows WARP/hardware draws are left for an independent retest. +**Scalar find-first-one.** Startup translation lowers `s_ff1_i32_b32` of a +general scalar to `firstbitlow` (0xffffffff when the word is zero), matching +`pixel_aot` / Mesa. The compute-entry special case on `exec_lo` still folds to +lane 0. SCC is unchanged. Lane masks and descriptor-bearing sources are +refused. `s_ff1_i32_b64` accepts a 32-bit SOP1 literal/inline (ISA zero-extends +the high word to 0) and folds it, and also lowers a live SGPR pair with the same +64-bit firstbitlow scan as the scalar bit-scan forms. Run +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_ff1$' -V` +(or `python3 -B tests/startup_gfx2hlsl_s_ff1_check.py`). The `_dispatch_warp` +/ `_dispatch_hardware` variants compile the translated `firstbitlow` into a +compute shader and compare UAV readback for inputs 0, 1, 0x80000000 and +0x00010000 (and the zero-extended b64 immediates) against the same reference: +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_ff1_dispatch_warp$' -V`. +[Refs #2022](https://github.com/R0Y00/Xbox2Windows/issues/2022) Item F. **Scalar find-first-bit-high.** Startup translation lowers `s_flbit_i32_b32` to `S0 == 0 ? 0xffffffff : 31 - firstbithigh(S0)`, matching `pixel_aot` / diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 1d7b56d179..2b3a7a19c0 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -1027,7 +1027,8 @@ def check_controls(): for code in (bytes.fromhex('8003febe') + ff1 + end, ff1 + bytes.fromhex('feff82bf') + end): refused('compute entry EXEC mask', lambda: translate('A', code=code)) - refused('requires compute exec_lo', lambda: translate('A', code=bytes.fromhex('6a1385be') + end)) + # vcc_lo is a lane mask; general-scalar ff1 (#2022 Item F) still refuses it. + refused('lane mask', lambda: translate('A', code=bytes.fromhex('6a1385be') + end)) assert 's5 = 0u;' in translate('A-ff1').hlsl_cs() assert expected('A-ff1') != expected('A') check_lds() diff --git a/tests/startup_gfx2hlsl_s_bit_scan_check.py b/tests/startup_gfx2hlsl_s_bit_scan_check.py index 0b97327eb7..34cc810d76 100644 --- a/tests/startup_gfx2hlsl_s_bit_scan_check.py +++ b/tests/startup_gfx2hlsl_s_bit_scan_check.py @@ -131,7 +131,9 @@ def ref(name, value): repr(lines)) # Refusals that must not widen: a 64-bit scan needs a register pair, 32-bit one register, and no lane-mask data. -check('b64 refuses an inline constant source', refused('s_ff1_i32_b64', 3, 0x80)) +# s_ff0 keeps the pair-only SCALAR_BITS path (inline constants refused). s_ff1_i32_b64 also accepts a +# zero-extended 32-bit immediate (#2022 Item F), so the pair-only refusal is checked on s_ff0. +check('b64 refuses an inline constant source', refused('s_ff0_i32_b64', 3, 0x80)) check('b32 refuses exec destination', refused('s_bcnt1_i32_b32', EXEC_LO, 2)) check('b32 refuses m0 destination', refused('s_bcnt1_i32_b32', M0, 2)) check('b32 refuses exec source', refused('s_bcnt1_i32_b32', 3, EXEC_LO)) diff --git a/tests/startup_gfx2hlsl_s_ff1_check.py b/tests/startup_gfx2hlsl_s_ff1_check.py new file mode 100644 index 0000000000..365a215215 --- /dev/null +++ b/tests/startup_gfx2hlsl_s_ff1_check.py @@ -0,0 +1,401 @@ +"""s_ff1_i32_b32 of a general scalar and s_ff1_i32_b64 immediates + SGPR pairs (#2022 Item F). + +usage: startup_gfx2hlsl_s_ff1_check.py + startup_gfx2hlsl_s_ff1_check.py dispatch + +RDNA 2 ISA SOP1: S_FF1_I32_B32 finds the lowest set bit of a 32-bit word (0xffffffff if zero); +SCC is unchanged. The compute-entry special case on exec_lo still folds to lane 0. Lane masks +and descriptor sources are refused. + +S_FF1_I32_B64 accepts a 32-bit SOP1 literal/inline (ISA zero-extends the high word to 0) and +folds it, and also lowers a live SGPR pair with the same 64-bit firstbitlow scan as the scalar +bit-scan forms. + +With `dispatch`, the b32 lowering runs as a compute shader on the adapter (D3D12 UAV +readback via offscreen_check, same pattern as startup_gfx2hlsl_pack_absdiff_check). The +reference is plain Python; the translated text is evaluated on the CPU without `dispatch`. +""" +import re +import struct +import subprocess +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 disasm_rdna2 as D # noqa: E402 +import gfx2hlsl as G # noqa: E402 +from startup_gfx2hlsl_bit_alu_check import Expr # noqa: E402 + +MASK = 0xffffffff +SOP1 = {name: op for op, (name, _) in D.SOP1_OPS.items()} +WORDS = (0x00000000, 0xffffffff, 0x80000001, 0x00010020, 0x55555555, 0xaaaaaaaa, 0x12345678, 0x10) +# GPU cases from review: prove firstbitlow(0) == 0xffffffff and the named edge bits. +DRAW_B32 = (0x00000000, 0x00000001, 0x80000000, 0x00010000) +# b64 immediates only (hi forced to 0 by zero-extension). hi-nonzero is #2072. +IMMEDIATE_B64 = (0x00000000, 0x00000001, 0x00010000, 0x80000000) +# Live SGPR pair s4:s5 as (lo, hi): both zero, lo only, hi only, both set (lo wins), edge bits in each half. +PAIR_B64 = ((0, 0), (1, 0), (0, 1), (0, 0x80000000), (0, 0x10), (0x80000000, 0xffffffff), (0x00010000, 0x1), + (0xffffffff, 0xffffffff)) +SCC_UNSET = 2 +VCC_LO, EXEC_LO = 106, 126 + +failures = [] + + +def check(label, ok, detail=''): + print('%s: %s' % (label, 'ok' if ok else 'MISMATCH'), flush=True) + if not ok: + failures.append(label) + if detail: + print(' ' + detail) + + +def sop1(name, sdst, ssrc, lit=None): + word = struct.pack(' 105: + raise Unsupported('s_ff1_i32_b32 destination %s' % ops[0]) + if ops[1] == 'exec_lo': + if self.kind != 'cs': + raise Unsupported('s_ff1_i32_b32 of exec_lo outside compute') + if getattr(self, 'past_entry', False) or not getattr(self, 'exec_known', False): + raise Unsupported('s_ff1_i32_b32 requires the compute entry EXEC mask') + # At dispatch entry even the last partial wave starts at lane zero. + self.set_s(dst[1], '0u', ('const', 0)) + return None + if self.is_lane_mask(ops[0]) or self.is_lane_mask(ops[1]) or dst[1] in self.masks: + raise Unsupported('s_ff1_i32_b32 on a lane mask') + imm = re.fullmatch(r'-?(0x[0-9a-fA-F]+|\d+)', ops[1]) + if imm: + value = int(ops[1], 0) & 0xffffffff + # firstbitlow of zero is -1; otherwise the lowest set bit index. + folded = 0xffffffff if value == 0 else (value & -value).bit_length() - 1 + self.set_s(dst[1], '0x%08xu' % folded, ('const', folded)) + return None + src = reg_range(ops[1]) + if not src or src[0] != 's' or src[1] != src[2] or src[1] > 105: + raise Unsupported('s_ff1_i32_b32 source %s' % ops[1]) + if src[1] in self.masks: + raise Unsupported('s_ff1_i32_b32 on a lane mask') + sym = self.sym.get(src[1]) + if sym and sym[0] not in ('const', 'ud', 'fixed_buffer_word'): + raise Unsupported('s_ff1_i32_b32 on %s data' % sym[0]) + if sym and sym[0] == 'const': + value = sym[1] & 0xffffffff + folded = 0xffffffff if value == 0 else (value & -value).bit_length() - 1 + self.set_s(dst[1], '0x%08xu' % folded, ('const', folded)) + return None + self.set_s(dst[1], 'firstbitlow(%s)' % self.u(ops[1])) + return None + + def op_s_ff1_i32_b64(self, ops, mods): + """Find first set bit of a 64-bit source; SCC unchanged (#2022 Item F). + + A 32-bit SOP1 literal or inline constant zero-extends to 64 bits (high word 0) + and folds. An SGPR pair uses the same 64-bit firstbitlow scan as SCALAR_BITS + (`_ctz64` / #2072). Kept as a Stage method so immediates are not refused by the + pair-only SCALAR_BITS template. + """ + if mods or len(ops) != 2: + raise Unsupported('s_ff1_i32_b64 with %d operands, expected 2 unmodified' % len(ops)) dst = reg_range(ops[0]) if not dst or dst[0] != 's' or dst[1] != dst[2] or dst[1] > 105: - raise Unsupported('s_ff1_i32_b32 requires one SGPR destination') - if self.past_entry or not self.exec_known: - raise Unsupported('s_ff1_i32_b32 requires the compute entry EXEC mask') - # At dispatch entry even the last partial wave starts at lane zero. - # FF1 reports its index and leaves SCC unchanged. - self.set_s(dst[1], '0u', ('const', 0)) + raise Unsupported('s_ff1_i32_b64 destination %s' % ops[0]) + if self.is_lane_mask(ops[0]) or dst[1] in self.masks: + raise Unsupported('s_ff1_i32_b64 on a lane mask') + imm = re.fullmatch(r'-?(0x[0-9a-fA-F]+|\d+)', ops[1]) + if imm: + # Encoding: one 32-bit literal/inline; the ISA zero-extends it (hi = 0). + value = int(ops[1], 0) & 0xffffffff + folded = 0xffffffff if value == 0 else (value & -value).bit_length() - 1 + self.set_s(dst[1], '0x%08xu' % folded, ('const', folded)) + return None + src = reg_range(ops[1]) + if src is None or src[0] != 's' or src[2] > 105 or src[2] - src[1] != 1: + raise Unsupported('s_ff1_i32_b64 source %s' % ops[1]) + if any(r in self.masks for r in range(src[1], src[2] + 1)): + raise Unsupported('s_ff1_i32_b64 on a lane mask') + lo, hi = self.S(src[1]), self.S(src[2]) + self.masks.discard(dst[1]) + self.set_s(dst[1], _ctz64(lo, hi)) + return None def op_s_abs_i32(self, ops, mods): """Signed absolute value; SCC = result != 0 (#1723). @@ -7197,7 +7261,8 @@ def _flbit_i64(lo, hi): 's_bcnt0_i32_b64': (True, lambda lo, hi: '(64u - countbits(%s) - countbits(%s))' % (lo, hi), '$ != 0'), 's_bcnt1_i32_b64': (True, lambda lo, hi: '(countbits(%s) + countbits(%s))' % (lo, hi), '$ != 0'), 's_ff0_i32_b64': (True, lambda lo, hi: _ctz64('(~%s)' % lo, '(~%s)' % hi), None), - 's_ff1_i32_b64': (True, _ctz64, None), + # s_ff1_i32_b64 stays on Stage.op_s_ff1_i32_b64 (immediates + SGPR pairs) so SCALAR_BITS + # does not overwrite the zero-extended immediate fold from #2022 Item F. 's_flbit_i32_b64': (True, _clz64, None), 's_flbit_i32_i64': (True, _flbit_i64, None), }