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..9dc1de169 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2324,6 +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); 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" 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 1806b9161..620b24722 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 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, 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 +``` + ## 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..7a9408df3 --- /dev/null +++ b/tests/startup_gfx2hlsl_swap_check.py @@ -0,0 +1,189 @@ +"""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 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 = [] + + +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(' 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 1a7bf631f..2c44abd6e 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1269,6 +1269,24 @@ def op_v_readfirstlane_b32(self, ops, mods): def op_v_mov_b32(self, ops, mods): self.set_v(reg_range(ops[0])[1], self.u(ops[1])) + def op_v_swap_b32(self, ops, mods): + # VOP1 only: VDST and SRC0 are both VGPRs and exchange values. It has no output or source modifier + # and no VOP3, SDWA or DPP form here, so anything extra is refused instead of dropped. + 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' 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): + self.uniform_v.discard(n) + self.used_v.add(n) + if a != b: + swap = '{ uint swap_tmp = v%d; v%d = v%d; v%d = swap_tmp; }' % (a, a, b, b) + self.emit(swap if self.exec_known else 'if (exec & 1) %s' % swap) + def op_v_cndmask_b32(self, ops, mods): # The select takes no output modifier: a clamp or mul on it would change the value, so a # modifier that reaches here is refused rather than dropped from the emitted text.