diff --git a/docs/design.md b/docs/design.md index daeb4eb425..3dcac6e2d6 100644 --- a/docs/design.md +++ b/docs/design.md @@ -472,6 +472,7 @@ How the generated SPIR-V expects the runtime to work: | `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. The source must carry scalar data. Bit replication can also write EXEC, or VCC in wave64. 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. 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. \| +| Lane masks read as numbers (`lane_masks.NUMBER_READERS`) | A lane mask (EXEC, VCC, a mask SGPR, or half of a wave64 one) read by a 32-bit integer op (`s_add/sub/addc/subb/mul/mul_hi/lshl/lshr/ashr/bfe/min/max/brev`, `s_lshlN_add_u32`, `v_add/sub/subrev_nc_u32`, the shifts, `v_mul_lo/hi_u32`, `v_min/max`, `v_bfe`, `v_add3_u32`, `v_lshl_add_u32`, `v_add_lshl_u32`, `v_bcnt_u32_b32`) is the wave's mask word (`Gen.lane_mask_word`, as `s_bcnt1` and `s_flbit` already did), and so is one copied by a `v_mov_b32` whose value nothing reads as a mask afterwards. **Until 2026-11-24 each lane read its own 0 / ~0**, so `s_add_u32 d, mask, 1` was 0 or 1. Still per lane: SDWA and DPP forms, compares, 16-bit, float and 64-bit ops, and a `v_mov_b32` whose value (followed through VGPR copies) a `v_readfirstlane_b32` or another vector bitwise op reads as a mask (the mask-through-a-VGPR idiom). Such an op is an exchange under `--wave64-emulate` and never joins an active run. CPU-checked by `tests/semantic/test_mask_as_data.py`; the model `tools/crosslane_fuzz.py` uses ("maskdata") was measured on RDNA3. Not measured on RDNA2. | | Constant lane masks (`Gen.mask_src` immediates) | A constant read as a lane mask is one bit per lane: lane L takes bit L. The 64-bit forms (`s_and/or/andn2_saveexec_b64`, `s_andn2_wrexec_b64`, `s_mov_b64`/`s_not_b64` into EXEC or VCC, `s_and/or/xor/andn2/orn2_b64` and `s_cselect_b64` on the lane-mask path) read the 64-bit value, an inline constant sign-extended (`-16` = lanes 4-63) and a literal with a zero high word, as `src64_data` reads 64-bit constants; the b32 forms (`s_*_saveexec_b32`, `s_andn2_wrexec_b32`) read the 32-bit value at bit L mod 32. 0 and -1 still give no lane and every lane, with no per-lane code. `s_bcnt0/1_i32_b64` and `s_ff0/1_i32_b64` of a constant read the constant's 64 bits via `src64_data` (scalar data: inline negative constants sign-extend to 64 bits, so `s_ff0_i32_b64 s0, -1` gives −1 rather than 32, and 32-bit literals zero-extend; CPU-checked in `tests/semantic/test_scalar_bitcmp.py`, **not yet GPU-verified**). `s_bcnt0/1_i32_b64` of a constant counts the constant's 64 bits (scalar data, like `s_ff0/1_i32_b64`). **Before this fix every lane took bit 0 of the constant**: `s_and_saveexec_b32 s7, 0xff00` with all 32 lanes on left none on, `5` left all 32 on, and `s_bcnt1_i32_b64 s0, 0xff00ff` gave 32. llvm-mc rejects a constant as the mask operand of `v_cndmask_b32_e64` / `v_add_co_ci_u32_e64` and decodes one as "Invalid immediate", so those VALU callers never see a constant. **A constant held in an SGPR** (`s_mov_b32 s20, 0x3ffffc00`) is read the same way when it reaches a mask consumer: `mask_src` takes bit L of an SGPR that `holds_lane_mask` doesn't flag (a pair in a wave64 64-bit context: the high SGPR for lanes 32-63). In a wave32 or wave64 program a 32-bit bitwise SALU op (`s_not_b32`, `s_and/or/xor/andn2/orn2/nand/nor/xnor_b32`, `s_cselect_b32`) that writes EXEC/VCC (`exec_lo`/`vcc_lo` in wave32; `exec_lo`/`exec_hi` and mask-holding `vcc_lo`/`vcc_hi` in wave64, including after `s_mov_b64 vcc, 0/-1`) skips scalar constant folding, reads its operands through `Gen._mask_b32_src` / `mask_src` (collecting the opposite half through `wave_mask` if a wave64 op mixes `lo` and `hi` halves), preserves the untouched half of EXEC/VCC in wave64 (`exec_half_write` / `put_mask`), and votes SCC over the wave (wave32) or that 32-lane half (wave64, `scc_any_half`), matching `s_not_b32` and the binary ops on lane-mask SGPRs too; into a plain SGPR with a constant it stays a data read, because `lane_masks.py` classifies that SGPR as data when the other operand is a constant (`s_and_b32 s20, exec_lo, 1` then `s_bcnt1` must count the wave's bits: found by @Pierre-Kookis on #1792, GPU-reproduced), and `s_mov_b32 exec_lo/exec_hi, sN` takes each lane's bit. In a wave32 program a 32-bit bitwise SOP2 (`s_and/or/xor/andn2/orn2/nand/nor/xnor_b32`, `s_cselect_b32`) that writes EXEC/VCC reads both operands through `mask_src` (`Gen.sop2_b32_srcs`); into a plain SGPR it stays a data read, because `lane_masks.py` classifies that SGPR as data when the other operand is a constant (`s_and_b32 s20, exec_lo, 1` then `s_bcnt1` must count the wave's bits: found by @Pierre-Kookis on #1792, GPU-reproduced), and `s_mov_b32 exec_lo/exec_hi, sN` takes each lane's bit. A real mask is unchanged: it holds 0 or ~0 per lane, and bit L of that is the same 0 or 1. Until 2026-11-22 these took the value whole, so `s_and_saveexec_b32 s6, s20` with that constant, or `s_and_b32 exec_lo, exec_lo, 0xff00`, left every lane on (found writing `cs_w32_saveexec_ext`, #1771; CPU-checked by `tests/semantic/test_mask_src_sgpr_const.py`, and `tools/cf_diff.py` / `tools/salu_diff.py` still pass). `s_not_b32` on lane masks, `s_and/or/xor_b32` with two immediates into `vcc_lo`, and wave64 half-mask bitwise ops (destination half preservation, `exec_hi`, and `scc_any_half`) are CPU-checked in `tests/semantic/test_salu_b32_lane_masks.py` (wave32, wave64, and `--wave64-emulate`, validated with `spirv-val`) and GPU-checked on gfx1036, RTX 5080 (`--wave64-emulate`), and RX 9060 XT (RADV). Not covered here: a b32 op on one half of a wave64 mask, masks read as data (#293), and a literal with bit 31 set in a 64-bit position (zero- vs sign-extension not measured). GPU-checked with `runtime/test/shaders/cs_w32_mask_imm.asm` and `cs_w64_mask_imm.asm` on an NVIDIA GeForce RTX 5080 (driver 616.92; wave64 with `LEONIDA_W64_EMULATE=1`) and an AMD Radeon RDNA2 iGPU (driver 26.10.1, native 64-wide subgroup): both failed before on both GPUs. Run across every lane in `tests/semantic/test_value_semantics.py` (`LaneMaskConstants`). **A computed value written to EXEC** (`s_lshl_b32`, `s_add_u32`, `s_bfm_b32`, and every other SALU result that reaches `exec_lo`/`exec_hi` through `Gen.dst`) takes each lane's bit too: until 2026-11-24 the generic path tested the value for non-zero, so `s_lshl_b32 exec_lo, 1, s5` ran all 32 lanes of that half (SHADER-1264; `Gen.exec_bit`). The words are the GPU's, recorded in `runtime/test/shaders/cs_w32_exec_computed.asm` and `cs_w64_exec_computed.asm` (RTX 5070), and `tests/semantic/test_exec_computed.py` replays them through the CPU interpreter. A value that already holds this lane's bit of a mask (0 or ~0) gives the same answer. | | Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `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, and `s_cmov_b64`, stay `Unsupported`, #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. 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*`: 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). | | Vector integer additions (`v_mul_hi_u32_u24`/`v_mul_hi_i32_i24`, `v_lshrrev_b64`/`v_ashrrev_i64`, `v_qsad_pk_u16_u8`/`v_mqsad_pk_u16_u8`) | `v_mul_hi_*24`: the high 32 bits of the 48-bit product of the low 24 bits, zero-extended (u24) or sign-extended from bit 23 (i24); the high halves of `v_mul_u32_u24` / `v_mul_i32_i24`. `v_lshrrev_b64` / `v_ashrrev_i64`: the pair (or a 64-bit constant) shifted right by S0 & 63, logically or arithmetically, through the `v_lshlrev_b64` code. `v_qsad_pk_u16_u8` / `v_mqsad_pk_u16_u8`: D.u16[k] = (S2.u16[k] + byte SAD of the 32-bit window S0[8k+31:8k] against S1) & 0xFFFF; the m form skips bytes where S1's byte is 0; `clamp` raises `Unsupported`. Source: RDNA ISA pseudocode. CPU-checked by `tests/semantic/test_alu_mul_hi_24.py`, `test_alu_shift_b64.py`, `test_alu_qsad.py`; run on NVIDIA by reviewers (`v_mul_hi_*24`: RTX 5090 and RTX 5070 in #440; `v_qsad`/`v_mqsad`: 24 cases bit-exact on an RTX 3060 in #623). **Not measured on RDNA2.** **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137), and on native `gfx1036` (AMD Radeon Graphics) with 0 mismatches across signed/unsigned products, shifts, and sliding windows. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). | diff --git a/tests/semantic/test_mask_as_data.py b/tests/semantic/test_mask_as_data.py new file mode 100644 index 0000000000..144c5290c6 --- /dev/null +++ b/tests/semantic/test_mask_as_data.py @@ -0,0 +1,182 @@ +"""Value tests for a lane mask read as a number (SHADER-MASK-AS-DATA, crosslane_fuzz.py "maskdata"). + +On hardware a lane mask is one word for the wave, a bit per lane; the translator keeps only each lane's own bit +(0 / ~0), so a 32-bit integer op that reads the mask has to rebuild the word. Wave32 and native wave64 run one whole +subgroup in spirv_mini_interp.py. Standard library only; no GPU, no LLVM, no game files. +""" +import os +import struct +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.dont_write_bytecode = True +sys.path.insert(0, HERE) + +import spirv_mini_interp as si # noqa: E402 +import support # noqa: E402 + +rdna2spirv = support.rdna2spirv +M32 = 0xFFFFFFFF +K = 0x1234 + + +def fssm_for(wave): + fssm = bytearray(0x148) + fssm[:4] = b"FSSM" + struct.pack_into("> 32), ("vcc_lo", bits & M32), ("vcc_hi", bits >> 32)] + + +def signed(x): + return x - (1 << 32) if x >> 31 else x + + +SCALAR = { + "s_add_u32 s20, {m}, 0x1234": lambda m: m + K, + "s_add_i32 s20, 0x1234, {m}": lambda m: m + K, + "s_sub_u32 s20, {m}, 0x1234": lambda m: m - K, + "s_sub_i32 s20, 0x1234, {m}": lambda m: K - m, + "s_mul_i32 s20, {m}, 0x1234": lambda m: m * K, + "s_mul_hi_u32 s20, {m}, 0x1234": lambda m: (m * K) >> 32, + "s_lshl_b32 s20, {m}, 3": lambda m: m << 3, + "s_lshr_b32 s20, {m}, 3": lambda m: m >> 3, + "s_ashr_i32 s20, {m}, 3": lambda m: signed(m) >> 3, + "s_bfe_u32 s20, {m}, 0x60002": lambda m: (m >> 2) & 63, + "s_min_u32 s20, {m}, 0x1234": lambda m: min(m, K), + "s_max_u32 s20, {m}, 0x1234": lambda m: max(m, K), + "s_brev_b32 s20, {m}": lambda m: int(f"{m:032b}"[::-1], 2), + "s_lshl2_add_u32 s20, {m}, 0x1234": lambda m: (m << 2) + K, +} +VECTOR = { + "v_mov_b32_e32 v4, {m}": lambda m: m, + "v_add_nc_u32_e32 v4, {m}, v3": lambda m: m + K, + "v_sub_nc_u32_e32 v4, {m}, v3": lambda m: m - K, + "v_subrev_nc_u32_e32 v4, {m}, v3": lambda m: K - m, + "v_lshlrev_b32_e64 v4, 3, {m}": lambda m: m << 3, + "v_lshrrev_b32_e64 v4, 3, {m}": lambda m: m >> 3, + "v_ashrrev_i32_e64 v4, 3, {m}": lambda m: signed(m) >> 3, + "v_mul_lo_u32 v4, {m}, v3": lambda m: m * K, + "v_min_u32_e32 v4, {m}, v3": lambda m: min(m, K), + "v_max_u32_e32 v4, {m}, v3": lambda m: max(m, K), + "v_bfe_u32 v4, {m}, 2, 6": lambda m: (m >> 2) & 63, + "v_add3_u32 v4, {m}, v3, v3": lambda m: m + 2 * K, + "v_lshl_add_u32 v4, {m}, 2, v3": lambda m: (m << 2) + K, + "v_bcnt_u32_b32 v4, {m}, v3": lambda m: bin(m).count("1") + K, +} + + +class MaskAsDataTests(unittest.TestCase): + def check(self, lines, wave, want): + self.assertEqual({hex(x) for x in run(lines, wave)}, {hex(want & M32)}) + + def test_scalar_ops(self): + for wave, lanes in ((32, 10), (64, 43)): + setup, sources = mask_setup(wave, lanes) + for (op, fn), (reg, word) in ((o, s) for o in SCALAR.items() for s in sources): + with self.subTest(wave=wave, op=op, reg=reg): + self.check(setup + [op.format(m=reg), "v_mov_b32_e32 v4, s20"], wave, fn(word)) + + def test_vector_ops(self): + for wave, lanes in ((32, 10), (64, 43)): + setup, sources = mask_setup(wave, lanes) + for (op, fn), (reg, word) in ((o, s) for o in VECTOR.items() for s in sources): + with self.subTest(wave=wave, op=op, reg=reg): + self.check(setup + [f"v_mov_b32_e32 v3, {K:#x}", op.format(m=reg)], wave, fn(word)) + + def test_exec_as_data(self): + self.check(["v_mov_b32_e32 v4, exec_lo"], 32, M32) + self.check(["v_cmp_gt_u32_e32 vcc_lo, 10, v0", "s_and_saveexec_b32 s12, vcc_lo", "s_add_u32 s20, exec_lo, 1", + "s_mov_b32 exec_lo, s12", "v_mov_b32_e32 v4, s20"], 32, 0x400) + + def test_copied_mask(self): + self.check(["v_cmp_gt_u32_e64 s10, 10, v0", "s_mov_b32 s11, s10", "s_add_u32 s20, s11, 1", + "v_mov_b32_e32 v4, s20"], 32, 0x400) + + def test_both_sources_are_masks(self): + self.check(["v_cmp_gt_u32_e64 s10, 10, v0", "v_cmp_gt_u32_e64 s11, 4, v0", "s_sub_u32 s20, s10, s11", + "v_mov_b32_e32 v4, s20"], 32, 0x3FF - 0xF) + + def test_v_mov_under_exec_keeps_inactive_lanes_and_sees_every_lane(self): + # lanes 5-31 are off at the v_mov: they keep v4, and the active lanes still get the whole 10-lane mask + got = run(["v_mov_b32_e32 v4, 7", "v_cmp_gt_u32_e64 s10, 10, v0", "v_cmp_gt_u32_e32 vcc_lo, 5, v0", + "s_and_saveexec_b32 s12, vcc_lo", "v_mov_b32_e32 v4, s10", "s_mov_b32 exec_lo, s12"], 32) + self.assertEqual(got, [0x3FF] * 5 + [7] * 27) + + def test_a_mask_that_goes_through_a_vgpr_stays_a_mask(self): + # v_mov / v_or3 / v_readfirstlane and back into EXEC: the idiom keeps each lane's own bit, no rebuild + lines = ["v_cmp_gt_u32_e64 s10, 10, v0", "v_cmp_lt_u32_e64 s11, 20, v0", "v_mov_b32_e32 v5, s10", + "v_or3_b32 v5, v5, s11, s11", "v_readfirstlane_b32 s14, v5", "v_mov_b32_e32 v4, 1", + "s_and_saveexec_b32 s12, s14", "v_mov_b32_e32 v4, 2", "s_mov_b32 exec_lo, s12"] + self.assertEqual(run(lines, 32), [2 if i < 10 or i > 20 else 1 for i in range(32)]) + + def test_a_register_reused_for_another_mask(self): + # v5 goes through the idiom first; the second mask copied into it is only read as a number + lines = ["v_cmp_gt_u32_e64 s10, 10, v0", "v_mov_b32_e32 v5, s10", "v_readfirstlane_b32 s14, v5", + "v_cmp_gt_u32_e64 s11, 4, v0", "v_mov_b32_e32 v5, s11", "v_add_nc_u32_e32 v4, 1, v5"] + self.check(lines, 32, 0x10) + + def test_a_number_before_the_register_is_used_for_the_idiom(self): + lines = ["v_cmp_gt_u32_e64 s11, 4, v0", "v_mov_b32_e32 v5, s11", "v_add_nc_u32_e32 v4, 1, v5", + "v_cmp_gt_u32_e64 s10, 10, v0", "v_mov_b32_e32 v5, s10", "v_readfirstlane_b32 s14, v5", + "s_and_saveexec_b32 s12, s14", "v_mov_b32_e32 v4, 2", "s_mov_b32 exec_lo, s12"] + self.assertEqual(run(lines, 32), [2 if i < 10 else 0x10 for i in range(32)]) + + def test_copies_of_the_vgpr(self): + # a chain of VGPR copies read as a number, and one that ends in the idiom + head = ["v_cmp_gt_u32_e64 s10, 10, v0", "v_mov_b32_e32 v5, s10", "v_mov_b32_e32 v6, v5"] + self.check(head + ["v_mov_b32_e32 v4, v6"], 32, 0x3FF) + self.assertEqual(run(head + ["v_mov_b32_e32 v4, 1", "v_readfirstlane_b32 s14, v6", "s_and_saveexec_b32 s12, s14", + "v_mov_b32_e32 v4, 2", "s_mov_b32 exec_lo, s12"], 32), + [2 if i < 10 else 1 for i in range(32)]) + + def test_two_moves_that_meet_at_a_mask_read(self): + # one of two copies on two paths, then read as a mask: both keep the lane's own bit + lines = ["v_cmp_gt_u32_e64 s10, 10, v0", "v_cmp_gt_u32_e64 s11, 4, v0", "v_mov_b32_e32 v5, s10", + "v_mov_b32_e32 v6, s11", "v_or_b32_e32 v5, v5, v6", "v_readfirstlane_b32 s14, v5", + "v_mov_b32_e32 v4, 1", "s_and_saveexec_b32 s12, s14", "v_mov_b32_e32 v4, 2", "s_mov_b32 exec_lo, s12"] + self.assertEqual(run(lines, 32), [2 if i < 10 else 1 for i in range(32)]) + + def test_plain_data_is_untouched(self): + lines = ["s_mov_b32 s10, 0x3ff", "s_add_u32 s20, s10, 1", "v_mov_b32_e32 v4, s20"] + self.check(lines, 32, 0x400) + self.assertNotIn("OpGroupNonUniform", translate(lines, 32)) + + def test_wave64_emulate_translates_with_an_exchange(self): + old = rdna2spirv.WAVE64_EMULATE + rdna2spirv.WAVE64_EMULATE = True + try: + spv = translate(["v_cmp_gt_u32_e64 s[10:11], 43, v0", "s_add_u32 s20, s11, 1", "v_mov_b32_e32 v4, s20"], 64) + finally: + rdna2spirv.WAVE64_EMULATE = old + self.assertIn("%x_buf", spv) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/semantic/test_salu64_bitwise_data.py b/tests/semantic/test_salu64_bitwise_data.py index c8e3f18388..6437692646 100644 --- a/tests/semantic/test_salu64_bitwise_data.py +++ b/tests/semantic/test_salu64_bitwise_data.py @@ -558,9 +558,9 @@ def test_wave32_all_six_ops_and_scc_vote(self): "v_mov_b32 v3, 0", ] outs, _ = run_ps_wave(lines, wave=32) + want_word = sum(1 << lane for lane in range(32) if pred(lane)) # v_mov_b32 reads the mask's word for lane in range(32): - want_bit = MASK32 if pred(lane) else 0 - self.assertEqual(outs[lane], (want_cnt, want_scc, want_bit, 0), f"lane={lane}") + self.assertEqual(outs[lane], (want_cnt, want_scc, want_word, 0), f"lane={lane}") # Empty mask intersection -> SCC=0 across all lanes, and null dst on lane-mask path sets SCC. lines = [ diff --git a/tests/semantic/test_salu_movrels_lane_mask.py b/tests/semantic/test_salu_movrels_lane_mask.py index 15a7916092..2922d3c380 100644 --- a/tests/semantic/test_salu_movrels_lane_mask.py +++ b/tests/semantic/test_salu_movrels_lane_mask.py @@ -80,7 +80,7 @@ def test_finding_reproducer(self): with self.subTest(lane=lane): self.assertEqual((d, bits), (5, 2)) self.assertEqual(ctl_bits, 2) - self.assertEqual(ctl_d, 0xFFFFFFFF if lane in (0, 2) else 0) # a copy stays a mask + self.assertEqual(ctl_d, 5) # the copy stays a mask; store()'s v_mov reads its word def test_index_from_m0_hits_mask_and_data_neighbours(self): # s10 data, s11 a v_cmp mask (lanes 1, 4, 9), s12 data; M0 picks each in turn from a base of s9. diff --git a/tests/semantic/test_scselect_b64_data.py b/tests/semantic/test_scselect_b64_data.py index 2dd7032b3a..17daed030a 100644 --- a/tests/semantic/test_scselect_b64_data.py +++ b/tests/semantic/test_scselect_b64_data.py @@ -383,7 +383,7 @@ def test_wave32_vcc_exec_and_sgpr_masks(self): for lane in range(32): in_mask = (lane < 10) if scc else (lane >= 16) want_v1 = 0x55 if (scc == 0 or in_mask) else 0xAA - want_s4 = MASK32 if in_mask else 0 + want_s4 = 0x3FF if scc else 0xFFFF0000 # v_mov_b32 reads the mask's word self.assertEqual(outs[lane], (want_cnt, want_v1, scc, want_s4), f"lane={lane}") def test_wave64_vcc_exec_and_sgpr_masks(self): diff --git a/tests/semantic/test_slot67_operand_forms.py b/tests/semantic/test_slot67_operand_forms.py index e54188c714..869cc072a7 100644 --- a/tests/semantic/test_slot67_operand_forms.py +++ b/tests/semantic/test_slot67_operand_forms.py @@ -18,11 +18,7 @@ def test_wave64_bitreplicate_exec_reenables_lanes_and_keeps_scc(self): expected = {i * 4: 7 for i in (0, 1, 32, 33, 62, 63)} self.assertEqual(_run_cs("cs_bitreplicate_exec", lines), expected) - # The merge into main kept main's wave32 VCC_HI model (47494e725: VCC_HI that holds a compare result is a - # per-lane mask), not this branch's packed-word form (1f001c739). On hardware `s_mov_b32 s8, vcc_hi` copies the - # packed 32-bit word (0x80000004 here); main's translator gives each lane its own mask value instead, so this - # case fails there. Kept as the expectation the translator should meet. - @unittest.expectedFailure + # `s_mov_b32 s8, vcc_hi` then `v_mov_b32 v1, s8` is the packed 32-bit word (0x80000004 here): SHADER-MASK-AS-DATA. def test_wave32_compare_high_word_preserves_low_and_clears_inactive_bits(self): lines = [ "v_cmp_lt_u32_e64 vcc_lo, v0, 6", diff --git a/tools/lane_masks.py b/tools/lane_masks.py index 07dddd6703..da66e39bfb 100644 --- a/tools/lane_masks.py +++ b/tools/lane_masks.py @@ -52,6 +52,19 @@ VECTOR_BITWISE = ("v_mov_b32", "v_or3_b32", "v_or_b32", "v_and_b32", "v_and_or_b32", "v_xor_b32", "v_xor3_b32", "v_xnor_b32", "v_bfi_b32", "v_not_b32") +# 32-bit integer ops that use their sources as numbers: a lane mask they read has to be the wave's mask word, so the +# translator rebuilds it from the lanes' bits (SHADER-MASK-AS-DATA). Bitwise ops, selects and compares are not here: +# they work on the lane's own bit, or have a mask path of their own. +NUMBER_READERS = frozenset(( + "s_add_u32", "s_add_i32", "s_sub_u32", "s_sub_i32", "s_addc_u32", "s_subb_u32", "s_mul_i32", "s_mul_hi_u32", + "s_mul_hi_i32", "s_lshl_b32", "s_lshr_b32", "s_ashr_i32", "s_bfe_u32", "s_bfe_i32", "s_min_u32", "s_min_i32", + "s_max_u32", "s_max_i32", "s_brev_b32", "s_lshl1_add_u32", "s_lshl2_add_u32", "s_lshl3_add_u32", + "s_lshl4_add_u32", + "v_add_nc_u32", "v_sub_nc_u32", "v_subrev_nc_u32", "v_lshlrev_b32", "v_lshrrev_b32", "v_ashrrev_i32", + "v_mul_lo_u32", "v_mul_hi_u32", "v_min_u32", "v_max_u32", "v_min_i32", "v_max_i32", "v_bfe_u32", "v_bfe_i32", + "v_add3_u32", "v_lshl_add_u32", "v_add_lshl_u32", "v_bcnt_u32_b32", +)) + def base_op(op): for suffix in ("_e32", "_e64", "_sdwa", "_dpp"): @@ -152,7 +165,78 @@ def wave32_vcc_pair_op(op, args): return len(args) == 3 and vcc[0] and ((vcc[1] and pairs[2]) or (pairs[1] and vcc[2])) -def step(state, ins, wave64=True): +def mask_word_source(state, a, wave64=True): + """Whether operand `a` is one 32-bit scalar register that holds a lane mask (or half of one) in `state`.""" + if a["k"] == "special": + t = a.get("t", "") + if t == "exec_lo" or (t == "exec_hi" and wave64): + return True + if t in ("vcc_lo", "vcc_hi"): + return state.get("vcc_hi" if t == "vcc_hi" and not wave64 else "vcc") in MASK_KINDS + return False + return a["k"] == "sgpr" and a.get("n", 1) == 1 and state.get(a["r"]) in MASK_KINDS + + +def _plain_move(ins): + a = ins["args"] + return (base_op(ins["op"]) == "v_mov_b32" and not ins["op"].endswith(("_sdwa", "_dpp", "_dpp8")) and len(a) == 2 + and not ins.get("mods") and a[0]["k"] == "vgpr" + and not any(a[1].get(m) for m in ("neg", "abs", "sext"))) + + +def number_moves(blocks, succ, masks, wave64=True): + """The v_mov_b32 of a mask into a VGPR whose value nothing reads as a mask: ids of the instructions. + + A value is read as a mask when a vector bitwise op or v_readfirstlane reads the VGPR while it holds one (the + v_mov / v_or3 / v_readfirstlane idiom). Each move is followed forward, through VGPR copies, while the register holds + a mask, so reuse of the register for another mask does not matter; moves that meet at such a read are all left as they were.""" + moves = {id(ins): ins["args"][0]["r"] for blk in blocks for ins in blk + if _plain_move(ins) and mask_word_source(masks.get(id(ins), {}), ins["args"][1], wave64)} + if not moves: + return set() + used, entry = set(), [None] * len(blocks) # entry: VGPR -> the moves whose value it may hold + entry[0] = {} + work = [0] + while work: + n = work.pop() + reach = dict(entry[n]) + for ins in blocks[n]: + op, a, state = base_op(ins["op"]), ins["args"], masks.get(id(ins), {}) + reach = {r: ids for r, ids in reach.items() if ("v", r) in state} + copied = None + if _plain_move(ins) and a[1]["k"] == "vgpr": + copied = reach.get(a[1]["r"]) # a copy of the value: what reads the copy decides + elif id(ins) not in moves and (op in VECTOR_BITWISE or op == "v_readfirstlane_b32"): + for x in a[1:]: + if x["k"] == "vgpr": + used |= reach.get(x["r"], frozenset()) + if id(ins) in moves: + reach[moves[id(ins)]] = frozenset((id(ins),)) + elif a and a[0]["k"] == "vgpr": # written again: a mask computed in the VGPR is not a move + for r in range(a[0]["r"], a[0]["r"] + a[0].get("n", 1)): + reach.pop(r, None) + if copied: + reach[a[0]["r"]] = copied + for s in succ[n]: + old = entry[s] + merged = dict(old or {}) + for r, ids in reach.items(): + merged[r] = merged.get(r, frozenset()) | ids + if old is None or merged != old: + entry[s] = merged + work.append(s) + return set(moves) - used + + +def mask_word_operands(ins, state, moves=frozenset(), wave64=True): + """Indices of the operands of `ins` that read a lane mask as a number.""" + op, a = base_op(ins["op"]), ins["args"] + if id(ins) not in moves and (op not in NUMBER_READERS or ins["op"].endswith(("_sdwa", "_dpp", "_dpp8"))): + return () + return tuple(i for i in range(1, len(a)) if mask_word_source(state, a[i], wave64)) + + +def step(state, ins, wave64=True, moves=frozenset()): """Update `state` (register -> MASK | MASK_LO | MASK_HI | EITHER) for one instruction. True if it writes a mask to a VGPR. `wave64` False: the program runs in 32-lane waves, where a 64-bit s_bfm_b64 result can't be a lane mask.""" @@ -224,7 +308,7 @@ def step(state, ins, wave64=True): written = range(a[0]["r"], a[0]["r"] + a[0].get("n", 1)) for key in [k for k in state if isinstance(k, tuple) and k[1] in written]: del state[key] - if None not in kinds and any(k in MASK_KINDS for k in kinds): + if id(ins) not in moves and None not in kinds and any(k in MASK_KINDS for k in kinds): ms = {k for k in kinds if k in MASK_KINDS} state[("v", a[0]["r"])] = ms.pop() if len(ms) == 1 else MASK # A plain copy (v_mov_b32) is an ordinary VALU write: inactive lanes keep the old @@ -288,7 +372,7 @@ def parked(blocks, masks): return sorted(found) -def analyse(blocks, succ, *, include_either=False, wave64=True): +def analyse(blocks, succ, *, include_either=False, wave64=True, moves=frozenset()): entry = [None] * len(blocks) # None: not reached yet if blocks: entry[0] = {} @@ -297,7 +381,7 @@ def analyse(blocks, succ, *, include_either=False, wave64=True): n = work.pop() state = dict(entry[n]) for ins in blocks[n]: - step(state, ins, wave64) + step(state, ins, wave64, moves) for s in succ[n]: if entry[s] is None: merged = dict(state) @@ -323,6 +407,6 @@ def analyse(blocks, succ, *, include_either=False, wave64=True): out[id(ins)] = {r: k for r, k in state.items() if k in MASK_KINDS} if include_either: either[id(ins)] = {r for r, k in state.items() if k == EITHER} - if step(state, ins, wave64): + if step(state, ins, wave64, moves): writes.add(id(ins)) return (out, writes, either) if include_either else (out, writes) diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 936de535a9..57aa751037 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -1736,14 +1736,15 @@ def dst(self, a, val, *, eager=False): def lane_local(self, ins): """True when ins only reads and writes this invocation's own registers, and its VGPR writes are the only ones EXEC gates. Each invocation is one lane, so skipping it where the lane is inactive is the same as - keeping the old value of everything it writes: what dst()'s per-write OpSelect does. A lane mask read as - data is this lane's bit (only s_bcnt1, s_ff1 and v_mbcnt gather the whole wave's); a mask on its way + keeping the old value of everything it writes: what dst()'s per-write OpSelect does. An op that + rebuilds a mask's word from every lane (mask_words) is not; a mask on its way through a VGPR (mask_writes) is written in every lane, so it stays out.""" op = base_op(ins["op"]) a = ins["args"] return (op.startswith("v_") and op not in NOOPS and not op.startswith(NOT_LANE_LOCAL) and "_co_" not in op and not ins["op"].endswith("_dpp") and not any(m.startswith(DPP_MODS) for m in ins["mods"]) - and bool(a) and a[0]["k"] == "vgpr" and id(ins) not in self.mask_writes) + and bool(a) and a[0]["k"] == "vgpr" and id(ins) not in self.mask_writes + and not ins.get("mask_words")) def insns(self, seq): """Translate seq, a straight run of instructions (no branch among them). A run of at least MIN_ACTIVE_RUN @@ -6163,6 +6164,16 @@ def park_masks(self, blocks): wave64_emulate_opt.mark_vcc_data_moves(*blocks) self.lane_masks, self.mask_writes, self.lane_either = lane_masks.analyse(*blocks, include_either=True, wave64=self.wave64) + moves = lane_masks.number_moves(*blocks, self.lane_masks, self.wave64) + if moves: # these VGPRs get the mask's word, so they hold data from here on + self.lane_masks, self.mask_writes, self.lane_either = lane_masks.analyse( + *blocks, include_either=True, wave64=self.wave64, moves=moves) + for blk in blocks[0]: + for ins in blk: + words = lane_masks.mask_word_operands(ins, self.lane_masks.get(id(ins), {}), moves, self.wave64) + ins.pop("mask_words", None) + if words: + ins["mask_words"] = words # read by insn() and by the emulation planner # lane_masks merges a mask on one path with plain data on another by dropping the register, # so "not in cur_masks" does not prove it is data on every path. Keep a conservative set. self.ever_mask = {r for m in self.lane_masks.values() for r in m if isinstance(r, int)} @@ -7185,6 +7196,13 @@ def insn(self, ins): if self.emulate: self.x_memory_order(base_op(ins["op"])) self.cur_either = self.lane_either.get(id(ins), ()) + if ins.get("mask_words"): + # a lane mask read as a number: the wave's mask word instead of this lane's 0 / ~0 + args = list(ins["args"]) + for i in ins["mask_words"]: + args[i] = {**{k: v for k, v in args[i].items() if k not in ("r", "n", "t")}, + "k": "ssa", "v": self.lane_mask_word(args[i])} + ins = {**ins, "args": args} try: if ins["op"].endswith("_dpp"): bop = base_op(ins["op"]) diff --git a/tools/wave32_pixel_scan.py b/tools/wave32_pixel_scan.py index ffabbf0dc3..fa60a4a3d0 100644 --- a/tools/wave32_pixel_scan.py +++ b/tools/wave32_pixel_scan.py @@ -196,6 +196,7 @@ def scan_shader(insns): """Per-family occurrence counts for one wave32 program (a Counter, empty when nothing matches).""" blocks, succ = blocks_of(insns) masks_at, _, either_at = lane_masks.analyse(blocks, succ, include_either=True, wave64=False) + moves = lane_masks.number_moves(blocks, succ, masks_at, wave64=False) hits = Counter() for blk in blocks: scc_from_mask = False # the last SCC writer in this block was a lane-mask SOP @@ -203,6 +204,8 @@ def scan_shader(insns): op = rdna2spirv.base_op(ins["op"]) masks, either = masks_at.get(id(ins), {}), either_at.get(id(ins), set()) fam = family_of(ins, masks, either) + if not fam and lane_masks.mask_word_operands(ins, masks, moves, wave64=False): + fam = "mask_word" # a lane mask read as a number if fam: hits[fam] += 1 elif op in MASK_BRANCHES or (reads_scc(ins, op) and scc_from_mask): diff --git a/tools/wave64_emulate_opt.py b/tools/wave64_emulate_opt.py index d6e1ad7d5b..94811467bf 100644 --- a/tools/wave64_emulate_opt.py +++ b/tools/wave64_emulate_opt.py @@ -924,6 +924,8 @@ def _classify_block_body(insns, lane_masks, scc_live, mask_writes): mem_w = True # 2. Cross-lane mask readers (`wave_mask`, `wave_equal`, live-SCC `scc_from_mask`). + if ins.get("mask_words"): # a lane mask read as a number (lane_masks.mask_word_operands) + return False, _EMPTY_MEM_STATS, 0, (), () a = ins["args"] mask_chk = m[11] if mask_chk: @@ -1291,6 +1293,9 @@ def find_local_exec_folds(blocks, raw_edges, succ, lane_masks, scc_live, mask_wr if sacc[1]: mem_w = True + if ins.get("mask_words"): + clean = False + continue a = ins["args"] mask_chk = m[11] if mask_chk: @@ -1531,7 +1536,7 @@ def _insn_may_barrier_with_meta(ins, meta, lane_masks, scc_live): if meta[1]: return False flags = meta[8] - if flags & _FLAG_ALWAYS_BARRIER or _vcc_pair_collects_mask(ins, lane_masks): + if flags & _FLAG_ALWAYS_BARRIER or ins.get("mask_words") or _vcc_pair_collects_mask(ins, lane_masks): return True mask_chk = meta[11] if not mask_chk: