diff --git a/docs/design.md b/docs/design.md index 656418c99a..4956992f9c 100644 --- a/docs/design.md +++ b/docs/design.md @@ -489,6 +489,7 @@ How the generated SPIR-V expects the runtime to work: | `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. | | 64-bit scalar data ops (`s_brev_b64`, `s_bfe_u64`/`s_bfe_i64`, `s_bitset0_b64`/`s_bitset1_b64`, `s_flbit_i32_b64`/`s_flbit_i32_i64`) | The SGPR pair normally carries 64-bit data. In wave64, `s_flbit_i32_b64` can also gather both halves of a lane mask or EXEC before scanning it. The other operations require scalar data. `test_library_compute_forms.py` checks an empty mask and set bits in either half on native and emulated waves; `tests/semantic/test_lane_mask_bitreplicate_flbit.py` runs EXEC and VCC masks on every lane of a wave64 (the 1.0.2.1 pipeline library reads EXEC this way). `s_brev_b64`: D.lo = reverse(S0.hi), D.hi = reverse(S0.lo). `s_bfe_*64`: offset = S1[5:0], width = S1[22:16]; width 0 gives 0; a field past bit 63 stops there (the 64-bit form of the `s_bfe_u32` rule, LLVM `constantFoldBFE`); SCC = D ≠ 0. `s_bitset*_b64`: bit S0 & 63 of the pair cleared / set. `s_flbit_i32_b64`: zeros above the top 1 from bit 63, −1 for 0; `s_flbit_i32_i64`: the same of ~S0 when S0 < 0, −1 for 0 and −1. None but `s_bfe` writes SCC. Source: RDNA2 ISA SOP1/SOP2. CPU-checked by `tests/semantic/test_salu_brev_b64.py`, `test_salu_bfe_64.py`, `test_salu_bitset_b64.py`, `test_salu_flbit_64.py` (#723, #726, #729, #733). **Hardware-verified** on an AMD Radeon RX 7900 XTX (gfx1100, Windows 11, driver 26.10.1), one raw wave32 kernel per op through `tools/hw_oracle`: `s_brev_b64` of 0x12345678_0000000F has low word 0x1E6A2C48, `s_bfe_u64(0x00000123_45678000, w16 o28)` = 0x1234, `s_bfe_i64(0x00000001_80000000, w2 o31)` has 0xFFFFFFFF in both words, `s_bitset1_b64(0, 40)` has high word 0x100, `s_flbit_i32_b64(0x00010000)` = 47 and `s_flbit_i32_i64(0xFFFFFFFF_FFFF0000)` = 48. These are the words `runtime/test/shaders/cs_w32_salu64.asm` expects. Also **hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). | +| `s_bitset0_b32`, `s_bitset1_b32` on lane masks | On a plain scalar destination D, bit `S0 & 31` is cleared or set in place without changing SCC (`sD & ~(1 << (S0 & 31))` / `sD \| (1 << (S0 & 31))`). When D holds a lane mask (`exec_lo`, `exec_hi`, mask-holding `vcc_lo`/`vcc_hi`, or a mask SGPR tracked by `tools/lane_masks.py`), only lane `(S0 & 31)` (or `(S0 & 31) + 32` for a wave64 high half) is updated via `Gen.put_mask`, preserving the untouched half of EXEC/VCC in wave64 and keeping D's mask kind in `lane_masks.py`; a lane-mask bit-index source S0 is materialized via `Gen.lane_mask_word`. Source: RDNA2 ISA SOP1 `S_BITSET0_B32` / `S_BITSET1_B32`. CPU-checked by `tests/semantic/test_bitset32_mask.py` (wave32 and wave64 EXEC/VCC/SGPR masks and mask bit-index source; **not GPU-verified**). | | `v_bfe_u32` / `v_bfe_i32` past bit 31 | offset = S1[4:0] and width = S2[4:0]: the bits above are ignored, so an offset of 36 reads as 4 and a width of 32 as 0 (a width of 0 gives 0). When offset + width > 32 the field stops at bit 31, as for `s_bfe`: width = min(width, 32 - offset), and `v_bfe_i32` sign-extends from bit 31. Example: `v_bfe_i32` of `0xd2f90159` with offset 28 and width 16 gives `0xfffffffd` (4-bit field `0xd`), not `0xd`, and `0x80000000` with offset 31 and width 4 gives `0xffffffff`. The translator (`bfe_width` for VGPR or SGPR offset and width, the same cut folded at translation time for constants) already did this. Source: **measured on RDNA2**, AMD Radeon 610M (`gfx1036`, Windows 11, AMD driver 32.0.11016.14003), wave32 and wave64 identical, 16 words over VGPR, inline-constant and literal operands: `tools/int_hw_probe.py --oracle both --group valu_bfe_past31`, 4/4 cases match the translator. The words are pinned on the CPU by `tests/test_int_hw_probe.py` (`ValuBfeMeasuredOnRdna2`). | | `s_quadmask_b32`/`_b64`, `s_bitreplicate_b64_b32` | On scalar data (RDNA2 ISA SOP1): `s_quadmask_b32` sets D[i] to the OR of S0[4i+3:4i] for the 8 quads, `s_quadmask_b64` does the same for the 16 quads of the 64-bit S0 (D[63:16] = 0), and both set SCC = D != 0. `s_bitreplicate_b64_b32` copies each bit S0[i] to D[2i] and D[2i+1] and leaves SCC. Built from fixed shift/mask steps, with no loop. Bit replication can also write EXEC, or VCC in wave64 (in wave32 VCC's high word is data, so it stays `Unsupported`). Each lane takes source bit `lane // 2`. `test_library_compute_forms.py` checks both halves of VCC and SCC on native and emulated waves; `tests/semantic/test_lane_mask_bitreplicate_flbit.py` checks VCC, which the 1.0.2.1 pipeline library writes from a constant, on every lane of a wave64. A 32-bit lane-mask source (`exec_lo`/`exec_hi`, `vcc_lo`/`vcc_hi`) of `s_bitreplicate_b64_b32` into a scalar (non-EXEC/VCC) destination reads that half of the wave's mask as data (`Gen.wave_mask`, as `s_ff1_i32_b64` reads one) and replicates it as any scalar (#998, for 6 pipeline-library programs of 1.0.2.1). Confirmed only by the CPU interpreter on every lane of a wave64 (`test_lane_mask_bitreplicate_flbit.py`: EXEC halves and a compare's VCC halves, synthetic lines) and by the ISA pseudocode above; not measured on hardware. Any other lane-mask source (EXEC, VCC or an SGPR that `holds_lane_mask`), and a lane-mask source with an EXEC/VCC destination, raises `Unsupported`: each invocation holds only its own lane's bit, and these ops read the other lanes of a quad or move bits between lanes. Until 2026-11-22 all three were rejected. CPU-checked by `tests/semantic/test_salu_quadmask_bitreplicate.py` (fixed and seeded values, SCC written or kept, immediates, aliasing, lane masks rejected). **Hardware-verified** on an AMD Radeon RX 7900 XTX (gfx1100, Windows 11, driver 26.10.1) through `tools/hw_oracle`, one raw wave32 kernel per op: `s_quadmask_b32(0xF)` = 1 with SCC 1, `(0xF0000000)` = 0x80, `(0x0F0F0F0F)` = 0x55 (the set quads are 0, 2, 4 and 6), `(0)` = 0 with SCC 0; `s_quadmask_b64` of 0xF = 1 and of 0xF0000000_00000000 (quad 15, bits 60..63) = 0x8000 in the low word with D[63:16] = 0; `s_bitreplicate_b64_b32` of 5 = 0x33 and of 0x80000000 = 0xC0000000_00000000, with SCC left as it was. **GPU-checked** by `runtime/test/shaders/cs_w32_new_ops.asm` (`s_quadmask_b32(0x01020408)` = 0x55, `s_quadmask_b64(0x80000000_00000001)` low word 0x8001, `s_bitreplicate_b64_b32(0x80000001)` high word 0xC0000000, with the expected words from the ISA pseudocode), exact on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92). **Not GPU-verified.** | | 64-bit scalar bitwise ops (`s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64`, `s_orn2_b64`, `s_nand_b64`, `s_nor_b64`, `s_xnor_b64`, `s_not_b64`) | Dispatched like `s_cselect_b64`: when destination is `exec`/`vcc` or any source is a lane mask (`holds_lane_mask` or `exec`/`vcc`), uses the per-lane mask path (`mask_src`/`mask_dst` including `null` dst) and votes SCC across the wave (`scc_from_mask`). Otherwise operates on both 32-bit dwords of non-mask 64-bit scalar data (SGPR pairs, sign-extended inline/literal immediates, `null` = 0), reads all source dwords before storing (preserving destination aliasing), accepts `null` destination, and sets `SCC = ((D.lo \ \| D.hi) != 0)`. Until 2026-11-22 these ops always took the lane-mask path, duplicating D.lo into D.hi and voting SCC on D.lo alone. `s_nand/nor/xnor_b64` compute `~(S0 op S1)` on both paths (a lane's mask value is 0 or ~0, so the NOT is per lane); until 2026-11-22 they were rejected as unsupported. Source: RDNA2 ISA SOP1/SOP2. CPU-checked by `tests/semantic/test_salu64_bitwise_data.py` (all 9 ops, 4-corner SCC, immediates, aliasing, stale mask overwrite, wave32/wave64 lane-mask regression, and `--wave64-emulate`); **Hardware-verified** on the same part through `tools/hw_oracle` for the plain-data path: `s_and_b64(0xFFFFFFFF_00000000, 0x0F0F0F0F_0F0F0F0F)` = 0x0F0F0000_00000000, `s_xnor_b64(0xFFFF0000_00000000, 0_FFFFFFFF)` = 0x0000FFFF_00000000, `s_nand_b64(0xFFFF0000_0, 0)` = 0xFFFFFFFF_FFFFFFFF, `s_not_b64(0x0000FFFF_0)` = 0xFFFF0000_FFFFFFFF and `s_andn2_b64(0xFFFF0000_0, 0)` = 0xFFFF0000_0, all with SCC 1, while `s_or_b64(0, 0)` = 0 with SCC 0: SCC = ((D.lo ≠ 0) or (D.hi ≠ 0)). The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way; on RDNA3 the same plain-data path is **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way; on RDNA3 the same plain-data path is **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo \| The measured plain-data words: The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way; on RDNA3 the same plain-data path is **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo \| The measured plain-data words: `s_and_b64(0xFFFFFFFF_00000000, 0x0F0F0F0F_0F0F0F0F)` = 0x0F0F0000_00000000, `s_xnor_b64(0xFFFF0000_00000000, 0_FFFFFFFF)` = 0x0000FFFF_00000000, `s_nand_b64(0xFFFF0000_0, 0)` = 0xFFFFFFFF_FFFFFFFF, `s_not_b64(0x0000FFFF_0)` = 0xFFFF0000_FFFFFFFF and `s_andn2_b64(0xFFFF0000_0, 0)` = 0xFFFF0000_0, all with SCC 1, while `s_or_b64(0, 0)` = 0 with SCC 0. The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way. **Not GPU-verified.** **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo \| The measured plain-data words: **Not GPU-verified.** **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo \| D.hi) != 0`, the `andn2`/`orn2` pairs separate negation of the second source from negation of the first, `s_not_b64` inverts both dwords and clears SCC on a zero result, and `s_nand_b64`/`s_nor_b64`/`s_xnor_b64` invert the per-dword result (`and`/`or` of all ones gives `0` with SCC 0, `xnor` of equal pairs gives all ones with SCC 1); the lane-mask dispatch (`exec`/`vcc` destinations) was not re-measured. The lane-mask dispatch (`exec`/`vcc` destinations) was not re-measured. D.hi) != 0`, the `andn2`/`orn2` pairs separate negation of the second source from negation of the first, `s_not_b64` inverts both dwords and clears SCC on a zero result, and `s_nand_b64`/`s_nor_b64`/`s_xnor_b64` invert the per-dword result (`and`/`or` of all ones gives `0` with SCC 0, `xnor` of equal pairs gives all ones with SCC 1); the lane-mask dispatch (`exec`/`vcc` destinations) was not re-measured. **Not GPU-verified.** **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way; on RDNA3 the same plain-data path is **hardware-verified on an AMD RX 7600 (gfx1102)**: on four A/B pairs with high-bit-only and cross-half values, `s_and_b64`, `s_or_b64`, `s_xor_b64`, `s_andn2_b64` and `s_orn2_b64` write both dwords and set `SCC = (D.lo | The measured plain-data words: `s_and_b64(0xFFFFFFFF_00000000, 0x0F0F0F0F_0F0F0F0F)` = 0x0F0F0000_00000000, `s_xnor_b64(0xFFFF0000_00000000, 0_FFFFFFFF)` = 0x0000FFFF_00000000, `s_nand_b64(0xFFFF0000_0, 0)` = 0xFFFFFFFF_FFFFFFFF, `s_not_b64(0x0000FFFF_0)` = 0xFFFF0000_FFFFFFFF and `s_andn2_b64(0xFFFF0000_0, 0)` = 0xFFFF0000_0, all with SCC 1, while `s_or_b64(0, 0)` = 0 with SCC 0. The lane-mask path (EXEC/VCC sources, or a mask destination) is a translator model and cannot be measured this way. The lane-mask dispatch (`exec`/`vcc` destinations) was not re-measured. D.hi) != 0`, the `andn2`/`orn2` pairs separate negation of the second source from negation of the first, `s_not_b64` inverts both dwords and clears SCC on a zero result, and `s_nand_b64`/`s_nor_b64`/`s_xnor_b64` invert the per-dword result (`and`/`or` of all ones gives `0` with SCC 0, `xnor` of equal pairs gives all ones with SCC 1); the lane-mask dispatch (`exec`/`vcc` destinations) was not re-measured. \| diff --git a/tests/semantic/test_bitset32_mask.py b/tests/semantic/test_bitset32_mask.py new file mode 100644 index 0000000000..30628deaa9 --- /dev/null +++ b/tests/semantic/test_bitset32_mask.py @@ -0,0 +1,231 @@ +"""Semantic tests for s_bitset0_b32 and s_bitset1_b32 on lane-mask destinations and sources. + +On RDNA2, s_bitset0_b32 / s_bitset1_b32 mutate bit (S0 & 31) of D in place without modifying SCC. +When D holds a lane mask (exec_lo/exec_hi, vcc_lo/vcc_hi holding a mask, or a mask SGPR), each +invocation stores only its own lane's bit (0 or 0xFFFFFFFF), so mutating a bit mask directly on +that per-lane word corrupts every lane instead of flipping only lane (S0 & 31) (or lane (S0 & 31) + 32 +for a wave64 high half). Likewise, a lane-mask bit-index source S0 must supply the wave's 32-bit +mask word rather than this lane's 0 / 0xFFFFFFFF. + + python -I tests/semantic/test_bitset32_mask.py +""" +import gc +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 lane_masks # noqa: E402 +import rdna2spirv # noqa: E402 +import shader_ir # noqa: E402 +import spirv_mini_interp as si # noqa: E402 + +M32 = 0xFFFFFFFF +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")) + os.mkdir(os.path.join(td, "fssm")) + 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) + 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 LaneMasksBitset32Tests(unittest.TestCase): + def _analyse(self, lines, *, wave64: bool): + insns = [shader_ir.parse_line(ln) for ln in lines] + masks, _ = lane_masks.analyse([insns], [[]], wave64=wave64) + return insns, masks + + def test_wave32_preserves_mask_sgpr_and_vcc(self): + insns, masks = self._analyse([ + "s_mov_b32 s0, exec_lo", + "s_bitset0_b32 s0, 5", + "s_bcnt1_i32_b32 s1, s0", + "v_cmp_lt_u32_e32 vcc_lo, v0, 10", + "s_bitset1_b32 vcc_lo, 12", + "s_bcnt1_i32_b32 s2, vcc_lo", + ], wave64=False) + self.assertEqual(masks[id(insns[2])].get(0), lane_masks.MASK) + self.assertEqual(masks[id(insns[5])].get("vcc"), lane_masks.MASK) + + def test_wave64_preserves_lo_and_hi_halves(self): + insns, masks = self._analyse([ + "s_mov_b32 s0, exec_lo", + "s_mov_b32 s1, exec_hi", + "s_bitset0_b32 s0, 3", + "s_bitset1_b32 s1, 4", + "s_bcnt1_i32_b32 s2, s0", + "s_bcnt1_i32_b32 s3, s1", + "v_cmp_lt_u32_e64 vcc, v0, 40", + "s_bitset0_b32 vcc_lo, 2", + "s_bitset1_b32 vcc_hi, 10", + "s_bcnt1_i32_b64 s4, vcc", + ], wave64=True) + self.assertEqual(masks[id(insns[4])].get(0), lane_masks.MASK_LO) + self.assertEqual(masks[id(insns[5])].get(1), lane_masks.MASK_HI) + self.assertEqual(masks[id(insns[9])].get("vcc"), lane_masks.MASK) + + def test_either_constant_mutated_becomes_plain_data(self): + insns, masks = self._analyse([ + "s_mov_b32 s0, 0", + "s_bitset1_b32 s0, 5", + "s_bcnt1_i32_b32 s1, s0", + ], wave64=False) + self.assertNotIn(0, masks[id(insns[2])]) + + +class Bitset32MaskExecTests(unittest.TestCase): + def test_wave32_exec_lo_bitset0_and_bitset1(self): + # Start with EXEC = 0x15 (lanes 0, 2, 4). Clear bit 2 -> {0, 4}; set bit 7 -> {0, 4, 7}. + spv = translate_cs("w32_exec_bitset", [ + "v_mov_b32_e32 v1, 0x1111", + "s_mov_b32 exec_lo, 0x15", + "s_bitset0_b32 exec_lo, 2", + "s_bitset1_b32 exec_lo, 7", + "v_mov_b32_e32 v1, 0xcafe", + "s_mov_b32 exec_lo, -1", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + ], wave64=False) + mem = run_cs(spv, wave64=False) + got = [mem[4 * i] for i in range(32)] + expected = [0xCAFE if i in (0, 4, 7) else 0x1111 for i in range(32)] + self.assertEqual(got, expected) + + def test_wave64_exec_lo_and_exec_hi_keep_other_half(self): + # EXEC_LO = 0x0F (lanes 0..3), EXEC_HI = 0x03 (lanes 32, 33). + # Clear exec_lo bit 1, set exec_lo bit 5, clear exec_hi bit 0 (lane 32), set exec_hi bit 4 (lane 36). + lines = [ + "v_mov_b32_e32 v1, 0x2222", + "s_mov_b32 exec_lo, 0x0f", + "s_mov_b32 exec_hi, 0x03", + "s_bitset0_b32 exec_lo, 1", + "s_bitset1_b32 exec_lo, 5", + "s_bitset0_b32 exec_hi, 0", + "s_bitset1_b32 exec_hi, 4", + "v_mov_b32_e32 v1, 0xbeef", + "s_mov_b64 exec, -1", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + ] + expected = [0xBEEF if i in (0, 2, 3, 5, 33, 36) else 0x2222 for i in range(64)] + for emu in (False, True): + with self.subTest(emulate=emu): + spv = translate_cs("w64_exec_bitset", lines, wave64=True, emulate=emu) + mem = run_cs(spv, wave64=True, emulate=emu) + self.assertEqual([mem[4 * i] for i in range(64)], expected) + + def test_wave32_vcc_lo_and_mask_sgpr_bitset(self): + spv = translate_cs("w32_vcc_sgpr_bitset", [ + "v_cmp_lt_u32_e32 vcc_lo, v0, 4", + "s_mov_b32 s10, vcc_lo", + "s_bitset0_b32 vcc_lo, 1", + "s_bitset1_b32 vcc_lo, 6", + "s_bitset0_b32 s10, 3", + "s_bitset1_b32 s10, 10", + "v_cndmask_b32_e32 v1, 0, 1, vcc_lo", + "v_cndmask_b32_e64 v2, 0, 1, s10", + "s_bcnt1_i32_b32 s11, vcc_lo", + "s_bcnt1_i32_b32 s12, s10", + "v_mov_b32_e32 v3, s11", + "v_mov_b32_e32 v4, s12", + "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 if i in (0, 2, 3, 6) else 0 for i in range(32)]) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [1 if i in (0, 1, 2, 10) else 0 for i in range(32)]) + self.assertEqual([mem[256 + 4 * i] for i in range(32)], [4] * 32) + self.assertEqual([mem[384 + 4 * i] for i in range(32)], [4] * 32) + + def test_wave64_vcc_and_mask_sgpr_halves_bitset(self): + lines = [ + "v_cmp_lt_u32_e64 vcc, v0, 34", + "s_bitset0_b32 vcc_lo, 0", + "s_bitset1_b32 vcc_hi, 5", + "v_cndmask_b32_e64 v1, 0, 1, vcc", + "s_bcnt1_i32_b64 s11, vcc", + "s_mov_b32 s10, exec_hi", + "s_bitset0_b32 s10, 2", + "s_bcnt1_i32_b32 s12, s10", + "v_mov_b32_e32 v2, s11", + "v_mov_b32_e32 v3, s12", + "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", + ] + want_vcc = [1 if (1 <= i <= 33 or i == 37) else 0 for i in range(64)] + for emu in (False, True): + with self.subTest(emulate=emu): + spv = translate_cs("w64_vcc_sgpr_bitset", lines, wave64=True, emulate=emu) + mem = run_cs(spv, wave64=True, emulate=emu) + self.assertEqual([mem[4 * i] for i in range(64)], want_vcc) + self.assertEqual([mem[256 + 4 * i] for i in range(64)], [34] * 64) + self.assertEqual([mem[512 + 4 * i] for i in range(64)], [31] * 64) + + def test_lane_mask_bit_index_source_uses_wave_mask_word(self): + # vcc_lo has lanes 0 and 2 active -> wave mask word = 0x5 -> bit 5 set (0x20) uniformly. + spv = translate_cs("w32_mask_bit_index", [ + "s_mov_b32 s10, 0", + "s_and_b32 vcc_lo, exec_lo, 0x05", + "s_bitset1_b32 s10, vcc_lo", + "v_mov_b32_e32 v1, s10", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v1, v6, s[0:3], 0 offen", + ], wave64=False) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [1 << 5] * 32) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/semantic/test_scalar_alu_imm_bitset.py b/tests/semantic/test_scalar_alu_imm_bitset.py index a41b5b99ee..08215d0ce4 100644 --- a/tests/semantic/test_scalar_alu_imm_bitset.py +++ b/tests/semantic/test_scalar_alu_imm_bitset.py @@ -742,12 +742,11 @@ def test_overwriting_lane_mask_sgpr_resets_to_scalar_data(self): ]) self.assertEqual(got, 0x10 if "movk" in op_line else 0xF00) - # Also verify s_addk_i32 / s_mulk_i32 / s_bitset0_b32 / s_bitset1_b32 clear lane-mask tracking. + # Also verify s_addk_i32 / s_mulk_i32 clear lane-mask tracking (s_bitset0/1_b32 on a lane mask + # keep lane-mask tracking and mutate only the addressed lane's bit; see test_bitset32_mask.py). for op_line in ( "s_addk_i32 s0, 8", "s_mulk_i32 s0, 4", - "s_bitset0_b32 s0, 0", - "s_bitset1_b32 s0, 3", ): with self.subTest(op=op_line): spv = translate_lines([ diff --git a/tools/lane_masks.py b/tools/lane_masks.py index 07dddd6703..e0ef9e91d8 100644 --- a/tools/lane_masks.py +++ b/tools/lane_masks.py @@ -268,6 +268,15 @@ def step(state, ins, wave64=True): return else: put(state, a[0], None, wave64) + elif op in ("s_bitset0_b32", "s_bitset1_b32"): + dk = kind(state, a[0], wave64) + dst_t = a[0].get("t") if a[0]["k"] == "special" else None + if dk in MASK_KINDS: + if wave64 and dst_t in ("vcc_lo", "vcc_hi") and state.get("vcc") in MASK_KINDS: + return + put(state, a[0], dk, wave64) + else: + put(state, a[0], None, wave64) else: put(state, a[0], None, wave64) if ((op.startswith("v_") and "_co_" in op) diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 73df48ed8e..b2d9df66fd 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -9284,15 +9284,37 @@ def f64(sv): self.emit(f"{want} = OpNot %uint {mk}") self.emit(f"{r} = OpBitwiseAnd %uint {old} {want}") self.store_reg("sgpr", d["r"] + half, r) - elif op == "s_bitset1_b32": - # sD |= 1 << (src & 31) (sD is both source and destination) - b = self.src_u(a[1]) - m, sh, old, r = (self.tmp() for _ in range(4)) + elif op in ("s_bitset0_b32", "s_bitset1_b32"): + # sD = sD & ~(1 << (src & 31)) / sD | (1 << (src & 31)); sD is both source and destination, SCC unchanged. + # When sD holds a lane mask (exec_lo/hi, vcc_lo/hi, or a mask SGPR), mutate only the addressed lane's bit. + b = self.lane_mask_word(a[1]) if self.holds_lane_mask(a[1]) else self.src_u(a[1]) + m = self.tmp() self.emit(f"{m} = OpBitwiseAnd %uint {b} {self.cu(31)}") - self.emit(f"{sh} = OpShiftLeftLogical %uint {self.cu(1)} {m}") - self.emit(f"{old} = OpCopyObject %uint {self.src_u(a[0])}") - self.emit(f"{r} = OpBitwiseOr %uint {old} {sh}") - self.dst(a[0], r) + if self.holds_lane_mask(a[0]): + half = self.mask_half(a[0]) if self.wave64 else None + tgt = m + if half == "hi": + tgt = self.tmp() + self.emit(f"{tgt} = OpIAdd %uint {m} {self.cu(32)}") + hit, r = self.tmp(), self.tmp() + self.emit(f"{hit} = OpIEqual %bool {self.wave_lane()} {tgt}") + old = (self.exec_value() if (a[0]["k"] == "special" and a[0].get("t", "").startswith("exec")) + else self.mask_src(a[0], wide=False)) + self.emit(f"{r} = OpSelect %uint {hit} {self.cu(0xFFFFFFFF if op == 's_bitset1_b32' else 0)} {old}") + self.put_mask(a[0], r, half) + if self.wave64 and a[0]["k"] == "special" and a[0].get("t") == "vcc_hi": + self.emit(f"OpStore %vcc_hi {r}") + else: + sh, old, r = self.tmp(), self.tmp(), self.tmp() + self.emit(f"{sh} = OpShiftLeftLogical %uint {self.cu(1)} {m}") + self.emit(f"{old} = OpCopyObject %uint {self.src_u(a[0])}") + if op == "s_bitset1_b32": + self.emit(f"{r} = OpBitwiseOr %uint {old} {sh}") + else: + n = self.tmp() + self.emit(f"{n} = OpNot %uint {sh}") + self.emit(f"{r} = OpBitwiseAnd %uint {old} {n}") + self.dst(a[0], r) elif op in ("v_mul_hi_u32_u24", "v_mul_hi_i32_i24"): # High 32 bits of the 48-bit product of the low 24 bits of each source: zero-extended for u24, # sign-extended from bit 23 for i24 (RDNA2 ISA). The low 32 bits are v_mul_u32_u24 / v_mul_i32_i24. @@ -9708,14 +9730,6 @@ def pair64(sv): self.emit(f"{c} = OpBitCount %uint {x}") self.emit(f"{r} = OpIAdd %uint {c} {acc}") self.dst(a[0], r) - elif op == "s_bitset0_b32": - b = self.src_u(a[1]) - m, sh, n, r = (self.tmp() for _ in range(4)) - self.emit(f"{m} = OpBitwiseAnd %uint {b} {self.cu(31)}") - self.emit(f"{sh} = OpShiftLeftLogical %uint {self.cu(1)} {m}") - self.emit(f"{n} = OpNot %uint {sh}") - self.emit(f"{r} = OpBitwiseAnd %uint {self.src_u(a[0])} {n}") - self.dst(a[0], r) elif op in ("s_min_i32", "s_max_i32"): x_val, y_val = self.imm_val(a[1]), self.imm_val(a[2]) if x_val is not None and y_val is not None: