From 3dc861e21ce8aa4f3b0b26a10eb304e1a14ef6aa Mon Sep 17 00:00:00 2001 From: denn0x <55638248+denn0x@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:31:08 +0000 Subject: [PATCH] startup: lower s_cselect_b32 into vcc_lo and exec_lo in gfx2hlsl 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 stopped the translation with a TypeError. It now writes the lane's bit of the selected word to vcc or exec (an exec_lo write clears exec_known), 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 and evaluates the emitted select for both SCC values, with an unreduced select as the negative control. On the translator before this change it fails with the TypeError. Test: ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$' -V --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 3 + docs/experiments.md | 10 ++ tests/startup_gfx2hlsl_cselect_mask_check.py | 108 +++++++++++++++++++ tools/startup/gfx2hlsl.py | 13 ++- 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 tests/startup_gfx2hlsl_cselect_mask_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 8c10eb6845..b2fc4ea69f 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -204,6 +204,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_capture_check.py \ tests/startup_translator_check.py \ tests/startup_gfx2hlsl_smem_offset_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 9eda0505e0..356a7b1221 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -163,6 +163,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_flat_param_check.py ./out/pe-env/bin/python -B tests/startup_inline_sampler_check.py ./out/pe-env/bin/python -B tests/startup_buffer_address_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index c67aa831e3..9b01135c59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2269,6 +2269,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") foreach(adapter IN ITEMS warp hardware) add_test(NAME startup_gfx2hlsl_cvt_alu_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cvt_alu_check.py" draw "${DXC_EXECUTABLE}" diff --git a/docs/experiments.md b/docs/experiments.md index ab4d77ad3d..3e508dbf24 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11605,6 +11605,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: explicit-LOD image sampling `tools/startup/gfx2hlsl.py` now lowers the gfx1013 `image_sample_l` form of a 2D or 3D texture sample to `tex.SampleLevel(smp, uv, level)`. The level is the last address register: the third for a contiguous 2D address, the fourth for 3D, and the last element of an NSA list. `tests/startup_image_sample_l_check.py` translates source-built fixtures (the words are checked against `llvm-mc`) and requires the level to come from that register; an address list `[v0, v1, v3]` must use `v3`, not the third consecutive register. Replacing the level with 0, replacing the call with implicit `Sample`, or reading the third register in every form each make the check fail. Offset (`_o`), compare (`_c_l`), gather, 1D, array, cube and multisample dimensions, an empty `dmask`, and the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers or any set reserved MIMG bit are refused with an `Unsupported` message; only the cache-policy bits `glc`, `slc` and `dlc` are accepted because they do not change the sampled value. The disassembler previously read `lwe` from reserved bit 14 of word 0; `llvm-mc` (19, gfx1013) encodes it in bit 17, and the check now has one `llvm-mc`-built fixture per refused modifier plus patched reserved-bit fixtures. Not measured: no draw samples a mip level on WARP or hardware through this lowering, so the level selection is checked on the generated HLSL text only. [HLSL SampleLevel](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-samplelevel) 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('