diff --git a/docs/design.md b/docs/design.md index cc0126679..0f0334f25 100644 --- a/docs/design.md +++ b/docs/design.md @@ -299,6 +299,7 @@ How the generated SPIR-V expects the runtime to work: | Piece | Contract | |---|---| | Heap-relative buffer device addresses | `Gen.host_addr` sign-extends the low 48 bits of the effective VA after adding any byte offset, then adds `%heap_base`. The runtime stores `device_address - heap_base` in a V#'s 48-bit base field. NVIDIA's Windows driver can place an imported buffer below the heap, so that offset is negative. Zero extension made the resulting device address too high by `2^48`. Console user-mode VAs below `2^47` and full-width root descriptor addresses already within the signed 48-bit range retain their values. A 64-bit base-plus-offset sum gives the correct address for effective VAs in that range, including when the sum crosses zero. Each new shader's `index.json` entry carries `"vsharp_base_sign_extended": true`. Check the field on all loaded entries before enabling below-arena imports for a library, because older modules can share an index with newly translated ones. `tests/semantic/test_vsharp_sext48.py` checks the emitted shifts, both reads against synthetic memory, and the metadata field. | +| SCC from a lane mask | A native subgroup votes when any control-flow path can read the scalar condition produced by a mask instruction before another instruction overwrites it. `tests/semantic/test_salu_b32_lane_masks.py` checks both predecessors of a branch join and an unconditional overwrite. | | 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. | diff --git a/tests/semantic/test_salu_b32_lane_masks.py b/tests/semantic/test_salu_b32_lane_masks.py index 8130ab22f..a4bc5d899 100644 --- a/tests/semantic/test_salu_b32_lane_masks.py +++ b/tests/semantic/test_salu_b32_lane_masks.py @@ -117,6 +117,28 @@ def bits_list(mask: int, width: int) -> list[int]: class Wave32B32LaneMaskTests(unittest.TestCase): + def test_native_scc_vote_at_a_join(self): + asm = [ + "v_cmp_ne_u32_e64 vcc_lo, 0, s4 ; encoding: [0x6a,0x00,0xc5,0xd4,0x80,0x08,0x00,0x02]", + "s_not_b32 s10, vcc_lo ; encoding: [0x6a,0x07,0x8a,0xbe]", + "s_cbranch_vccz 1 ; encoding: [0x01,0x00,0x86,0xbf]", + "s_and_b32 s11, 0, 0 ; encoding: [0x80,0x80,0x0b,0x87]", + "s_cselect_b32 s11, 1, 0 ; encoding: [0x81,0x80,0x0b,0x85]", + "v_mov_b32_e32 v5, s11 ; encoding: [0x0b,0x02,0x0a,0x7e]", + "v_lshlrev_b32_e32 v6, 2, v0 ; encoding: [0x82,0x00,0x0c,0x34]", + "buffer_store_dword v5, v6, s[0:3], 0 offen ; encoding: [0x00,0x10,0x70,0xe0,0x06,0x05,0x00,0x80]", + "s_endpgm ; encoding: [0x00,0x00,0x81,0xbf]", + ] + # One predecessor carries the vote's SCC to the select; the other overwrites it. + joined = translate_cs("scc_vote_join", asm, 32) + for value, want in ((0, 1), (1, 0)): + self.assertEqual(run_cs(joined, 32, s4=value), {4 * i: want for i in range(32)}) + + # Without the branch the overwrite is unconditional, so the mask needs no subgroup vote. + straight = translate_cs("scc_vote_overwritten", asm[:2] + asm[3:], 32) + self.assertNotIn("OpGroupNonUniformAny ", straight) + self.assertEqual(run_cs(straight, 32), {4 * i: 0 for i in range(32)}) + def test_s_not_b32_on_vcc_into_sgpr_and_scc(self): asm = W32_BUILD_VCC + [ "s_not_b32 s10, vcc_lo", diff --git a/tests/semantic/test_wave64_emulate_opt.py b/tests/semantic/test_wave64_emulate_opt.py index 995504234..616f65e6f 100644 --- a/tests/semantic/test_wave64_emulate_opt.py +++ b/tests/semantic/test_wave64_emulate_opt.py @@ -706,8 +706,7 @@ def test_src_scc_keeps_mask_vote_live(self): self.assertEqual(actual, [1] * threads, f"{actual.count(1)}/{threads} lanes observed the live SCC") - def test_non_emulated_unchanged_keeps_wqm_scc_vote(self): - # Verify WAVE64_EMULATE=False keeps dead-SCC votes (required by test_translate.py WQM_MASK). + def test_dead_wqm_scc_skips_vote_in_native_and_emulated_waves(self): insns = [ ("s_mov_b64 exec, -1", 4), ("v_cmp_gt_u32_e32 vcc, 16, v0", 4), @@ -715,8 +714,8 @@ def test_non_emulated_unchanged_keeps_wqm_scc_vote(self): ("s_bcnt1_i32_b64 s8, s[6:7]", 4), ("s_endpgm", 4), ] - spv_native, _ = _translate_encoded("cs_wqm_native_keep", insns, (64, 1, 1), emulate=False) - self.assertIn("OpGroupNonUniformAny", spv_native) + spv_native, _ = _translate_encoded("cs_wqm_native_dead_scc", insns, (64, 1, 1), emulate=False) + self.assertNotIn("OpGroupNonUniformAny", spv_native) spv_emu, _ = _translate_encoded("cs_wqm_emu_dead_scc", insns, (64, 1, 1), emulate=True) self.assertNotIn("OpGroupNonUniform", spv_emu) # Only s_bcnt1's wave_mask uses OpAtomicOr (2 words); s_wqm's dead SCC vote is elided. diff --git a/tests/semantic/test_wrexec_b32.py b/tests/semantic/test_wrexec_b32.py index 4b5bbd2c4..b62bff529 100644 --- a/tests/semantic/test_wrexec_b32.py +++ b/tests/semantic/test_wrexec_b32.py @@ -223,6 +223,7 @@ def test_spirv_text_negative_checks(self): "v_mbcnt_lo_u32_b32 v1, -1, 0", "v_cmp_eq_u32_e32 vcc_lo, 0, v1", f"{op} s6, vcc_lo", + "s_cselect_b32 s8, 1, 0", "s_endpgm", ], fssm=fssm(32), diff --git a/tests/test_translate.py b/tests/test_translate.py index 4dc8f0047..41a7b6d94 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -843,7 +843,7 @@ def test_wave32_bpermute_zeroes_inactive_and_missing_source_lanes(make_shader): "source mask must read the active lane predicate" assert f"OpBitwiseAnd %uint {shuffle['result']} " in asm, "the destination write must also mask the shuffle" -WQM_MASK = "s_mov_b64 exec, -1\nv_cmp_gt_u32_e32 vcc_lo, 16, v0\ns_wqm_b64 s[6:7], vcc\ns_bcnt1_i32_b64 s8, s[6:7]\ns_endpgm\n" +WQM_MASK = "s_mov_b64 exec, -1\nv_cmp_gt_u32_e32 vcc_lo, 16, v0\ns_wqm_b64 s[6:7], vcc\ns_cselect_b32 s9, 1, 0\ns_bcnt1_i32_b64 s8, s[6:7]\ns_endpgm\n" WQM_DATA = "s_mov_b32 s20, 0x00800010\ns_mov_b32 s21, 1\ns_wqm_b64 s[22:23], s[20:21]\ns_add_u32 s24, s22, s23\ns_endpgm\n" diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index a41db7eb7..f450552ec 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -550,8 +550,8 @@ def __init__(self, stage, hoist_reg_ptrs=False, cache_active=False): self.lane_either = {} # id(instruction) -> SGPRs holding constant 0/-1 (EITHER) there self.cur_either = () self.x_lanes = 64 # emulate: workgroup invocations rounded up to whole waves - self.scc_live = None # emulate: set of id(ins) whose SCC write is live-out (wave64_emulate_opt) - self.cur_scc_live = True # emulate: whether the current instruction's SCC result is live-out + self.scc_live = None # instructions whose SCC result can reach a reader + self.cur_scc_live = True # keep the SCC result when emitting without a whole-shader analysis self.bar_rounds = False # native wave32 cs with an s_barrier outside uniform control flow (bar_scheduler) self.uses_storage3d = False self.uses_heap = False @@ -7551,7 +7551,7 @@ def insn(self, ins): self.cur_masks = self.lane_masks.get(id(ins), {}) self.write_all_lanes = id(ins) in self.mask_writes self.cur_uniform_readlane = bool(self.w64g) and id(ins) in self.w64g["uniform_readlane"] - self.cur_scc_live = (not self.emulate) or (self.scc_live is None) or (id(ins) in self.scc_live) + self.cur_scc_live = self.scc_live is None or id(ins) in self.scc_live if self.emulate: self.x_memory_order(base_op(ins["op"])) self.cur_either = self.lane_either.get(id(ins), ()) @@ -18869,6 +18869,7 @@ def _translate(path): if not branchy and not g.emulate: g.vm_latch = stage == "ps" and exec_changes_after_vm([insns], [[]]) g.park_masks(([insns], [[]])) + g.scc_live = wave64_emulate_opt.scc_live_outs([insns], [[]]) halves_analysis(g, [insns], [[]]) g.build_meta = _shader_meta.collect(path, info["name"], [insns], [[]], user_sgpr_limit=user_limit, system_sgprs=sys_sgprs) @@ -18945,8 +18946,8 @@ def target(ins): g.bar_rounds = needs_barrier_rounds(g, blocks, succ) folds, fold_chain, absorbed = {}, {}, set() + g.scc_live = wave64_emulate_opt.scc_live_outs(blocks, succ) if g.emulate: - g.scc_live = wave64_emulate_opt.scc_live_outs(blocks, succ) folds, fold_chain, absorbed = wave64_emulate_opt.find_local_exec_folds( blocks, raw_edges, succ, g.lane_masks, g.scc_live, g.mask_writes) if STRUCTURED and WAVE64_PLAN: