diff --git a/docs/design.md b/docs/design.md index 656418c99a..98d703a0fc 100644 --- a/docs/design.md +++ b/docs/design.md @@ -547,6 +547,7 @@ How the generated SPIR-V expects the runtime to work: | 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. | +| Scalar bit tests on mixed pairs, wave32 VCC, and mask bit indices (SHADER-BITCMP64-MIXED-AND-SPECIAL-SRCS) | `s_bitcmp0/1_b64` keeps a single `wave_mask` for canonical wave64 `(MASK_LO, MASK_HI)` SGPR pairs and wave64 `exec`/`vcc`, and materializes each dword independently (`lane_mask_word` if tracked as a mask, else scalar load) for mixed mask/data pairs, wave32 `(mask, -1)`/`(mask, data)` pairs, swapped or duplicated wave64 half-mask pairs, and wave32 FSPX `vcc` (`{vcc_hi, vcc_lo}`). When the bit-index operand `S1` of `s_bitcmp0/1_b32` or `s_bitcmp0/1_b64` holds a lane mask, it is materialized via `lane_mask_word` instead of reading per-lane 0 / 0xFFFFFFFF. CPU-checked by `tests/semantic/test_bitcmp64_mixed_and_special.py` (wave32, FSPX wave32, native wave64, and `--wave64-emulate`) and `tests/semantic/test_scalar_bitcmp.py`. Not yet GPU-verified. | | 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)). | | `v_cmp_class_f64`, `v_cmpx_class_f64` | IEEE-754 binary64 numeric class test against the 10-bit mask (`0` sNaN, `1` qNaN, `2` `-inf`, `3` `-normal`, `4` `-subnormal`, `5` `-0`, `6` `+0`, `7` `+subnormal`, `8` `+normal`, `9` `+inf`), lowered in `Gen.cmp_class_f64` using pure 32-bit `%uint` bit extraction on `(lo, hi)` with no `Float64` capability required. Supports VGPR/SGPR pairs, float inline constants (`fimm`), sign-extended inline integers (`-16..64`), 32-bit literals placed in bits `[63:32]` (as on RDNA2), and VOP3 `abs`/`neg` sign-bit modifiers on `src0` (`abs` clears bit 63, `neg` flips bit 63 without quieting sNaN). Inactive lanes produce 0 for `v_cmp_class_f64` and stay inactive for `v_cmpx_class_f64`. Source: AMD RDNA2 ISA VOPC/VOP3 `V_CMP_CLASS_F64` / `V_CMPX_CLASS_F64`. CPU-checked by `tests/semantic/test_cmp_class_f64.py` (all 10 classes with low- and high-word payload bits, mask combinations and high-bit ignores, SGPR pairs, `fimm`/inline/literal constants, `abs`/`neg`/`-\|x\|`, `v_cmpx_class_f64` EXEC narrowing, inactive lanes, no `Float64` capability, bad-operand rejections). **Not GPU-verified.** Hardware-measured natively on `gfx1100`** (RX 7900 XTX, driver 26.10.1) through `tools/hw_oracle`: the ten classes as `f64` pairs (`0x7ff0000000000001` sNaN, `0x7ff8000000000000` qNaN, `0xfff0000000000000` -inf, `0xbff0000000000000` -normal, `0x8000000000000001` -denormal, `-0`, `+0`, `0x0000000000000001` +denormal, `0x3ff0000000000000` +normal, `0x7ff0000000000000` +inf) against their own bit, all ten bits, no bits and the nine other bits (40), and `v_cmpx_class_f64` on the launched lane true and false (2), all matching the same CPU model. Not measured: `abs`/`neg`, inactive lanes and wave64. **Not GPU-verified.** | | Mesh/compute input registers (historical measurement) | An early read-before-write analysis over all compute and mesh shaders found **compute** v0 (880), v0–v1 (614), v0–v2 (50), consistent with AMD's local thread ID x/y/z in v0/v1/v2, and scalar inputs centred on a pointer at s[4:5] and s6/s7. **Mesh:** every one of 2,969 reads exactly **v5**, plus s3 (always arithmetic), s[16:17] and s19–s23. The early pass also read FSSM 0x13C as the group width and compared the first SGPR multiply against it (82 match, 151 don't, 1,447 no simple multiply) and wrongly concluded that 0x13C was not the group width; the matching register varied by shader (s6/s10/s7/s9…). PSV0 gave the real thread-group sizes (32×1×1 for 1,253, 8×8×1 for 416, …). **Superseded:** 0x13C/0x140/0x144 are the thread-group size, `COMPUTE_PGM_RSRC2` at 0x138 gives the per-shader user-SGPR count and enabled workgroup IDs, and mesh takes NumThreads, limits and topology from PSV0 with `v5 = linearGroupIndex * NumThreads + LocalInvocationIndex`. All 2,689 CS and 9,282 mesh shaders translate now (see the Compute shaders and Mesh shaders rows). | diff --git a/tests/semantic/test_bitcmp64_mixed_and_special.py b/tests/semantic/test_bitcmp64_mixed_and_special.py new file mode 100644 index 0000000000..89cee55070 --- /dev/null +++ b/tests/semantic/test_bitcmp64_mixed_and_special.py @@ -0,0 +1,262 @@ +"""Semantic tests for s_bitcmp0/1_b64 and s_bitcmp0/1_b32 on mixed pairs, wave32 VCC, and mask bit indices. + +Covers four edge cases in Gen.bitcmp: + 1. Mixed SGPR pair (one dword holds a lane mask, the other holds plain scalar data or EITHER -1) + in both wave32 and wave64. + 2. Swapped / duplicated wave64 half-mask SGPR pairs (e.g. s0 = exec_hi, s1 = exec_lo; or both exec_lo) + in native wave64 and --wave64-emulate. + 3. Wave32 `s_bitcmp0/1_b64 vcc, bit` when `vcc_lo` holds a lane mask and `vcc_hi` holds plain scalar data + (or a separate wave32 lane mask): bits 32..63 must read `vcc_hi`, not 0. + 4. Lane-mask bit-index operand `a[1]` (e.g. `s_bitcmp1_b32 s10, vcc_lo` or `s_bitcmp1_b64 s[10:11], vcc_lo`): + must use the wave's 32-bit mask word rather than per-lane 0 / 0xFFFFFFFF. + + python -I tests/semantic/test_bitcmp64_mixed_and_special.py +""" +import gc +import json +import os +import struct +import sys +import tempfile +import unittest +import warnings + +SEMANTIC_DIR = os.path.dirname(os.path.abspath(__file__)) +ROOT = os.path.dirname(os.path.dirname(SEMANTIC_DIR)) +TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") +for _p in (TOOLS_DIR, SEMANTIC_DIR): + if _p not in sys.path: + sys.path.insert(0, _p) + +import rdna2spirv # noqa: E402 +import spirv_mini_interp as si # noqa: E402 + +STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" str: + threads = 64 if wave64 else 32 + with tempfile.TemporaryDirectory() as td: + os.mkdir(os.path.join(td, "disasm")) + src = os.path.join(td, "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 body_lines: + f.write(f"\t{ln}\n") + f.write("\ts_endpgm\n") + with open(os.path.join(td, f"cs_{name}.dxbc"), "wb") as f: + f.write(STUB_DXBC) + if fspx: + os.mkdir(os.path.join(td, "fspx")) + meta = {"wave32": not wave64, "rsrc2": 0x8090, "threads": [threads, 1, 1]} + with open(os.path.join(td, "fspx", f"cs_{name}.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump(meta, f) + else: + os.mkdir(os.path.join(td, "fssm")) + fssm = bytearray(0x148) + fssm[0:4] = b"FSSM" + struct.pack_into(" dict[int, int]: + threads = 64 if wave64 else 32 + sub = 32 if emulate else threads + pc = VSHARP + [0] * 28 + return si.run_workgroup( + spv, threads, sub, + inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i % sub}, + push_constants=pc, memory={}, + ) + + +class BitcmpMixedAndSpecialTests(unittest.TestCase): + def test_wave32_mixed_mask_and_data_pair(self): + # s10 = mask with lanes {0, 5} set (0x21); s11 = plain data 0x00000008 (bit 35 set). + spv = translate_cs("w32_bitcmp64_mixed", [ + "s_and_b32 vcc_lo, exec_lo, 0x21", + "s_mov_b32 s10, vcc_lo", + "s_mov_b32 s11, 0x08", + "s_bitcmp1_b64 s[10:11], 5", + "s_cselect_b32 s12, 1, 0", + "s_bitcmp1_b64 s[10:11], 4", + "s_cselect_b32 s13, 1, 0", + "s_bitcmp1_b64 s[10:11], 35", + "s_cselect_b32 s14, 1, 0", + "s_bitcmp0_b64 s[10:11], 36", + "s_cselect_b32 s15, 1, 0", + "v_mov_b32_e32 v1, s12", + "v_mov_b32_e32 v2, s13", + "v_mov_b32_e32 v3, s14", + "v_mov_b32_e32 v4, s15", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:128", + "buffer_store_dword v3, v6, s[0:3], 0 offen offset:256", + "buffer_store_dword v4, v6, s[0:3], 0 offen offset:384", + ], wave64=False) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [0] * 32) + self.assertEqual([mem[256 + 4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[384 + 4 * i] for i in range(32)], [1] * 32) + + def test_wave32_mask_and_either_minus1_pair_preserves_high_word(self): + # s10 = mask (0x01), s11 = -1 (EITHER: all high bits 32..63 are 1). + # On origin/main, holds_lane_mask(s[10:11]) was True and wave_mask dropped hi to 0. + spv = translate_cs("w32_bitcmp64_either_hi", [ + "s_and_b32 vcc_lo, exec_lo, 0x01", + "s_mov_b32 s10, vcc_lo", + "s_mov_b32 s11, -1", + "s_bitcmp1_b64 s[10:11], 0", + "s_cselect_b32 s12, 1, 0", + "s_bitcmp1_b64 s[10:11], 40", + "s_cselect_b32 s13, 1, 0", + "v_mov_b32_e32 v1, s12", + "v_mov_b32_e32 v2, s13", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:128", + ], wave64=False) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [1] * 32) + + def test_wave32_vcc_mask_lo_and_data_hi(self): + # In wave32, vcc = {vcc_hi, vcc_lo}. vcc_lo is a lane mask (0x04 -> bit 2), + # vcc_hi is plain scalar data (0x10 -> bit 36). + spv = translate_cs("w32_bitcmp64_vcc_mixed", [ + "s_and_b32 vcc_lo, exec_lo, 0x04", + "s_mov_b32 vcc_hi, 0x10", + "s_bitcmp1_b64 vcc, 2", + "s_cselect_b32 s12, 1, 0", + "s_bitcmp1_b64 vcc, 36", + "s_cselect_b32 s13, 1, 0", + "s_bitcmp1_b64 vcc, 35", + "s_cselect_b32 s14, 1, 0", + "v_mov_b32_e32 v1, s12", + "v_mov_b32_e32 v2, s13", + "v_mov_b32_e32 v3, s14", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:128", + "buffer_store_dword v3, v6, s[0:3], 0 offen offset:256", + ], wave64=False, fspx=True) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[256 + 4 * i] for i in range(32)], [0] * 32) + + def test_wave64_bitcmp32_high_half_mask(self): + # In wave64, vcc has lane 2 (lo bit 2) and lane 35 (hi bit 3) set. + # s_bitcmp1_b32 on vcc_hi or a MASK_HI SGPR must test hi (bit 3 = 1, bit 2 = 0), + # not lo (bit 3 = 0, bit 2 = 1). + lines = [ + "v_cmp_eq_u32_e64 s[14:15], v0, 2", + "v_cmp_eq_u32_e64 vcc, v0, 35", + "s_or_b64 vcc, vcc, s[14:15]", + "s_mov_b32 s10, vcc_hi", + "s_bitcmp1_b32 vcc_hi, 3", + "s_cselect_b32 s16, 1, 0", + "s_bitcmp1_b32 vcc_hi, 2", + "s_cselect_b32 s17, 1, 0", + "s_bitcmp1_b32 s10, 3", + "s_cselect_b32 s18, 1, 0", + "s_bitcmp1_b32 s10, 2", + "s_cselect_b32 s19, 1, 0", + "v_mov_b32_e32 v1, s16", + "v_mov_b32_e32 v2, s17", + "v_mov_b32_e32 v3, s18", + "v_mov_b32_e32 v4, s19", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:256", + "buffer_store_dword v3, v6, s[0:3], 0 offen offset:512", + "buffer_store_dword v4, v6, s[0:3], 0 offen offset:768", + ] + for emu in (False, True): + with self.subTest(emulate=emu): + spv = translate_cs("w64_bitcmp32_hi", lines, wave64=True, emulate=emu) + mem = run_cs(spv, wave64=True, emulate=emu) + self.assertEqual([mem[4 * i] for i in range(64)], [1] * 64) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], [0] * 64) + self.assertEqual([mem[512 + 4 * i] for i in range(64)], [1] * 64) + self.assertEqual([mem[768 + 4 * i] for i in range(64)], [0] * 64) + + def test_wave64_swapped_halves_and_mixed_pair(self): + # vcc has lanes 2 (lo bit 2) and 35 (hi bit 3) set. + # Put swapped halves into s[10:11]: s10 = vcc_hi (0x08 -> bit 3), s11 = vcc_lo (0x04 -> bit 34). + # Also test mixed pair s[12:13]: s12 = vcc_lo (0x04 -> bit 2), s13 = 0x20 (bit 37). + lines = [ + "v_cmp_eq_u32_e64 s[14:15], v0, 2", + "v_cmp_eq_u32_e64 vcc, v0, 35", + "s_or_b64 vcc, vcc, s[14:15]", + "s_mov_b32 s10, vcc_hi", + "s_mov_b32 s11, vcc_lo", + "s_mov_b32 s12, vcc_lo", + "s_mov_b32 s13, 0x20", + "s_bitcmp1_b64 s[10:11], 3", + "s_cselect_b32 s16, 1, 0", + "s_bitcmp1_b64 s[10:11], 34", + "s_cselect_b32 s17, 1, 0", + "s_bitcmp1_b64 s[12:13], 2", + "s_cselect_b32 s18, 1, 0", + "s_bitcmp1_b64 s[12:13], 37", + "s_cselect_b32 s19, 1, 0", + "v_mov_b32_e32 v1, s16", + "v_mov_b32_e32 v2, s17", + "v_mov_b32_e32 v3, s18", + "v_mov_b32_e32 v4, s19", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:256", + "buffer_store_dword v3, v6, s[0:3], 0 offen offset:512", + "buffer_store_dword v4, v6, s[0:3], 0 offen offset:768", + ] + for emu in (False, True): + with self.subTest(emulate=emu): + spv = translate_cs("w64_bitcmp64_swapped", lines, wave64=True, emulate=emu) + mem = run_cs(spv, wave64=True, emulate=emu) + self.assertEqual([mem[4 * i] for i in range(64)], [1] * 64) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], [1] * 64) + self.assertEqual([mem[512 + 4 * i] for i in range(64)], [1] * 64) + self.assertEqual([mem[768 + 4 * i] for i in range(64)], [1] * 64) + + def test_lane_mask_bit_index_operand(self): + # vcc_lo = 0x05 (lanes 0 and 2 active) -> bit index = 5. + # s10 = 0x20 (bit 5 set, bit 0 clear, bit 31 clear), s11 = 0. + spv = translate_cs("w32_bitcmp_mask_idx", [ + "s_mov_b32 s10, 0x20", + "s_mov_b32 s11, 0", + "s_and_b32 vcc_lo, exec_lo, 0x05", + "s_bitcmp1_b32 s10, vcc_lo", + "s_cselect_b32 s12, 1, 0", + "s_bitcmp1_b64 s[10:11], vcc_lo", + "s_cselect_b32 s13, 1, 0", + "v_mov_b32_e32 v1, s12", + "v_mov_b32_e32 v2, s13", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + "buffer_store_dword v2, v6, s[0:3], 0 offen offset:128", + ], wave64=False) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [1] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [1] * 32) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 73df48ed8e..6cadd8d640 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -2367,29 +2367,40 @@ def bitcmp(self, op, a): """s_bitcmp0/1_b32/b64: test if bit (a[1]) of scalar a[0] is 0 or 1, result in SCC.""" is_64 = op.endswith("b64") test_one = op.startswith("s_bitcmp1") - if self.holds_lane_mask(a[0]): - lo, hi = self.wave_mask(a[0]) - if not is_64 and self.wave64: - half = self.mask_half(a[0]) - if half == "hi": - lo = hi - elif is_64 and a[0]["k"] == "sgpr" and a[0].get("n") == 2: - lo, hi = self.load_reg("sgpr", a[0]["r"]), self.load_reg("sgpr", a[0]["r"] + 1) - elif is_64 and a[0]["k"] == "imm": - v = a[0]["v"] & 0xFFFFFFFFFFFFFFFF + src0 = a[0] + if not is_64: + lo = self.lane_mask_word(src0) if self.holds_lane_mask(src0) else self.src_u(src0) + elif src0["k"] == "sgpr" and src0.get("n") == 2: + r = src0["r"] + if (self.wave64 and self.cur_masks.get(r) == lane_masks.MASK_LO + and self.cur_masks.get(r + 1) == lane_masks.MASK_HI): + lo, hi = self.wave_mask(src0) + else: + words = [] + for i in range(2): + reg = {"k": "sgpr", "r": r + i, "n": 1} + words.append(self.lane_mask_word(reg) if (r + i) in self.cur_masks else self.load_reg("sgpr", r + i)) + lo, hi = words[0], words[1] + elif not self.wave64 and src0["k"] == "special" and src0.get("t") == "vcc": + vlo = {"k": "special", "t": "vcc_lo"} + vhi = {"k": "special", "t": "vcc_hi"} + lo = self.lane_mask_word(vlo) if self.holds_lane_mask(vlo) else self.src_u(vlo) + hi = self.lane_mask_word(vhi) if self.holds_lane_mask(vhi) else self.src_u(vhi) + elif self.holds_lane_mask(src0): + lo, hi = self.wave_mask(src0) + elif src0["k"] == "imm": + v = src0["v"] & 0xFFFFFFFFFFFFFFFF lo, hi = self.cu(v & 0xFFFFFFFF), self.cu(v >> 32) - elif is_64 and a[0]["k"] == "special" and a[0].get("t") == "null": + elif src0["k"] == "special" and src0.get("t") == "null": lo, hi = self.cu(0), self.cu(0) - elif is_64 and a[0]["k"] == "special" and a[0].get("t") == "vcc": + elif src0["k"] == "special" and src0.get("t") == "vcc": lo, hi = self.tmp(), self.tmp() self.emit(f"{lo} = OpLoad %uint %vcc") self.emit(f"{hi} = OpLoad %uint %vcc_hi") - elif is_64: - lo, hi = self.mask_src(a[0]), self.cu(0) else: - lo = self.src_u(a[0]) + lo, hi = self.src_u(src0), self.cu(0) - sh = self.src_u(a[1]) + sh = self.lane_mask_word(a[1]) if self.holds_lane_mask(a[1]) else self.src_u(a[1]) mask_bits = 63 if is_64 else 31 m = self.tmp() self.emit(f"{m} = OpBitwiseAnd %uint {sh} {self.cu(mask_bits)}")