From dc5f203b95074505c596f153765f9b8da6a72c36 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:48:28 +0100 Subject: [PATCH 1/2] startup: translate v_swap_b32 in gfx2hlsl --- .github/workflows/windows.yml | 1 + CMakeLists.txt | 3 + docs/experiments.md | 10 ++ tests/startup_gfx2hlsl_swap_check.py | 132 +++++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 16 ++++ 5 files changed, 162 insertions(+) create mode 100644 tests/startup_gfx2hlsl_swap_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 37a3e5297..345a43fa2 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -195,6 +195,7 @@ jobs: tests/startup_translator_check.py \ tests/startup_gfx2hlsl_smem_offset_check.py \ tests/startup_gfx2hlsl_scalar_m0_check.py \ + tests/startup_gfx2hlsl_swap_check.py \ tests/startup_triage_run_check.py \ tests/startup_run_ps1_check.py \ tests/startup_flat_param_check.py \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a0aabfa0..2bbe16b38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2324,6 +2324,9 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: the SMEM offset 'off' is the literal 0; m0 and other operands keep their handling (#1674). add_test(NAME startup_gfx2hlsl_smem_offset COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_smem_offset_check.py") + # v_swap_b32 exchanges two VGPRs in the translator (#1723). + add_test(NAME startup_gfx2hlsl_swap COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_swap_check.py") add_test(NAME startup_triage_run COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_triage_run_check.py") add_test(NAME startup_run_ps1 COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index 1806b9161..3eb51f593 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11194,6 +11194,16 @@ old lowering used as y. Full-width loads still accept cache-policy flags. Plain patched 2D `a16` instruction carrying an unexpected NSA extension are refused, and that the runtime and offline cache keys use the same revision. The check verifies translation on the CPU. +## Startup translator: v_swap_b32 + +The VOP1 `v_swap_b32 vdst, vsrc` exchanges two VGPRs. The translator refused it with `Unsupported: v_swap_b32`; in the offline shader-archive census of #1723, 2 of the 1,171 pixel and vertex program pairs stop there. The handler (`op_v_swap_b32` in `tools/startup/gfx2hlsl.py`) emits `{ uint swap_tmp = vA; vA = vB; vB = swap_tmp; }`, guarded by `if (exec & 1)` when the exec mask is not known, emits nothing when both registers are the same, and drops the proven-uniform status of both registers. The instruction has no output or source modifier and no VOP3 form (LLVM 19 rejects `v_swap_b32_e64` for gfx1010 and gfx1030); two VGPR operands are required, so a scalar or constant source, a modifier or an extra operand is refused, and the SDWA spelling stays refused by the decoder. The operation follows the [AMD RDNA 2 ISA](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) (VOP1 opcode 101). + +The fixture (`tests/startup_gfx2hlsl_swap_check.py`) is synthetic only: it builds the instruction words from the public VOP1 field layout and checks them against the bytes LLVM 19 assembles (`02 cb 02 7e` for `v_swap_b32 v1, v2`). It checks the emitted exchange, the exec guard, the same-register case, a later read of the swapped register, the uniform status, five refusals, a modifier and the SDWA spelling. It was not run on Windows: the emitted text is compared, not compiled or drawn. The pixel path lowers the same instruction in `tools/pixel_aot.py`. + +```powershell +ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_swap$' -V +``` + ## Startup translator: pixel depth export The startup translator emits `SV_Depth` for an uncompressed `exp mrtz vN, off, off, off` in a straight-line pixel shader that still uses its entry execution mask. Color exports in the same shader keep their `SV_Target` outputs. Additional MRTZ channels, packed exports, multiple depth exports, branches and changes to the execution mask before the depth export are refused. diff --git a/tests/startup_gfx2hlsl_swap_check.py b/tests/startup_gfx2hlsl_swap_check.py new file mode 100644 index 000000000..a6b61ac71 --- /dev/null +++ b/tests/startup_gfx2hlsl_swap_check.py @@ -0,0 +1,132 @@ +"""v_swap_b32 in tools/startup/gfx2hlsl.py (found by the offline shader census in #1723). + +usage: startup_gfx2hlsl_swap_check.py + +The VOP1 `v_swap_b32 vdst, vsrc` exchanges two VGPRs. The translator refused it with +`Unsupported: v_swap_b32`; 2 of the 1,171 pixel and vertex program pairs of the census stop there. + +The instruction words are **constructed here** from the public VOP1 field layout (AMD RDNA 2 ISA, VOP1: +[8:0] SRC0, [16:9] OP, [24:17] VDST, [31:25] 0b0111111) with OP = 101 for V_SWAP_B32, and are also checked +against the bytes LLVM 19 assembles for `v_swap_b32 v1, v2` (02 cb 02 7e, gfx1010 and gfx1030). +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +V_SWAP_B32 = 101 +VOP1_PREFIX = 0b0111111 + + +def encode_vop1(op, vdst, src0): + """A VOP1 word; src0 uses the 9-bit source field (VGPRs from 256).""" + return struct.pack(' Date: Tue, 24 Nov 2026 20:04:16 +0100 Subject: [PATCH 2/2] startup: reject VGPR ranges in v_swap_b32 and compile emitted HLSL with DXC/FXC --- CMakeLists.txt | 5 ++- docs/experiments.md | 4 +- tests/startup_gfx2hlsl_swap_check.py | 63 ++++++++++++++++++++++++++-- tools/startup/gfx2hlsl.py | 4 +- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bbe16b38..9dc1de169 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2324,9 +2324,10 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: the SMEM offset 'off' is the literal 0; m0 and other operands keep their handling (#1674). add_test(NAME startup_gfx2hlsl_smem_offset COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_smem_offset_check.py") - # v_swap_b32 exchanges two VGPRs in the translator (#1723). + # v_swap_b32 exchanges two VGPRs in the translator (#1723); compiles the emitted HLSL with DXC and FXC. add_test(NAME startup_gfx2hlsl_swap COMMAND "${INTAKE_PYTHON}" -B - "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_swap_check.py") + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_swap_check.py" compile "${DXC_EXECUTABLE}" + $<$:${STARTUP_FXC}>) add_test(NAME startup_triage_run COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_triage_run_check.py") add_test(NAME startup_run_ps1 COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index 3eb51f593..620b24722 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11196,9 +11196,9 @@ and offline cache keys use the same revision. The check verifies translation on ## Startup translator: v_swap_b32 -The VOP1 `v_swap_b32 vdst, vsrc` exchanges two VGPRs. The translator refused it with `Unsupported: v_swap_b32`; in the offline shader-archive census of #1723, 2 of the 1,171 pixel and vertex program pairs stop there. The handler (`op_v_swap_b32` in `tools/startup/gfx2hlsl.py`) emits `{ uint swap_tmp = vA; vA = vB; vB = swap_tmp; }`, guarded by `if (exec & 1)` when the exec mask is not known, emits nothing when both registers are the same, and drops the proven-uniform status of both registers. The instruction has no output or source modifier and no VOP3 form (LLVM 19 rejects `v_swap_b32_e64` for gfx1010 and gfx1030); two VGPR operands are required, so a scalar or constant source, a modifier or an extra operand is refused, and the SDWA spelling stays refused by the decoder. The operation follows the [AMD RDNA 2 ISA](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) (VOP1 opcode 101). +The VOP1 `v_swap_b32 vdst, vsrc` exchanges two VGPRs. The translator refused it with `Unsupported: v_swap_b32`; in the offline shader-archive census of #1723, 2 of the 1,171 pixel and vertex program pairs stop there. The handler (`op_v_swap_b32` in `tools/startup/gfx2hlsl.py`) emits `{ uint swap_tmp = vA; vA = vB; vB = swap_tmp; }`, guarded by `if (exec & 1)` when the exec mask is not known, emits nothing when both registers are the same, and drops the proven-uniform status of both registers. The instruction has no output or source modifier and no VOP3 form (LLVM 19 rejects `v_swap_b32_e64` for gfx1010 and gfx1030); two single VGPR operands (`v0`..`v255`) are required, so a scalar, constant, register range, out-of-range VGPR, modifier or wrong operand count is refused, and the SDWA, DPP16 and VOP3A spellings stay refused by the decoder. The operation follows the [AMD RDNA 2 ISA](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) (VOP1 opcode 101). -The fixture (`tests/startup_gfx2hlsl_swap_check.py`) is synthetic only: it builds the instruction words from the public VOP1 field layout and checks them against the bytes LLVM 19 assembles (`02 cb 02 7e` for `v_swap_b32 v1, v2`). It checks the emitted exchange, the exec guard, the same-register case, a later read of the swapped register, the uniform status, five refusals, a modifier and the SDWA spelling. It was not run on Windows: the emitted text is compared, not compiled or drawn. The pixel path lowers the same instruction in `tools/pixel_aot.py`. +The fixture (`tests/startup_gfx2hlsl_swap_check.py`) is synthetic only: it builds the instruction words from the public VOP1 field layout and checks them against the bytes LLVM 19 assembles (`02 cb 02 7e` for `v_swap_b32 v1, v2`). It checks the emitted exchange, the exec guard, the same-register case, a later read of the swapped register, the uniform status, nine operand refusals, a modifier, and the SDWA, DPP16 and VOP3A spellings. On Windows (`compile` mode, registered in CTest), it also compiles a full translated pixel shader containing two sequential swaps and an exec-guarded swap with DXC (`ps_6_0`) and FXC (`ps_5_1`) cleanly with no warnings. Not measured: no WARP or hardware draw runs through this check. The pixel path lowers the same instruction in `tools/pixel_aot.py`. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_swap$' -V diff --git a/tests/startup_gfx2hlsl_swap_check.py b/tests/startup_gfx2hlsl_swap_check.py index a6b61ac71..7a9408df3 100644 --- a/tests/startup_gfx2hlsl_swap_check.py +++ b/tests/startup_gfx2hlsl_swap_check.py @@ -11,12 +11,17 @@ """ import os import struct +import subprocess import sys +import tempfile +from pathlib import Path here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, here) sys.path.insert(0, os.path.join(here, '..', 'tools')) sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) import gfx2hlsl as G # noqa: E402 +from startup_flat_param_check import VS_WORDS, root_blob # noqa: E402 failures = [] @@ -99,10 +104,14 @@ def main(): check('uniform status is dropped for both swapped registers', stage.uniform_v == {6}, repr(stage.uniform_v)) # Refusals: a scalar or constant source cannot be encoded by the hardware, so a hand-written line - # must not be accepted either; modifiers and extra operands are refused, not dropped. + # must not be accepted either; modifiers, register ranges, out-of-range VGPRs and extra operands are refused. for label, asm in (('scalar source', 'v_swap_b32 v1, s2'), ('constant source', 'v_swap_b32 v1, 0'), ('scalar destination', 'v_swap_b32 s1, v2'), ('missing source', 'v_swap_b32 v1'), - ('third operand', 'v_swap_b32 v1, v2, v3')): + ('third operand', 'v_swap_b32 v1, v2, v3'), + ('destination register range', 'v_swap_b32 v[1:2], v3'), + ('source register range', 'v_swap_b32 v1, v[2:3]'), + ('out-of-range destination VGPR', 'v_swap_b32 v256, v1'), + ('out-of-range source VGPR', 'v_swap_b32 v1, v256')): stage = Stage() try: stage.one('v_swap_b32', asm) @@ -118,15 +127,63 @@ def main(): ok = True check('a modifier is refused', ok and stage.lines == []) - # The SDWA spelling (src0 field 249 plus a second dword) is not a swap the translator may run. + # The SDWA, DPP16 and VOP3A spellings are not swaps the translator may run. sdwa = struct.pack(' 1: + if sys.argv[1] != 'compile' or len(sys.argv) not in (3, 4): + raise SystemExit('usage: startup_gfx2hlsl_swap_check.py [compile [fxc]]') + check_compile(ps_hlsl, sys.argv[2], sys.argv[3] if len(sys.argv) == 4 else None) + if failures: print('\nFAILED: ' + ', '.join(failures)) sys.exit(1) print('PASS') +def pipeline_hlsl_ps(): + prolog = bytes.fromhex('0203fcbe' '020002c8' '020106c8' '02020ac8' '02030ec8') + swaps = encode_vop1(V_SWAP_B32, 0, 256 + 1) + encode_vop1(V_SWAP_B32, 2, 256 + 3) + saveexec = struct.pack(' 0 and not run.stderr.strip() + check('%s (%s) compiles emitted swap HLSL cleanly' % (kind, profile), ok, + 'rc=%d stdout=%r stderr=%r' % (run.returncode, run.stdout, run.stderr)) + + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index dfffc2e82..2c44abd6e 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1275,7 +1275,9 @@ def op_v_swap_b32(self, ops, mods): if mods or len(ops) != 2: raise Unsupported('v_swap_b32 with %d operands and modifiers %s' % (len(ops), mods)) dst, src = reg_range(ops[0]), reg_range(ops[1]) - if not dst or not src or dst[0] != 'v' or src[0] != 'v': + if (not dst or not src or dst[0] != 'v' or src[0] != 'v' or + dst[1] != dst[2] or src[1] != src[2] or + not (0 <= dst[1] <= 255 and 0 <= src[1] <= 255)): raise Unsupported('v_swap_b32 needs two VGPRs, got %s and %s' % (ops[0], ops[1])) a, b = dst[1], src[1] for n in (a, b):