diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 5fbdd778c2..459be91ffa 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -205,6 +205,7 @@ jobs: tests/startup_live_telemetry_check.py \ tests/startup_gfx2hlsl_cvt_alu_check.py \ tests/startup_gfx2hlsl_int_med3_check.py \ + tests/startup_gfx2hlsl_cselect_mask_check.py \ tests/startup_gfx2hlsl_mask_overwrite_check.py \ tests/startup_gfx2hlsl_capture_check.py \ tests/startup_gfx2hlsl_watch_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 365b0df6a6..2dbb6d9ac1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -164,6 +164,7 @@ python3 -m venv out/pe-env ./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_int_med3_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_cselect_mask_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mask_overwrite_check.py ./out/pe-env/bin/python -B tests/startup_flat_param_check.py ./out/pe-env/bin/python -B tests/startup_inline_sampler_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 47eefe9acd..141e10d19c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2300,6 +2300,9 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: signed and unsigned three-source integer min, max and median. add_test(NAME startup_gfx2hlsl_int_med3_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_int_med3_check.py") + # Startup shader translator: s_cselect_b32 into the wave32 lane masks vcc_lo and exec_lo. + add_test(NAME startup_gfx2hlsl_cselect_mask_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cselect_mask_check.py") # Startup shader translator: a scalar load or select into a former lane-mask SGPR clears its mask mark. add_test(NAME startup_gfx2hlsl_mask_overwrite_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_mask_overwrite_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index a040cdc1bb..a4594b998e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11658,6 +11658,16 @@ Tests: `startup_gfx2hlsl_cvt_alu_controls`, `startup_gfx2hlsl_cvt_alu_warp` and ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_int_med3_controls$' -V ``` +## Startup translator: scalar select into the wave32 lane masks + +In wave32, `vcc_lo` and `exec_lo` hold the whole lane mask, and the generic scalar path in `tools/startup/gfx2hlsl.py` already writes them as `vcc` and `exec`. `s_cselect_b32` went through a separate select path that assumed an SGPR destination, so `s_cselect_b32 vcc_lo, -1, 0` stopped the translation with a `TypeError` instead of a lowering or an `Unsupported` refusal. The select path now writes the lane's bit of the selected word to `vcc` or `exec` for `vcc_lo` and `exec_lo` (an `exec_lo` write clears `exec_known`, as an `exec` write does), keeps the SGPR and `m0` destinations as before, and refuses any other destination with an `Unsupported` message. + +`tests/startup_gfx2hlsl_cselect_mask_check.py` decodes synthetic SOP2 words, evaluates the emitted select for both SCC values with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`, and checks that an unreduced select (the `& 1` removed) gives a different `vcc`. It also checks the SGPR and `m0` lines and the refusal of the null SGPR as a destination. On the translator before this change the first case raises the `TypeError`. Test: `startup_gfx2hlsl_cselect_mask_controls`. Not covered: a draw through a translated pipeline, and the guard that refuses exec writes behind a fetch call, which still accepts only the `exec` spelling. + +```powershell +ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$' -V +``` + ## Startup translator: lane-mask marks after a scalar overwrite `tools/startup/gfx2hlsl.py` records the SGPRs that hold a lane mask (a `v_cmp` destination, a saveexec copy) in `Stage.masks`, and lowers a scalar logic op with a mask source as a mask op whose result is the lane's bit. The generic scalar path cleared the mark when it wrote ordinary data into such a register, but the other scalar writers did not. After `v_cmp_eq_u32_e64 s6, v0, v1` and `s_buffer_load_dword s6, s[16:19], 0x0`, s6 holds a buffer word, yet `s_or_b32 s8, s6, s7` was lowered as `s8 = (s6 | s7) & 1;`. Every write through `set_s` now clears the mark, the writers of a mask mark the register again after the write, and `s_load` clears the mark on its destination registers, also in the forms that only record a descriptor symbol. diff --git a/tests/startup_gfx2hlsl_cselect_mask_check.py b/tests/startup_gfx2hlsl_cselect_mask_check.py new file mode 100644 index 0000000000..2f4c67d863 --- /dev/null +++ b/tests/startup_gfx2hlsl_cselect_mask_check.py @@ -0,0 +1,108 @@ +"""s_cselect_b32 into vcc_lo and exec_lo in tools/startup/gfx2hlsl.py. + +usage: startup_gfx2hlsl_cselect_mask_check.py + +Synthetic SOP2 words only (op [29:23] = 10, sdst [22:16], ssrc1 [15:8], ssrc0 [7:0]; the names are asserted from the +translator's decoder). In wave32, vcc_lo and exec_lo hold the whole lane mask, and the generic scalar path already +writes them as `vcc` and `exec`. The select path assumed an SGPR destination, so `s_cselect_b32 vcc_lo, -1, 0` +raised a TypeError instead of a translation or an Unsupported refusal. + +Checked: vcc_lo and exec_lo receive the lane's bit of the selected word for both SCC values (evaluated with the +restricted evaluator of startup_gfx2hlsl_bit_alu_check), exec_lo clears exec_known, an SGPR and an m0 destination +lower as before, and a destination that is neither (the null SGPR) is refused with an Unsupported message. +Negative control: the same evaluation with the selected word not reduced to the lane bit gives a different vcc. +""" +import struct +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +from startup_gfx2hlsl_bit_alu_check import Expr # noqa: E402 + +failures = 0 + + +def check(what, ok, detail=''): + global failures + print('%s %s%s' % ('ok ' if ok else 'FAIL', what, '' if ok or not detail else ': %s' % detail)) + if not ok: + failures += 1 + + +class Pipe: + root = {'params': [], 'flags': 0} + + def ud(self, source): + return 0 + + +SOP2_CSELECT_B32 = 0x80000000 | 10 << 23 +INLINE_ZERO, INLINE_ONE, INLINE_MINUS_ONE = 128, 129, 193 +VCC_LO, EXEC_LO, M0, NULL = 106, 126, 124, 125 + + +def cselect(sdst, ssrc0, ssrc1): + return SOP2_CSELECT_B32 | sdst << 16 | ssrc1 << 8 | ssrc0 + + +def one(word): + s = G.Stage(Pipe(), 'ps', b'') + s.lines = [] + _, _, name, text = G.decode(struct.pack('