diff --git a/docs/design.md b/docs/design.md index 1889e30860..46437f3455 100644 --- a/docs/design.md +++ b/docs/design.md @@ -465,7 +465,7 @@ How the generated SPIR-V expects the runtime to work: | `v_mqsad_u32_u8` | Four masked byte-SADs with full 32-bit accumulators: D[k] = S2[k] + `v_msad_u8` of the 32-bit window S0[8k+31:8k] of the 64-bit S0 against S1, wrapping at 32 bits (S2 and D are four VGPRs). Every source is read before the first write; `clamp` raises `Unsupported`. Source: RDNA ISA pseudocode. CPU-checked by `tests/semantic/test_alu_mqsad_u32.py`. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across 64-bit sliding window and masked byte values against the 32-bit accumulators: bit-exact with the ISA reference. | | `s_sext_i32_i8`, `s_sext_i32_i16`, `s_flbit_i32` | The first two sign-extend the low byte or the low half of S0 to 32 bits (`OpBitFieldSExtract`). `s_flbit_i32` counts how many bits in a row from the MSB equal the sign bit, and gives −1 when all bits are the same (0 and −1); it is the scalar form of `v_ffbh_i32` and goes through the same code. None of the three writes SCC. Source: RDNA2 ISA SOP1 opcodes 23, 25 and 26. CPU-checked by `tests/semantic/test_salu_sext_flbit.py` (#549): edge words with SCC starting at 0 and at 1, and 30 seeded values with long runs of sign bits. Not in the translated corpus. `s_sext_i32_i8` and `s_flbit_i32` are GPU-checked on run-time operands by `runtime/test/shaders/cs_w32_salu_ext.asm` (exact on an RTX 4070 Ti, NVIDIA 620.70.06, Linux, and on llvmpipe). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py (500 programs, --oracle translator,hw): 500 pass, 0 mismatches against the reference; confirmed s_flbit_i32 gives 0xFFFFFFFF for 0 and -1. | | `v_lerp_u8` | Per byte i: `D.b[i] = (S0.b[i] + S1.b[i] + (S2.b[i] & 1)) >> 1`, an unsigned average where bit 0 of S2's byte decides whether 0.5 rounds up. The translator uses the per-byte identity `x + y = 2*(x & y) + (x ^ y)`. It clears each byte's low bit before shifting, then adds the rounding bit only to an odd sum. Each result fits in one byte, so the packed additions do not carry across bytes. The 9-bit sum shifted right by 1 fits back in its byte, so nothing carries between bytes. `clamp` raises `Unsupported` (the ISA doesn't define it for this op). Source: RDNA2 ISA VOP3 opcode 333. CPU-checked by `tests/semantic/test_alu_lerp_u8.py` (#577): a 9×9×5 grid of edge and rounding words, 40 seeded cases, an SGPR source with an inline S2, `clamp` rejected. The synthetic `tests/semantic/fixtures/lerp_packed_bytes.asm` gives exact words across a 64x64 draw on an RTX 3070 (NVIDIA 591.86). Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across byte edge values and rounding bit variations: 0 mismatches against the ISA reference. | -| Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `s_cmov_b64`, `s_pack_ll/lh/hh_b32_b16`) | `s_cmov_b32` / `s_cmovk_i32`: D = SCC ? S0 : D (`s_cmovk_i32`'s S0 is the 16-bit immediate, sign-extended); SCC is read, not written; the destination must be an SGPR (a lane-mask destination stays `Unsupported`, #210). `s_cmov_b64`: D[63:0] = SCC ? S0[63:0] : D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask (EXEC, VCC or a mask SGPR) stays `Unsupported` (#210) — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask. D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask stays `Unsupported` (#210). D[63:0], SCC read and not written, on plain 64-bit data pairs (SGPR pair, immediate or `null`); any dword that is or has been a lane mask (EXEC, VCC or a mask SGPR) makes the pair `Unsupported` — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask (#210). `s_pack_*`: the low half of D from S0, the high half from S1, the letters naming the half of each (l = 15:0, h = 31:16), S0 first; GFX10 has no `s_pack_hl`; SCC not written. Source: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU `SOPInstructions.td` (`S_CMOV_B64 : RDNA2 ISA SOP1/SOP2/SOPK. CPU-checked by `tests/semantic/test_salu_cmov.py` and `test_salu_pack.py`; the translated shaders were run on NVIDIA by reviewers (`s_cmov*`: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU [SOPInstructions.td](https://github.com/llvm/llvm-project/blob/2bfedf3338162e2c841a73cd7cd9e98d00add608/llvm/lib/Target/AMDGPU/SOPInstructions.td) (`S_CMOV_B64 : SOP1_64` under `Uses = [SCC]`). CPU-checked by `tests/semantic/test_salu_cmov.py`, `tests/semantic/test_salu_cmov_b64.py` and `test_salu_pack.py`; no corpus count; **not GPU-verified, no corpus count**; the translated shaders were run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: **no corpus count**; Hardware: on native `gfx1036` (Ryzen 9 9950X3D iGPU, Windows 11, driver 26.10.1) `tools/int_hw_probe.py --oracle both --wave both` matches the translator in wave32 and wave64 for all five `salu_cmov_pack` cases and for the 14 `s_cmov_b64` cases (`--group salu_cmov_b64`: GPU-checked for `s_cmov_b32` (`cs_w32_salu64.asm`, SCC = 1 selects S0) and `s_pack_lh_b32_b16` (`cs_w32_int_ext.asm`) on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92); `s_cmovk_i32`, `s_cmov_b64` and the other `s_pack_*` forms have no GPU test on main; the translated shaders were also run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: RTX 5090, RTX 4050 and Radeon 740M in #613; `s_pack_*`: RTX 5090 in #449). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept), and on native gfx1036 (AMD Radeon Graphics, Windows 11, driver 26.10.1) through `tools/hw_oracle`: all five `salu_cmov_pack` cases and the 14 `s_cmov_b64` cases match the translator in wave32 and wave64 (SCC 0 and 1; an SGPR pair, the inline constants -1/-16/64, the literal 0x89abcdef, `null` and the destination itself; SCC = 1 takes S0, 0 keeps D, SCC is never written), `s_cmov_b32` matches under SCC=0 and SCC=1, and `s_pack_ll/lh/hh_b32_b16` is bit-exact on 500 random programs with 0 mismatches (`--oracle translator,hw`). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Also **measured on RDNA2 gfx1036** (Radeon iGPU of a Ryzen 9 9950X3D, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher, rocm-sdk-core clang): `tools/int_hw_probe.py --oracle both --wave both` gives the same words as the translator for all five `salu_cmov_pack` cases in wave32 and wave64, and the 14 `s_cmov_b64` cases of the `salu_cmov_b64` group (`python -I tools/int_hw_probe.py --oracle both --wave both --group salu_cmov_b64`; SCC 0 and 1; source an SGPR pair, the inline constants -1, -16 and 64, the literal 0x89abcdef, `null`, and the destination itself; SCC preset and read back after): SCC preset and read back after) match the translator in wave32 and wave64: SCC = 1 takes S0 (a literal zero-extended, an inline constant sign-extended, `null` gives 0), SCC = 0 keeps D, SCC is never written. Also on `gfx1035` via `int_hw_probe` (SHADER-137, SCC kept), and on `gfx1036` and an RTX 5080 through the fixtures `cs_w32_salu64.asm` (`s_cmov_b32`) and `cs_w32_int_ext.asm` (`s_pack_lh_b32_b16`). `tools/hw_oracle` + `tools/salu_diff.py` verify `s_cmov_b32` under SCC 0/1 and `s_pack_ll/lh/hh_b32_b16` bit-exact on 500 random programs with 0 mismatches. Reviewers ran `s_cmov*` on an RTX 5090, an RTX 4050 and a Radeon 740M (#613) and `s_pack_*` on an RTX 5090 (#449). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py: s_cmov_b32 verified under SCC=0 and SCC=1 (preserving destination and updating respectively); s_pack_ll/lh/hh_b32_b16 verified bit-exact on 500 random programs with 0 mismatches (--oracle translator,hw). **Not measured on RDNA2.** | +| Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `s_cmov_b64`, `s_pack_ll/lh/hh_b32_b16`) | `s_cmov_b32` / `s_cmovk_i32`: D = SCC ? S0 : D (`s_cmovk_i32`'s S0 is the 16-bit immediate, sign-extended); SCC is read, not written; accepts SGPRs, `exec_lo`/`exec_hi`, `vcc_lo`/`vcc_hi`, `m0`, and `null` destinations, using the 32-bit lane-mask path (`_mask_b32_dst_half`/`_mask_b32_src`/`_store_b32_mask_dst`) for `exec`/`vcc` (preserving the untouched half in wave64 and extracting per-lane bits for constants/scalars or gathering opposite-half masks) and preserving `lane_masks` state for mask SGPRs, `vcc`, and `s_movk_i32` `0`/`-1` half-writes (CPU-checked by `tests/semantic/test_cmov32_mask_and_dst.py`, SHADER-CMOV32-MASK-AND-DST). `s_cmov_b64`: D[63:0] = SCC ? S0[63:0] : D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask (EXEC, VCC or a mask SGPR) stays `Unsupported` (#210) — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask. D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask stays `Unsupported` (#210). D[63:0], SCC read and not written, on plain 64-bit data pairs (SGPR pair, immediate or `null`); any dword that is or has been a lane mask (EXEC, VCC or a mask SGPR) makes the pair `Unsupported` — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask (#210). `s_pack_*`: the low half of D from S0, the high half from S1, the letters naming the half of each (l = 15:0, h = 31:16), S0 first; GFX10 has no `s_pack_hl`; SCC not written. Source: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU `SOPInstructions.td` (`S_CMOV_B64 : RDNA2 ISA SOP1/SOP2/SOPK. CPU-checked by `tests/semantic/test_salu_cmov.py` and `test_salu_pack.py`; the translated shaders were run on NVIDIA by reviewers (`s_cmov*`: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU [SOPInstructions.td](https://github.com/llvm/llvm-project/blob/2bfedf3338162e2c841a73cd7cd9e98d00add608/llvm/lib/Target/AMDGPU/SOPInstructions.td) (`S_CMOV_B64 : SOP1_64` under `Uses = [SCC]`). CPU-checked by `tests/semantic/test_salu_cmov.py`, `tests/semantic/test_salu_cmov_b64.py` and `test_salu_pack.py`; no corpus count; **not GPU-verified, no corpus count**; the translated shaders were run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: **no corpus count**; Hardware: on native `gfx1036` (Ryzen 9 9950X3D iGPU, Windows 11, driver 26.10.1) `tools/int_hw_probe.py --oracle both --wave both` matches the translator in wave32 and wave64 for all five `salu_cmov_pack` cases and for the 14 `s_cmov_b64` cases (`--group salu_cmov_b64`: GPU-checked for `s_cmov_b32` (`cs_w32_salu64.asm`, SCC = 1 selects S0) and `s_pack_lh_b32_b16` (`cs_w32_int_ext.asm`) on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92); `s_cmovk_i32`, `s_cmov_b64` and the other `s_pack_*` forms have no GPU test on main; the translated shaders were also run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: RTX 5090, RTX 4050 and Radeon 740M in #613; `s_pack_*`: RTX 5090 in #449). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept), and on native gfx1036 (AMD Radeon Graphics, Windows 11, driver 26.10.1) through `tools/hw_oracle`: all five `salu_cmov_pack` cases and the 14 `s_cmov_b64` cases match the translator in wave32 and wave64 (SCC 0 and 1; an SGPR pair, the inline constants -1/-16/64, the literal 0x89abcdef, `null` and the destination itself; SCC = 1 takes S0, 0 keeps D, SCC is never written), `s_cmov_b32` matches under SCC=0 and SCC=1, and `s_pack_ll/lh/hh_b32_b16` is bit-exact on 500 random programs with 0 mismatches (`--oracle translator,hw`). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Also **measured on RDNA2 gfx1036** (Radeon iGPU of a Ryzen 9 9950X3D, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher, rocm-sdk-core clang): `tools/int_hw_probe.py --oracle both --wave both` gives the same words as the translator for all five `salu_cmov_pack` cases in wave32 and wave64, and the 14 `s_cmov_b64` cases of the `salu_cmov_b64` group (`python -I tools/int_hw_probe.py --oracle both --wave both --group salu_cmov_b64`; SCC 0 and 1; source an SGPR pair, the inline constants -1, -16 and 64, the literal 0x89abcdef, `null`, and the destination itself; SCC preset and read back after): SCC preset and read back after) match the translator in wave32 and wave64: SCC = 1 takes S0 (a literal zero-extended, an inline constant sign-extended, `null` gives 0), SCC = 0 keeps D, SCC is never written. Also on `gfx1035` via `int_hw_probe` (SHADER-137, SCC kept), and on `gfx1036` and an RTX 5080 through the fixtures `cs_w32_salu64.asm` (`s_cmov_b32`) and `cs_w32_int_ext.asm` (`s_pack_lh_b32_b16`). `tools/hw_oracle` + `tools/salu_diff.py` verify `s_cmov_b32` under SCC 0/1 and `s_pack_ll/lh/hh_b32_b16` bit-exact on 500 random programs with 0 mismatches. Reviewers ran `s_cmov*` on an RTX 5090, an RTX 4050 and a Radeon 740M (#613) and `s_pack_*` on an RTX 5090 (#449). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py: s_cmov_b32 verified under SCC=0 and SCC=1 (preserving destination and updating respectively); s_pack_ll/lh/hh_b32_b16 verified bit-exact on 500 random programs with 0 mismatches (--oracle translator,hw). **Not measured on RDNA2.** | | `v_bfi_b32` | `(S0 & S1) \| (~S0 & S2)` (RDNA2 ISA, VOP3 opcode 330). A literal or known same-block SGPR mask of zero or all ones selects S2 or S1 directly. A contiguous mask uses `OpBitFieldInsert` with S1 shifted down by the field offset, since RDNA2 reads the bits in place. For sparse constant masks the translator precomputes the complement. Runtime masks use the full bitwise expression. Checked by the hand-written `bfi_known_mask.asm` and `tests/semantic/test_bfi_known_mask.py`. | | `v_bfm_b32` | The low five bits of S0 give the width and those of S1 give the offset. The result is the low 32 bits of `((1 << width) - 1) << offset` (RDNA2 ISA §12.12, VOP3 opcode 867). Literal and same-block SGPR constants are folded in the translator, including zero width and shifts that discard the high bits. Runtime operands keep their low-five-bit masks. Checked by `tests/semantic/test_bfm_known_operands.py` and `test_alu_int_misc.py`. | | `s_bfe_u32` / `s_bfe_i32` past bit 31 | offset = S1[4:0], width = S1[22:16] (7 bits; S1 bits 23 to 31 are ignored). When offset + width > 32 the field stops at bit 31: width = min(width, 32 - offset), and `s_bfe_i32` sign-extends from the top bit that exists. Example: `s_bfe_i32 s4, 0xd2f90159, 0x7f001c` gives `0xfffffffd` (4-bit field `0xd`), not `0xd`. SCC = D ≠ 0. Source: measured on gfx1150 (Radeon 880M) with `tools/salu_diff.py --oracle hw`, 4000 random control words, all bit-exact, on gfx1201 (Radeon RX 9070, RDNA4) with 10,000 `salu_diff --oracle hw` programs, 0 mismatches (seed 500 with `--bfe-any`), and **on RDNA2** (AMD Radeon 680M, `gfx1035`: `tools/salu_diff.py --oracle hw,translator --bfe-any --programs 500 --seed 1`, 500/500 match, plus `tools/int_hw_probe.py --oracle both` wave32 and wave64, SHADER-137; and RDNA2 `gfx1036`, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher: `tools/salu_diff.py --oracle translator,hw --bfe-any --ops s_bfe_u32,s_bfe_i32 --programs 2000` with seeds 1, 2 and 3, 6,000 of 6,000 programs pass on both sides, plus seed 4 over every opcode with `--bfe-any`, 2,000 of 2,000, and `int_hw_probe.py`'s `salu_bfe_past31` cases in wave32 and wave64); the translator (`bfe_width`) already did this. `tools/salu_diff.py --oracle hw,translator --bfe-any --programs 500 --seed 1`, 500/500 match, plus `tools/int_hw_probe.py --oracle both` wave32 and wave64, SHADER-137); the translator (`bfe_width`) already did this. Source: measured on gfx1150 (Radeon 880M) with `tools/salu_diff.py --oracle hw`, 4000 random control words, all bit-exact, and on gfx1201 (Radeon RX 9070, RDNA4) with 10,000 `salu_diff --oracle hw` programs, 0 mismatches (seed 500 with `--bfe-any`); the translator (`bfe_width`) already did this. LLVM's v_bfe fold (`constantFoldBFE`) is analogous but has a 5-bit width. **RDNA2 is not measured**; the ISA pseudocode does not say. | diff --git a/tests/semantic/test_cmov32_mask_and_dst.py b/tests/semantic/test_cmov32_mask_and_dst.py new file mode 100644 index 0000000000..0d5d7ae9dd --- /dev/null +++ b/tests/semantic/test_cmov32_mask_and_dst.py @@ -0,0 +1,321 @@ +"""Semantic tests for s_cmov_b32, s_cmovk_i32, and s_movk_i32 with special and lane-mask destinations. + +Verifies (wave32 and wave64, CPU interpreter, stdlib only): + 1. s_cmov_b32 / s_cmovk_i32 targeting exec_lo / exec_hi: per-lane bit extraction for constants and scalar + SGPRs, cross-half wave64 gather (exec_lo <- vcc_hi), untouched-half preservation in wave64, and SCC kept. + 2. s_cmov_b32 / s_cmovk_i32 targeting vcc_lo / vcc_hi: per-lane mask update, untouched-half preservation in + wave64, and lane-mask tracking preserved for subsequent v_cndmask_b32 / s_bcnt1_i32_b64. + 3. s_cmov_b32 / s_cmovk_i32 targeting m0 and null (SCC preserved). + 4. s_cmov_b32 / s_cmovk_i32 targeting a lane-mask SGPR (mask source, 0, -1, 0xffff): preserves lane_masks + state so a subsequent s_bcnt1_i32_b32 counts the wave's bits instead of treating the lane bool as data. + 5. s_movk_i32 targeting wave64 vcc_lo / vcc_hi with 0 and -1 (0xffff) preserves the other half of VCC, and + s_movk_i32 sN, 0/-1 produces EITHER in lane_masks so CFG merges with a lane mask stay a lane mask. +""" +from __future__ import annotations + +import gc +import os +import struct +import sys +import tempfile +import unittest +import warnings + +HERE = os.path.dirname(os.path.abspath(__file__)) +ROOT = os.path.dirname(os.path.dirname(HERE)) +TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") + +sys.dont_write_bytecode = True +for _p in (HERE, TOOLS_DIR): + if _p not in sys.path: + sys.path.insert(0, _p) + +import rdna2spirv # noqa: E402 +import spirv_mini_interp as si # noqa: E402 + +M32 = 0xFFFFFFFF +STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" str: + has_labels = any(ln.endswith(":") for ln in lines) + encoded_lines = lines + if has_labels: + lbl_idx = {} + ins_count = 0 + for ln in lines: + if ln.endswith(":"): + lbl_idx[ln[:-1]] = ins_count + else: + ins_count += 1 + encoded_lines = [] + idx = 0 + for ln in lines: + if ln.endswith(":"): + continue + enc = 0 + if ln.startswith(("s_cbranch_", "s_branch ")): + op, target = ln.split() + rel = (lbl_idx[target] - idx - 1) & 0xFFFF + enc = 0xBF880000 | rel + signed = rel if rel < 0x8000 else rel - 0x10000 + ln = f"{op} {signed}" + enc_s = ",".join(f"0x{x:02x}" for x in enc.to_bytes(4, "little")) + encoded_lines.append(f"{ln:<44} ; encoding: [{enc_s}]") + idx += 1 + with tempfile.TemporaryDirectory() as tmp: + os.mkdir(os.path.join(tmp, "disasm")) + os.mkdir(os.path.join(tmp, "fssm")) + src = os.path.join(tmp, "disasm", f"cs_{name}.s") + with open(src, "w", encoding="utf-8", newline="\n") as f: + f.write(f"; cs_{name}\n") + for ln in encoded_lines: + f.write(f"\t{ln}\n") + with open(os.path.join(tmp, f"cs_{name}.dxbc"), "wb") as f: + f.write(STUB_DXBC) + with open(os.path.join(tmp, "fssm", f"cs_{name}.fssm"), "wb") as f: + # FSSM magic + wave64 bit (u32[0xD4] bit 5 = 0) so wave64 shaders stay wave64 even with b32-only ops. + hdr = bytearray(0x138) + hdr[0:4] = b"FSSM" + if threads == 32: + struct.pack_into(" dict[int, int]: + pc = VSHARP + [s4 & M32, s5 & M32, s6 & M32, s7 & M32] + [0] * 24 + return si.run_workgroup( + spv, threads, threads, + inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i}, + push_constants=pc, memory={}, + ) + + +def bits_list(mask: int, width: int) -> list[int]: + return [1 if (mask >> i) & 1 else 0 for i in range(width)] + + +class Cmov32MaskAndDstTests(unittest.TestCase): + def test_cmov_and_cmovk_exec_wave32_and_wave64(self): + # Wave32: s_cmov_b32 exec_lo, s4 and s_cmovk_i32 exec_lo, 0x5 with SCC in {0, 1}. + asm32 = [ + "s_mov_b32 s10, exec_lo", + "v_mov_b32_e32 v4, 10", + "s_cmp_eq_u32 s6, 1", + "s_cmov_b32 exec_lo, s4", + "s_cselect_b32 s20, 1, 0", + "v_mov_b32_e32 v4, 20", + "s_mov_b32 exec_lo, s10", + "v_mov_b32_e32 v5, 30", + "s_cmp_eq_u32 s6, 1", + "s_cmovk_i32 exec_lo, 0x5", + "s_cselect_b32 s21, 1, 0", + "v_mov_b32_e32 v5, 40", + "s_mov_b32 exec_lo, s10", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v4, v6, s[0:3], 0 offen", + "buffer_store_dword v5, v6, s[0:3], 0 offen offset:128", + "v_mov_b32_e32 v7, s20", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:256", + "v_mov_b32_e32 v7, s21", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:384", + "s_endpgm", + ] + spv32 = translate_cs("cmov_exec_w32", asm32, 32) + for s4, scc in ((0x000000A5, 0), (0x000000A5, 1), (0x80000001, 1)): + with self.subTest(wave=32, s4=hex(s4), scc=scc): + mem = run_cs(spv32, 32, s4=s4, s6=scc) + ex1 = s4 if scc else M32 + ex2 = 0x5 if scc else M32 + self.assertEqual([mem[4 * i] for i in range(32)], + [20 if (ex1 >> i) & 1 else 10 for i in range(32)]) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], + [40 if (ex2 >> i) & 1 else 30 for i in range(32)]) + self.assertEqual([mem[256 + 4 * i] for i in range(32)], [scc] * 32) + self.assertEqual([mem[384 + 4 * i] for i in range(32)], [scc] * 32) + + # Wave64: s_cmov_b32 exec_lo, vcc_hi (cross-half, keeps exec_hi) and s_cmovk_i32 exec_hi, 0x3 (keeps exec_lo). + asm64 = W64_BUILD_VCC + [ + "s_mov_b64 s[10:11], exec", + "v_mov_b32_e32 v4, 10", + "s_cmp_eq_u32 s6, 1", + "s_cmov_b32 exec_lo, vcc_hi", + "v_mov_b32_e32 v4, 20", + "s_mov_b64 exec, s[10:11]", + "v_mov_b32_e32 v5, 30", + "s_cmp_eq_u32 s6, 1", + "s_cmovk_i32 exec_hi, 0x3", + "v_mov_b32_e32 v5, 40", + "s_mov_b64 exec, s[10:11]", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v4, v6, s[0:3], 0 offen", + "buffer_store_dword v5, v6, s[0:3], 0 offen offset:256", + "s_endpgm", + ] + spv64 = translate_cs("cmov_exec_w64", asm64, 64) + for lo, hi, scc in ((0x00FF00FF, 0x00010005, 0), (0x00FF00FF, 0x00010005, 1), (0x0, 0x80000002, 1)): + with self.subTest(wave=64, lo=hex(lo), hi=hex(hi), scc=scc): + mem = run_cs(spv64, 64, s4=lo, s5=hi, s6=scc) + ex1 = (hi | (M32 << 32)) if scc else ((M32 << 32) | M32) + ex2 = (M32 | (0x3 << 32)) if scc else ((M32 << 32) | M32) + self.assertEqual([mem[4 * i] for i in range(64)], + [20 if (ex1 >> i) & 1 else 10 for i in range(64)]) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], + [40 if (ex2 >> i) & 1 else 30 for i in range(64)]) + + def test_cmov_and_cmovk_vcc_m0_null(self): + # Wave64: s_cmov_b32 vcc_lo, s7 (preserves vcc_hi), s_cmovk_i32 vcc_hi, 0x0f (preserves vcc_lo), + # plus m0 and null destinations (SCC preserved). + asm = W64_BUILD_VCC + [ + "s_mov_b32 m0, 0x11112222", + "s_cmp_eq_u32 s6, 1", + "s_cmov_b32 vcc_lo, s7", + "s_cmovk_i32 vcc_hi, 0x0f", + "s_cmov_b32 m0, s7", + "s_cmovk_i32 null, 0x1234", + "s_cselect_b32 s20, 1, 0", + "s_bcnt1_i32_b64 s21, vcc", + "v_cndmask_b32_e64 v4, 0, 1, vcc", + "v_mov_b32_e32 v5, m0", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v4, v6, s[0:3], 0 offen", + "buffer_store_dword v5, v6, s[0:3], 0 offen offset:256", + "v_mov_b32_e32 v7, s20", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:512", + "v_mov_b32_e32 v7, s21", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:768", + "s_endpgm", + ] + spv = translate_cs("cmov_vcc_m0_null", asm, 64) + for lo, hi, s7, scc in ((0x0F0F0003, 0x80000001, 0x000000A5, 0), + (0x0F0F0003, 0x80000001, 0x000000A5, 1)): + with self.subTest(lo=hex(lo), hi=hex(hi), s7=hex(s7), scc=scc): + mem = run_cs(spv, 64, s4=lo, s5=hi, s6=scc, s7=s7) + want_vcc = (s7 | (0x0F << 32)) if scc else (lo | (hi << 32)) + want_m0 = s7 if scc else 0x11112222 + self.assertEqual([mem[4 * i] for i in range(64)], bits_list(want_vcc, 64)) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], [want_m0] * 64) + self.assertEqual([mem[512 + 4 * i] for i in range(64)], [scc] * 64) + self.assertEqual([mem[768 + 4 * i] for i in range(64)], [want_vcc.bit_count()] * 64) + + def test_cmov_and_cmovk_preserve_sgpr_lane_mask_state(self): + # Wave32: s12 holds VCC mask; s_cmov_b32 s12, s13 (another lane mask) and s_cmovk_i32 s12, 0 / -1 + # must keep s12 in lane_masks so s_bcnt1_i32_b32 counts the wave's lanes rather than returning 32 or 0. + asm = W32_BUILD_VCC + [ + "s_mov_b32 s12, vcc_lo", + "v_mov_b32_e32 v1, s5", + "v_and_b32_e32 v2, 31, v0", + "v_lshrrev_b32_e32 v3, v2, v1", + "v_and_b32_e32 v3, 1, v3", + "v_cmp_ne_u32_e64 s13, 0, v3", + "s_cmp_eq_u32 s6, 1", + "s_cmov_b32 s12, s13", + "s_bcnt1_i32_b32 s20, s12", + "s_cmp_eq_u32 s7, 1", + "s_cmovk_i32 s12, 0", + "s_bcnt1_i32_b32 s21, s12", + "s_cmp_eq_u32 s7, 2", + "s_cmovk_i32 s12, 0xffff", + "s_bcnt1_i32_b32 s22, s12", + "v_lshlrev_b32_e32 v6, 2, v0", + "v_mov_b32_e32 v7, s20", + "buffer_store_dword v7, v6, s[0:3], 0 offen", + "v_mov_b32_e32 v7, s21", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:128", + "v_mov_b32_e32 v7, s22", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:256", + "s_endpgm", + ] + spv = translate_cs("cmov_sgpr_mask", asm, 32) + for s4, s5, s6, s7 in ((0x00000007, 0x0000001F, 0, 0), + (0x00000007, 0x0000001F, 1, 1), + (0x00000007, 0x0000001F, 0, 2)): + with self.subTest(s4=hex(s4), s5=hex(s5), s6=s6, s7=s7): + mem = run_cs(spv, 32, s4=s4, s5=s5, s6=s6, s7=s7) + m1 = s5 if s6 == 1 else s4 + m2 = 0 if s7 == 1 else m1 + m3 = M32 if s7 == 2 else m2 + self.assertEqual([mem[4 * i] for i in range(32)], [m1.bit_count()] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [m2.bit_count()] * 32) + self.assertEqual([mem[256 + 4 * i] for i in range(32)], [m3.bit_count()] * 32) + + def test_movk_i32_vcc_half_preserves_other_half_and_cfg_merge(self): + # Wave64: s_movk_i32 vcc_lo, 0 preserves vcc_hi; s_movk_i32 vcc_hi, 0 clears lanes 32..63 and preserves vcc_lo; + # s_movk_i32 vcc_hi, -1 sets lanes 32..63 and preserves vcc_lo. + cases = [ + ("vcc_lo, 0", lambda lo, hi: hi << 32), + ("vcc_hi, 0", lambda lo, hi: lo), + ("vcc_lo, -1", lambda lo, hi: M32 | (hi << 32)), + ("vcc_hi, 0xffff", lambda lo, hi: lo | (M32 << 32)), + ] + for idx, (op_args, ref) in enumerate(cases): + asm = W64_BUILD_VCC + [ + f"s_movk_i32 {op_args}", + "v_cndmask_b32_e64 v4, 0, 1, vcc", + "s_bcnt1_i32_b64 s20, vcc", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v4, v6, s[0:3], 0 offen", + "v_mov_b32_e32 v7, s20", + "buffer_store_dword v7, v6, s[0:3], 0 offen offset:256", + "s_endpgm", + ] + spv = translate_cs(f"movk_vcc_half_{idx}", asm, 64) + lo, hi = 0x00FF0005, 0x80000013 + with self.subTest(op_args=op_args): + mem = run_cs(spv, 64, s4=lo, s5=hi) + want = ref(lo, hi) + self.assertEqual([mem[4 * i] for i in range(64)], bits_list(want, 64)) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], [want.bit_count()] * 64) + + # CFG join: s_movk_i32 s12, 0 on one path and vcc_lo on the other must merge as MASK so s_bcnt1 counts lanes. + asm_cfg = W32_BUILD_VCC + [ + "s_movk_i32 s12, 0", + "s_cmp_eq_u32 s6, 0", + "s_cbranch_scc1 BB_join", + "s_mov_b32 s12, vcc_lo", + "BB_join:", + "s_bcnt1_i32_b32 s20, s12", + "v_lshlrev_b32_e32 v6, 2, v0", + "v_mov_b32_e32 v7, s20", + "buffer_store_dword v7, v6, s[0:3], 0 offen", + "s_endpgm", + ] + spv_cfg = translate_cs("movk_cfg_merge", asm_cfg, 32) + for s6 in (0, 1): + with self.subTest(cfg_s6=s6): + mem = run_cs(spv_cfg, 32, s4=0x00000015, s6=s6) + want = 0 if s6 == 0 else (0x00000015).bit_count() + self.assertEqual([mem[4 * i] for i in range(32)], [want] * 32) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/lane_masks.py b/tools/lane_masks.py index 07dddd6703..604d5e0481 100644 --- a/tools/lane_masks.py +++ b/tools/lane_masks.py @@ -249,6 +249,29 @@ def step(state, ins, wave64=True): elif op == "s_not_b32" and dst_t == "vcc_hi" and wave64 and state.get("vcc") in MASK_KINDS and _maskable_b32(a[1]): return put(state, a[0], k, wave64) + elif op == "s_movk_i32" and len(a) >= 2: + k = EITHER if (a[1]["k"] == "imm" and (a[1]["v"] & 0xFFFF) in (0, 0xFFFF)) else None + put(state, a[0], k, wave64) + elif op in ("s_cmov_b32", "s_cmovk_i32") and len(a) >= 2: + s0 = ({"k": "imm", "v": -1 if (a[1]["v"] & 0xFFFF) == 0xFFFF else (a[1]["v"] & 0xFFFF)} + if op == "s_cmovk_i32" and a[1]["k"] == "imm" else a[1]) + srcs = (s0, a[0]) + kinds = [kind(state, x, wave64) for x in srcs] + ms = {k for k in kinds if k in MASK_KINDS} + dst_t = a[0].get("t") if a[0]["k"] == "special" else None + if None not in kinds: + if dst_t == "vcc_hi" and wave64 and state.get("vcc") in MASK_KINDS: + return + put(state, a[0], (ms.pop() if len(ms) == 1 else MASK) if ms else EITHER, wave64) + elif not wave64 and dst_t in ("vcc", "vcc_lo") and all(_maskable_b32(x) for x in srcs): + put(state, a[0], MASK, wave64) + elif wave64 and dst_t in ("vcc_lo", "vcc_hi") and (state.get("vcc") in MASK_KINDS or ms) and \ + all(_maskable_b32(x) for x in srcs): + if dst_t == "vcc_hi" and state.get("vcc") in MASK_KINDS: + return + put(state, a[0], MASK, wave64) + else: + put(state, a[0], None, wave64) elif op.startswith(BITWISE): kinds = [kind(state, x, wave64) for x in a[1:]] ms = {k for k in kinds if k in MASK_KINDS} diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 6b32d01024..6c39665003 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -11137,18 +11137,26 @@ def f64(sv): self.sgpr_imm[a[0]["r"]] = val elif op in ("s_cmov_b32", "s_cmovk_i32"): # D = SCC ? S0 : D (s_cmovk_i32: S0 is the 16-bit immediate, sign-extended). SCC is read, not written. - if a[0]["k"] != "sgpr": - raise Unsupported(op + " destination") + d = a[0] + if d["k"] == "special" and d.get("t") == "null": + return if op == "s_cmovk_i32": v = a[1]["v"] & 0xFFFF - x = self.cu((v - 0x10000 if v & 0x8000 else v) & 0xFFFFFFFF) + s0 = {"k": "imm", "v": v - 0x10000 if v & 0x8000 else v} else: - x = self.src_u(a[1]) - old, c, r = self.tmp(), self.tmp(), self.tmp() - self.emit(f"{old} = OpCopyObject %uint {self.load_reg('sgpr', a[0]['r'])}") + s0 = a[1] + to_mask_dst, dst_half = self._mask_b32_dst_half(d, [s0, d]) + use_mask = to_mask_dst and self._maskable_b32(s0) and self._maskable_b32(d) + if use_mask: + x = self._mask_b32_src(s0, dst_half) + old = self._mask_b32_src(d, dst_half) + else: + x = self.src_u(s0) + old = self.src_u(d) + c, r = self.tmp(), self.tmp() self.emit(f"{c} = OpLoad %uint %scc") self.emit(f"{r} = OpSelect %uint {self.tmp_bool_nz(c)} {x} {old}") - self.dst(a[0], r) + self._store_b32_mask_dst(d, r, use_mask, dst_half) elif op == "s_cmov_b64": # RDNA2 ISA SOP1: D = SCC ? S0 : D, SCC read and not written (LLVM AMDGPU SOPInstructions.td: # S_CMOV_B64 : SOP1_64 under Uses = [SCC]). Plain 64-bit data only: a lane-mask pair holds one