Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ How the generated SPIR-V expects the runtime to work:

| Piece | Contract |
|---|---|
| Execution model | One invocation = one lane. A wave = a subgroup, so cross-lane ops (`ds_swizzle`, `v_permlane*`, `v_readlane`/`v_writelane`, `v_mbcnt`) become subgroup shuffles. **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). |
| Execution model | One invocation = one lane. Compute and mesh waves derive lanes from the workgroup-local index; other stages use the subgroup lane. Cross-lane ops (`ds_swizzle`, `v_permlane*`, `v_readlane`/`v_writelane`, `v_mbcnt`) use subgroup operations. 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. Other stage/wave combinations still require subgroup size == wave size or wave64 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. 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). Semantics from the RDNA2 ISA, AMD LLVM modifier syntax and the GPUOpen DPP notes. |
| 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. A pixel lane still inactive at the end is discarded. 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 lane that was inactive at the last `exp ... vm` it executed is discarded, and on a path without one, a lane inactive at the end (EXEC is kept at the export only where a path can write it afterwards). **The `vm` rule is from the RDNA2 ISA reference guide (30-November-2020, §11.1 and §11.2.1: EXEC at the export is the valid mask and the final one sent is used), not measured on hardware**; checked on the CPU by `tests/semantic/test_export_valid_mask.py`. 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 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`). 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.; 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`). The `*_co_ci_` forms, `v_mad_u64_u32` and `v_mad_i64_i32` share the same path but were not measured separately. |
| `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`, `orn2`, `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). **Not GPU-verified** for the seven new kinds. |
Expand Down
9 changes: 4 additions & 5 deletions runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
(runtime/build by default). With
the Visual Studio generator they are in runtime/build/RelWithDebInfo, Release and so on: that subfolder is found by
itself (the newest complete one if there are several) and its name is printed.
Each line is one shader: ok, or what came back instead. The wave32 test ps_bitmask_perm is only judged on lanes
0-31, since a GPU with 64-wide subgroups puts half the pixels in lanes it wasn't written for.
Each line is one shader: ok, or what came back instead. The wave32 test ps_bitmask_perm checks every pixel, including
the second 32-lane half when a GPU runs fragment shaders in 64-wide subgroups.
The two whole-wave mask checks are skipped when the device cannot provide a full 64-wide compute subgroup.
The cs_w64x_* / ms_w64x_* cross-half tests (lanes 32-63 against lanes 0-31, see their .asm headers) are skipped there
too. With LEONIDA_W64_EMULATE=1 they run without that requirement, on a device whose subgroups are narrower (NVIDIA),
Expand Down Expand Up @@ -1451,9 +1451,8 @@ def pixel_test(tools, spirv, name):
if name == "ps_bitmask_perm":
if skip := bitmask_perm_narrow_skip(out, px):
return skip
low = [p for p in px if p[0] < 32]
wrong = sum(not (a == lane ^ 9 and b == ((lane & 5) | 16) ^ 1) for lane, a, b, _ in low)
return "" if low and not wrong else f"{wrong} of {len(low)} pixels in lanes 0-31 wrong"
wrong = sum(not (a == lane ^ 9 and b == ((lane & 5) | 16) ^ 1) for lane, a, b, _ in px)
return "" if px and not wrong else f"{wrong} of {len(px)} pixels wrong"
if name == "ps_wqm": # (wqm, own, wqm, own) per pixel, ps_wqm.asm
w = int(n ** 0.5)
wrong = sum(p != (int(i % w % 4 < 2 and i // w % 4 >= 2), int(i % w % 4 == 1 and i // w % 4 == 2)) * 2 for i, p in
Expand Down
4 changes: 1 addition & 3 deletions scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,14 @@ def check_bitmask_perm(raw_path: str, msg: str = "") -> tuple[bool | None, str]:
first = ""
judged = 0
for i, (lane, v1, v2, v3) in enumerate(px):
if lane >= 32: # wave32 test: in a 64-wide subgroup lanes 32-63 hold values it never wrote
continue
judged += 1
w1 = lane ^ 9
w2 = ((lane & 5) | 16) ^ 1
if v1 != w1 or v2 != w2 or v3 != 0:
if not bad:
first = f"at ({i%64},{i//64}) lane={lane}: got ({v1},{v2},{v3}), want ({w1},{w2},0)"
bad += 1
return (bad == 0 and judged > 0), (first if bad else f"{judged}/{judged} pixels in lanes 0-31 exact")
return (bad == 0 and judged > 0), (first if bad else f"{judged}/{judged} pixels exact")


# Pixel shaders that read memory: vk_harness puts this V# in s[0:3] and seeds the heap it points at, as
Expand Down
3 changes: 3 additions & 0 deletions tests/semantic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ What they check (`tools/rdna2spirv.py`, pixel shaders plus one compute shader):
undefined in SPIR-V (`UndefinedResult`); a "lane mod 32" model of issue #94 gives the words a 32-wide GPU
shows (all 24 matched on an RTX 4070 SUPER, driver 610.88, without `--wave64-emulate`). Two SPIR-V sabotages (readlane lane & 31, bpermute without its half base) must change the
64-wide result.
- `test_wave32_pixel_subgroup64.py` translates wave32 pixel shaders with lane-specific inputs in each physical half
and runs them as one 64-lane subgroup and as two 32-lane subgroups. `v_readfirstlane`, `v_readlane`,
`v_writelane`, `ds_bpermute`, `ds_permute`, ballots, and both votes must produce the same per-half results.
- `test_w64x_scalar_load_order.py` translates wave64 programs with `--wave64-emulate` and checks the emitted rounds:
no case of the round loop's switch reads or writes buffer memory after a write, or writes after a read, so a
`buffer_store` and a later `s_load` / `s_buffer_load` of the wave (and a scalar load and a later store) run in
Expand Down
10 changes: 7 additions & 3 deletions tests/semantic/spirv_mini_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class UndefinedResult(Exception):


def run_workgroup(text, invocations, subgroup_size, inputs_for=None, push_constants=None, memory=None,
out_of_range="raise"):
out_of_range="raise", return_outputs=False):
"""Execute `invocations` invocations of `text` in lockstep and return the shared `memory`.

Invocation i is subgroup i // subgroup_size, lane i % subgroup_size (a 1-D workgroup split into subgroups in
Expand All @@ -366,19 +366,23 @@ def run_workgroup(text, invocations, subgroup_size, inputs_for=None, push_consta
* OpGroupNonUniformBallot sets bit l for each running lane l whose predicate is true.
* OpGroupNonUniformAny / All: the predicate of any / every running lane.
* OpGroupNonUniformBitwiseOr Reduce: the OR of every running lane's value.

With `return_outputs=True`, also return each invocation's Output variables in invocation order.
"""
if out_of_range not in ("raise", "wrap"):
raise ValueError(f"out_of_range: {out_of_range!r}")
memory = {} if memory is None else memory
gens = [_invocation(text, (inputs_for(i) if inputs_for else None), push_constants, memory, step=True)
for i in range(invocations)]
pending = [None] * invocations # what each invocation yielded last; None once it has returned
invocation_outputs = [None] * invocations

def advance(i, value):
try:
pending[i] = gens[i].send(value)
except StopIteration:
except StopIteration as done:
pending[i] = None
invocation_outputs[i] = done.value

for i in range(invocations):
advance(i, None)
Expand Down Expand Up @@ -424,7 +428,7 @@ def advance(i, value):
raise UnsupportedConstruct(kind)
for i in range(invocations):
advance(i, results[i])
return memory
return (memory, invocation_outputs) if return_outputs else memory


_TICK = ("tick", None, None, None)
Expand Down
9 changes: 7 additions & 2 deletions tests/semantic/test_control_flow_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ def test_branch_condition_execz_emission(self):

body_text = "\n".join(g.body)
self.assertIn("OpLoad %bool %active", body_text)
self.assertIn("OpGroupNonUniformAny %bool %c3", body_text)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal %c3", body_text)
self.assertIn("%c20", body_text) # select the ballot word for this wave32 pixel half
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn(f"{cond} = OpLogicalNot %bool", body_text)

def test_branch_condition_execnz_emission(self):
Expand All @@ -458,7 +460,10 @@ def test_branch_condition_execnz_emission(self):

body_text = "\n".join(g.body)
self.assertIn("OpLoad %bool %active", body_text)
self.assertIn(f"{cond} = OpGroupNonUniformAny %bool %c3", body_text)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal %c3", body_text)
self.assertIn("%c20", body_text) # select the ballot word for this wave32 pixel half
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn(f"{cond} = OpCopyObject %bool", body_text)
self.assertNotIn("OpLogicalNot", body_text)

def test_spirv_subgroup_simulation_matches_isa_reference(self):
Expand Down
9 changes: 7 additions & 2 deletions tests/semantic/test_control_flow_vcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,9 @@ def test_branch_condition_vccz_emission(self):
body_text = "\n".join(g.body)
self.assertIn("OpLoad %uint %vcc", body_text)
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn("OpGroupNonUniformAny %bool %c3", body_text)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal %c3", body_text)
self.assertIn("%c20", body_text) # select the ballot word for this wave32 pixel half
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn(f"{cond} = OpLogicalNot %bool", body_text)

def test_branch_condition_vccnz_emission(self):
Expand All @@ -828,7 +830,10 @@ def test_branch_condition_vccnz_emission(self):
body_text = "\n".join(g.body)
self.assertIn("OpLoad %uint %vcc", body_text)
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn(f"{cond} = OpGroupNonUniformAny %bool %c3", body_text)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal %c3", body_text)
self.assertIn("%c20", body_text) # select the ballot word for this wave32 pixel half
self.assertIn("OpINotEqual %bool", body_text)
self.assertIn(f"{cond} = OpCopyObject %bool", body_text)
self.assertNotIn("OpLogicalNot", body_text)

def test_required_capabilities_for_vcc_vote(self):
Expand Down
9 changes: 4 additions & 5 deletions tests/semantic/test_mbcnt_lo_hi.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,11 @@ def test_ps_wave64_thread_rank_all_lanes(self):
self.assertEqual(results, list(range(64)))

def test_ps_mbcnt_lo_wave32(self):
"""v_mbcnt_lo on lanes 0..31 gives lane; lanes >= 32 clamp 32."""
"""A wave32 pixel shader counts lanes independently in both halves of a 64-lane subgroup."""
lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0"]
for lane in range(32):
self.assertEqual(run_ps_mbcnt_lines(lines, lane), lane)
for lane in (32, 45, 63):
self.assertEqual(run_ps_mbcnt_lines(lines, lane), 32)
for lane in range(64):
with self.subTest(subgroup_lane=lane):
self.assertEqual(run_ps_mbcnt_lines(lines, lane), lane & 31)

def test_ps_mbcnt_hi_lanes(self):
"""v_mbcnt_hi on lanes < 32 adds 0; lanes >= 32 count relative bits."""
Expand Down
3 changes: 2 additions & 1 deletion tests/semantic/test_salu_wqm32.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def test_ps_lane_mask_emits_quad_swap_and_vote_any(self):
],
)
self.assertGreaterEqual(spv.count("OpGroupNonUniformQuadSwap %uint"), 2)
self.assertIn("OpGroupNonUniformAny %bool", spv)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal", spv)
self.assertIn("%c20", spv)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion tests/semantic/test_scalar_alu_abs_neg.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,8 @@ def test_rdna2spirv_not_b64_emission(self):
body_exec = "\n".join(gen_exec.body)

self.assertIn("OpNot %uint", body_exec)
self.assertIn("OpGroupNonUniformAny", body_exec)
self.assertIn("OpGroupNonUniformBallot", body_exec)
self.assertIn("%c20", body_exec)
self.assertIn("OpStore %scc", body_exec)

def test_spirv_math_model_equivalence(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/semantic/test_scalar_bitcmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def test_s_bitcmp1_b64_exec_lane_mask(self):
}
g.insn(ins)
emitted = "\n".join(g.body)
self.assertIn("OpGroupNonUniformBitwiseOr", emitted)
self.assertIn("OpGroupNonUniformBallot %v4uint_bal", emitted)
self.assertIn("%c20", emitted)
self.assertIn("OpStore %scc", emitted)

def test_s_bcnt0_i32_b32_emission(self):
Expand Down
Loading