diff --git a/docs/design.md b/docs/design.md index 656418c99a..223de03771 100644 --- a/docs/design.md +++ b/docs/design.md @@ -302,7 +302,7 @@ How the generated SPIR-V expects the runtime to work: | Null sources in 64-bit integer VALU compares | `v_cmp` and `v_cmpx` relations `eq`, `ne`, `lt`, `le`, `gt`, `ge` on `u64` and `i64` read `null` as the 64-bit value 0 in either source, like immediate `0`. Register pairs and immediates retain their existing semantics; `v_cmp` writes the active lane's comparison to VCC/SGPR, while `v_cmpx` narrows EXEC without changing VCC. Source: RDNA2 ISA special operand `null` reads zero; LLVM `llvm-mc -mcpu=gfx1013` accepts the encoding. CPU-checked by `tests/semantic/test_vcmp64_null_src.py` (both sources, signed/unsigned ordering, equality, e32/e64, EXEC and inactive lanes); **not GPU-verified, no corpus count**. | | `s_load_dword*` with `vcc` 64-bit base pointer | SMEM `s_load_dword*` accepts `vcc` (`{vcc_lo, vcc_hi}`, hardware `s[106:107]`) as its 64-bit base pointer in `Gen.scalar_load`, loading `%vcc` as the low dword and `%vcc_hi` as the high dword; `s_buffer_load*` still requires a 4-dword V# and rejects 2-dword `vcc`. Source: RDNA2 ISA SMEM `SBASE` encoding (`sbase = 53` -> `s[106:107] = vcc`). Confirmed by corpus count over the 2,684-shader pipeline library compute corpus (79 shaders use `vcc` as the `s_load` base: 72 `s_load_dwordx4 sgpr, vcc`, 6 `s_load_dwordx8 sgpr, vcc`, 1 `s_load_dwordx4 slist, vcc`; all 79 translate and pass `spirv-val` under `--wave64-emulate`) and CPU-checked by `tests/semantic/test_sload_vcc_base.py` (`spirv-as` + `spirv-val`) and `tests/semantic/test_scalar_mem_smem.py` (both halves and the resulting addresses); **not GPU-verified**: the synthetic `runtime/test/shaders/cs_smem_vcc_base.asm` reads back eight seeded words through the pair in the standard GPU runner. | | Execution model | One invocation = one lane. Compute and mesh waves derive lanes from the workgroup-local index; other stages use the subgroup lane. A wave = a subgroup, so cross-lane ops (`ds_swizzle`, `v_permlane*`, `v_readlane`/`v_writelane`, `v_mbcnt`) become subgroup shuffles. Wave32 pixel shaders split a 64-lane physical subgroup into two logical waves: lane IDs are `SubgroupLocalInvocationId & 31`, shuffle sources retain the current half (`& 32`), and ballots/votes use that half's 32-bit word. A value the translator knows is uniform (the bindless descriptor index) is read from the half's first running lane, never from a fixed lane, which need not be running (SHADER-1016). Other stage/wave combinations still require subgroup size == wave size or wave64 emulation. **Requires subgroup size == wave size** (wave32 maps directly onto NVIDIA GPUs; wave64 shaders need a 64-wide subgroup or emulation). `v_readlane_b32`/`v_writelane_b32` mask the lane select with 63 in wave64 (`S1[5:0]`) and 31 in wave32 (`S1[4:0]`); until SHADER-52 both used 63, so a wave32 select ≥ 32 pointed past a 32-wide subgroup. GPU-checked with `cs_w32_lane_wrap.asm` (lane 40 → 8). For the fragment-half rule, `ps_bitmask_perm` produced 4096/4096 exact pixels on an AMD Radeon RX 5700 XT; Vulkan reported a default subgroup size of 64 (range 32..64), but does not permit pinning fragment subgroup size on this device. CPU semantic tests compare a 64-lane wave32 pixel subgroup against two 32-lane subgroups for reads/writes, permutations, ballots, and votes. | -| DPP | VOP1/VOP2 `_dpp` applies to src0. DPP16 controls (`quad_perm`, `row_shl/shr/ror/share/xmask/mirror/half_mirror`) and DPP8 (`dpp8` + `fi`) select the source lane, then `row_mask` (one bit per 16-lane row) and `bank_mask` (one bit per 4-lane bank) decide which destination lanes update. A lane whose row/bank is off is not written. A source lane outside the row is invalid: `bound_ctrl` 1 uses 0 as src0, otherwise the lane is not written. Hardware applies src0 float modifiers (`neg`/`abs`) after the invalid-lane zero selection, so `-src0` on an invalid lane yields `-0.0f` (`0x80000000`) and `\|src0\|` yields `+0.0f` (HW-DPP-NEG-INVALID-LANE, measured on RDNA3 (gfx1101) RX 7800 XT, RDNA2 not measured; tested by `tests/semantic/test_dpp_bound_ctrl_modifiers.py`). A source lane inactive under EXEC is used when `fi` is 1; otherwise `bound_ctrl` decides. DPP8 always writes an enabled lane: `fi` 1 reads the inactive source, `fi` 0 uses 0. DPP16/8 never leaves its 16/8-lane group, so this is exact on every subgroup at least that wide; under `--wave64-emulate` the exchange goes through the workgroup buffer like the other cross-lane ops. GPU-checked with `runtime/test/shaders/cs_dpp*.asm` on an RTX 3070 and hardware-measured on an RX 6800. On an RX 6700 XT (gfx1031), `tools/crosslane_fuzz.py --dpp-sweep --oracle hw` checks every DPP16 control × `bound_ctrl` × `fi`, every `row_mask`/`bank_mask` pair and the DPP8 selects in wave32 and wave64: 14,412 cases, all matching these rules (BUILD-162, `docs/shader_validation.md` FUZZ-7). The ballot of the source lanes' EXEC bits and the shuffle are each stored to a Function variable of their own (`Gen.eager`): SPIRV-Cross (MoltenVK) inlines a single-use result into the expression that uses it, which put the ballot on the right of a short-circuiting `&&` and the shuffle in a ternary's arm, so lanes out of their row skipped the ballot and row_shr:1's lane 1 found lane 0 inactive (`cs_dpp_ops` on MoltenVK; SHADER-529). A store is a statement every lane runs. For native FI=0, the source-lane ballot bit also selects the shuffle index: an absent or EXEC-off source is redirected to the current subgroup lane before `OpGroupNonUniformShuffle`, because masking an undefined shuffle result afterwards is not sufficient; FI=1 is unchanged. CPU-checked by `tests/semantic/test_dpp_eager_subgroup.py::test_fi0_shuffle_index_is_ballot_safe` with a hand-written `quad_perm` case whose active lane 6 reads absent lane 7 in a seven-invocation wave; **not GPU-verified**. Checked in SPIRV-Cross 1.3.239's MSL for the `cs_dpp*` tests (each op is now its own statement) and by `tests/semantic/test_dpp_eager_subgroup.py`; not yet run on a Mac. Semantics from the RDNA2 ISA, AMD LLVM modifier syntax and the GPUOpen DPP notes. | +| DPP | VOP1/VOP2 `_dpp` applies to src0. DPP16 controls (`quad_perm`, `row_shl/shr/ror/share/xmask/mirror/half_mirror`) and DPP8 (`dpp8` + `fi`) select the source lane, then `row_mask` (one bit per 16-lane row) and `bank_mask` (one bit per 4-lane bank) decide which destination lanes update. A lane whose row/bank is off is not written. A source lane outside the row is invalid: `bound_ctrl` 1 uses 0 as src0, otherwise the lane is not written. Hardware applies src0 float modifiers (`neg`/`abs`) after the invalid-lane zero selection, so `-src0` on an invalid lane yields `-0.0f` (`0x80000000`) and `\|src0\|` yields `+0.0f` (HW-DPP-NEG-INVALID-LANE, measured on RDNA3 (gfx1101) RX 7800 XT, RDNA2 not measured; tested by `tests/semantic/test_dpp_bound_ctrl_modifiers.py`). `Gen.dpp_insn` forwards `neg`/`abs` on the post-select `ssa` operand rather than eagerly calling 32-bit `modify()`, so 16-bit float DPP ops (`v_*_f16_dpp`, `v_cvt_f32_f16_dpp`, `v_cmp_*_f16_dpp`) apply `neg`/`abs` to the f16 sign bit (bit 15) and sign-bit-move/sNaN-preserving ops (`v_cndmask_b32_dpp`, `v_min/max_f32_dpp`, `v_cmp_class_f32_dpp`) keep raw bitwise sign moves without `FAbs`/`OpFNegate` (SHADER-DPP-F16-AND-SIGN-MODS; CPU-checked by `tests/semantic/test_dpp_f16_mods.py`, not yet GPU-verified). A source lane inactive under EXEC is used when `fi` is 1; otherwise `bound_ctrl` decides. DPP8 always writes an enabled lane: `fi` 1 reads the inactive source, `fi` 0 uses 0. DPP16/8 never leaves its 16/8-lane group, so this is exact on every subgroup at least that wide; under `--wave64-emulate` the exchange goes through the workgroup buffer like the other cross-lane ops. GPU-checked with `runtime/test/shaders/cs_dpp*.asm` on an RTX 3070 and hardware-measured on an RX 6800. On an RX 6700 XT (gfx1031), `tools/crosslane_fuzz.py --dpp-sweep --oracle hw` checks every DPP16 control × `bound_ctrl` × `fi`, every `row_mask`/`bank_mask` pair and the DPP8 selects in wave32 and wave64: 14,412 cases, all matching these rules (BUILD-162, `docs/shader_validation.md` FUZZ-7). The ballot of the source lanes' EXEC bits and the shuffle are each stored to a Function variable of their own (`Gen.eager`): SPIRV-Cross (MoltenVK) inlines a single-use result into the expression that uses it, which put the ballot on the right of a short-circuiting `&&` and the shuffle in a ternary's arm, so lanes out of their row skipped the ballot and row_shr:1's lane 1 found lane 0 inactive (`cs_dpp_ops` on MoltenVK; SHADER-529). A store is a statement every lane runs. For native FI=0, the source-lane ballot bit also selects the shuffle index: an absent or EXEC-off source is redirected to the current subgroup lane before `OpGroupNonUniformShuffle`, because masking an undefined shuffle result afterwards is not sufficient; FI=1 is unchanged. CPU-checked by `tests/semantic/test_dpp_eager_subgroup.py::test_fi0_shuffle_index_is_ballot_safe` with a hand-written `quad_perm` case whose active lane 6 reads absent lane 7 in a seven-invocation wave; **not GPU-verified**. Checked in SPIRV-Cross 1.3.239's MSL for the `cs_dpp*` tests (each op is now its own statement) and by `tests/semantic/test_dpp_eager_subgroup.py`; not yet run on a Mac. Semantics from the RDNA2 ISA, AMD LLVM modifier syntax and the GPUOpen DPP notes. | | DPP on a VOPC compare | A VOPC compare whose src0 field is a DPP marker (250 for DPP16, 233/234 for DPP8) takes src0 through the DPP lane selection of the row above and writes VCC: a lane's bit is `compare(src0 of the selected lane, its own src1)`, with 0 for src0 when the selected lane is out of its row or inactive (`bound_ctrl` 1, or DPP8 with `fi` 0). The second word has the layout of a VOP2's DPP word. `llvm-mc` has no decoding for it on gfx10 (LLVM 21.1.8 rejects the first word for `gfx1013`; LLVM defines the form from GFX11 on), so `tools/shader_disasm.py` prints it: it decodes the pair with the first word turned into `v_max_f32` into `v0`, which takes the same second word, and gives the line the compare's name, a `vcc_lo` destination and the real encoding back (`with_vopc_dpp`); `tools/rdna2_decode.py` decodes it directly. **Only the forms where every lane writes its bit are translated**: `row_mask` and `bank_mask` 0xF and either `bound_ctrl` 1 or DPP8. Any other form, and `v_cmpx`, is refused (`DPP compare with lanes that keep their mask bit`), because what a lane DPP leaves unwritten does to its VCC bit is not measured. Found in 3 of the 2,684 pipeline-library compute programs of 1.0.2.1: 4 `v_cmp_gt_f32` each, with `quad_perm:[1,0,3,2]`, `quad_perm:[2,3,0,1]`, `row_ror:8` (all `bound_ctrl:1`, masks 0xF) and `dpp8:[4,5,6,7,0,1,2,3]`, beside `v_min/max_f32_dpp` and `v_mov_b32_dpp` on the same lanes. **Not measured on hardware**: the rule follows from the encoding, from LLVM's GFX11 definition of the same form and from the VOP2 DPP rules measured in the row above; the sweep that measured those (`tools/crosslane_fuzz.py --dpp-sweep --oracle hw`) does not cover compares. Lane values are checked in `tests/semantic/test_dpp_compare.py` (the SPIR-V interpreter) and on a GPU with `runtime/test/shaders/cs_dpp_cmp.asm`. | | EXEC | A per-invocation `active` flag. VALU writes and exports are masked by it, scalar ops aren't, and `v_cmpx`, saveexec and EXEC writes update it. Measured on native gfx1100 (8 threads): a `v_mov_b32` under `EXEC=0x0F` leaves the other lanes untouched, `EXEC=0` writes nothing, a store writes only active lanes and `v_cmpx_gt_u32` narrows EXEC (4 of 4 cases); note that AMD clang 20 rejects `v_add_co_u32`, `v_sub_co_u32`, `v_add_co_ci_u32` and `v_mad_u64_u32` for `gfx1100`, `gfx1101` and `gfx1102`, so this toolchain could not reproduce the RX 7600 result below, which leaves open whether those parts support the opcodes and says only that they could not be assembled here. **A pixel is written when its lane was on in EXEC at the last `exp ... vm` the path executed (if it executed one) and at the last export to the color target; a path with no export at all goes by EXEC at the end.** The `vm` half is the RDNA2 ISA reference guide's rule (30-November-2020, §11.1 and §11.2.1: EXEC at the export is the valid mask and the final one sent is used; an `exp null ... vm` carries only the mask). The color half is measured: an export sends data only for the lanes that are on, a later export to the same target replaces the earlier one for the whole wave, and a lane with no data from the last one is not written even when a later `exp null done vm` marks it valid. So `exp mrt0 done` (no `vm`) with every lane on, followed by an EXEC write, still writes every pixel, and EXEC off at that export and back on afterwards writes none. **Measured on RDNA2** (AMD Radeon iGPU, gfx1036, driver 26.10.1) with the pixel-shader oracle of #3061 (`ps_oracle.py`), hand-written ISA as the pixel shader of a real draw (wave64 stage): 18 programs that cut EXEC to half of the target before, between or after `exp mrt0` (with and without `vm` and `done`) and `exp null done vm`; all 18 fit the rule, 6 of them differed from the translator, which until 2026-11-23 discarded by EXEC at the end on a path without `vm` and kept the older export's value (or the output's initial value) for a lane that was off at the last color export. The latches (`%vm_mask`, `%col_mask_mrtN`) are only emitted where a path can write EXEC after a `vm` or color export; everywhere else EXEC at the end is the same mask and the SPIR-V is unchanged. With several color targets a pixel stays when any target got an export from its lane (Vulkan cannot skip one attachment per pixel; not measured, the oracle's pipeline has one target); `mrtz` is not part of the color mask (not measured). **Measuring exports needs `s_waitcnt expcnt(0)` after each one**: an export is asynchronous, and without the wait the hardware takes the data and the EXEC of a later moment (`v_mov v9, A; exp mrt0 v9 done vm; v_mov v9, B` shows B, and an EXEC write right after the export decides which pixels are written, which looks like "EXEC at the end decides"); 64 `s_nop 15` have the same effect as the wait. Checked on the CPU by `tests/semantic/test_export_valid_mask.py` and on the GPU by `runtime/test/shaders/ps_exp_color_mask.asm` (RTX 5080 and the Radeon). VALU scalar lane-mask destinations (`v_cmp_*`, `v_add_co_u32`, `v_add_co_ci_u32`, `v_sub_co_u32`, `v_sub_co_ci_u32`, `v_subrev_co_u32`, `v_subrev_co_ci_u32`, `v_mad_u64_u32`, `v_mad_i64_i32`) write 0 for lanes off in EXEC (`SDST[lane] = EXEC[lane] ? bit : 0`, AMD RDNA2 ISA). Until 2026-11-22 the six `*_co_*` ops and `v_mad_u64_u32`/`v_mad_i64_i32` did not AND the carry/sign bit with `active`, so inactive lanes leaked 1 bits into `VCC`/SGPR masks (`s_bcnt1`, `s_cbranch_vccz`, `v_cndmask_b32`). Checked on the CPU by `tests/semantic/test_carry_exec_mask.py` (wave32, wave64, and `--wave64-emulate` structure), and **hardware-verified on an AMD RX 7600 (gfx1102)** with `v_add_co_u32` and `v_add_co_ci_u32` carrying on every active lane: VCC reads `0xffffffff` with full EXEC, `0x0000ffff` for the mask `0x0000ffff` and `0xaaaaaaaa` for `0xaaaaaaaa` in wave32, `vcc_lo = 0x0000ffff` with `vcc_hi = 0x0000000f` for the same partial mask in wave64, and `0` when EXEC is 0 even with VCC preloaded to all ones; with one carrying lane under mask `0x0000ffff` VCC is `0x00000001`, and `v_sub_co_u32`/`v_subrev_co_u32` with a borrow under that mask give `0x0000ffff` too. The `*_co_ci_` forms, `v_mad_u64_u32` and `v_mad_i64_i32` share the same path but were not measured separately; it is also GPU-verified on native RDNA2 (gfx1036, AMD Radeon Graphics) with partial EXEC masks (inactive lanes write 0 in sdst for `v_add_co_u32` and `v_mad_u64_u32`). In a shader that writes EXEC, a run of two or more lane-local VALU instructions (no cross-lane read, no lane-mask, SCC, carry or EXEC write) sits inside one `if (active)` and writes its VGPRs directly, instead of an `OpSelect(active, new, old)` per write: the same result, but the driver predicates the run with its own EXEC instead of keeping both values live. On an RX 5700 XT (RADV) a pixel shader with 8 divergent if/else regions of `v_fma`/`v_mul`/`v_add`/`v_min`/`v_max` went from 0.85 ms to 0.69 ms (the same program as native SPIR-V: 0.65 ms). Not under `--wave64-emulate`. Checked by `tests/semantic/test_active_runs.py`. | | `s_*_saveexec_b32/b64`, `s_andn1/andn2_wrexec_b32/b64` | RDNA2 ISA SOP1. saveexec: `D = EXEC; EXEC = f(S0, EXEC); SCC = (EXEC != 0)`. wrexec: `EXEC = f(S0, EXEC); D = EXEC`. `f` is one of `and`, `or`, `xor`, `andn1` (`~S0 & EXEC`), `andn2` (`S0 & ~EXEC`), `orn1` (`~S0 \| EXEC`), `orn2` (`S0 \| ~EXEC`), `nand`, `nor`, `xnor`, the table in `Gen.EXEC_LOGIC`. The definitions match `tools/cf_ref.py` `_SAVEEXEC`, the FUZZ-8 model. Each invocation holds its own lane's bit (0 or ~0), so `f` is the bitwise op on that bit. A lane EXEC had off becomes active when `f` gives 1 for it (`nand`, `nor`, `orn1`, `orn2`, `xnor`, ...), as on hardware, and SCC is the wave vote. Until 2026-11-22 only `and`/`or`/`andn2_saveexec` and `andn2_wrexec` were translated. CPU-checked by `tests/semantic/test_saveexec_variants.py` (all ten saveexec kinds in wave32 and wave64, both wrexec kinds, partial EXEC and S0, an empty result, every lane against `cf_ref._SAVEEXEC`) and by `tools/cf_diff.py` (300 programs × wave32/64 still pass). Hardware-verified on native RDNA2 (`gfx1036`, Windows 11, driver `32.0.21045.29003`, wave32 & wave64, 0 mismatches): all 10 `saveexec` kinds and both `wrexec` kinds (`andn1`/`andn2`) match bit-for-bit on non-symmetric masks (`src0 = 0x0F0F0F0F`, `exec = 0x33333333` in wave32; `0x0F0F0F0F_00FF00FF` / `0x33333333_55555555` in wave64), zero-result `SCC = 0`, wave64 upper-32-bit-only `new_exec = 0x00000001_00000000` setting `SCC = 1`, and `sdst == src0` register aliasing (`s4, s4` and `s[4:5], s[4:5]`) reading original `src0` before writing old `EXEC` to `sdst`. **Not GPU-verified** for the seven new kinds. Measured on native gfx1100 (single 8-lane workgroup, 36 programs): all ten `saveexec` kinds and both `wrexec` forms follow `f` for three `S0`/`EXEC` pairs, including lanes `EXEC` had off being switched back on (`nand` with `S0=0x03`, `EXEC=0x0F` leaves `0xFFFFFFFC`, activating lanes 4-7), `D` holding the old `EXEC` for `saveexec` and the new one for `wrexec`, and `SCC` the `EXEC != 0` vote. | diff --git a/tests/semantic/test_dpp_f16_mods.py b/tests/semantic/test_dpp_f16_mods.py new file mode 100644 index 0000000000..3453e2f65a --- /dev/null +++ b/tests/semantic/test_dpp_f16_mods.py @@ -0,0 +1,188 @@ +"""Semantic tests for DPP src0 neg/abs modifiers on f16 ops and sNaN-preserving f32 ops. + +Verifies that Gen.dpp_insn forwards `neg`/`abs` on the post-select SSA operand instead of +eagerly applying 32-bit float `Gen.modify()` (bit 31 FAbs/OpFNegate): + 1. f16 DPP ops (`v_add_f16_dpp`, `v_mul_f16_dpp`, `v_min_f16_dpp`, `v_cvt_f32_f16_dpp`, + `v_cmp_gt_f16_dpp`) apply `neg`/`abs` to the f16 sign bit (bit 15) after invalid-lane + zero selection, preserving the destination high half and yielding `-0.0h` for `-v1` + on invalid `bound_ctrl:1` lanes. + 2. Sign-bit-move / sNaN-preserving f32 DPP ops (`v_cndmask_b32_dpp`, `v_cmp_class_f32_dpp`, + `v_min_f32_dpp`) use raw integer bit-31 sign moves (`src_u_sign_mods` / `src_f_sign_mods` / + `cmp_class`) rather than `FAbs`/`OpFNegate`. + 3. Wave64 emulation (`--wave64-emulate`) produces the same f16 DPP modifier results. + + python -I tests/semantic/test_dpp_f16_mods.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 rdna2spirv # noqa: E402 +import spirv_mini_interp as si # noqa: E402 + +STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" int: + return struct.unpack(" int: + return struct.unpack(" 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 DppF16AndSignModsTests(unittest.TestCase): + def test_v_add_f16_dpp_neg_and_abs_wave32_and_emulate(self): + # Put f16(2.0) = 0x4000 in lane 0's low half and f16(-3.0) = 0xC200 in lane 1's low half + # (with 0xABCD in the high half to verify high-half preservation on dst). + # With quad_perm:[1,0,3,2]: + # lane 0 reads lane 1 (-3.0h): -(-3.0h) + 1.0h = +4.0h (0x4400); |-3.0h| * 2.0h = +6.0h (0x4600) + # lane 1 reads lane 0 (+2.0h): -(+2.0h) + 1.0h = -1.0h (0xBC00); |+2.0h| * 2.0h = +4.0h (0x4400) + lines = [ + "v_and_b32_e32 v4, 1, v0", + "v_cmp_eq_u32_e32 vcc_lo, 0, v4", + "v_cndmask_b32_e32 v1, 0x0000c200, 0x00004000, vcc_lo", + "v_mov_b32_e32 v2, 0xabcd0000", + "v_mov_b32_e32 v3, 0x00003c00", # +1.0h + "v_add_f16_dpp v2, -v1, v3 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_mov_b32_e32 v5, 0x12340000", + "v_mov_b32_e32 v7, 0x00004000", # +2.0h + "v_mul_f16_dpp v5, |v1|, v7 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v2, v6, s[0:3], 0 offen", + "buffer_store_dword v5, v6, s[0:3], 0 offen offset:256", + ] + # Wave32 + spv = translate_cs("w32_dpp_f16_add_mul", lines, wave64=False) + mem = run_cs(spv, wave64=False) + for lane in range(32): + exp_add = (0xABCD << 16) | (f16_bits(4.0) if (lane & 1) == 0 else f16_bits(-1.0)) + exp_mul = (0x1234 << 16) | (f16_bits(6.0) if (lane & 1) == 0 else f16_bits(4.0)) + self.assertEqual(mem[4 * lane], exp_add, f"add lane {lane}") + self.assertEqual(mem[256 + 4 * lane], exp_mul, f"mul lane {lane}") + + # Wave64 emulated (replace vcc_lo with vcc) + lines_w64 = [ln.replace("vcc_lo", "vcc") for ln in lines] + spv_emu = translate_cs("w64_dpp_f16_add_mul", lines_w64, wave64=True, emulate=True) + mem_emu = run_cs(spv_emu, wave64=True, emulate=True) + for lane in range(64): + exp_add = (0xABCD << 16) | (f16_bits(4.0) if (lane & 1) == 0 else f16_bits(-1.0)) + exp_mul = (0x1234 << 16) | (f16_bits(6.0) if (lane & 1) == 0 else f16_bits(4.0)) + self.assertEqual(mem_emu[4 * lane], exp_add, f"emu add lane {lane}") + self.assertEqual(mem_emu[256 + 4 * lane], exp_mul, f"emu mul lane {lane}") + + def test_v_cvt_f32_f16_dpp_neg_and_bound_ctrl_zero(self): + # row_shr:1 with bound_ctrl:1: lane 0 and lane 16 read invalid source -> 0x0000 (+0.0h), + # then `-` negates it to -0.0h -> widened to -0.0f (0x80000000). + # Other lanes read previous lane's +2.0h (0x4000) -> negated to -2.0f. + spv = translate_cs("w32_dpp_cvt_f32_f16", [ + "v_mov_b32_e32 v1, 0x00004000", # +2.0h + "v_cvt_f32_f16_dpp v2, -v1 row_shr:1 row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v2, v6, s[0:3], 0 offen", + ], wave64=False) + mem = run_cs(spv, wave64=False) + for lane in range(32): + expected = 0x80000000 if (lane % 16) == 0 else f32_bits(-2.0) + self.assertEqual(mem[4 * lane], expected, f"lane {lane}") + + def test_v_min_f16_and_v_cmp_gt_f16_dpp_neg(self): + # All lanes have v1 = +3.0h (0x4200), v3 = +1.0h (0x3c00). + # With -v1: + # v_min_f16_dpp(-3.0h, +1.0h) = -3.0h (0xC200) (on origin/main: min(+3.0h, +1.0h) = +1.0h) + # v_cmp_gt_f16_dpp(-3.0h, +1.0h) = false (0) (on origin/main: +3.0h > +1.0h = true) + spv = translate_cs("w32_dpp_f16_min_cmp", [ + "v_mov_b32_e32 v1, 0x00004200", # +3.0h + "v_mov_b32_e32 v3, 0x00003c00", # +1.0h + "v_mov_b32_e32 v2, 0", + "v_min_f16_dpp v2, -v1, v3 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_cmp_gt_f16_dpp vcc_lo, -v1, v3 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_cndmask_b32_e32 v4, 0, 1, vcc_lo", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v2, v6, s[0:3], 0 offen", + "buffer_store_dword v4, 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)], [f16_bits(-3.0)] * 32) + self.assertEqual([mem[128 + 4 * i] for i in range(32)], [0] * 32) + + def test_v_cndmask_b32_and_cmp_class_f32_dpp_preserve_sign_bit_moves(self): + # v1 = 0x7f800001 (positive sNaN). + # v_cndmask_b32_dpp with -v1 (and vcc = 0 so src0 is chosen) must flip bit 31 via + # OpBitwiseXor (no FAbs/OpFNegate that could quiet the sNaN) -> 0xff800001. + # v_cmp_class_f32_dpp with -v1 and class mask 0x001 (sNaN) must also keep raw bit ops + # (no FAbs/OpFNegate) and report True. + spv = translate_cs("w32_dpp_snan_sign_moves", [ + "s_mov_b32 vcc_lo, 0", + "v_mov_b32_e32 v1, 0x7f800001", + "v_mov_b32_e32 v3, 0", + "v_cndmask_b32_dpp v2, -v1, v3, vcc_lo quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_cmp_class_f32_dpp vcc_lo, -v1, 1 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "v_cndmask_b32_e32 v4, 0, 1, vcc_lo", + "v_lshlrev_b32_e32 v6, 2, v0", + "buffer_store_dword v2, v6, s[0:3], 0 offen", + "buffer_store_dword v4, v6, s[0:3], 0 offen offset:128", + ], wave64=False) + self.assertNotIn("OpFNegate", spv) + self.assertNotIn("FAbs", spv) + mem = run_cs(spv, wave64=False) + self.assertEqual([mem[4 * i] for i in range(32)], [0xFF800001] * 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..b046d2468e 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -7824,13 +7824,16 @@ def selectors(key, count, limit): zero = self.cu(0) src0 = self.tmp() self.emit(f"{src0} = OpSelect %uint {src_ok} {shuffled} {zero}") - src0 = self.modify(src0, src) if dpp8 is not None or bound: write = enabled else: write = self.tmp() self.emit(f"{write} = OpLogicalAnd %bool {enabled} {src_ok}") args[1] = {"k": "ssa", "v": src0} + if src.get("neg"): + args[1]["neg"] = True + if src.get("abs"): + args[1]["abs"] = True if compare: # Every lane is enabled and an invalid source reads 0 (checked above), so the compare's own EXEC guard # is the only one: the mask write is the plain compare's.