From 69bd312336692b1c0ea5a1398fe8061bf91022cb Mon Sep 17 00:00:00 2001 From: plentilha <35773234+plentilha@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:21:23 -0300 Subject: [PATCH 1/2] wave64_halves: fix soundness gaps in RMW, SCC, movrel, s_bfm_b64, and EXEC tracking Closes several soundness gaps where partition-dependent wave64 VS/PS/GS programs could be classified as Tier A or B instead of Tier C: - RMW destinations & s_pack: track in-place read-modify-write sources (s_addk_i32, s_mulk_i32, s_cmovk_i32, s_cmov_b32/b64, s_bitset*, v_*fmac/mac*, v_dot*c*), remove s_pack from NO_DEST, handle v_swap_b32 rank/base/tag/uniformity swap (conservatively unioning under narrowed/elected exec), and attach implicit vcc (len(srcs) == 2), scc (s_cmovk_i32), and m0 (MOVREL_OPS). - SCC tracking: add SCC-preserving scalar ops (s_mul, s_pack, s_sext, s_flbit, s_ff0/ff1, s_brev, s_bfm, s_bitreplicate, s_get_waveid) to SCC_LESS, stop s_ff1_i32_b64 from marking SCC as a wave vote, and keep s_bcnt0_*/s_bcnt1_i32_b32 SCC taint without creating a vote. - s_bfm_b64 & dynamic register indexing: taint s_bfm_b64 with LANE; propagate VGPR/SGPR taint, mask-SGPR/parked-VGPR-mask usage (MASKD | X), elected-lane taint, uniformity/literal invalidation, and stale SGPR checks across v_movrels_b32, s_movrels_b32/b64, v_movreld_b32, v_movrelsd_b32, v_movrelsd_2_b32, and v_swaprel_b32. - EXEC / mask-half rules: check s_*_saveexec_b32 / s_*_wrexec_b32 via mask-half, handle s_andn1_saveexec_b*/s_andn1_wrexec_b*/s_andn2_wrexec_b64 narrowing and destination tokens accurately, evaluate SAVEEXEC source before overwriting dest, and check EXEC presence/operand order in s_and_b64 / s_or_b64 / s_andn2_b64. - Add synthetic regression classification and 1x64-vs-2x32 wave-interpreter execution tests in tests/semantic/test_wave64_halves_soundness.py. Claim: https://github.com/ItsNotRexed/Leonida/issues/1745#issuecomment-7080924095 --- .../semantic/test_wave64_halves_soundness.py | 355 ++++++++++++++++++ tools/wave64_halves.py | 180 +++++++-- 2 files changed, 513 insertions(+), 22 deletions(-) create mode 100644 tests/semantic/test_wave64_halves_soundness.py diff --git a/tests/semantic/test_wave64_halves_soundness.py b/tests/semantic/test_wave64_halves_soundness.py new file mode 100644 index 000000000..84bde3b9e --- /dev/null +++ b/tests/semantic/test_wave64_halves_soundness.py @@ -0,0 +1,355 @@ +"""Soundness regression tests for tools/wave64_halves.py. + +Each regression checks a bug where an unsafe wave64 VS/PS/GS program used to pass as Tier A or B +(or lose a genuine vote SCC), together with clean control cases that must remain Tier A or B. +Vertex-shader cases using integer arithmetic also execute on spirv_wave_interp.py as one 64-lane +wave vs two 32-lane waves to prove the two runs actually diverge (or agree for clean controls). + + python -I tests/semantic/test_wave64_halves_soundness.py +""" +import os +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import support # noqa: E402,F401 (sets the tools path) +from test_wave64_halves import ( # noqa: E402 + AGG_HEAD, + AGG_RANK, + meta_of, + out_param, + ps, + run_cut, + vs, +) + +ATOMIC = ["v_mov_b32_e32 v11, s6", "v_mov_b32_e32 v12, 0", + "buffer_atomic_add v11, v12, s[0:3], 0 offen glc", + "s_or_b64 exec, exec, s[14:15]"] +SLOT = ["v_readfirstlane_b32 s7, v11", "v_add_nc_u32_e32 v13, s7, {rank}", + "v_lshlrev_b32_e32 v13, 2, v13", + "buffer_store_dword v0, v13, s[0:3], 0 offen offset:256"] +ELECT_NARROWED = ["v_cmp_eq_u32_e32 vcc, 0, v10", "s_and_saveexec_b64 s[14:15], vcc"] + + +class SoundnessRegressions(unittest.TestCase): + def assert_tier_c_and_diverges(self, name, body, contains=None, push=None): + lines = vs(body) + (tier, reasons, _), asm = meta_of(name, lines) + self.assertEqual(tier, "C", f"{name}: expected tier C, got {tier} (reasons={reasons})") + if contains is not None: + self.assertTrue(set(contains) <= set(reasons), f"{name}: reasons {reasons} lack {contains}") + whole = out_param(run_cut(asm, 64, push)) + halves = out_param(run_cut(asm, 32, push)) + self.assertNotEqual(whole, halves, f"{name}: expected 1x64 and 2x32 outputs to diverge") + + def assert_ps_tier(self, name, body, tier, reasons=None, contains=None, idioms=None): + (got_tier, got_reasons, got_idioms), _ = meta_of(name, ps(body)) + self.assertEqual(got_tier, tier, f"{name}: got tier {got_tier}, reasons={got_reasons}, idioms={got_idioms}") + if reasons is not None: + self.assertEqual(sorted(got_reasons), sorted(reasons), name) + if contains is not None: + self.assertTrue(set(contains) <= set(got_reasons), f"{name}: reasons {got_reasons} lack {contains}") + if idioms is not None: + self.assertEqual(sorted(got_idioms), sorted(idioms), name) + + # -- RMW destinations and s_pack + def test_s_addk_i32_preserves_lane_count_taint(self): + self.assert_tier_c_and_diverges("vs_rmw_addk", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_addk_i32 s6, 1", + "v_mov_b32_e32 v6, s6", + ], contains=["lane-count"]) + + def test_s_mulk_i32_preserves_lane_count_taint(self): + self.assert_tier_c_and_diverges("vs_rmw_mulk", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_mulk_i32 s6, 3", + "v_mov_b32_e32 v6, s6", + ], contains=["lane-count"]) + + def test_s_bitset_preserves_lane_count_taint(self): + self.assert_tier_c_and_diverges("vs_rmw_bitset1", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_bitset1_b32 s6, 7", + "v_mov_b32_e32 v6, s6", + ], contains=["lane-count"]) + self.assert_tier_c_and_diverges("vs_rmw_bitset0", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_bitset0_b32 s6, 0", + "v_mov_b32_e32 v6, s6", + ], contains=["lane-count"]) + + def test_s_cmovk_i32_reads_vote_scc(self): + self.assert_tier_c_and_diverges("vs_rmw_cmovk", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_cmp_lg_u64 vcc, 0", + "s_mov_b32 s5, 0", + "s_cmovk_i32 s5, 9", + "v_mov_b32_e32 v6, s5", + ], contains=["cross-lane"]) + + def test_s_cmov_b32_keeps_tainted_dst_when_scc_false(self): + self.assert_tier_c_and_diverges("vs_rmw_cmov_dst", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_mov_b32 s4, 0", + "s_cmp_eq_u32 s4, 1", + "s_cmov_b32 s6, 9", + "v_mov_b32_e32 v6, s6", + ], contains=["lane-count"]) + + def test_s_pack_is_not_no_dest(self): + self.assert_tier_c_and_diverges("vs_spack", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_pack_ll_b32_b16 s5, s6, 1", + "v_mov_b32_e32 v6, s5", + ], contains=["lane-count"]) + + def test_s_pack_behind_vote_is_tracked_as_stale_scalar(self): + self.assert_tier_c_and_diverges("vs_spack_stale", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_mov_b32 s5, 7", + "s_cbranch_vccz 1", + "s_pack_ll_b32_b16 s5, 9, 2", + "v_mov_b32_e32 v6, s5", + ], contains=["stale-scalar"]) + + def test_v_dot4c_i32_i8_preserves_lane_id_taint(self): + self.assert_tier_c_and_diverges("vs_rmw_dot4c", [ + "v_mbcnt_lo_u32_b32 v6, -1, 0", + "v_mbcnt_hi_u32_b32 v6, -1, v6", + "v_dot4c_i32_i8 v6, v5, v5", + ], contains=["lane-id"]) + + def test_v_fmac_f32_on_lane_id_does_not_become_uniform(self): + self.assert_ps_tier("ps_rmw_fmac_uniform", [ + "v_mbcnt_lo_u32_b32 v6, -1, 0", + "v_mbcnt_hi_u32_b32 v6, -1, v6", + "v_fmac_f32_e32 v6, 0, v5", + "v_readfirstlane_b32 s5, v6", + "v_mov_b32_e32 v6, s5", + ], "C", contains=["lane-id"]) + + def test_v_swap_b32_swaps_taint_into_exported_vgpr(self): + self.assert_tier_c_and_diverges("vs_vswap", [ + "v_mbcnt_lo_u32_b32 v7, -1, 0", + "v_mbcnt_hi_u32_b32 v7, -1, v7", + "v_swap_b32 v6, v7", + ], contains=["lane-id"]) + + # -- SCC-preserving scalar ops + def test_s_mul_i32_does_not_clear_vote_scc(self): + self.assert_tier_c_and_diverges("vs_scc_mul", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_cmp_lg_u64 vcc, 0", + "s_mul_i32 s8, 3, 4", + "s_cselect_b32 s5, 1, 0", + "v_mov_b32_e32 v6, s5", + ], contains=["cross-lane"]) + + def test_s_ff1_i32_b64_does_not_turn_tainted_scc_into_exec_vote(self): + self.assert_tier_c_and_diverges("vs_ff1_preserves_scc", [ + "v_cmp_lt_u32_e32 vcc, 4, v0", + "s_bcnt1_i32_b64 s6, vcc", + "s_cmp_eq_u32 s6, 59", + "s_ff1_i32_b64 s7, exec", + "s_cbranch_scc0 1", + "v_mov_b32_e32 v6, 9", + ], contains=["lane-count"]) + + # -- s_bfm_b64 uses per-half wave_lane (0..31) + def test_s_bfm_b64_lane_mask_is_lane_id_tainted(self): + self.assert_tier_c_and_diverges("vs_sbfm64", [ + "s_bfm_b64 s[14:15], 4, 0", + "v_cndmask_b32_e64 v6, 0, 1, s[14:15]", + ], contains=["lane-id"]) + + # -- Dynamic register indexing (movrel) + def test_v_movrels_b32_with_tainted_m0(self): + self.assert_tier_c_and_diverges("vs_vmovrels_m0", [ + "v_and_b32_e32 v4, 15, v0", + "v_cmp_eq_u32_e32 vcc, 1, v4", + "s_bcnt1_i32_b64 s6, vcc", + "v_mov_b32_e32 v10, 10", + "v_mov_b32_e32 v12, 12", + "s_mov_b32 m0, s6", + "v_movrels_b32 v6, v8", + ], contains=["lane-count"]) + + def test_s_movrels_b32_reads_tainted_and_stale_sgpr(self): + self.assert_ps_tier("ps_smovrels_tainted", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_bcnt1_i32_b64 s9, vcc", + "s_mov_b32 m0, 3", + "s_movrels_b32 s5, s6", + "v_mov_b32_e32 v6, s5", + ], "C", contains=["lane-count"]) + lines = [ + "s_wqm_b64 exec, exec", + "v_mov_b32_e32 v5, 0", + "v_mov_b32_e32 v6, 0", + "v_mov_b32_e32 v7, 0", + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_mov_b32 s9, 1", + "s_cbranch_vccz 1", + "s_mov_b32 s9, 2", + "s_mov_b32 m0, 3", + "s_movrels_b32 s5, s6", + "v_mov_b32_e32 v6, s5", + "exp mrt0 v6, v7, v5, v5 done vm", + "s_endpgm", + ] + (got_tier, got_reasons, _), _ = meta_of("ps_smovrels_stale", lines) + self.assertEqual(got_tier, "C", f"ps_smovrels_stale: got {got_tier}, reasons={got_reasons}") + self.assertIn("stale-scalar", got_reasons) + + def test_v_movreld_b32_with_tainted_m0_or_src(self): + self.assert_tier_c_and_diverges("vs_vmovreld_m0", [ + "v_and_b32_e32 v4, 15, v0", + "v_cmp_eq_u32_e32 vcc, 1, v4", + "s_bcnt1_i32_b64 s6, vcc", + "s_sub_i32 s6, s6, 2", + "s_mov_b32 m0, s6", + "v_mov_b32_e32 v5, 9", + "v_movreld_b32 v6, v5", + ], contains=["lane-count"]) + self.assert_tier_c_and_diverges("vs_vmovreld_src", [ + "v_mbcnt_lo_u32_b32 v5, -1, 0", + "v_mbcnt_hi_u32_b32 v5, -1, v5", + "s_mov_b32 m0, 0", + "v_movreld_b32 v6, v5", + ], contains=["lane-id"]) + + def test_v_add_co_ci_u32_reads_implicit_vcc(self): + self.assert_tier_c_and_diverges("vs_add_co_ci_vcc", [ + "v_mbcnt_lo_u32_b32 v4, -1, 0", + "v_mbcnt_hi_u32_b32 v4, -1, v4", + "v_cmp_lt_u32_e32 vcc, 31, v4", + "v_add_co_ci_u32_e32 v6, vcc, 0, v6", + ], contains=["lane-id"]) + + def test_v_swap_b32_under_narrowed_exec_is_not_uniform(self): + self.assert_tier_c_and_diverges("vs_vswap_masked_uniform", [ + "v_mov_b32_e32 v5, v0", + "v_mov_b32_e32 v7, 11", + "v_cmp_eq_u32_e32 vcc, 0, v0", + "s_and_saveexec_b64 s[12:13], vcc", + "v_swap_b32 v5, v7", + "s_mov_b64 exec, s[10:11]", + "v_readfirstlane_b32 s20, v5", + "v_mov_b32_e32 v6, s20", + ], contains=["cross-lane"]) + + def test_s_movrels_b32_reads_mask_sgpr_as_data(self): + self.assert_tier_c_and_diverges("vs_smovrels_mask", [ + "s_mov_b32 m0, 5", + "s_movrels_b32 s5, s6", + "v_mov_b32_e32 v6, s5", + ], contains=["cross-lane"]) + + # -- saveexec / wrexec / s_andn2_b64 / _b32 mask-half + def test_s_andn1_saveexec_on_elected_mask_is_refused(self): + body = (AGG_HEAD + ["s_bcnt1_i32_b64 s6, exec"] + AGG_RANK + + ["v_cmp_eq_u32_e32 vcc, 0, v10", "s_andn1_saveexec_b64 s[14:15], vcc"] + + ATOMIC + [x.format(rank="v10") for x in SLOT] + ["s_mov_b64 exec, s[10:11]"]) + self.assert_ps_tier("ps_andn1_saveexec_elect", body, "C", contains=["tainted-exec"]) + + def test_s_andn2_saveexec_flips_out_of_elected_region(self): + body = (AGG_HEAD + ["s_bcnt1_i32_b64 s6, exec"] + AGG_RANK + ELECT_NARROWED + + ["s_andn2_saveexec_b64 s[16:17], s[14:15]"] + + ATOMIC + [x.format(rank="v10") for x in SLOT] + ["s_mov_b64 exec, s[10:11]"]) + self.assert_ps_tier("ps_andn2_saveexec_flip", body, "C", contains=["lane-count"]) + + def test_s_andn1_wrexec_on_elected_mask_is_refused(self): + body = (AGG_HEAD + ["s_bcnt1_i32_b64 s6, exec"] + AGG_RANK + + ["v_cmp_eq_u32_e32 vcc, 0, v10", "s_andn1_wrexec_b64 s[14:15], vcc"] + + ATOMIC + [x.format(rank="v10") for x in SLOT] + ["s_mov_b64 exec, s[10:11]"]) + self.assert_ps_tier("ps_andn1_wrexec_elect", body, "C", contains=["tainted-exec"]) + + def test_s_andn2_wrexec_writes_new_exec_not_old(self): + self.assert_tier_c_and_diverges("vs_andn2_wrexec_saved", [ + "v_mov_b32_e32 v5, v0", + "v_cmp_eq_u32_e32 vcc, 0, v0", + "s_andn2_wrexec_b64 s[12:13], vcc", + "s_mov_b64 exec, s[12:13]", + "v_mov_b32_e32 v5, 7", + "s_mov_b64 exec, s[10:11]", + "v_readfirstlane_b32 s20, v5", + "v_mov_b32_e32 v6, s20", + ], contains=["cross-lane"]) + + def test_s_andn1_wrexec_writes_new_exec_not_old(self): + self.assert_tier_c_and_diverges("vs_andn1_wrexec_saved", [ + "v_mov_b32_e32 v5, v0", + "v_cmp_eq_u32_e32 vcc, 0, v0", + "s_andn1_wrexec_b64 s[12:13], vcc", + "s_mov_b64 exec, s[12:13]", + "v_mov_b32_e32 v5, 7", + "s_mov_b64 exec, s[10:11]", + "v_readfirstlane_b32 s20, v5", + "v_mov_b32_e32 v6, s20", + ], contains=["cross-lane"]) + + def test_s_and_saveexec_b32_is_mask_half(self): + self.assert_ps_tier("ps_saveexec_b32", [ + "s_mov_b32 s14, 1", + "s_and_saveexec_b32 s12, s14", + ], "C", contains=["mask-half"]) + + def test_s_andn2_b64_exec_rules(self): + self.assert_ps_tier("ps_andn2_exec_elect", ( + AGG_HEAD + AGG_RANK + ["v_cmp_eq_u32_e32 vcc, 0, v10", "s_andn2_b64 exec, exec, vcc", + "s_mov_b64 exec, s[10:11]"] + ), "C", contains=["tainted-exec"]) + self.assert_ps_tier("ps_andn2_exec_flip", ( + AGG_HEAD + ["s_bcnt1_i32_b64 s6, exec"] + AGG_RANK + ELECT_NARROWED + + ["s_andn2_b64 exec, s[14:15], exec"] + + ATOMIC + [x.format(rank="v10") for x in SLOT] + ["s_mov_b64 exec, s[10:11]"] + ), "C", contains=["lane-count"]) + + # -- Clean controls (no false positives) + def test_clean_controls_stay_invariant(self): + for name, body in ( + ("vs_clean_spack", ["s_pack_ll_b32_b16 s5, s4, 7", "v_mov_b32_e32 v6, s5"]), + ("vs_clean_rmw", ["s_mov_b32 s5, 3", "s_addk_i32 s5, 4", "s_mulk_i32 s5, 2", + "s_bitset1_b32 s5, 4", "s_cmp_eq_u32 s5, 14", "s_cmovk_i32 s5, 9", + "v_mov_b32_e32 v6, s5"]), + ("vs_clean_vswap", ["v_mbcnt_lo_u32_b32 v6, -1, 0", "v_mbcnt_hi_u32_b32 v6, -1, v6", + "v_mov_b32_e32 v8, 11", "v_swap_b32 v6, v8"]), + ("vs_clean_vmovrels", ["v_mov_b32_e32 v8, 5", "s_mov_b32 m0, 0", "v_movrels_b32 v6, v8"]), + ("vs_clean_vmovreld", ["v_mov_b32_e32 v5, 9", "s_mov_b32 m0, 0", "v_movreld_b32 v6, v5"]), + ("vs_clean_andn2_saveexec", ["v_cmp_eq_u32_e32 vcc, 1, v0", + "s_and_saveexec_b64 s[12:13], vcc", + "v_mov_b32_e32 v6, 5", + "s_andn2_saveexec_b64 s[12:13], s[12:13]", + "v_mov_b32_e32 v6, 7", + "s_mov_b64 exec, s[10:11]"]), + ): + lines = vs(body) + (tier, reasons, _), asm = meta_of(name, lines) + self.assertEqual(tier, "A", f"{name}: got {tier}, reasons={reasons}") + self.assertEqual(out_param(run_cut(asm, 64)), out_param(run_cut(asm, 32)), name) + + lines = vs(["s_mov_b32 s8, 5", "s_mov_b32 m0, 0", "s_movrels_b32 s5, s8", "v_mov_b32_e32 v6, s5"], + prologue=["s_wqm_b64 exec, exec"]) + (tier, reasons, _), asm = meta_of("vs_clean_smovrels", lines) + self.assertEqual(tier, "A", f"vs_clean_smovrels: got {tier}, reasons={reasons}") + self.assertEqual(out_param(run_cut(asm, 64)), out_param(run_cut(asm, 32)), "vs_clean_smovrels") + + self.assert_ps_tier("ps_clean_fmac_uniform", [ + "v_mov_b32_e32 v5, s4", + "v_fmac_f32_e32 v5, 0, v5", + "v_readfirstlane_b32 s5, v5", + "v_mov_b32_e32 v6, s5", + ], "B", reasons=[], idioms=["readfirstlane-uniform"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index f5da3bfd0..1de05ef83 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -68,17 +68,29 @@ "s_nop", "s_endpgm", "s_barrier", "s_sendmsg", "s_setprio", "s_inst_prefetch", "s_clause", "s_trap", "s_icache", "s_dcache", "s_sleep", "s_code_end", "s_denorm_mode", "s_round_mode", "s_waitcnt_vscnt", "s_ttracedata", "v_nop", "s_set", "s_sethalt", "s_setkill", "s_wakeup", "s_version", "buffer_gl", - "buffer_wbinvl", "ds_gws", "ds_nop", "s_pack") + "buffer_wbinvl", "ds_gws", "ds_nop") LOADS = ("buffer_load", "global_load", "flat_load", "scratch_load", "tbuffer_load", "image_load", "image_sample", "image_gather", "image_get", "s_load", "s_buffer_load", "ds_read", "v_interp") SINKS = ("exp", "buffer_store", "global_store", "flat_store", "scratch_store", "tbuffer_store", "image_store", "ds_write", "ds_store") SCC_LESS = ("s_mov", "s_cmov", "s_cselect", "s_load", "s_buffer_load", "s_getpc", "s_setpc", "s_nop", "s_waitcnt", - "s_sendmsg", "s_getreg", "s_setreg", "s_movrel", "s_bitset", "s_movk", "s_cbranch", "s_branch", "s_endpgm", - "s_barrier", "v_", "buffer_", "global_", "ds_", "exp", "image_", "flat_", "s_set", "s_inst", "s_clause") + "s_sendmsg", "s_getreg", "s_setreg", "s_movrel", "s_bitset", "s_movk", "s_mul", "s_pack", "s_sext", + "s_flbit", "s_ff0", "s_ff1", "s_brev", "s_bfm", "s_bitreplicate", "s_get_waveid", "s_cbranch", "s_branch", + "s_endpgm", "s_barrier", "v_", "buffer_", "global_", "ds_", "exp", "image_", "flat_", "s_set", "s_inst", + "s_clause") SAVEEXEC = ("s_and_saveexec_b", "s_or_saveexec_b", "s_xor_saveexec_b", "s_andn2_saveexec_b", "s_orn2_saveexec_b", "s_nand_saveexec_b", "s_nor_saveexec_b", "s_xnor_saveexec_b", "s_andn1_saveexec_b", "s_orn1_saveexec_b", - "s_and_wrexec_b", "s_andn2_wrexec_b") + "s_and_wrexec_b", "s_andn2_wrexec_b", "s_andn1_wrexec_b") +RMW_DEST = ( + "v_fmac_f32", "v_fmac_f16", "v_mac_f32", "v_mac_legacy_f32", "v_pk_fmac_f16", + "v_dot2c_f32_f16", "v_dot2c_f32_bf16", "v_dot2c_i32_i16", "v_dot4c_i32_i8", "v_dot8c_i32_i4", + "s_addk_i32", "s_mulk_i32", "s_cmovk_i32", "s_cmov_b32", "s_cmov_b64", + "s_bitset0_b32", "s_bitset0_b64", "s_bitset1_b32", "s_bitset1_b64", +) +MOVREL_OPS = ( + "v_movrels_b32", "v_movreld_b32", "v_movrelsd_b32", "v_movrelsd_2_b32", + "v_swaprel_b32", "s_movrels_b32", "s_movrels_b64", +) MASK_BITWISE = ("s_and_b", "s_or_b", "s_xor_b", "s_andn2_b", "s_orn2_b", "s_nand_b", "s_nor_b", "s_xnor_b", "s_not_b", "s_mov_b", "s_cselect_b", "s_wqm_b") DPP_BAD = ("row_bcast", "wave_") @@ -148,8 +160,13 @@ def operands(ins): returns = op.startswith(("ds_read", "ds_bpermute", "ds_permute", "ds_swizzle", "ds_consume", "ds_append")) \ or "_rtn" in op dests, srcs = ([a[0]], list(a[1:])) if returns and a else ([], list(a)) + elif op == "v_swap_b32" and len(a) >= 2: + dests, srcs = [a[0], a[1]], [a[0], a[1]] elif a: - dests, srcs = [a[0]], list(a[1:]) + if op in RMW_DEST: + dests, srcs = [a[0]], [a[0], *a[1:]] + else: + dests, srcs = [a[0]], list(a[1:]) if op.startswith("v_") and len(a) > 2 and ( "_co_" in op or op in ("v_mad_u64_u32", "v_mad_i64_i32", "v_div_scale_f32", "v_div_scale_f64")) \ and a[1].get("k") in ("sgpr", "special"): @@ -160,14 +177,19 @@ def operands(ins): defs += ["exec", "scc"] if op.startswith("s_") and not op.startswith(SCC_LESS) and "scc" not in defs and not op.startswith("s_cbranch"): defs.append("scc") - if op in ("v_cndmask_b32", "v_addc_co_u32", "v_subb_co_u32", "v_subbrev_co_u32", "v_addc_u32", "v_subb_u32", - "v_div_fmas_f32", "v_div_fmas_f64") and len(a) <= 4 and "vcc" not in [r for x in a for r in _regs(x)]: + if op in ("v_add_co_ci_u32", "v_sub_co_ci_u32", "v_subrev_co_ci_u32", + "v_addc_co_u32", "v_subb_co_u32", "v_subbrev_co_u32", + "v_cndmask_b32", "v_addc_u32", "v_subb_u32") and len(srcs) == 2: + uses.append("vcc") + if op in ("v_div_fmas_f32", "v_div_fmas_f64") and "vcc" not in [r for x in srcs for r in _regs(x)]: uses.append("vcc") if op in ("s_cselect_b32", "s_cselect_b64", "s_addc_u32", "s_subb_u32", "s_cmov_b32", "s_cmov_b64", - "s_cbranch_scc0", "s_cbranch_scc1"): + "s_cmovk_i32", "s_cbranch_scc0", "s_cbranch_scc1"): uses.append("scc") if op in ("s_cbranch_vccz", "s_cbranch_vccnz"): uses.append("vcc") + if op in MOVREL_OPS: + uses.append("m0") return dests, srcs, defs, uses @@ -229,6 +251,25 @@ def step(self, n, i, ins, st, ev): """One instruction. Around the transfer function it keeps, per register, the exec tokens of the lane ranks (st["r"]) and of the aggregated-atomic bases (st["b"]) its value is made of: a slot address is exact only when its rank and its base belong to the same mask.""" + if base_op(ins["op"]) == "v_swap_b32" and len(ins["args"]) == 2: + r0, r1 = _regs(ins["args"][0]), _regs(ins["args"][1]) + if len(r0) == 1 and len(r1) == 1: + full = st["f"] + rk0, rk1 = st["r"].get(r0[0], frozenset()), st["r"].get(r1[0], frozenset()) + bs0, bs1 = st["b"].get(r0[0], frozenset()), st["b"].get(r1[0], frozenset()) + self.new_rank = self.new_base = None + self.transfer(n, i, ins, st, ev) + pairs = ((r0[0], rk1, bs1), (r1[0], rk0, bs0)) if full else ( + (r0[0], rk0 | rk1, bs0 | bs1), (r1[0], rk0 | rk1, bs0 | bs1)) + for r, r_toks, b_toks in pairs: + t = st["t"].get(r, 0) + for key, bit, toks in (("r", RNK, r_toks), ("b", BASE, b_toks)): + kept = toks if t & bit else frozenset() + if kept: + st[key][r] = kept + else: + st[key].pop(r, None) + return self.new_rank = self.new_base = None dests, srcs, _, uses = operands(ins) read = [r for x in srcs for r in _regs(x)] + [r for r in uses if r != "exec"] @@ -390,13 +431,17 @@ def bits_violation(t, allowed=0): else: ctok = st["g"].get(mreg, (None, None))[1] if st["g"].get(mreg, ("",))[0] == "mask" else None put(a[0], bit | (t_src & ELECT), ("cnt", ctok) if bit == CNT and ctok is not None else None) - T["scc"] = X | (t_src & ELECT) - st["g"]["scc"] = ("vote", _regs(a[1])[0] if _regs(a[1]) else "exec") + if op == "s_bcnt1_i32_b64": + T["scc"] = X | (t_src & ELECT) + st["g"]["scc"] = ("vote", _regs(a[1])[0] if _regs(a[1]) else "exec") return if op in ("s_bcnt0_i32_b64", "s_bcnt0_i32_b32", "s_bcnt1_i32_b32", "s_ff0_i32_b64", "s_ff0_i32_b32", "s_ff1_i32_b32", "s_flbit_i32_b64", "s_flbit_i32_b32", "s_flbit_i32") and len(a) == 2 \ and self.is_mask(ins, a[1]): put(a[0], X | t_src) + if op.startswith("s_bcnt"): + T["scc"] = X | t_src + st["g"].pop("scc", None) return if op in ("v_mbcnt_lo_u32_b32", "v_mbcnt_hi_u32_b32") and len(a) == 3: m, acc = a[1], a[2] @@ -418,6 +463,11 @@ def bits_violation(t, allowed=0): else: put(a[0], X | LANE | tacc) return + if op == "s_bfm_b64" and dests: + # rdna2spirv.py compares per-half wave_lane (0..31) against offset..offset+size, so the upper half + # produces bits for lanes 0..31 instead of 32..63. + put(dests[0], t_src | LANE) + return # --- cross-lane reads and writes if op == "v_readfirstlane_b32" and len(a) == 2: @@ -522,6 +572,78 @@ def bits_violation(t, allowed=0): st["g"]["scc"] = ("vote", _regs(dests[0])[0] if _regs(dests[0]) else "exec") return + # --- register swap and dynamic indexing + if op == "v_swap_b32" and len(a) == 2: + r0, r1 = _regs(a[0]), _regs(a[1]) + t0, t1 = self.rd(st, ins, a[0]), self.rd(st, ins, a[1]) + if st["f"] and not (st["xg"] and st["xg"][0] == "elect"): + g0 = st["g"].get(r0[0]) if r0 else None + g1 = st["g"].get(r1[0]) if r1 else None + u0 = bool(r0 and isinstance(r0[0], tuple) and r0[0][0] == "v" and r0[0][1] in st["u"]) + u1 = bool(r1 and isinstance(r1[0], tuple) and r1[0][0] == "v" and r1[0][1] in st["u"]) + put(a[0], t1, g1, uniform=u1) + put(a[1], t0, g0, uniform=u0) + else: + tb = t0 | t1 | (ELECT if st["xg"] and st["xg"][0] == "elect" else 0) + put(a[0], tb, uniform=False) + put(a[1], tb, uniform=False) + return + if op in ("s_movrels_b32", "s_movrels_b64") and dests: + any_s = 0 + for r, tv in T.items(): + if isinstance(r, tuple) and r[0] == "s": + any_s |= tv + if any(isinstance(k, int) and v in lane_masks.MASK_KINDS for k, v in self.masks.get(id(ins), {}).items()): + any_s |= MASKD | X + if any_s & TAG_BITS: + any_s |= X + put(dests[0], t_src | any_s) + return + if op == "v_movrels_b32" and dests: + src_base = a[1]["r"] if len(a) > 1 and a[1].get("k") == "vgpr" else 0 + any_v = 0 + for r, tv in T.items(): + if isinstance(r, tuple) and r[0] == "v" and r[1] >= src_base: + any_v |= tv + if any(isinstance(k, tuple) and k[0] in ("v", "lane") and k[1] >= src_base and v in lane_masks.MASK_KINDS + for k, v in self.masks.get(id(ins), {}).items()): + any_v |= MASKD | X + if any_v & TAG_BITS: + any_v |= X + if st["xg"] and st["xg"][0] == "elect": + any_v |= ELECT + put(dests[0], t_src | any_v, uniform=False) + return + if op in ("v_movreld_b32", "v_movrelsd_b32", "v_movrelsd_2_b32", "v_swaprel_b32") and len(a) >= 2: + dst_base = (min(a[0]["r"], a[1]["r"]) + if op == "v_swaprel_b32" and a[0].get("k") == "vgpr" and a[1].get("k") == "vgpr" + else (a[0]["r"] if a[0].get("k") == "vgpr" else 0)) + any_v = 0 + if op != "v_movreld_b32": + src_base = dst_base if op == "v_swaprel_b32" else (a[1]["r"] if a[1].get("k") == "vgpr" else 0) + for r, tv in T.items(): + if isinstance(r, tuple) and r[0] == "v" and r[1] >= src_base: + any_v |= tv + if any(isinstance(k, tuple) and k[0] in ("v", "lane") and k[1] >= src_base + and v in lane_masks.MASK_KINDS for k, v in self.masks.get(id(ins), {}).items()): + any_v |= MASKD | X + wt = t_src | any_v + if wt & TAG_BITS: + wt |= X + if st["xg"] and st["xg"][0] == "elect": + wt |= ELECT + st["u"] = frozenset(u for u in st["u"] if u < dst_base) + for k in [k for k in st["l"] if k[0] >= dst_base]: + del st["l"][k] + for k, v in list(st["g"].items()): + if v[0] in ("wfs", "wfmask") and v[1] >= dst_base: + del st["g"][k] + T[k] = T.get(k, 0) | X + if wt: + bits_violation(wt) + T[("v", dst_base)] = T.get(("v", dst_base), 0) | wt + return + # --- everything else: results depend on the sources if op in ("v_cndmask_b32", "v_cndmask_b16") or op.startswith("v_"): tj = t_src @@ -600,18 +722,21 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ T = st["t"] site = ("n", n, i) old = st["x"] - if any(_half(x) for x in dests + srcs): + if any(_half(x) for x in dests + srcs) or (op.startswith("s_") and op.endswith("_b32")): violation("mask-half") if any(op.startswith(p) for p in SAVEEXEC): m = srcs[0] if srcs else None + mt = self.rd(st, ins, m) if m else 0 + mg = st["g"].get(_regs(m)[0]) if m and _regs(m) else None + is_wrexec = "_wrexec_" in op + narrowing = op.startswith(("s_and_saveexec_", "s_andn1_saveexec_", "s_and_wrexec_", "s_andn1_wrexec_")) + pos_mask = op.startswith(("s_and_saveexec_", "s_and_wrexec_")) + self.new_exec(site, old, st, narrowing, mg if pos_mask else None, mt, violation, idiom) if dests: - T[_regs(dests[0])[0]] = 0 + saved_tok = site if is_wrexec else old for r in _regs(dests[0]): T[r] = 0 - st["g"][r] = ("mask", old) - mt = self.rd(st, ins, m) if m else 0 - mg = st["g"].get(_regs(m)[0]) if m and _regs(m) else None - self.new_exec(site, old, st, op.startswith(("s_and", "s_andn2")), mg, mt, violation, idiom) + st["g"][r] = ("mask", saved_tok) T["scc"] = X st["g"]["scc"] = ("vote", "exec") return @@ -624,17 +749,23 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ if mg: mt = 0 else: - mg = st["g"].get(_regs(x0)[0]) if x0 is not None and _regs(x0) else None - mt = self.rd(st, ins, x0) if x0 is not None else 0 - if op in ("s_or_b64", "s_mov_b64") and mg and mg[0] == "mask" and ( - op == "s_mov_b64" or self.ancestor(mg[1], old)): + mg = st["g"].get(_regs(x0)[0]) if len(srcregs) == 1 and x0 is not None and _regs(x0) else None + mt = 0 + for x in srcregs: + mt |= self.rd(st, ins, x) + if (op == "s_mov_b64" or (op == "s_or_b64" and any(_regs(x) == ["exec"] for x in srcs))) \ + and mg and mg[0] == "mask" and (op == "s_mov_b64" or self.ancestor(mg[1], old)): st["x"] = mg[1] st["xg"], st["f"] = self.tok.get(mg[1], (None, None, False))[1:] T["scc"] = X st["g"]["scc"] = ("vote", "exec") return - if op in ("s_and_b64", "s_andn2_b64", "s_and_b32") or op.startswith("v_cmpx"): + if op.startswith("v_cmpx"): + self.new_exec(site, old, st, True, mg, mt, violation, idiom) + elif op == "s_and_b64" and any(_regs(x) == ["exec"] for x in srcs): self.new_exec(site, old, st, True, mg, mt, violation, idiom) + elif op == "s_andn2_b64" and len(srcs) == 2 and _regs(srcs[0]) == ["exec"]: + self.new_exec(site, old, st, True, None, mt, violation, idiom) else: st["x"] = site self.tok[site] = (None, None, False) @@ -765,7 +896,8 @@ def narrows(self, blk, reg): if op in ("s_waitcnt", "s_nop"): continue a = ins["args"] - if op.startswith(("s_and_saveexec_b64", "s_andn2_saveexec_b64")) and len(a) == 2: + if op.startswith(("s_and_saveexec_b64", "s_andn2_saveexec_b64", + "s_and_wrexec_b64", "s_andn2_wrexec_b64")) and len(a) == 2: return (_regs(a[1]) or [None])[0] == reg if op in ("s_and_b64",) and len(a) == 3 and _regs(a[0]) == ["exec"]: return reg in [(_regs(x) or [None])[0] for x in a[1:] if _regs(x) != ["exec"]] @@ -907,6 +1039,10 @@ def sregs(x): rd = st.get(r, frozenset({(-1, 0)})) if len(rd) > 1 and any(d[0] >= 0 and ctrl[d[0]] for d in rd): stale.add((id(ins), r)) + if base_op(ins["op"]) in ("s_movrels_b32", "s_movrels_b64") and any( + isinstance(r, tuple) and r[0] == "s" and len(rd) > 1 and any(d[0] >= 0 and ctrl[d[0]] for d in rd) + for r, rd in st.items()): + stale.add((id(ins), "m0")) for r in defs_of[(b, i)]: st[r] = frozenset({(b, i)}) return stale From 1222fd54a0277b7e9ad3ff8b7c317d22773bc923 Mon Sep 17 00:00:00 2001 From: plentilha <35773234+plentilha@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:05:32 -0300 Subject: [PATCH 2/2] wave64_halves: propagate movrel dst taint across VGPRs and drop ANDN2 from narrows() Address @Flyander's review comments on PR #1982: - Propagate wt and rank/base tokens (st['r'], st['b']) across every referenced VGPR >= dst_base on v_movreld_b32/v_movrelsd_b32/v_movrelsd_2_b32/v_swaprel_b32 (and read-side VGPRs >= src_base on v_movrels_b32/relsd/swaprel in step()), so non-zero M0 targets (e.g. M0 = 1) preserve downstream taint into sinks. - Remove s_andn2_saveexec_b64 and s_andn2_wrexec_b64 from narrows(); keep s_andn1_saveexec_b64 and s_andn1_wrexec_b64 excluded as well because ~S0 & EXEC runs all lanes when S0 == 0, which diverges on 1x64 vs 2x32 behind s_cbranch_vccz. - Add regression and 1x64-vs-2x32 divergence tests for non-zero M0 v_movreld_b32 (both tainted and clean control) and ANDN1/ANDN2 in vote branches. --- .../semantic/test_wave64_halves_soundness.py | 33 +++++++++++++++++++ tools/wave64_halves.py | 30 +++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/semantic/test_wave64_halves_soundness.py b/tests/semantic/test_wave64_halves_soundness.py index 84bde3b9e..f00b8a4bd 100644 --- a/tests/semantic/test_wave64_halves_soundness.py +++ b/tests/semantic/test_wave64_halves_soundness.py @@ -14,6 +14,7 @@ HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import support # noqa: E402,F401 (sets the tools path) +import rdna2spirv # noqa: E402 from test_wave64_halves import ( # noqa: E402 AGG_HEAD, AGG_RANK, @@ -227,6 +228,17 @@ def test_v_movreld_b32_with_tainted_m0_or_src(self): "v_movreld_b32 v6, v5", ], contains=["lane-id"]) + def test_v_movreld_b32_nonzero_offset_propagates_taint(self): + self.assert_tier_c_and_diverges("vs_vmovreld_src_off1", [ + "v_mbcnt_lo_u32_b32 v8, -1, 0", + "v_mbcnt_hi_u32_b32 v8, -1, v8", + "s_mov_b32 m0, 1", + "v_movreld_b32 v5, v8", + ], contains=["lane-id"]) + w64g = rdna2spirv.translate.last_gen.w64g + self.assertIn("exp", [op for _, _, op in w64g["sites"]], + f"expected downstream exp sink to observe propagated taint: {w64g['sites']}") + def test_v_add_co_ci_u32_reads_implicit_vcc(self): self.assert_tier_c_and_diverges("vs_add_co_ci_vcc", [ "v_mbcnt_lo_u32_b32 v4, -1, 0", @@ -314,6 +326,26 @@ def test_s_andn2_b64_exec_rules(self): + ATOMIC + [x.format(rank="v10") for x in SLOT] + ["s_mov_b64 exec, s[10:11]"] ), "C", contains=["lane-count"]) + def test_s_andn2_exec_in_vote_branch_is_unsafe(self): + for tag, op in (("saveexec", "s_andn2_saveexec_b64"), ("wrexec", "s_andn2_wrexec_b64")): + self.assert_tier_c_and_diverges(f"vs_vote_{tag}_andn2", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_cbranch_vccz 1", + f"{op} s[14:15], vcc", + "v_mov_b32_e32 v6, 9", + "s_mov_b64 exec, s[10:11]", + ], contains=["unsafe-vote-branch"]) + + def test_s_andn1_exec_in_vote_branch_is_unsafe(self): + for tag, op in (("saveexec", "s_andn1_saveexec_b64"), ("wrexec", "s_andn1_wrexec_b64")): + self.assert_tier_c_and_diverges(f"vs_vote_{tag}_andn1", [ + "v_cmp_eq_u32_e32 vcc, 1, v0", + "s_cbranch_vccz 3", + f"{op} s[14:15], vcc", + "v_mov_b32_e32 v6, 9", + "s_mov_b64 exec, s[10:11]", + ], contains=["unsafe-vote-branch"]) + # -- Clean controls (no false positives) def test_clean_controls_stay_invariant(self): for name, body in ( @@ -325,6 +357,7 @@ def test_clean_controls_stay_invariant(self): "v_mov_b32_e32 v8, 11", "v_swap_b32 v6, v8"]), ("vs_clean_vmovrels", ["v_mov_b32_e32 v8, 5", "s_mov_b32 m0, 0", "v_movrels_b32 v6, v8"]), ("vs_clean_vmovreld", ["v_mov_b32_e32 v5, 9", "s_mov_b32 m0, 0", "v_movreld_b32 v6, v5"]), + ("vs_clean_vmovreld_off1", ["v_mov_b32_e32 v8, 9", "s_mov_b32 m0, 1", "v_movreld_b32 v5, v8"]), ("vs_clean_andn2_saveexec", ["v_cmp_eq_u32_e32 vcc, 1, v0", "s_and_saveexec_b64 s[12:13], vcc", "v_mov_b32_e32 v6, 5", diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index 1de05ef83..a7a838215 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -197,6 +197,8 @@ class Analysis: def __init__(self, blocks, succ, masks, stage): self.blocks, self.succ, self.masks, self.stage = blocks, succ, masks or {}, stage self.n = len(blocks) + self.vgprs = sorted({r[1] for b in blocks for ins in b for x in ins["args"] + for r in _regs(x) if isinstance(r, tuple) and r[0] == "v"}) self.tok = {("e",): (None, None, True)} # exec token -> (parent token, exec tag after, exec is the entry exec) self.stale = set() self.voted = {} # block -> mask register key the terminating branch votes on @@ -271,11 +273,33 @@ def step(self, n, i, ins, st, ev): st[key].pop(r, None) return self.new_rank = self.new_base = None + op, a = base_op(ins["op"]), ins["args"] dests, srcs, _, uses = operands(ins) read = [r for x in srcs for r in _regs(x)] + [r for r in uses if r != "exec"] + if op == "v_movrels_b32" and len(a) > 1: + src_base = a[1]["r"] if a[1].get("k") == "vgpr" else 0 + read.extend(("v", vr) for vr in self.vgprs if vr >= src_base) + dyn_v = op in ("v_movreld_b32", "v_movrelsd_b32", "v_movrelsd_2_b32", "v_swaprel_b32") and len(a) >= 2 + if dyn_v: + dst_base = (min(a[0]["r"], a[1]["r"]) + if op == "v_swaprel_b32" and a[0].get("k") == "vgpr" and a[1].get("k") == "vgpr" + else (a[0]["r"] if a[0].get("k") == "vgpr" else 0)) + if op != "v_movreld_b32": + src_base = dst_base if op == "v_swaprel_b32" else (a[1]["r"] if a[1].get("k") == "vgpr" else 0) + read.extend(("v", vr) for vr in self.vgprs if vr >= src_base) rk = frozenset().union(*[st["r"].get(r, frozenset()) for r in read]) bs = frozenset().union(*[st["b"].get(r, frozenset()) for r in read]) self.transfer(n, i, ins, st, ev) + if dyn_v: + for vr in sorted(set(self.vgprs) | {dst_base}): + if vr < dst_base: + continue + r = ("v", vr) + t = st["t"].get(r, 0) + for key, bit, inherited in (("r", RNK, rk), ("b", BASE, bs)): + if t & bit and inherited: + st[key][r] = st[key].get(r, frozenset()) | inherited + return for d in dests: for r in _regs(d): if r == "exec": @@ -641,6 +665,9 @@ def bits_violation(t, allowed=0): T[k] = T.get(k, 0) | X if wt: bits_violation(wt) + for vr in self.vgprs: + if vr >= dst_base: + T[("v", vr)] = T.get(("v", vr), 0) | wt T[("v", dst_base)] = T.get(("v", dst_base), 0) | wt return @@ -896,8 +923,7 @@ def narrows(self, blk, reg): if op in ("s_waitcnt", "s_nop"): continue a = ins["args"] - if op.startswith(("s_and_saveexec_b64", "s_andn2_saveexec_b64", - "s_and_wrexec_b64", "s_andn2_wrexec_b64")) and len(a) == 2: + if op.startswith(("s_and_saveexec_b64", "s_and_wrexec_b64")) and len(a) == 2: return (_regs(a[1]) or [None])[0] == reg if op in ("s_and_b64",) and len(a) == 3 and _regs(a[0]) == ["exec"]: return reg in [(_regs(x) or [None])[0] for x in a[1:] if _regs(x) != ["exec"]]