From 35677cda526667ca505f63d8ec97a8ac35f617e5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 00:02:40 +0000 Subject: [PATCH 1/5] shader: lower s_ff1 of a general scalar (#2022 Item F) Keep the compute-entry exec_lo fold to lane 0, and add firstbitlow / lo-then-hi scan for ordinary SGPR and immediate sources. Lane masks and descriptor words stay refused. CPU fixture with a negative control. Co-authored-by: SimSalabimse --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 3 + docs/experiments.md | 9 ++ tests/startup_gfx2hlsl_compute_check.py | 3 +- tests/startup_gfx2hlsl_s_ff1_check.py | 194 ++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 85 ++++++++++- 7 files changed, 287 insertions(+), 9 deletions(-) create mode 100644 tests/startup_gfx2hlsl_s_ff1_check.py 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..cec9d313f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3355,6 +3355,9 @@ 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/b64 of a general scalar (#2022 Item F): CPU evaluation and refusals. + add_test(NAME startup_gfx2hlsl_s_ff1 COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_ff1_check.py") # 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..0443fe0333 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8115,6 +8115,15 @@ 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` and +`s_ff1_i32_b64` of a general scalar to `firstbitlow` (and a lo-then-hi scan for +the 64-bit form), 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. Run +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_ff1$' -V` +(or `python3 -B tests/startup_gfx2hlsl_s_ff1_check.py`). The fixture is +CPU-only; Windows WARP/hardware draws are left for an independent retest. +[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_ff1_check.py b/tests/startup_gfx2hlsl_s_ff1_check.py new file mode 100644 index 0000000000..0085d29ef4 --- /dev/null +++ b/tests/startup_gfx2hlsl_s_ff1_check.py @@ -0,0 +1,194 @@ +"""s_ff1_i32_b32 / s_ff1_i32_b64 of a general scalar in tools/startup/gfx2hlsl.py (#2022 Item F). + +usage: startup_gfx2hlsl_s_ff1_check.py + +RDNA 2 ISA SOP1: S_FF1_I32_B32 finds the lowest set bit of a 32-bit word (0xffffffff if zero); +S_FF1_I32_B64 scans the low word first, then the high word at offset 32. SCC is unchanged. +The compute-entry special case on exec_lo still folds to lane 0. Lane masks and descriptor +sources are refused. The reference is plain Python; the translated text is evaluated on the CPU. +""" +import re +import struct +import sys +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) +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): + return 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 across a 64-bit SGPR pair; SCC unchanged (#2022 Item F).""" + 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') + src = reg_range(ops[1]) + if not src or src[0] != 's' or src[2] != src[1] + 1 or src[1] % 2 or src[2] > 105: + raise Unsupported('s_ff1_i32_b64 source %s' % ops[1]) + if self.is_lane_mask(ops[1]) or src[1] in self.masks or (src[1] + 1) in self.masks: + raise Unsupported('s_ff1_i32_b64 on a lane mask') + for half in (src[1], src[1] + 1): + sym = self.sym.get(half) + if sym and sym[0] not in ('const', 'ud', 'fixed_buffer_word'): + raise Unsupported('s_ff1_i32_b64 on %s data' % sym[0]) + lo_sym = self.sym.get(src[1]) + hi_sym = self.sym.get(src[1] + 1) + if (lo_sym and lo_sym[0] == 'const' and hi_sym and hi_sym[0] == 'const'): + lo_v, hi_v = lo_sym[1] & 0xffffffff, hi_sym[1] & 0xffffffff + if lo_v: + folded = (lo_v & -lo_v).bit_length() - 1 + elif hi_v: + folded = 32 + ((hi_v & -hi_v).bit_length() - 1) + else: + folded = 0xffffffff + self.set_s(dst[1], '0x%08xu' % folded, ('const', folded)) + return None + lo, hi = self.S(src[1]), self.S(src[1] + 1) + # Scan the low word first; only then the high word at offset 32 (pixel_aot / Mesa). + expr = ('(%s != 0u ? firstbitlow(%s) : (%s != 0u ? 32u + firstbitlow(%s) : 0xffffffffu))' + % (lo, lo, hi, hi)) + self.set_s(dst[1], expr) + return None def op_s_abs_i32(self, ops, mods): """Signed absolute value; SCC = result != 0 (#1723). From 9d1abeadad02f40c6ada54a281f820ad7c05d426 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 00:19:45 +0000 Subject: [PATCH 2/5] shader: keep s_ff1 b32 general; leave b64 SGPR pair to #2072 Refuse live s_ff1_i32_b64 SGPR pairs (owned by open #2072). Keep zero-extended b64 immediates and add a D3D12 UAV dispatch fixture for the b32 edge words (including firstbitlow(0) == 0xffffffff). Co-authored-by: SimSalabimse --- CMakeLists.txt | 10 +- docs/experiments.md | 20 +- tests/startup_gfx2hlsl_s_ff1_check.py | 372 ++++++++++++++++++-------- tools/startup/gfx2hlsl.py | 41 ++- 4 files changed, 295 insertions(+), 148 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cec9d313f7..6edcb0b930 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3355,9 +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/b64 of a general scalar (#2022 Item F): CPU evaluation and refusals. + # 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 0443fe0333..f6bab07ed8 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8115,14 +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` and -`s_ff1_i32_b64` of a general scalar to `firstbitlow` (and a lo-then-hi scan for -the 64-bit form), 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. Run +**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` of a live SGPR pair is owned by open +[#2072](https://github.com/R0Y00/Xbox2Windows/pull/2072); this slice only +accepts a 32-bit SOP1 literal/inline, which the ISA zero-extends (high word 0), +and folds it. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_ff1$' -V` -(or `python3 -B tests/startup_gfx2hlsl_s_ff1_check.py`). The fixture is -CPU-only; Windows WARP/hardware draws are left for an independent retest. +(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 diff --git a/tests/startup_gfx2hlsl_s_ff1_check.py b/tests/startup_gfx2hlsl_s_ff1_check.py index 0085d29ef4..8fa062fe42 100644 --- a/tests/startup_gfx2hlsl_s_ff1_check.py +++ b/tests/startup_gfx2hlsl_s_ff1_check.py @@ -1,15 +1,25 @@ -"""s_ff1_i32_b32 / s_ff1_i32_b64 of a general scalar in tools/startup/gfx2hlsl.py (#2022 Item F). +"""s_ff1_i32_b32 of a general scalar and s_ff1_i32_b64 of a 32-bit immediate (#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); -S_FF1_I32_B64 scans the low word first, then the high word at offset 32. SCC is unchanged. -The compute-entry special case on exec_lo still folds to lane 0. Lane masks and descriptor -sources are refused. The reference is plain Python; the translated text is evaluated on the CPU. +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 of a live SGPR pair is owned by open #2072. This fixture only accepts a 32-bit +SOP1 literal/inline, which the ISA zero-extends (high word 0), and folds it. A hi-nonzero +source needs a register pair and is refused here. + +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 @@ -23,6 +33,10 @@ 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) SCC_UNSET = 2 VCC_LO, EXEC_LO = 106, 126 @@ -37,8 +51,11 @@ def check(label, ok, detail=''): print(' ' + detail) -def sop1(name, sdst, ssrc): - return struct.pack(' 105: - raise Unsupported('s_ff1_i32_b64 source %s' % ops[1]) - if self.is_lane_mask(ops[1]) or src[1] in self.masks or (src[1] + 1) in self.masks: - raise Unsupported('s_ff1_i32_b64 on a lane mask') - for half in (src[1], src[1] + 1): - sym = self.sym.get(half) - if sym and sym[0] not in ('const', 'ud', 'fixed_buffer_word'): - raise Unsupported('s_ff1_i32_b64 on %s data' % sym[0]) - lo_sym = self.sym.get(src[1]) - hi_sym = self.sym.get(src[1] + 1) - if (lo_sym and lo_sym[0] == 'const' and hi_sym and hi_sym[0] == 'const'): - lo_v, hi_v = lo_sym[1] & 0xffffffff, hi_sym[1] & 0xffffffff - if lo_v: - folded = (lo_v & -lo_v).bit_length() - 1 - elif hi_v: - folded = 32 + ((hi_v & -hi_v).bit_length() - 1) - else: - folded = 0xffffffff + 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 - lo, hi = self.S(src[1]), self.S(src[1] + 1) - # Scan the low word first; only then the high word at offset 32 (pixel_aot / Mesa). - expr = ('(%s != 0u ? firstbitlow(%s) : (%s != 0u ? 32u + firstbitlow(%s) : 0xffffffffu))' - % (lo, lo, hi, hi)) - self.set_s(dst[1], expr) - return None + src = reg_range(ops[1]) + if src and src[0] == 's' and src[2] == src[1] + 1: + raise Unsupported('s_ff1_i32_b64 of an SGPR pair (register-pair form is in open #2072)') + raise Unsupported('s_ff1_i32_b64 source %s' % ops[1]) def op_s_abs_i32(self, ops, mods): """Signed absolute value; SCC = result != 0 (#1723). From d441d9a8bdf660d3fda32aa7c36742848afe077d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 01:34:08 +0000 Subject: [PATCH 3/5] shader: keep s_ff1_i32_b64 out of SCALAR_BITS after rebase Main's SCALAR_BITS install was overwriting Stage.op_s_ff1_i32_b64. Leave the dedicated handler for zero-extended immediates (#2022 F / #2072). Co-authored-by: SimSalabimse --- tools/startup/gfx2hlsl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6d78988c16..1e5a9f84ec 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -7255,7 +7255,7 @@ 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: Stage.op_s_ff1_i32_b64 owns immediates here; live SGPR pairs stay with #2072. 's_flbit_i32_b64': (True, _clz64, None), 's_flbit_i32_i64': (True, _flbit_i64, None), } From 3b5863553fa94ca03e410cde841db8ff13acf633 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 01:43:31 +0000 Subject: [PATCH 4/5] shader: lower s_ff1_i32_b64 SGPR pairs after #2072 merge Keep the Stage handler for zero-extended immediates and emit the same 64-bit firstbitlow scan for register pairs so bit_scan CI stays green. Co-authored-by: SimSalabimse --- docs/experiments.md | 7 +++--- tests/startup_gfx2hlsl_s_bit_scan_check.py | 4 +++- tests/startup_gfx2hlsl_s_ff1_check.py | 26 +++++++++++++--------- tools/startup/gfx2hlsl.py | 23 ++++++++++++------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index f6bab07ed8..42bd2e56a6 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8119,10 +8119,9 @@ CPU-only; Windows WARP/hardware draws are left for an independent retest. 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` of a live SGPR pair is owned by open -[#2072](https://github.com/R0Y00/Xbox2Windows/pull/2072); this slice only -accepts a 32-bit SOP1 literal/inline, which the ISA zero-extends (high word 0), -and folds it. Run +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 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 index 8fa062fe42..0dd6d24107 100644 --- a/tests/startup_gfx2hlsl_s_ff1_check.py +++ b/tests/startup_gfx2hlsl_s_ff1_check.py @@ -1,4 +1,4 @@ -"""s_ff1_i32_b32 of a general scalar and s_ff1_i32_b64 of a 32-bit immediate (#2022 Item F). +"""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 @@ -7,9 +7,9 @@ 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 of a live SGPR pair is owned by open #2072. This fixture only accepts a 32-bit -SOP1 literal/inline, which the ISA zero-extends (high word 0), and folds it. A hi-nonzero -source needs a register pair and is refused here. +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 @@ -175,17 +175,23 @@ def controls(): lines == ['s3 = 0xffffffffu;'] and sym.get(3) == ('const', MASK), str((lines, sym.get(3)))) - # Live SGPR pair is owned by #2072. + # SGPR pairs use the same 64-bit firstbitlow scan as #2072 / SCALAR_BITS. lines, _ = translate(sop1('s_ff1_i32_b64', 3, 4)) - check('s_ff1_i32_b64 refuses a live SGPR pair (#2072)', - lines[0].startswith('refused:') and 'SGPR pair' in lines[0] and '#2072' in lines[0], + check('s_ff1_i32_b64 of a live SGPR pair emits firstbitlow over the pair', + (not lines[0].startswith('refused:') + and 'firstbitlow' in lines[0] + and 's4' in lines[0] + and 's5' in lines[0]), str(lines)) - # Even a known-constant pair stays refused so this PR does not own that slice. lines, _ = translate(sop1('s_ff1_i32_b64', 3, 4), sym={4: ('const', 0), 5: ('const', 0x10)}) - check('s_ff1_i32_b64 refuses a constant SGPR pair (#2072)', - lines[0].startswith('refused:') and 'SGPR pair' in lines[0], + # Known consts substitute into the scan text (hi half at +32); still not refused. + check('s_ff1_i32_b64 of a constant SGPR pair still scans both halves', + (not lines[0].startswith('refused:') + and 'firstbitlow' in lines[0] + and '32u' in lines[0] + and '0x00000010u' in lines[0]), str(lines)) # Refusals: lane masks and descriptors. diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 1e5a9f84ec..22186c19f7 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -5798,11 +5798,12 @@ def op_s_ff1_i32_b32(self, ops, mods): return None def op_s_ff1_i32_b64(self, ops, mods): - """Find first set bit of a 64-bit immediate; SCC unchanged (#2022 Item F). + """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). - Live SGPR-pair sources are refused here: that register-pair lowering is owned by - open #2072 (`s_ff1_i32_b64` among the scalar bit-scan forms). + 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)) @@ -5819,9 +5820,14 @@ def op_s_ff1_i32_b64(self, ops, mods): self.set_s(dst[1], '0x%08xu' % folded, ('const', folded)) return None src = reg_range(ops[1]) - if src and src[0] == 's' and src[2] == src[1] + 1: - raise Unsupported('s_ff1_i32_b64 of an SGPR pair (register-pair form is in open #2072)') - raise Unsupported('s_ff1_i32_b64 source %s' % 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). @@ -7255,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: Stage.op_s_ff1_i32_b64 owns immediates here; live SGPR pairs stay with #2072. + # 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), } From ec7ce3bd07d6083dfdbb3f125a26f7989d95d26f Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 02:57:14 +0100 Subject: [PATCH 5/5] test: read back s_ff1_i32_b64 of a live SGPR pair on D3D12 (lo/hi cases against a CPU reference) --- tests/startup_gfx2hlsl_s_ff1_check.py | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/startup_gfx2hlsl_s_ff1_check.py b/tests/startup_gfx2hlsl_s_ff1_check.py index 0dd6d24107..365a215215 100644 --- a/tests/startup_gfx2hlsl_s_ff1_check.py +++ b/tests/startup_gfx2hlsl_s_ff1_check.py @@ -37,6 +37,9 @@ 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 @@ -273,6 +276,52 @@ def dispatch_b32(dxc, runner, adapter, cases, directory): return actual[len(fill):word_count] +def ff1_64(lo, hi): + """CPU reference for s_ff1_i32_b64: lowest set bit of the pair, -1 when both words are zero.""" + lo, hi = lo & MASK, hi & MASK + return ff1_32(lo) if lo else (32 + ff1_32(hi) if hi else MASK) + + +def dispatch_b64_pairs(dxc, runner, adapter, cases, directory): + """Translate s_ff1_i32_b64 of the live pair s4:s5 once; feed each (lo, hi) through a UAV and read it back.""" + lines, _ = translate(sop1('s_ff1_i32_b64', 3, 4)) + assert lines and not lines[0].startswith('refused'), lines + fill = tuple(((i + 1) * 0x9e3779b1 & MASK) | 1 for i in range(2 * len(cases))) + hlsl = ['RWByteAddressBuffer data : register(u0);', '[numthreads(1, 1, 1)]', 'void main() {'] + for i, (lo, hi) in enumerate(cases): + hlsl.extend(( + ' {', + ' uint s4 = data.Load(%d) ^ %s;' % (8 * i, '%#010xu' % (fill[2 * i] ^ lo)), + ' uint s5 = data.Load(%d) ^ %s;' % (8 * i + 4, '%#010xu' % (fill[2 * i + 1] ^ hi)), + ' uint s3;', + ' bool scc = true;', + )) + hlsl.extend(' ' + line for line in lines) + hlsl.append(' data.Store(%d, s3);' % (4 * (len(fill) + i))) + hlsl.append(' }') + hlsl.append('}') + + src, cso, output = (directory / ('s_ff1_b64_pair' + suffix) for suffix in ('.hlsl', '.cso', '.bin')) + src.write_text('\n'.join(hlsl) + '\n', encoding='ascii') + compile_result = subprocess.run([dxc, '-T', 'cs_6_0', '-E', 'main', '-Fo', str(cso), str(src)], + stderr=subprocess.PIPE, text=True) + if compile_result.returncode or compile_result.stderr: + raise AssertionError(compile_result.stderr or 'DXC exited with %d' % compile_result.returncode) + word_count = len(fill) + len(cases) + dump_words = (word_count + 3) // 4 * 4 + run_result = subprocess.run( + [runner, adapter, '--raw-uav-dump', str(cso), '1', str(4 * dump_words), str(output)], + capture_output=True, text=True) + print(run_result.stdout, end='') + if run_result.returncode == 77: + sys.exit(77) + if run_result.returncode or run_result.stderr: + raise AssertionError(run_result.stderr or 'raw UAV dump exited with %d' % run_result.returncode) + actual = struct.unpack('<%dI' % dump_words, output.read_bytes()) + assert actual[:len(fill)] == fill, 'operand region changed: %s' % (actual[:len(fill)],) + return actual[len(fill):word_count] + + def dispatch_b64_immediates(dxc, runner, adapter, cases, directory): """Store the folded b64-immediate results; proves the zero-extend fold on the adapter.""" hlsl = ['RWByteAddressBuffer data : register(u0);', '[numthreads(1, 1, 1)]', 'void main() {'] @@ -329,6 +378,14 @@ def check_dispatch(dxc, runner, adapter): want = ff1_32(word) # hi = 0 by zero-extension check('dispatch s_ff1_i32_b64 immediate %#010x (hi=0)' % word, got64[i] == want, 'got %#x want %#x' % (got64[i], want)) + gotpair = dispatch_b64_pairs(dxc, runner, adapter, PAIR_B64, directory) + for i, (lo, hi) in enumerate(PAIR_B64): + want = ff1_64(lo, hi) + check('dispatch s_ff1_i32_b64 pair hi=%#010x lo=%#010x' % (hi, lo), gotpair[i] == want, + 'got %#x want %#x' % (gotpair[i], want)) + # Negative control: a lo-first scan that ignored the high word would answer 0xffffffff here. + check('dispatch scans the high word when lo is zero', gotpair[PAIR_B64.index((0, 0x10))] == 36, + hex(gotpair[PAIR_B64.index((0, 0x10))])) if failures: print('FAILED: %s' % ', '.join(failures)) sys.exit(1)