diff --git a/docs/design.md b/docs/design.md index 0a77c2404f..6c9bf8a486 100644 --- a/docs/design.md +++ b/docs/design.md @@ -303,7 +303,7 @@ How the generated SPIR-V expects the runtime to work: | `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 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`. | +| 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 is refused (`DPP compare with lanes DPP leaves unwritten`), because what such a lane writes is measured only on RDNA3, not on RDNA2. On RDNA3 (gfx1101, RX 7800 XT, `tools/hw_oracle`, wave32 and wave64; [measurement](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161289307)) a lane DPP leaves unwritten does not keep its bit: `v_cmp` writes 0 to its VCC bit and `v_cmpx` turns it off in EXEC. That holds for an invalid source with `bound_ctrl` 0, and for a lane `row_mask` or `bank_mask` turns off, even with `bound_ctrl` 1. Translating these forms on RDNA2 (gfx1013) would need the same measurement there. `v_cmpx` in DPP form takes the same lane selection and the same forms, and writes EXEC as the plain `v_cmpx` does (a lane that was off stays off); it prints no destination, so its src0 is the first operand (SHADER-VOPCX-DPP). 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. `v_cmpx` in DPP form is in 78 of the 18,837 graphics programs of the pipeline library (all pixel shaders, 113 instructions), all `v_cmpx_gt_f32_dpp vN, -vM quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1`, a compare against the lane two above or below in its quad. **Not measured on RDNA2 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. The RDNA3 measurement above also ran the translated forms (`bound_ctrl` 1, masks 0xF): an invalid source lane gets the compare of a 0 source, as the rule says. 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` and, for `v_cmpx`, `cs_dpp_cmpx.asm` (RTX 4070 Laptop, native wave32 and `--wave64-emulate`). | | 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. | | Control flow | Reducible graphs use structured branches and natural loops. Irreducible graphs use the dispatch-loop form, which can also be forced with `--dispatch-loop` for comparisons. The control-flow unit test checks 18,228 generated graphs. Irreducible graphs use node splitting (`cfg_structure.split_irreducible`, up to `min(2*N, N+64)` blocks) and fall back to the dispatch-loop form when capped; it can also be forced with `--dispatch-loop` for comparisons. `cfg_structure.structure`, `cfg_structure.run`, and `rdna2spirv.assemble`'s structured emitter are iterative and hoist each guarded member's dominated tail continuation out of its enclosing guard (exact by strict dominance: no block outside the guard's subtree can target the continuation), keeping `OpSelectionMerge` nesting O(1) on sequential diamond/loop chains instead of nesting O(N) deep and hitting Python's recursion limit on multi-thousand-block shaders (e.g. 2,000 sequential diamonds nest to depth 2 instead of 2,000). The control-flow unit test (`tests/test_cfg_structure.py`) checks 20,000 generated graphs plus 2,000-diamond, 1,200-loop, and 1,500-early-exit chains. `capped_tree()` also runs `cfg_structure.unwrap_known_pc_guards` (shared with `wave64_emulate_opt`) to unwrap tautological `("guard", g, ...)` nodes whenever abstract interpretation proves `%pc == g` on all incoming paths (e.g. post-dominating merge blocks after diamonds and loops); this keeps `ms_wave_info_s3`'s `s_barrier` in uniform top-level control flow (fixing an AMD Windows LLPC `0xC0000005` pipeline-creation crash on AMD Ryzen 5 7600 RDNA2 iGPU driver `24.10.24`, `4096/4096` exact on AMD RDNA2 and RTX 4070 SUPER) and avoids false `needs_barrier_rounds` triggers (`SHADER-STRUCTURED-PC-UNWRAP`). AMD A/B checks compare the two forms for selected pixel shaders with defined test inputs. For shaders that never export colour, the comparison checks pixel coverage. **Nesting is bounded (SHADER-299):** a tree deeper than `MAX_STRUCTURED_DEPTH` (16) is emitted in the dispatch-loop form instead, since `spirv-val` time grows roughly with the cube of the measured nesting depth. **Nesting is bounded (SHADER-299, SHADER-427):** a tree deeper than `MAX_STRUCTURED_DEPTH` (64; `--max-structured-depth N`, 0 = no bound) is emitted in the dispatch-loop form instead, since `spirv-val` time grows roughly with the cube of the measured nesting depth. Before #1904, over the 1.0.2.1 corpus (26,503 modules, `spirv-val` 2026.4): the deepest block of a module is nested 118 constructs deep, 12,963 modules are structured (median 15) and 6,000 of those are deeper than 16; an estimated 85% of the corpus's `spirv-val` time is in modules nested 20 or deeper. Median `spirv-val` time for the same module in each form: depth 20 181 -> 129 ms (1.4x), 30 507 -> 371 (1.4x), 50 1,712 -> 738 (2.3x), 64 2,692 -> 1,163 (2.3x), 77 5,122 -> 1,130 (4.5x), and the deepest (118) 13,683 -> 1,352 ms (10.1x). Depth 5-12 shows no gain (at depth 6 the flat form is slightly slower), which is why the bound first sat at 16. Since #1904 (`cfg_structure` hoists tails) the deepest tree in the corpus is 29: of 12,944 structured modules, 1,031 are deeper than 16 (747 CS, 284 PS), 57 deeper than 20, 24 deeper than 24. For those 1,031 the flat form saves only 12% of `spirv-val` time (1,235 -> 1,091 s summed, slowest module 8.3 -> 5.9 s), while on an RTX 5070 Ti (NVIDIA 621.24) it compiles slower: the 284 pixel shaders 1.67x (827 -> 1,382 s summed, two rounds), four compute modules at depth 19-24 2.0x (297 -> 591 s, 1.6-2.7x each), so the bound now sits above the corpus (SHADER-427). Both forms pass `spirv-val` on a 200-shader slice (200/200 each), and `gen.nesting` records the tree depth. AMD's Windows driver adds a reason to keep the structured form (SHADER-995): on a Radeon gfx1036 iGPU (32.0.21042.62) the dispatch-loop module of `XboxBvhBuilder.dll`'s `LeafRehydration` (tree depth 18, a 581-case switch) takes 34 minutes to create (XXDESIGN), where its structured module is created in 2.9 s; ten other programs of that DLL compile in the dispatch-loop form there in 0.4-40 s, 2.5-5x their structured time ([embedded_shaders.md](embedded_shaders.md), section 7). | diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 39a3179a46..987a4eb530 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py @@ -128,7 +128,9 @@ def not_found_message(build, missing): ("cs_dpp_bank", [0xffffffff, 0xfffffffe, 0xfffffffd, 0xfffffffc, 7, 7, 7, 7]), ("cs_dpp_fi", [1, 2, 3, 0x7777, 5, 6, 7, 0x7777]), # a VOPC compare with a DPP src0: 1 for the pair compare, 2 for the dpp8 one (cs_dpp_cmp.asm header) - ("cs_dpp_cmp", [3, 2, 3, 2, 1, 0, 1, 0])] + ("cs_dpp_cmp", [3, 2, 3, 2, 1, 0, 1, 0]), + # v_cmpx with a DPP src0: 1 where the plain compare keeps the lane, 2 for the negated one (cs_dpp_cmpx.asm) + ("cs_dpp_cmpx", [1, 1, 0, 0, 3, 3, 2, 2])] WAVE32_DUMPS += tuple(n for n, _ in CS_DPP) WAVE32_LANE_DUMPS += tuple(n for n, _ in CS_DPP) CS_DUMPS = [*CS_DPP, ("cs_smem_vcc_base", CS_SMEM_VCC), ("cs_store", CS_STORE), ("cs_smem_soffset_imm", CS_SMEM), diff --git a/runtime/test/shaders/cs_dpp_cmpx.asm b/runtime/test/shaders/cs_dpp_cmpx.asm new file mode 100644 index 0000000000..85f18c93fa --- /dev/null +++ b/runtime/test/shaders/cs_dpp_cmpx.asm @@ -0,0 +1,24 @@ +; v_cmpx in DPP form (SHADER-VOPCX-DPP): src0 is read from the permuted lane, src1 from the lane itself, and the result +; goes to EXEC. v1 = float(lane - 4). Both compares take src0 from the other half of each quad (quad_perm [2,3,0,1], +; with bound_ctrl:1 and fi:1, the form the game's pixel shaders use). The first keeps a lane where +; "neighbour > self": lanes 0, 1, 4, 5 of each 8. The second negates src1 ("neighbour > -self"): lanes 4-7 of the first +; 8 and every lane after. out = (first keeps the lane ? 1 : 0) + (second keeps it ? 2 : 0), EXEC restored in between. +; Run: vk_compute_test cs_dpp_cmpx.spv 1 dump -> out[0..7] = 1 1 0 0 3 3 2 2 +; llvm-mc assembles neither compare for gfx1013 ("dpp variant of this instruction is not supported"), so the GPU +; test takes this file's text as its listing; tools/shader_disasm.py prints the same lines for the encoded words. +; run_gpu_tests: text only, llvm-mc has no gfx1013 encoding for an instruction here +; regen_fixtures: this source has a translator fixture in tests/semantic/fixtures/disasm +v_mbcnt_lo_u32_b32 v0, -1, 0 +v_add_nc_u32 v7, -4, v0 +v_cvt_f32_i32 v1, v7 +v_mov_b32 v6, 0 +s_mov_b32 s10, exec_lo +v_cmpx_gt_f32_dpp v1, v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1 +v_mov_b32 v6, 1 +s_mov_b32 exec_lo, s10 +v_cmpx_gt_f32_dpp v1, -v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1 +v_add_nc_u32 v6, 2, v6 +s_mov_b32 exec_lo, s10 +v_lshlrev_b32 v2, 2, v0 +buffer_store_dword v6, v2, s[0:3], 0 offen +s_endpgm diff --git a/scripts/run_gpu_tests.py b/scripts/run_gpu_tests.py index f87290ec65..60e9687830 100644 --- a/scripts/run_gpu_tests.py +++ b/scripts/run_gpu_tests.py @@ -1205,7 +1205,7 @@ def run_suite(a, work: str) -> int: results.append((name,) + wavemask_check(vk_compute, spv, cs_env, w64_emulated())) elif name in ("cs_smem_vcc_base", "cs_store", "cs_signed_zero_nan", "cs_smem_soffset_imm", "cs_w32_lane_wrap", "cs_movrelsd_2", "cs_w32_permute", "cs_w32_sincos", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", "cs_exec_const", "cs_w32_mask_imm", "cs_w64_mask_imm", "cs_w32_lds_u8_d16", "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_wave64_ballot", "cs_wm_half_ops", "cs_wm_half_cmp", "cs_wm_bfm64", "cs_wm_scc_rfl", "cs_wqm_w64", "cs_wqm_w32", "cs_wqm_w64_half", "cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8", "cs_dpp_ops", "cs_dpp_bank", "cs_dpp_fi", - "cs_dpp_bound", "cs_dpp_cmp", "cs_dpp_w64", + "cs_dpp_bound", "cs_dpp_cmp", "cs_dpp_cmpx", "cs_dpp_w64", "cs_w64_mask_detect", "cs_w64x_readlane", "cs_w64x_writelane", "cs_w64x_readfirstlane", "cs_w64x_bpermute", "cs_w64x_permute", "cs_w64x_branch", "cs_w64x_ballot", "cs_w64x_wg128_branches", "cs_w64x_lds_raw", @@ -1274,6 +1274,7 @@ def run_suite(a, work: str) -> int: "cs_dpp_bank": [0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFC, 7, 7, 7, 7], "cs_dpp_fi": [1, 2, 3, 0x7777, 5, 6, 7, 0x7777], "cs_dpp_cmp": [3, 2, 3, 2, 1, 0, 1, 0], + "cs_dpp_cmpx": [1, 1, 0, 0, 3, 3, 2, 2], "cs_dpp_w64": [1, 0, 3, 2, 5, 4, 7, 6], # SHADER-99: -0 and NaN survive (SignedZeroInfNanPreserve, cs_signed_zero_nan.asm); 0 in out[4..7] # without it @@ -1352,7 +1353,7 @@ def run_suite(a, work: str) -> int: "cs_bpermute32", "cs_w32_partial_scmp", "cs_w32_partial_permute", "cs_w32_permlane_fi", "cs_w32_sincos", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_lds_b64_cmpst", "cs_lds_xchg64_ext", "cs_lds_atomics_ext", "cs_w32_wrxchg2_ext", "cs_w32_wrxchg2st64", "cs_exec_const", "cs_w32_mask_imm", "cs_mask_vgpr_exec", "cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8", - "cs_dpp_ops", "cs_dpp_bound", "cs_dpp_bank", "cs_dpp_fi", "cs_dpp_cmp", "cs_w32_andn2_exec", + "cs_dpp_ops", "cs_dpp_bound", "cs_dpp_bank", "cs_dpp_fi", "cs_dpp_cmp", "cs_dpp_cmpx", "cs_w32_andn2_exec", "cs_w32_exec_computed") and not emulated and not partial if partial: cs_env = dict(os.environ, VKC_SUBGROUP_SIZE="32" if emulated else "64") diff --git a/tests/semantic/fixtures/disasm/cs_dpp_cmpx.s b/tests/semantic/fixtures/disasm/cs_dpp_cmpx.s new file mode 100644 index 0000000000..90172ddfa1 --- /dev/null +++ b/tests/semantic/fixtures/disasm/cs_dpp_cmpx.s @@ -0,0 +1,15 @@ +; cs_dpp_cmpx test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/cs_dpp_cmpx.asm + v_mbcnt_lo_u32_b32 v0, -1, 0 + v_add_nc_u32 v7, -4, v0 + v_cvt_f32_i32 v1, v7 + v_mov_b32 v6, 0 + s_mov_b32 s10, exec_lo + v_cmpx_gt_f32_dpp v1, v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1 + v_mov_b32 v6, 1 + s_mov_b32 exec_lo, s10 + v_cmpx_gt_f32_dpp v1, -v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1 + v_add_nc_u32 v6, 2, v6 + s_mov_b32 exec_lo, s10 + v_lshlrev_b32 v2, 2, v0 + buffer_store_dword v6, v2, s[0:3], 0 offen + s_endpgm diff --git a/tests/semantic/test_dpp_compare.py b/tests/semantic/test_dpp_compare.py index c6f429fb5e..46cdc0b3e4 100644 --- a/tests/semantic/test_dpp_compare.py +++ b/tests/semantic/test_dpp_compare.py @@ -9,6 +9,9 @@ writes are translated: all rows and banks enabled and either `bound_ctrl:1` or DPP8. A form that leaves a lane unwritten is refused, because what that lane's mask bit becomes is not measured. +`v_cmpx` in DPP form (SHADER-VOPCX-DPP) takes the same forms and writes EXEC instead of VCC. 78 pipeline-library +pixel shaders of 1.0.2.1 hold it, all as `v_cmpx_gt_f32_dpp vN, -vM quad_perm:[2,3,0,1] ... bound_ctrl:1 fi:1`. + The values come from running the translated module in tests/semantic/spirv_wave_interp.py. Standard library only; no GPU, no LLVM, no SPIRV-Tools, no game files. @@ -66,34 +69,40 @@ def compare_vs(op, form, exec_off=None): def compare_reference(op, form, exec_off=None, wave=32): """The VCC bit per lane after compare_vs: an inactive lane's bit is 0, as for a plain compare.""" - mods = form.split() - fi = any(m == "fi:1" for m in mods) relation = COMPARES[op][1] active = [lane != exec_off for lane in range(wave)] out = [] for lane in range(wave): - loc, ok = lane & 15, True - if mods[0].startswith("dpp8:"): - src = (lane & ~7) | [int(x) for x in mods[0][6:-1].split(",")][lane & 7] - elif mods[0].startswith("quad_perm:"): - src = (lane & ~3) | [int(x) for x in mods[0][11:-1].split(",")][lane & 3] - else: - kind, _, n = mods[0].partition(":") - n = int(n or 0) - if kind == "row_shr": - ok, s = loc >= n, loc - n - elif kind == "row_shl": - ok, s = loc + n < 16, loc + n - elif kind == "row_ror": - s = (loc - n) & 15 - else: # row_mirror - s = 15 - loc - src = (lane & ~15) | (s & 15) - valid = ok and (active[src] or fi) - out.append(int(active[lane] and relation(src if valid else 0, lane))) + src = source_lane(form, lane, active) + out.append(int(active[lane] and relation(src if src is not None else 0, lane))) return out +def source_lane(form, lane, active): + """The lane DPP `form` reads src0 from for `lane`, or None where src0 reads 0 (out of the row, or inactive + without fi:1).""" + mods = form.split() + fi = any(m == "fi:1" for m in mods) + loc, ok = lane & 15, True + if mods[0].startswith("dpp8:"): + src = (lane & ~7) | [int(x) for x in mods[0][6:-1].split(",")][lane & 7] + elif mods[0].startswith("quad_perm:"): + src = (lane & ~3) | [int(x) for x in mods[0][11:-1].split(",")][lane & 3] + else: + kind, _, n = mods[0].partition(":") + n = int(n or 0) + if kind == "row_shr": + ok, s = loc >= n, loc - n + elif kind == "row_shl": + ok, s = loc + n < 16, loc + n + elif kind == "row_ror": + s = (loc - n) & 15 + else: # row_mirror + s = 15 - loc + src = (lane & ~15) | (s & 15) + return src if ok and (active[src] or fi) else None + + def run(op, form, exec_off=None): res = wi.run_wave(compare_vs(op, form, exec_off), wave=32, builtins={"VertexIndex": lambda lane: lane, "InstanceIndex": lambda lane: 0}) @@ -124,30 +133,105 @@ def test_reference_by_hand(self): def test_a_form_that_leaves_a_lane_unwritten_is_refused(self): for form in UNWRITTEN_FORMS: with self.subTest(form=form): - with self.assertRaisesRegex(Unsupported, "DPP compare with lanes that keep their mask bit"): + with self.assertRaisesRegex(Unsupported, "DPP compare with lanes DPP leaves unwritten"): compare_vs("v_cmp_gt_f32_dpp", form) - def test_cmpx_is_refused(self): - lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", - "v_cmpx_gt_u32_dpp v0, v0 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", + +# v_cmpx in DPP form (SHADER-VOPCX-DPP): the same lane selection, with the result written to EXEC. src0 is the first +# operand, since v_cmpx prints no destination. f32 compares read v1 = float(lane - 4), so a negated src1 changes the +# result; integer compares read v0 = lane. +CMPX = { + "v_cmpx_gt_f32_dpp": ("v1", lambda a, b: a > b), + "v_cmpx_lt_u32_dpp": ("v0", lambda a, b: a < b), + "v_cmpx_eq_u32_dpp": ("v0", lambda a, b: a == b), +} +# The form all 113 DPP v_cmpx of the 78 refused pipeline-library pixel shaders of 1.0.2.1 use, with src1 negated. +GAME_FORM = "quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1" + + +def cmpx_vs(op, form, exec_off=None, neg=False): + """A vertex shader: EXEC = `op`(DPP(src), [-]src) over the lanes on (all but `exec_off`); param0.x exports 1 + where the new EXEC keeps the lane and 0 where it drops it.""" + src = CMPX[op][0] + lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", "v_add_nc_u32_e32 v7, -4, v0", "v_cvt_f32_i32_e32 v1, v7", + "v_mov_b32 v4, 1", "v_mov_b32 v6, 0", "s_mov_b32 s10, exec_lo"] + if exec_off is not None: + lines.append(f"v_cmpx_ne_u32_e32 {exec_off}, v0") + lines += [f"{op} {src}, {'-' if neg else ''}{src} {form}", "v_mov_b32 v6, v4", "s_mov_b32 exec_lo, s10", + "v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done", "exp param0 v6, v5, v5, v5", "s_endpgm"] + return support.translate_lines("vs_dpp_cmpx", lines) + + +def cmpx_reference(op, form, exec_off=None, neg=False, wave=32): + """The EXEC bit per lane after the v_cmpx of cmpx_vs: 0 for a lane that was off, as for a plain v_cmpx.""" + src, relation = CMPX[op] + value = (lambda lane: float(lane - 4)) if src == "v1" else (lambda lane: lane) + active = [lane != exec_off for lane in range(wave)] + out = [] + for lane in range(wave): + s = source_lane(form, lane, active) + a = value(s) if s is not None else 0 + b = -value(lane) if neg else value(lane) + out.append(int(active[lane] and relation(a, b))) + return out + + +def run_cmpx(op, form, exec_off=None, neg=False): + res = wi.run_wave(cmpx_vs(op, form, exec_off, neg), wave=32, + builtins={"VertexIndex": lambda lane: lane, "InstanceIndex": lambda lane: 0}) + return [o["%out_param0"][0] for o in res["outputs"]] + + +class DppCompareExec(unittest.TestCase): + def test_values(self): + for op in CMPX: + for form in WRITTEN_FORMS + [GAME_FORM]: + for exec_off in (None, 5, 16): + with self.subTest(op=op, form=form, exec_off=exec_off): + self.assertEqual(run_cmpx(op, form, exec_off), cmpx_reference(op, form, exec_off)) + + def test_game_form_with_negated_src1(self): + for exec_off in (None, 1, 6): + with self.subTest(exec_off=exec_off): + want = cmpx_reference("v_cmpx_gt_f32_dpp", GAME_FORM, exec_off, neg=True) + self.assertEqual(run_cmpx("v_cmpx_gt_f32_dpp", GAME_FORM, exec_off, neg=True), want) + self.assertNotEqual(want, cmpx_reference("v_cmpx_gt_f32_dpp", GAME_FORM, exec_off)) + + def test_reference_by_hand(self): + # quad_perm:[2,3,0,1] swaps the halves of each quad, and lanes 0-7 hold -4..3: lane 0 compares lane 2's -2 + # with its own -4, so -2 > -4 keeps it and, src1 negated, -2 > 4 drops it. + self.assertEqual(cmpx_reference("v_cmpx_gt_f32_dpp", GAME_FORM)[:8], [1, 1, 0, 0, 1, 1, 0, 0]) + self.assertEqual(cmpx_reference("v_cmpx_gt_f32_dpp", GAME_FORM, neg=True)[:8], [0, 0, 0, 0, 1, 1, 1, 1]) + # A lane that was off stays off, whatever its compare gives. + self.assertEqual(cmpx_reference("v_cmpx_gt_f32_dpp", GAME_FORM, exec_off=5)[:8], [1, 1, 0, 0, 1, 0, 0, 0]) + + def test_a_form_that_leaves_a_lane_unwritten_is_refused(self): + for form in UNWRITTEN_FORMS: + with self.subTest(form=form): + with self.assertRaisesRegex(Unsupported, "DPP compare with lanes DPP leaves unwritten"): + cmpx_vs("v_cmpx_gt_f32_dpp", form) + + def test_a_destination_operand_is_refused(self): + # v_cmpx prints no destination: a line with one is not a form the disassembler writes. + lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", f"v_cmpx_gt_u32_dpp vcc_lo, v0, v0 {WRITTEN_FORMS[0]}", "v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done", "s_endpgm"] - with self.assertRaises(Unsupported): - support.translate_lines("vs_dpp_cmpx", lines) + with self.assertRaisesRegex(Unsupported, "DPP compare operands"): + support.translate_lines("vs_dpp_cmpx_dst", lines) def vopc(opcode, src0, vsrc1): return 0x3E << 25 | opcode << 17 | vsrc1 << 9 | src0 -def dpp16_word(src0_reg, ctrl, bound=0, fi=0, bank_mask=0xF, row_mask=0xF): - return row_mask << 28 | bank_mask << 24 | bound << 19 | fi << 18 | ctrl << 8 | src0_reg +def dpp16_word(src0_reg, ctrl, bound=0, fi=0, bank_mask=0xF, row_mask=0xF, src1_neg=0): + return row_mask << 28 | bank_mask << 24 | src1_neg << 22 | bound << 19 | fi << 18 | ctrl << 8 | src0_reg def dpp8_word(src0_reg, lanes): return sum(lane << (8 + 3 * i) for i, lane in enumerate(lanes)) | src0_reg -GT_F32, LT_U32 = 0x04, 0xC1 # VOPC opcodes (tools/rdna2_tables.py) +GT_F32, LT_U32, CMPX_GT_F32 = 0x04, 0xC1, 0x14 # VOPC opcodes (tools/rdna2_tables.py) S_ENDPGM = 0xBF810000 # (first word, second word, the line both printers give) PAIRS = [ @@ -161,6 +245,9 @@ def dpp8_word(src0_reg, lanes): "v_cmp_gt_f32_dpp vcc_lo, v20, v20 dpp8:[4,5,6,7,0,1,2,3]"), (vopc(GT_F32, 234, 6), dpp8_word(5, [7, 6, 5, 4, 3, 2, 1, 0]), "v_cmp_gt_f32_dpp vcc_lo, v5, v6 dpp8:[7,6,5,4,3,2,1,0] fi:1"), + # The game's v_cmpx form: no destination, src1 negated (bytes fa 02 28 7c 01 4e 4c ff). + (vopc(CMPX_GT_F32, 250, 1), dpp16_word(1, 0x4E, bound=1, fi=1, src1_neg=1), + "v_cmpx_gt_f32_dpp v1, -v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1"), ] @@ -175,6 +262,7 @@ def test_opcodes_are_the_tables(self): import rdna2_decode self.assertEqual(rdna2_decode.T.VOPC[GT_F32][0], "v_cmp_gt_f32") self.assertEqual(rdna2_decode.T.VOPC[LT_U32][0], "v_cmp_lt_u32") + self.assertEqual(rdna2_decode.T.VOPC[CMPX_GT_F32][0], "v_cmpx_gt_f32") def test_python_decoder(self): import rdna2_decode @@ -186,8 +274,8 @@ def test_python_decoder(self): self.assertEqual((lines, bad), ([want, "s_endpgm"], 0)) def test_a_decoded_line_translates(self): - # What the disassembler prints is what the translator takes: the two forms it accepts, from PAIRS. - for _, _, line in (PAIRS[0], PAIRS[3]): + # What the disassembler prints is what the translator takes: the forms it accepts, from PAIRS. + for _, _, line in (PAIRS[0], PAIRS[3], PAIRS[5]): with self.subTest(line=line): lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", line, "v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done", "s_endpgm"] diff --git a/tests/semantic/test_library_compute_forms.py b/tests/semantic/test_library_compute_forms.py index 0e1c84bf40..a650b240cc 100644 --- a/tests/semantic/test_library_compute_forms.py +++ b/tests/semantic/test_library_compute_forms.py @@ -100,6 +100,16 @@ def test_vopc_dpp_encoded_fields_and_recovery(self): self.assertEqual((recovered["pc"], recovered["enc"]), (0, blob.hex())) self.assertFalse(rdna2_decode.decode_bytes(blob[:4])["insns"]) + def test_vopcx_dpp_recovery(self): + # SHADER-VOPCX-DPP: the same pass recovers v_cmpx in DPP form (opcode 0x14, v_cmpx_gt_f32), which prints no + # destination: the game's form, quad_perm:[2,3,0,1] bound_ctrl:1 fi:1 with src1 negated (fa 02 28 7c 01 4e 4c ff). + blob = bytes.fromhex("fa02287c014e4cff") + row, = rdna2_decode.decode_bytes(blob, wave32=True)["insns"] + self.assertEqual((row["op"], len(row["args"]), row["args"][0]["r"]), ("v_cmpx_gt_f32_dpp", 2, 1)) + junk = {"pc": 4, "enc": blob[4:].hex(), "op": "s_nop", "args": [{"k": "imm", "v": 0}], "mods": []} + recovered, = rdna2spirv.restore_vcc_tuples([junk], blob, 0, len(blob), wave32=True) + self.assertEqual((recovered["pc"], recovered["enc"], recovered["op"]), (0, blob.hex(), "v_cmpx_gt_f32_dpp")) + def test_vopc_dpp_reads_the_selected_lane(self): spv, _ = translate_fspx(fixture("vopc_dpp_compare")[:-1], wave32=False) memory = {} diff --git a/tests/semantic/test_wave64_halves.py b/tests/semantic/test_wave64_halves.py index 15a9426542..b112ce3311 100644 --- a/tests/semantic/test_wave64_halves.py +++ b/tests/semantic/test_wave64_halves.py @@ -1348,5 +1348,25 @@ def test_ps_w64_fixtures(self): self.assertEqual(slots64, slots32) +class VcmpxDppExec(unittest.TestCase): + """SHADER-VOPCX-DPP review: a v_cmpx in DPP form writes EXEC like any v_cmpx, and the cross-lane branch of the + analysis must not drop that write. The repro from the review: after the compare, v5 = s4 is not one value in every + lane, so v_readlane_b32 of it is not a uniform read.""" + + BODY = ["v_mov_b32_e32 v5, 0", "s_mov_b64 s[10:11], exec", None, "v_mov_b32_e32 v5, s4", "s_mov_b64 exec, s[10:11]", + "v_readlane_b32 s5, v5, 0", "v_mov_b32_e32 v6, s5"] + + def meta(self, compare): + body = [compare if line is None else line for line in self.BODY] + return meta_of("ps_vcmpx_dpp_exec", ps(body))[0] + + def test_dpp_form_is_classified_as_the_e32_form(self): + e32 = self.meta("v_cmpx_gt_f32_e32 v1, v2") + dpp = self.meta("v_cmpx_gt_f32_dpp v1, -v1 quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1 fi:1") + self.assertEqual(e32, ("C", ["cross-lane"], [])) + self.assertEqual(dpp, e32) + self.assertNotIn("readlane-uniform", dpp[2]) + + if __name__ == "__main__": unittest.main() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index bf51eabc61..b185f66240 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -7356,22 +7356,23 @@ def selectors(key, count, limit): raise Unsupported("DPP operands") # A VOPC compare in DPP form (docs/design.md, "DPP on a VOPC compare"): src0 comes through the same lane # selection and the result is a mask bit, not a VGPR. Only the forms where every lane writes its bit are - # taken: what a lane that DPP leaves unwritten does to its VCC bit is not measured. + # taken: what a lane that DPP leaves unwritten does to its VCC bit is measured on RDNA3 only (it gets 0), not + # on RDNA2. v_cmpx writes EXEC instead and prints no destination, so its src0 is the first operand + # (SHADER-VOPCX-DPP). compare = base_op(ins["op"]).startswith("v_cmp") + first = 0 if base_op(ins["op"]).startswith("v_cmpx") else 1 if compare: - if base_op(ins["op"]).startswith("v_cmpx"): - raise Unsupported("DPP on v_cmpx") - if len(args) != 3: + if len(args) != first + 2: raise Unsupported("DPP compare operands") if row_mask != 0xF or bank_mask != 0xF or not (dpp8 is not None or bound): - raise Unsupported("DPP compare with lanes that keep their mask bit") + raise Unsupported("DPP compare with lanes DPP leaves unwritten") elif args[0].get("k") != "vgpr" or args[0].get("n", 1) != 1: raise Unsupported("DPP destination") - if args[1].get("n", 1) != 1: + if args[first].get("n", 1) != 1: raise Unsupported("DPP 64-bit source") - if args[1].get("sext"): + if args[first].get("sext"): raise Unsupported("DPP sext") - src = args[1] + src = args[first] raw_src = dict(src) raw_src.pop("neg", None) raw_src.pop("abs", None) @@ -7552,10 +7553,10 @@ def selectors(key, count, limit): else: write = self.tmp() self.emit(f"{write} = OpLogicalAnd %bool {enabled} {src_ok}") - args[1] = {"k": "ssa", "v": src0} + args[first] = {"k": "ssa", "v": src0} 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. + # is the only one: the mask write is the plain compare's (for v_cmpx, EXEC's). return self._insn({**ins, "args": args}) previous = self.dpp_guard self.dpp_guard = write @@ -17574,12 +17575,12 @@ def restore_mix_inline_literal(ins, raw, literal): def skipped_form(ins, blob, at): """Whether rdna2_decode's instruction at blob offset ``at`` is one of the forms llvm-mc skips that - restore_vcc_tuples takes from the blob: a tuple that reaches vcc (SGPRs 106 and 107), a VOPC f32 compare in DPP - form (docs/design.md, "DPP on a VOPC compare"), or the measured v_dot4_i32_i8 with op_sel 2 / op_sel_hi 5 and no + restore_vcc_tuples takes from the blob: a tuple that reaches vcc (SGPRs 106 and 107), a VOPC f32 compare (v_cmp or + v_cmpx) in DPP form (docs/design.md, "DPP on a VOPC compare"), or the measured v_dot4_i32_i8 with op_sel 2 / op_sel_hi 5 and no clamp or neg, all reserved bits clear (tedin7, upstream PR #281; docs/fspx.md). shader_disasm.py prints the last two itself; this covers a listing made without that pass (#984).""" op = ins["op"] - if op.startswith("v_cmp_") and op.endswith("_f32_dpp"): + if op.startswith(("v_cmp_", "v_cmpx_")) and op.endswith("_f32_dpp"): return True if op == "v_dot4_i32_i8" and int.from_bytes(blob[at:at + 4], "little") >> 8 == 0xCC1650 and blob[at + 7] >> 3 == 1: return True diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index 54db61acd0..9c0df3bf6e 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -921,6 +921,10 @@ def bits_violation(t, allowed=0): put_t |= T.get(r, 0) | (CTRL if (id(ins), r) in self.stale else 0) for d in dests: put(d, put_t) + # v_cmpx in DPP form (SHADER-VOPCX-DPP) writes EXEC and prints no destination: it is an EXEC write like + # any v_cmpx, and never a uniform vote, as a lane whose DPP source is invalid compares 0. + if "exec" in defs: + self.exec_write(n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, put_t, vote=False) return # --- exec writes and mask producers @@ -1261,7 +1265,7 @@ def cmp_tag(self, st, ins, op, srcs, t_src, idiom): tag, tj = ("elect", g[1]), ELECT return tag, tj - def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_src): + def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_src, vote=True): T = st["t"] site = ("n", n, i) old = st["x"] @@ -1292,7 +1296,9 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ # s_mov / s_and / s_or / s_xor / s_andn2 ... with EXEC as the destination, or v_cmpx srcregs = [x for x in srcs if _regs(x) != ["exec"]] x0 = srcregs[0] if srcregs else None - if op.startswith("v_cmpx"): + if op.startswith("v_cmpx") and not vote: + mg, mt = None, t_src + elif op.startswith("v_cmpx"): mg, mt = self.cmp_tag(st, ins, "v_cmp" + op[len("v_cmpx"):], srcs, t_src, idiom) if mg: mt = 0