From 9f1a072f7056b420096125317ceb001142e33f11 Mon Sep 17 00:00:00 2001 From: milk4sale <53350507+milk4sale@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:22:13 -0600 Subject: [PATCH] tests: a GPU fixture and whole-corpus counts for one-register wave64 mask destinations (TEST-W64-SDST-PAIR-GPU) SHADER-W64-SDST-PAIR makes the SGPR a wave64 VALU instruction names for a lane mask the pair it is on hardware. Its own test compares the two printed forms on the interpreter; this adds what it does not have. cs_w64x_cmp_sdst is written like the game's listings (no mattr line, so llvm-mc prints one register): six compares and a carry-out whose pairs are read back across both halves by s_and_b64, s_or_b64, s_xor_b64, s_andn2_b64, s_and_saveexec_b64, s_mov_b64 exec, s_bcnt1 / s_ff1 and SCC. RTX 4070 Ti (620.70.06, subgroup 32) under LEONIDA_W64_EMULATE=1: the eight words its header derives; main before the rule gave none of them. docs/design.md gets the counts over all 26,503 shaders: every one of the 292,733 one-register compares in wave64 code names an even register, 13,314 wave64 shaders read such a pair back, the rule's full translation passes spirv-val 26,503 of 26,503, and 504 wave64 VS/PS leave SHADER-10 tier C (8,245 to 7,741). Co-Authored-By: Claude Fable 5.2 --- docs/design.md | 1 + runtime/test/shaders/check.py | 4 +- runtime/test/shaders/cs_w64x_cmp_sdst.asm | 131 +++++++++++++++ scripts/run_gpu_tests.py | 3 +- .../fixtures/disasm/cs_w64x_cmp_sdst.s | 89 ++++++++++ .../semantic/test_wave64_sdst_pair_fixture.py | 155 ++++++++++++++++++ 6 files changed, 381 insertions(+), 2 deletions(-) create mode 100644 runtime/test/shaders/cs_w64x_cmp_sdst.asm create mode 100644 tests/semantic/fixtures/disasm/cs_w64x_cmp_sdst.s create mode 100644 tests/semantic/test_wave64_sdst_pair_fixture.py diff --git a/docs/design.md b/docs/design.md index 6ad0398057..ef10904378 100644 --- a/docs/design.md +++ b/docs/design.md @@ -504,6 +504,7 @@ How the generated SPIR-V expects the runtime to work: | `v_permlane16_b32` / `v_permlanex16_b32` with the source lane off in EXEC (SHADER-74) | The VOP3 modifier `op_sel:[FI, BOUND_CTRL]` decides what a lane gets when the lane it reads is off in EXEC. **FI=1** (`op_sel` bit 0, encoding 0x0800): the source lane's value, as before. **FI=0** (the default): the destination lane is not written and keeps its old value when BOUND_CTRL=0, and gets 0 when BOUND_CTRL=1 (`op_sel` bit 1, 0x1000). A source lane that is on gives its value whatever FI / BOUND_CTRL are, and a lane that is itself off is never written (normal EXEC rule). The translator shuffles the active bit with the same selector as the value (also in `--wave64-emulate`, where it is a second exchange). On a native subgroup the two shuffles are combined with bitwise masks, not a scalar select: absent source lanes are redirected to the calling lane first (as `ds_lane_read`), the value and the activity bit are fetched through the redirected index, and `ok` masks the shuffled value over the old one (over zero with BOUND_CTRL=1) and the destination write is masked too, so the ballot and both shuffles run before lane predication — MoltenVK 1.4.2 and MoltenVK main at SPIRV-Cross aa217aeb lowered the scalar select to a short-circuiting Metal ternary and the FI=0 lanes read the source lane's value instead, which `cs_w32_permlane_fi` caught on an Apple M1 Max (#5895); `v_permlanex16_b32` pairs the rows 0↔1 and 2↔3 of a wave64 as before. Until SHADER-74 every case returned the source value (right only for FI=1). Source of the bit mapping: LLVM `llvm.amdgcn.permlane16/permlanex16 ` (IntrinsicsAMDGPU.td) is selected in `VOP3Instructions.td` (`PermlanePat`) with `fi` as the `src0` op_sel bit and `bound_control` as the `src1` one, and the CodeGen tests `llvm.amdgcn.permlane.ll` print `op_sel:[1,0]` for FI and `op_sel:[0,1]` for BOUND_CTRL; `llvm-mc -mcpu=gfx1013` encodes `op_sel:[1,0]` as 0x0800 and `op_sel:[0,1]` as 0x1000 in the second byte pair of the first word (0xD777 / 0xD778). Behaviour: hardware measurement on RDNA2 (Ryzen 7950X3D iGPU, raw kernel, wave64 and wave32 consistent; the same on RDNA3.5) by aslan2142 in [#374](https://github.com/ItsNotRexed/Leonida/issues/374#issuecomment-7071597516). Pinned by `runtime/test/shaders/cs_w32_permlane_fi.asm` (EXEC 0x55555555, every active lane's source is off; out[0..7] = 1000 0 4 1 1000 0 52 49) in `check.py` and `scripts/run_gpu_tests.py`, and on the CPU by `tests/semantic/test_permlane_fi.py`. | | Null mask source | The scalar register `null` supplies zero when read as a lane mask. The wave64 fixture in `tests/semantic/fixtures/null_mask_source.asm` checks a high-bit test, a zero-bit count, a first-set-bit search and a masked vector write after `s_and_saveexec_b64`. LLVM 21.1.8 assembles each instruction for gfx1030. [LLVM GFX10 null operand](https://releases.llvm.org/15.0.0/docs/AMDGPUOperandSyntax.html#null) | | Wave64 VALU mask operands | The listings come from llvm-mc without `+wavefrontsize64`, so a VALU instruction's lane-mask SGPR is printed as one register (`v_cmp_gt_u32_e64 s8`, the `s10` of `v_cndmask_b32_e64`) where the wave64 hardware writes or reads the pair (`s[8:9]`, RDNA2 ISA VOP3A/VOP3B SDST). `widen_wave64_masks` makes it the pair before analysis: `v_cmp_*_e64`/`_sdwa` SDST, the carry out and in of `v_*_co_*`, `v_div_scale`, `v_mad_[ui]64_[ui]32`, the mask of `v_cndmask`. Before, the high register kept its old value and a later `s_mov_b64 exec, s[8:9]` gave lanes 32..63 its bits. Confirmed by `tests/semantic/test_wave64_single_sdst.py` (both printed forms translate to the same module, all 64 lanes run) and on an RTX 5080, where the pipeline-library program `lib_cs_03141` hung the GPU. | +| Wave64 VALU mask operands: corpus and GPU check (TEST-W64-SDST-PAIR-GPU) | What the row above rests on, measured over the whole corpus and on a GPU. **Encoding**: the same eight bytes assemble from `v_cmp_gt_u32_e64 s4, 40, v1` without `+wavefrontsize64` and from `v_cmp_gt_u32_e64 s[4:5], 40, v1` with `-mattr=+wavefrontsize64,-wavefrontsize32` (LLVM 23.1.4; the carry-out of `v_add_co_u32` likewise), and `tools/rdna2_decode.py` prints the pair for them when told the program is wave64 (`shader_disasm.py --backend python`), so the one-register print is llvm-mc's wave32 default and nothing in the code. **Corpus** (1.0.2.1, all 26,503 listings of one work folder, `llvm-mc` backend): no compare names a pair. The 18,864 wave64 shaders with a compare into an SGPR (9,282 MS, 7,855 PS, 774 VS, 758 CS, 195 GS) have 292,733 of them and **every one names an even register**, as do all 6,988 carry-outs into an SGPR, while wave32 code names an odd one in 137,832 of 299,058 compares and 1,250 of 2,788 carry-outs: aligned pairs, as a wave64 mask is allocated. Before the rule 13,314 wave64 shaders (7,120 PS, 4,918 MS, 664 CS, 612 VS) read such a pair back with a 64-bit scalar op while `lane_masks.py` knew its low register as a mask and nothing of its high one: 162,431 reads (`s_and_b64` 86,137, `s_or_b64` 50,843, `s_and_saveexec_b64` 15,399, `s_xor_b64` 6,387, `s_andn2_b64` 3,625, `s_mov_b64` 37, `s_bcnt1_i32_b64` 3), counted on `main` `1504c1af6`. **Full translation** with the rule (its commit on `main` `3ed8e50b7`, no `--wave64-emulate`): 26,503 translated and 26,503 pass `spirv-val`, as on `main` `1504c1af6`; 19,453 modules differ from that `main`'s, all of them wave64 (MS 9,282 of 9,282, PS 8,021 of 10,974, VS 1,169 of 2,236, CS 786 of 1,154, GS 195 of 195) and none of the 2,662 wave32 ones. **SHADER-10 tiers** of the wave64 VS/PS/GS (`--census` over the two indexes): on a 32-wide subgroup those shaders read the low register and were right, but `tools/wave64_halves.py` took the pair for "a lane mask combined with a register that is not one"; with the rule 504 shaders leave tier C (PS 149 to A and 7 to B, VS 59 to A and 289 to B) and none enters it: A 5,059 to 5,267, B 101 to 397, C 8,245 to 7,741 (PS 3,396/27/7,551 to 3,545/34/7,395, VS 1,468/74/694 to 1,527/363/346, GS 195/0/0). The reasons name what is left: `tainted-branch` 6,534 to 1,181, `tainted-exec` 6,360 to 5,829, `cross-lane` 4,630 to 3,721, `stale-scalar` 3,487 to 3,287, and `unsafe-vote-branch` 6,496 to 7,362 (a vote on such a pair was hidden behind `tainted-branch`); the other reasons are unchanged. **GPU**: `cs_w64x_cmp_sdst` has no `mattr` line, so llvm-mc assembles and prints it like the game's listings (every other wave64 fixture names the pair, which is why none saw the stale register): six compares and a carry-out whose pairs are read back across both halves by `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64`, `s_and_saveexec_b64`, `s_mov_b64 exec`, `s_bcnt1_i32_b64` / `s_ff1_i32_b64` and SCC, with another mask, 0 or nothing in the high register beforehand. On an NVIDIA GeForce RTX 4070 Ti (Linux, driver 620.70.06, subgroup 32 only) with `LEONIDA_W64_EMULATE=1` it stores `10240 6144 2053 8224 7714 6184 4144 5`, the words its header derives from the ISA; `main` `1504c1af6`, before the rule, stores `16384 16384`, five times `0xffffffff` and `1` in the same run. That run confirms the translated SPIR-V against the ISA reference, not native RDNA2 behaviour, and no 64-wide device (`--require-wave64`) was measured. CPU twin: `tests/semantic/test_wave64_sdst_pair_fixture.py` (the fixture on a 64-wide subgroup and under `--wave64-emulate`, its pair form translating to the same text, wave32 code keeping its register, and the LLVM form of a uniform branch, `v_cmp_* sN` / `s_and_b64 vcc, exec, s[N:N+1]` / `s_cbranch_vccnz`, as tier A with equal words from one wave of 64 lanes and two of 32). | | Wave64 mask ballot | `tools/lane_masks.py` tracks per-lane masks (including 32-bit `MASK_LO`/`MASK_HI` halves, `vcc_lo`/`vcc_hi`, `s_bfm_b64` bitfields, `_b64` copies of immediates into `vcc`, and `_b64` bitwise/select ops combining a lane mask or `vcc` destination with immediates; 32-bit mask + non-0/-1 immediate into an SGPR stays plain data, #1792). Split wave64 SGPR pairs whose two dwords hold separately written half-masks or a half-mask paired with a 0/-1 `EITHER` constant (`include_either=True`) are recognized by `holds_lane_mask` and read `s[r]` for lanes 0–31 and `s[r+1]` for lanes 32–63 in `mask_src`, and `--wave64-emulate`'s `x_mcache` keys `wave_mask` on every dword of an SGPR operand so rewriting `s[r+1]` invalidates a cached 64-bit exchange. `s_bcnt1_i32_b64`/`b32`, `s_bcnt0_i32_b64`/`b32`, `s_ff1_i32_b64`/`b32`, `s_ff0_i32_b64`/`b32`, `s_bitcmp0/1_b32/b64`, and `v_mbcnt_lo/hi_u32_b32` reconstruct the 64-bit wave mask via `OpGroupNonUniformBitwiseOr` (`GroupNonUniformArithmetic`), restricting 32-bit half ops (including `v_mbcnt_lo/hi_u32_b32` on `vcc_hi`/`vcc_lo`/`exec_lo` via `mask_half`) and `s_cmp_eq/lg_u32` (`wave_equal`) to the known 32-bit half in wave64. Opposite-half `s_cmp_*_u32/i32` comparisons (lo vs hi), ordering comparisons on mask halves, and `s_cmp_*_u32/i32/u64` comparing a lane mask against an arbitrary scalar or immediate reconstruct the exact 32/64-bit mask words via `wave_mask`. A compare with 0 keeps the vote, but a compare with -1 (all ones) does not: lanes that were never launched (a group smaller than the wave) don't vote, while their bits are 0 in the real mask, so until 2026-11-22 `s_cmp_eq_u32 vcc_hi, -1` read 1 in a 40-thread wave64 for `vcc_hi` = 0xff. GPU-checked with `cs_w64x_partial_scmp.asm` (40-thread wave64, native and `--wave64-emulate`) and `cs_w32_partial_scmp.asm` (20-thread wave32) on an AMD Radeon RX 7800 XT (RADV, Mesa 26.2.6): all 8 words as the headers derive, and 4 of 8 wrong in each with the old translator. GPU-checked with `runtime/test/shaders/cs_wave64_ballot.asm` and `cs_wm_*.asm` on AMD Radeon RX 9070 XT (64-wide subgroup); semantic test coverage in `tests/semantic/test_scalar_bitcmp.py`, `tests/semantic/test_scalar_alu_shifts.py`, `tests/semantic/test_loop_ballot_barrier.py`, and `tests/test_lane_masks.py` (split SGPR pairs, dual-dword `x_mcache`, `_b64` mask+imm, and `v_mbcnt` half selection CPU-checked, not yet GPU-verified). | | 64-bit mask equality (SHADER-551) | `s_cmp_eq_u64` / `s_cmp_lg_u64` compare both exact 32-bit words and set a uniform SCC. Canonical full-wave masks (`exec`, mask-valued `vcc`, or a wave64 `MASK_LO`/`MASK_HI` SGPR pair) compared with another full mask or zero may use a whole-wave equality vote. Other SGPR pairs materialize each mask half separately and retain scalar data and constant words, including split, swapped, duplicated, mixed mask/data and wave32 pairs. Comparing with all ones also materializes words, so unlaunched lanes remain zero bits in partial waves. CPU-checked by `tests/semantic/test_scmp_u64_mask_pairs.py` with full and partial wave64, wave32, and wave64-emulation cases; independent synthetic Vulkan readback is reported in [PR #3007](https://github.com/ItsNotRexed/Leonida/pull/3007). No native RDNA2 instruction oracle was run for this rule. | | VOP3 FP modifiers on constants | `tools/shader_ir.py` keeps VOP3 FP source modifiers (`neg(...)`, `\|...\|`, `-\|...\|`, `abs(...)`, and nested combinations) as `abs`/`neg` flags on `imm` and `fimm` operands so `abs` clears the sign bit first and `neg` flips it second (matching LLVM `AMDGPUInstPrinter.cpp::printOperandAndFPInputMods` and `llvm/test/MC/AMDGPU/vop3-modifiers.s`). Plain signed literals (`-1`, `-1.0`) are unchanged. Until 2026-11-22, `-\|1.0\|` folded `-` into the float before `FAbs` (yielding `+1.0`), `neg(0x3f800000)` / `neg(1)` applied two's-complement integer negation (`-4.0f` / `0xffffffff` NaN instead of `-1.0f` / `0x80000001`), and `\|0x...\|` / `-\|0x...\|` dropped `abs`. `tools/shader_meta.py` applies the same bit operations in `_imm_u32`. Checked on the CPU by `tests/test_shader_ir.py`, `tests/test_shader_meta.py` and `tests/semantic/test_vop3_const_mods.py` (f32, f16, `v_fma_mix_f32`, `v_cmp_class_f32`, f64 SPIR-V structure); GPU-checked for `-\|-2.0\|`, `neg(0x3f800000)`, `\|0xbf800000\|` and f16 `-\|0x3c00\|` by `ps_vop3_const_mods.asm` (Tesla V100 (NVIDIA 580.95.05, Linux)). | diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 7b4bcdf139..20b1f476e0 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py @@ -437,7 +437,9 @@ def shift64_test(tools, spirv, env=None): # s_andn2_saveexec_b64 / s_andn2_wrexec_b64: EXEC = S0 & ~EXEC over both halves, D old / new EXEC, SCC ("cs_w64x_andn2_exec", [1, 16, 32, 4144, 2052, 3, 8, 1544]), # s_and/s_or_saveexec_b64 (VCC and SGPR-pair sources), s_andn2_saveexec/wrexec_b64 (SGPR pair): D, EXEC, SCC - ("cs_w64x_saveexec_b64", [6160, 12304, 5120, 2088, 6168, 8192, 7176, 1837171])] + ("cs_w64x_saveexec_b64", [6160, 12304, 5120, 2088, 6168, 8192, 7176, 1837171]), + # SHADER-W64-SDST-PAIR: v_cmp_* / carry-out SGPR destinations printed as one register (no mattr line) + ("cs_w64x_cmp_sdst", [10240, 6144, 2053, 8224, 7714, 6184, 4144, 5])] # A 40-thread group (one partial wave) cannot use --require-wave64 (full subgroups), so it asks for subgroup size 64. W64X_PARTIAL = [("cs_w64x_partial", [40, 40, 528, 0, 0, 532, 539, 524820]), ("cs_w64x_partial_scmp", [0, 1, 0, 1, 1, 1, 1, 0])] diff --git a/runtime/test/shaders/cs_w64x_cmp_sdst.asm b/runtime/test/shaders/cs_w64x_cmp_sdst.asm new file mode 100644 index 0000000000..edbdd44eee --- /dev/null +++ b/runtime/test/shaders/cs_w64x_cmp_sdst.asm @@ -0,0 +1,131 @@ +; regen_fixtures: this source has a translator fixture in tests/semantic/fixtures/disasm +; TEST-W64-SDST-PAIR-GPU: the GPU test of SHADER-W64-SDST-PAIR (docs/design.md, "Wave64 VALU mask operands"): in +; wave64 code a VALU instruction that writes a lane mask to an SGPR writes the pair. +; A wave64 lane mask is 64 bits: v_cmp_*_e64 with SDST 4 writes s[4:5], and so does the carry-out of v_add_co_u32. +; llvm-mc names that destination by the wave size it is told, and the game's listings are disassembled without +; +wavefrontsize64, so they read "s4" while the 64-bit scalar ops after them read "s[4:5]". This file is written +; the same way on purpose: it has NO "mattr:" line, so llvm-mc assembles and prints it as it prints the game's +; shaders (one register), and the translator knows the wave size from the code (v_mbcnt_hi_u32_b32, s_*_b64 on +; EXEC). Every other wave64 fixture here names the pair, which is why none of them saw the stale high register. +; One 64-lane wave, v1 = lane 0..63, all lanes on. A mask is reported as bcnt << 8 | ff1 (population count, lowest +; lane), read with s_bcnt1_i32_b64 / s_ff1_i32_b64. Each compare below has lanes on both sides of lane 32, and the +; high register of its pair holds something else before it (another mask, 0, or nothing). +; 0) s[4:5] = EXEC (every lane), then v_cmp_gt_u32_e64 s4, 40, v1: lanes 0..39. +; s_and_b64 s[6:7], exec, s[4:5] = lanes 0..39: out[0] = 40 << 8 | 0 = 10240. SCC 1. +; (the old s5 for lanes 32..63: every lane, 16384.) +; 1) s[10:11] = 0, then v_cmp_le_u32_e64 s10, 48, v1: lanes 48..63. v_cmp_gt_u32_e64 s14, 8, v1: lanes 0..7 +; (s15 never written). s_or_b64 s[12:13], s[14:15], s[10:11] = lanes 0..7 and 48..63: +; out[1] = 24 << 8 | 0 = 6144. (the old s11 and s15: lanes 0..7 only, 2048.) +; 2) v2 = lane & 7; v_cmp_eq_u32_e64 s16, 5, v2: lanes 5, 13, .., 61 (8 lanes). +; s_and_saveexec_b64 s[18:19], s[16:17]; v3 = 1 under it; EXEC restored; v_cmp_ne_u32_e64 s20, 0, v3 and +; s_bcnt1 / s_ff1 of s[20:21]: out[2] = 8 << 8 | 5 = 2053. +; 3) v_add_co_u32_e64 v4, s22, v1, v5 with v5 = 0xffffffe0: the carry-out is lanes 32..63 (lane + 2^32 - 32 wraps). +; s_and_b64 s[24:25], exec, s[22:23]: out[3] = 32 << 8 | 32 = 8224. +; 4) v_cmp_gt_u32_e64 s26, 34, v1: lanes 0..33. s_xor_b64 s[28:29], exec, s[26:27] = lanes 34..63: +; out[4] = 30 << 8 | 34 = 7714. +; 5) s_andn2_b64 s[32:33], exec, s[4:5] (the mask of case 0) = lanes 40..63: out[5] = 24 << 8 | 40 = 6184. +; 6) s_mov_b64 exec, s[10:11] (lanes 48..63, all in the upper half); v7 = 1 under it; EXEC = every lane again; +; v_cmp_ne_u32_e32 vcc_lo, 0, v7 (VCC is printed vcc_lo the same way) and s_bcnt1 / s_ff1 of vcc: +; out[6] = 16 << 8 | 48 = 4144. +; 7) SCC of three 64-bit ANDs: case 0 (1); s_and_b64 s[38:39], s[10:11], s[14:15] = no lane (0); +; s_and_b64 s[40:41], s[10:11], s[22:23] = lanes 48..63, upper half only (1): out[7] = 1 + 0 * 2 + 1 * 4 = 5. +; Lane 0 stores the eight words. +; Run: vk_compute_test cs_w64x_cmp_sdst.spv 1 dump --require-wave64 +; -> out[0..7] = 10240 6144 2053 8224 7714 6184 4144 5 +v_mbcnt_lo_u32_b32 v1, -1, 0 +v_mbcnt_hi_u32_b32 v1, -1, v1 +; 0) the high register holds another mask +s_mov_b64 s[4:5], exec +v_cmp_gt_u32_e64 s4, 40, v1 +s_and_b64 s[6:7], exec, s[4:5] +s_cselect_b32 s50, 1, 0 +s_bcnt1_i32_b64 s8, s[6:7] +s_ff1_i32_b64 s9, s[6:7] +s_lshl_b32 s8, s8, 8 +s_or_b32 s60, s8, s9 +; 1) the high register holds 0, or nothing +s_mov_b64 s[10:11], 0 +v_cmp_le_u32_e64 s10, 48, v1 +v_cmp_gt_u32_e64 s14, 8, v1 +s_or_b64 s[12:13], s[14:15], s[10:11] +s_bcnt1_i32_b64 s8, s[12:13] +s_ff1_i32_b64 s9, s[12:13] +s_lshl_b32 s8, s8, 8 +s_or_b32 s61, s8, s9 +; 2) s_and_saveexec_b64 from the pair, and a count of a pair +v_and_b32_e32 v2, 7, v1 +v_mov_b32_e32 v3, 0 +v_cmp_eq_u32_e64 s16, 5, v2 +s_and_saveexec_b64 s[18:19], s[16:17] +v_mov_b32_e32 v3, 1 +s_mov_b64 exec, s[18:19] +v_cmp_ne_u32_e64 s20, 0, v3 +s_bcnt1_i32_b64 s8, s[20:21] +s_ff1_i32_b64 s9, s[20:21] +s_lshl_b32 s8, s8, 8 +s_or_b32 s62, s8, s9 +; 3) a carry-out +v_mov_b32_e32 v5, 0xffffffe0 +v_add_co_u32_e64 v4, s22, v1, v5 +s_and_b64 s[24:25], exec, s[22:23] +s_bcnt1_i32_b64 s8, s[24:25] +s_ff1_i32_b64 s9, s[24:25] +s_lshl_b32 s8, s8, 8 +s_or_b32 s63, s8, s9 +; 4) s_xor_b64 +v_cmp_gt_u32_e64 s26, 34, v1 +s_xor_b64 s[28:29], exec, s[26:27] +s_bcnt1_i32_b64 s8, s[28:29] +s_ff1_i32_b64 s9, s[28:29] +s_lshl_b32 s8, s8, 8 +s_or_b32 s64, s8, s9 +; 5) s_andn2_b64 +s_andn2_b64 s[32:33], exec, s[4:5] +s_bcnt1_i32_b64 s8, s[32:33] +s_ff1_i32_b64 s9, s[32:33] +s_lshl_b32 s8, s8, 8 +s_or_b32 s65, s8, s9 +; 6) the pair as EXEC +v_mov_b32_e32 v7, 0 +s_mov_b64 exec, s[10:11] +v_mov_b32_e32 v7, 1 +s_mov_b64 exec, -1 +v_cmp_ne_u32_e32 vcc_lo, 0, v7 +s_bcnt1_i32_b64 s8, vcc +s_ff1_i32_b64 s9, vcc +s_lshl_b32 s8, s8, 8 +s_or_b32 s66, s8, s9 +; 7) SCC +s_and_b64 s[38:39], s[10:11], s[14:15] +s_cselect_b32 s51, 2, 0 +s_and_b64 s[40:41], s[10:11], s[22:23] +s_cselect_b32 s52, 4, 0 +s_or_b32 s67, s50, s51 +s_or_b32 s67, s67, s52 +; lane 0 stores the words +v_cmpx_eq_u32_e32 0, v1 +v_mov_b32_e32 v20, s60 +v_mov_b32_e32 v21, 0 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s61 +v_mov_b32_e32 v21, 4 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s62 +v_mov_b32_e32 v21, 8 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s63 +v_mov_b32_e32 v21, 12 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s64 +v_mov_b32_e32 v21, 16 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s65 +v_mov_b32_e32 v21, 20 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s66 +v_mov_b32_e32 v21, 24 +buffer_store_dword v20, v21, s[0:3], 0 offen +v_mov_b32_e32 v20, s67 +v_mov_b32_e32 v21, 28 +buffer_store_dword v20, v21, s[0:3], 0 offen +s_endpgm diff --git a/scripts/run_gpu_tests.py b/scripts/run_gpu_tests.py index e8fce42777..da0ff572a6 100644 --- a/scripts/run_gpu_tests.py +++ b/scripts/run_gpu_tests.py @@ -1174,7 +1174,7 @@ def run_suite(a, work: str) -> int: "cs_w64x_lds_war", "cs_w64x_partial", "cs_w64x_sbarrier", "cs_w64x_load_ballot_load", "cs_w64x_mask_reuse", "cs_w64x_mask_reuse2", "cs_w64x_chain_barrier", "cs_barrier_divergent", "cs_bpermute32", "cs_w64x_partial_scmp", "cs_w32_partial_scmp", "cs_w32_partial_permute", "cs_barrier_early_end", - "cs_barrier_loop", "cs_w64x_andn2_exec", "cs_w64x_saveexec_b64", "cs_lds_b64_cmpst", "cs_lds_xchg64_ext", "cs_lds_atomics_ext", "cs_w32_andn2_exec", + "cs_barrier_loop", "cs_w64x_andn2_exec", "cs_w64x_saveexec_b64", "cs_w64x_cmp_sdst", "cs_lds_b64_cmpst", "cs_lds_xchg64_ext", "cs_lds_atomics_ext", "cs_w32_andn2_exec", "cs_mask_vgpr_exec", "cs_w32_exec_computed", "cs_w64_exec_computed"): cs_dump_wants = { "cs_store": [0x400080FF, 0xE00003FF, 0x3C004000, 0xEEEEEEEE, 0x40003C00, 0xBC003800, 0x40FF8000, 0x3FF00000], @@ -1272,6 +1272,7 @@ def run_suite(a, work: str) -> int: "cs_w64x_load_ballot_load": [43, 74, 75, 42, 42, 18, 41, 32], "cs_w64x_andn2_exec": [1, 16, 32, 4144, 2052, 3, 8, 1544], # s_andn2_saveexec/wrexec_b64 (.asm header) "cs_w64x_saveexec_b64": [6160, 12304, 5120, 2088, 6168, 8192, 7176, 1837171], # .asm header + "cs_w64x_cmp_sdst": [10240, 6144, 2053, 8224, 7714, 6184, 4144, 5], # .asm header "cs_bpermute32": [0, 50, 162, 99, 36, 218, 183, 15], # wave32: needs 32 lanes # SHADER-91: s_barrier sites in different wave-uniform branches meet (cs_barrier_divergent.asm) "cs_barrier_divergent": [127, 126, 125, 124, 123, 122, 121, 120], diff --git a/tests/semantic/fixtures/disasm/cs_w64x_cmp_sdst.s b/tests/semantic/fixtures/disasm/cs_w64x_cmp_sdst.s new file mode 100644 index 0000000000..7759ed83dd --- /dev/null +++ b/tests/semantic/fixtures/disasm/cs_w64x_cmp_sdst.s @@ -0,0 +1,89 @@ +; cs_w64x_cmp_sdst test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/cs_w64x_cmp_sdst.asm + v_mbcnt_lo_u32_b32 v1, -1, 0 + v_mbcnt_hi_u32_b32 v1, -1, v1 + s_mov_b64 s[4:5], exec + v_cmp_gt_u32_e64 s4, 40, v1 + s_and_b64 s[6:7], exec, s[4:5] + s_cselect_b32 s50, 1, 0 + s_bcnt1_i32_b64 s8, s[6:7] + s_ff1_i32_b64 s9, s[6:7] + s_lshl_b32 s8, s8, 8 + s_or_b32 s60, s8, s9 + s_mov_b64 s[10:11], 0 + v_cmp_le_u32_e64 s10, 48, v1 + v_cmp_gt_u32_e64 s14, 8, v1 + s_or_b64 s[12:13], s[14:15], s[10:11] + s_bcnt1_i32_b64 s8, s[12:13] + s_ff1_i32_b64 s9, s[12:13] + s_lshl_b32 s8, s8, 8 + s_or_b32 s61, s8, s9 + v_and_b32_e32 v2, 7, v1 + v_mov_b32_e32 v3, 0 + v_cmp_eq_u32_e64 s16, 5, v2 + s_and_saveexec_b64 s[18:19], s[16:17] + v_mov_b32_e32 v3, 1 + s_mov_b64 exec, s[18:19] + v_cmp_ne_u32_e64 s20, 0, v3 + s_bcnt1_i32_b64 s8, s[20:21] + s_ff1_i32_b64 s9, s[20:21] + s_lshl_b32 s8, s8, 8 + s_or_b32 s62, s8, s9 + v_mov_b32_e32 v5, 0xffffffe0 + v_add_co_u32_e64 v4, s22, v1, v5 + s_and_b64 s[24:25], exec, s[22:23] + s_bcnt1_i32_b64 s8, s[24:25] + s_ff1_i32_b64 s9, s[24:25] + s_lshl_b32 s8, s8, 8 + s_or_b32 s63, s8, s9 + v_cmp_gt_u32_e64 s26, 34, v1 + s_xor_b64 s[28:29], exec, s[26:27] + s_bcnt1_i32_b64 s8, s[28:29] + s_ff1_i32_b64 s9, s[28:29] + s_lshl_b32 s8, s8, 8 + s_or_b32 s64, s8, s9 + s_andn2_b64 s[32:33], exec, s[4:5] + s_bcnt1_i32_b64 s8, s[32:33] + s_ff1_i32_b64 s9, s[32:33] + s_lshl_b32 s8, s8, 8 + s_or_b32 s65, s8, s9 + v_mov_b32_e32 v7, 0 + s_mov_b64 exec, s[10:11] + v_mov_b32_e32 v7, 1 + s_mov_b64 exec, -1 + v_cmp_ne_u32_e32 vcc_lo, 0, v7 + s_bcnt1_i32_b64 s8, vcc + s_ff1_i32_b64 s9, vcc + s_lshl_b32 s8, s8, 8 + s_or_b32 s66, s8, s9 + s_and_b64 s[38:39], s[10:11], s[14:15] + s_cselect_b32 s51, 2, 0 + s_and_b64 s[40:41], s[10:11], s[22:23] + s_cselect_b32 s52, 4, 0 + s_or_b32 s67, s50, s51 + s_or_b32 s67, s67, s52 + v_cmpx_eq_u32_e32 0, v1 + v_mov_b32_e32 v20, s60 + v_mov_b32_e32 v21, 0 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s61 + v_mov_b32_e32 v21, 4 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s62 + v_mov_b32_e32 v21, 8 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s63 + v_mov_b32_e32 v21, 12 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s64 + v_mov_b32_e32 v21, 16 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s65 + v_mov_b32_e32 v21, 20 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s66 + v_mov_b32_e32 v21, 24 + buffer_store_dword v20, v21, s[0:3], 0 offen + v_mov_b32_e32 v20, s67 + v_mov_b32_e32 v21, 28 + buffer_store_dword v20, v21, s[0:3], 0 offen + s_endpgm diff --git a/tests/semantic/test_wave64_sdst_pair_fixture.py b/tests/semantic/test_wave64_sdst_pair_fixture.py new file mode 100644 index 0000000000..9da105f788 --- /dev/null +++ b/tests/semantic/test_wave64_sdst_pair_fixture.py @@ -0,0 +1,155 @@ +"""TEST-W64-SDST-PAIR-GPU: the one-register form of a wave64 lane-mask destination, on the fixture the GPU runs. + +SHADER-W64-SDST-PAIR (docs/design.md, "Wave64 VALU mask operands"; tests/semantic/test_wave64_single_sdst.py) makes +the SGPR a wave64 VALU instruction names for a lane mask the pair it is on hardware: llvm-mc prints `v_cmp_gt_u32_e64 +s4, ...` for the s[4:5] the compare writes, because the listings are made without `+wavefrontsize64`. This file adds +what that rule's own test does not have: + + * runtime/test/shaders/cs_w64x_cmp_sdst.asm, the GPU fixture (scripts/run_gpu_tests.py, check.py): six compares + and a carry-out in the one-register form, each read back across both halves by another 64-bit scalar op + (s_and_b64, s_or_b64, s_xor_b64, s_andn2_b64, s_and_saveexec_b64, s_mov_b64 exec, s_bcnt1 / s_ff1, SCC), with + the high register holding another mask, 0 or nothing beforehand. Its eight words are derived from the ISA in its + header; here it runs on a 64-wide subgroup and under --wave64-emulate, and the same program with every destination + written as a pair translates to the same SPIR-V, text for text; + * wave32 code through translate(): `v_cmp_* s5` there writes one register and s6 keeps its value; + * tools/wave64_halves.py (SHADER-10) on the one-register form, which it used to read as "a lane mask combined + with a register that is not one": the LLVM form of a uniform branch (`v_cmp_* sN` of scalars, `s_and_b64 vcc, + exec, s[N:N+1]`, `s_cbranch_vccnz`) is tier A and exports the same words as one wave of 64 lanes and as two of + 32, a per-lane compare that narrows EXEC through the pair is a plain skip, and the mask read as data is still + refused. + +All programs are hand-written (no game data). + + python -I tests/semantic/test_wave64_sdst_pair_fixture.py +""" +import os +import re +import struct +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import support # noqa: E402 (sets the tools path) +import rdna2spirv # noqa: E402 +import spirv_mini_interp as si # noqa: E402 +from test_wave64_emulate_opt import run_emulated_workgroup # noqa: E402 +from test_wave64_halves import UNIFORM, meta_of, out_param, ps, run_cut, vs # noqa: E402 + +FIXTURE = "cs_w64x_cmp_sdst" +EXPECTED = [10240, 6144, 2053, 8224, 7714, 6184, 4144, 5] # derivation in cs_w64x_cmp_sdst.asm +VSHARP = [0, 0, 0x1000, 0x31016FAC] +# a VALU lane-mask destination printed as one register: a compare's first operand, a carry-out's second +ONE_REGISTER = re.compile(r"^(\s*(?:v_cmp_\w+ |v_(?:add|sub|subrev)_co_u32\w* v\d+, ))s(\d+),") + + +def fssm(threads): + h = bytearray(0x148) + struct.pack_into("<4I", h, 0x138, 0x8088, threads, 1, 1) + return bytes(h) + + +def fixture_lines(): + with open(os.path.join(support.FIXTURE_DIR, FIXTURE + ".s"), encoding="utf-8") as f: + return f.read().splitlines() + + +def as_pairs(lines): + """The listing with every one-register VALU mask destination written as the pair, as llvm-mc prints it with + +wavefrontsize64 (and tools/rdna2_decode.py for a wave64 shader).""" + return [ONE_REGISTER.sub(lambda m: f"{m.group(1)}s[{m.group(2)}:{int(m.group(2)) + 1}],", ln) for ln in lines] + + +def run_native(spv, threads=64, subgroup=64): + return si.run_workgroup( + spv, threads, subgroup, + inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i % subgroup}, + push_constants=VSHARP + [0] * 28, memory={}) + + +class Translation(unittest.TestCase): + def test_fixture_on_a_64_wide_subgroup(self): + spv = support.translate_fixture(FIXTURE, fssm=fssm(64)) + self.assertTrue(rdna2spirv.translate.last_gen.wave64, "no mattr line: wave64 from the code markers") + memory = run_native(spv) + self.assertEqual(sorted(memory), list(range(0, 32, 4)), "lane 0 stores out[0..7] only") + self.assertEqual([memory[4 * k] for k in range(8)], EXPECTED) + + def test_fixture_under_wave64_emulation(self): + prev = rdna2spirv.WAVE64_EMULATE + rdna2spirv.WAVE64_EMULATE = True + try: + spv = support.translate_fixture(FIXTURE, fssm=fssm(64)) + finally: + rdna2spirv.WAVE64_EMULATE = prev + memory, _ = run_emulated_workgroup(spv, (64, 1, 1), push_constants=VSHARP + [0] * 28) + self.assertEqual([memory.get(4 * k, 0) for k in range(8)], EXPECTED) + + def test_one_register_and_pair_forms_translate_alike(self): + lines = fixture_lines() + pairs = as_pairs(lines) + self.assertEqual(sum(a != b for a, b in zip(lines, pairs)), 7, "six compares into SGPRs and one carry-out") + one = support.translate_listing(FIXTURE, "\n".join(lines).encode() + b"\n", fssm=fssm(64)) + two = support.translate_listing(FIXTURE, "\n".join(pairs).encode() + b"\n", fssm=fssm(64)) + self.assertEqual(one, two) + + def test_wave32_code_keeps_the_single_register(self): + # no wave64 marker: s5 is a 32-bit lane mask of its own (odd registers are common in the wave32 corpus) and + # s6, which a wave64 compare into s5 would not even be aligned for, keeps its value + lines = ["v_mbcnt_lo_u32_b32 v1, -1, 0", "s_mov_b32 s6, 77", "v_cmp_gt_u32_e64 s5, 12, v1", + "s_bcnt1_i32_b32 s7, s5", "v_cmpx_eq_u32_e32 0, v1", + "v_mov_b32_e32 v20, s6", "v_mov_b32_e32 v21, 0", "buffer_store_dword v20, v21, s[0:3], 0 offen", + "v_mov_b32_e32 v20, s7", "v_mov_b32_e32 v21, 4", "buffer_store_dword v20, v21, s[0:3], 0 offen", + "s_endpgm"] + spv = support.translate_lines("cs_w32_cmp_sdst", lines, fssm=fssm(32)) + self.assertFalse(rdna2spirv.translate.last_gen.wave64) + memory = run_native(spv, threads=32, subgroup=32) + self.assertEqual((memory[0], memory[4]), (77, 12)) + + +class Halves(unittest.TestCase): + """What tools/wave64_halves.py makes of the one-register form (it read the pair as "a lane mask combined with a + register that is not one": tier C `tainted-branch`, `tainted-exec` or `cross-lane`).""" + + def test_a_uniform_branch_in_the_llvm_form(self): + # s4 is a user SGPR: 5 < s4 is the same in every lane, so the mask is every launch lane or none and each + # half votes as the wave does; both sides set a VGPR and an SGPR that are read after the join + body = ["v_cmp_lt_u32_e64 s14, 5, s4", "s_and_b64 vcc, exec, s[14:15]", "s_cbranch_vccnz 3", + "v_mov_b32_e32 v6, 5", "s_mov_b32 s20, 2", "s_branch 2", "v_mov_b32_e32 v6, 7", "s_mov_b32 s20, 1", + "v_add_nc_u32_e32 v7, s20, v0"] + for push in UNIFORM: + with self.subTest(s4=push[4]): + (tier, reasons, idioms), asm = meta_of("vs_uv_one_register", vs(body)) + self.assertEqual((tier, reasons, idioms), ("A", [], [])) + whole, halves = out_param(run_cut(asm, 64, push)), out_param(run_cut(asm, 32, push)) + self.assertEqual(whole, halves) + want = (lambda k: (7, 1 + k)) if push[4] > 5 else (lambda k: (5, 2 + k)) + self.assertEqual([w[:2] for w in whole], [want(k) for k in range(64)]) + + def test_a_per_lane_compare_read_back_in_both_halves(self): + # 40 > vertex index: lanes 0..39 of the wave, every lane of the first half and lanes 0..7 of the second + body = ["v_cmp_gt_u32_e64 s14, 40, v0", "s_and_b64 s[16:17], exec, s[14:15]", "v_mov_b32_e32 v6, 1", + "v_cndmask_b32_e64 v6, v6, 9, s16", "v_mov_b32_e32 v7, 3", "s_and_saveexec_b64 s[12:13], s[14:15]", + "s_cbranch_execz 1", "v_add_nc_u32_e32 v7, v7, v0", "s_or_b64 exec, exec, s[12:13]"] + (tier, reasons, idioms), asm = meta_of("vs_cmp_one_register", vs(body)) + self.assertEqual((tier, reasons, idioms), ("A", [], [])) + whole, halves = out_param(run_cut(asm, 64)), out_param(run_cut(asm, 32)) + self.assertEqual(whole, halves) + self.assertEqual([w[:2] for w in whole], [(9, 3 + k) if k < 40 else (1, 3) for k in range(64)]) + + def test_a_pixel_shader_skip_through_the_pair(self): + (tier, reasons, _), _ = meta_of("ps_cmp_one_register", ps([ + "v_and_b32_e32 v4, 3, v0", "v_cmp_eq_u32_e64 s14, 1, v4", "s_and_saveexec_b64 s[12:13], s[14:15]", + "s_cbranch_execz 1", "v_mul_u32_u24_e32 v6, 7, v0", "s_or_b64 exec, exec, s[12:13]"])) + self.assertEqual((tier, reasons), ("A", [])) + + def test_c_the_mask_itself_as_data_is_still_refused(self): + # the 64 lane bits on hardware, the bits of the half here: widening the destination must not hide that + (tier, reasons, _), _ = meta_of("vs_cmp_mask_as_data", vs([ + "v_cmp_gt_u32_e64 s14, 40, v0", "v_mov_b32_e32 v6, s14"])) + self.assertEqual(tier, "C") + self.assertIn("cross-lane", reasons) + + +if __name__ == "__main__": + unittest.main()