From d77ce933aa468564b1ab32f8511f342b7af97185 Mon Sep 17 00:00:00 2001 From: madhyn Date: Tue, 24 Nov 2026 11:01:41 -0300 Subject: [PATCH 1/2] wave64_halves: recognize UVOTE under non-zero EXEC and boolean UVOTE combinations (SHADER-10) --- tests/semantic/test_wave64_halves.py | 25 ++++++++++++++ tools/wave64_halves.py | 51 ++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/tests/semantic/test_wave64_halves.py b/tests/semantic/test_wave64_halves.py index 15a9426542..92ba8a8c1a 100644 --- a/tests/semantic/test_wave64_halves.py +++ b/tests/semantic/test_wave64_halves.py @@ -135,6 +135,31 @@ def test_a_vccz_narrowed(self): "v_cmp_eq_u32_e32 vcc, 2, v0", "s_cbranch_vccz 3", "s_and_saveexec_b64 s[16:17], vcc", "v_add_nc_u32_e32 v6, 9, v6", "s_or_b64 exec, exec, s[16:17]"]), "A", [], []) + def test_a_uvote_under_nonzero_exec(self): + self.tier("ps_a_uvote_nz", ps([ + "s_mov_b64 s[16:17], exec", "v_cmpx_lt_f32_e32 s0, v0", "s_cbranch_execz 5", + "v_cmp_eq_f32_e64 s[0:1], s4, -1.0", "s_and_b64 vcc, exec, s[0:1]", "s_cbranch_vccnz 1", + "v_add_f32_e32 v6, 1.0, v6", "s_mov_b64 exec, s[16:17]"]), "A", [], []) + + def test_c_uvote_under_narrowed_exec_without_execz_guard(self): + self.tier("ps_c_uvote_no_guard", ps([ + "s_mov_b64 s[16:17], exec", "v_cmpx_lt_f32_e32 s0, v0", + "v_cmp_eq_f32_e64 s[0:1], s4, -1.0", "s_and_b64 vcc, exec, s[0:1]", "s_cbranch_vccnz 1", + "v_add_f32_e32 v6, 1.0, v6", "s_mov_b64 exec, s[16:17]"]), "C", contains=["unsafe-vote-branch"]) + + def test_a_uvote_and_or_combine(self): + self.tier("ps_a_uvote_comb", ps([ + "v_cmp_le_f32_e64 s[14:15], s12, 0", "v_cmp_le_f32_e64 s[16:17], s13, 0", + "s_and_b64 s[14:15], s[14:15], s[16:17]", "s_and_b64 vcc, exec, s[14:15]", + "s_cbranch_vccnz 1", "v_add_f32_e32 v6, 1.0, v6"]), "A", [], []) + + def test_a_uvote_cselect_from_uniform_scc(self): + self.tier("ps_a_uvote_csel", ps([ + "s_cmp_eq_u32 s17, 0", "s_cselect_b64 s[24:25], exec, 0", + "v_cmp_eq_f32_e64 s[18:19], s4, 0", "s_or_b64 s[24:25], s[18:19], s[24:25]", + "s_and_b64 vcc, exec, s[24:25]", "s_cbranch_vccnz 1", + "v_add_f32_e32 v6, 1.0, v6"]), "A", [], []) + def test_a_loop_execnz_no_scalar_state(self): self.tier("ps_a_loop", ps([ "v_and_b32_e32 v4, 3, v0", "v_add_nc_u32_e32 v10, 1, v4", "v_mov_b32_e32 v9, 0", diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index 54db61acd0..fac5ae7fc1 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -449,7 +449,7 @@ def join(self, a, b, blk=0): u = a["u"] & b["u"] ua = (a["ua"] & b["ua"]) if a["x"] == b["x"] else u return {"t": t, "g": g, "x": x, "xg": a["xg"] if a["xg"] == b["xg"] else None, "f": a["f"] and b["f"], - "q": a["q"] and b["q"], "qr": a["qr"] or b["qr"], + "q": a["q"] and b["q"], "qr": a["qr"] or b["qr"], "nz": a["nz"] and b["nz"], "u": u, "ua": ua, "l": {k: v for k, v in a["l"].items() if b["l"].get(k) == v}, "r": self.union_map(a["r"], b["r"]), "b": self.union_map(a["b"], b["b"])} @@ -458,13 +458,13 @@ def union_map(a, b): return {k: a.get(k, frozenset()) | b.get(k, frozenset()) for k in set(a) | set(b)} def entry(self): - return {"t": {}, "g": {}, "x": ("e",), "xg": None, "f": True, "q": False, "qr": False, "u": frozenset(), - "ua": frozenset(), "l": {}, "r": {}, "b": {}} + return {"t": {}, "g": {}, "x": ("e",), "xg": None, "f": True, "q": False, "qr": False, "nz": False, + "u": frozenset(), "ua": frozenset(), "l": {}, "r": {}, "b": {}} # ---- the transfer function ---- def block(self, n, st, ev): st = {"t": dict(st["t"]), "g": dict(st["g"]), "x": st["x"], "xg": st["xg"], "f": st["f"], "q": st["q"], - "qr": st["qr"], "u": st["u"], "ua": st["ua"], "l": dict(st["l"]), "r": dict(st["r"]), + "qr": st["qr"], "nz": st["nz"], "u": st["u"], "ua": st["ua"], "l": dict(st["l"]), "r": dict(st["r"]), "b": dict(st["b"])} for i, ins in enumerate(self.blocks[n]): self.step(n, i, ins, st, ev) @@ -933,6 +933,8 @@ def bits_violation(t, allowed=0): if tag is None and self.uniform_cmp(st, ins, srcs, uses, tj): tag = UVOTE put(d, tj, tag) + if d.get("k") == "sgpr" and d.get("n", 1) == 1: + put({"k": "sgpr", "r": d["r"] + 1, "n": 1}, tj, tag) if len(dests) > 1: put(dests[1], t_src) return @@ -961,7 +963,9 @@ def bits_violation(t, allowed=0): violation("mask-half") put(dests[0], t_src | X) return - if any(not self.is_mask(ins, x) and not (x.get("k") == "imm" and x["v"] in ( + if any(not self.is_mask(ins, x) + and not (_regs(x) and all(st["g"].get(r) == UVOTE for r in _regs(x))) + and not (x.get("k") == "imm" and x["v"] in ( 0, -1, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF)) for x in srcs): # a lane mask combined with a register that is not one (a user value, a loaded mask): the result is # no mask the translator tracks, and its bits are the lanes of the half @@ -976,14 +980,23 @@ def bits_violation(t, allowed=0): tag = ("mask", st["x"]) elif r and r[0] in st["g"] and st["g"][r[0]][0] in ("mask", "elect", "wfmask", "uvote"): tag = st["g"][r[0]] - elif op == "s_and_b64" and not t_src and all( - (_regs(x) == ["exec"] and (st["f"] or st["q"])) - or (_regs(x) and all(st["g"].get(r) == UVOTE for r in _regs(x))) - for x in srcs) and any(_regs(x) != ["exec"] for x in srcs): - # a uniform compare ANDed with EXEC while EXEC is the launch mask or its whole quads (LLVM: - # s_and_b64 vcc, exec, s[..]); either way the result is no lane or a set with a lane in every half + elif op == "s_cselect_b64" and len(srcs) == 2 and (st["f"] or st["q"] or st["nz"]) and not T.get("scc", 0): + is_exec = lambda x: _regs(x) == ["exec"] + is_zero = lambda x: x.get("k") == "imm" and x["v"] == 0 + if (is_exec(srcs[0]) and is_zero(srcs[1])) or (is_zero(srcs[0]) and is_exec(srcs[1])): + tag = UVOTE + t_src = 0 + elif op in ("s_and_b64", "s_or_b64") and all( + (_regs(x) == ["exec"] and (st["f"] or st["q"] or st["nz"])) + or (not self.rd(st, ins, x) and _regs(x) and all(st["g"].get(r) == UVOTE for r in _regs(x))) + for x in srcs): + # uniform compares combined with each other or ANDed/ORed with EXEC (while launch mask, whole quads, + # or proven non-zero) tag = UVOTE + t_src = 0 put(dests[0], t_src, tag) + if tag == UVOTE and dests[0].get("k") == "sgpr" and dests[0].get("n", 1) == 1: + put({"k": "sgpr", "r": dests[0]["r"] + 1, "n": 1}, t_src, tag) if not op.startswith(("s_mov", "s_cselect")): # those leave SCC as it was: no vote on their dest T["scc"] = X st["g"]["scc"] = ("vote", _regs(dests[0])[0] if _regs(dests[0]) else "exec") @@ -1217,7 +1230,7 @@ def uniform_cmp(self, st, ins, srcs, uses, tj): half that runs, because a quad never straddles the halves and a half that runs has a launch lane. A VGPR is refused there even when st["u"] calls it uniform: it was written under the launch mask, and the helper lanes EXEC now includes never got that value, so they compare whatever they hold.""" - if tj or uses or not (st["f"] or st["q"]) or not srcs: + if tj or uses or not (st["f"] or st["q"] or st["nz"]) or not srcs: return False for x in srcs: k = x.get("k") @@ -1305,6 +1318,7 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ 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:] + st["nz"] = False st["ua"] = st["u"] if op == "s_or_b64": # s_mov_b64 leaves SCC as it was T["scc"] = X @@ -1320,6 +1334,7 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ st["x"] = site self.tok[site] = (None, None, False) st["xg"], st["f"], st["q"] = None, False, quads + st["nz"] = False st["ua"] = st["u"] if mt & ~0: for b, name in NAMES.items(): @@ -1333,6 +1348,7 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ st["x"] = site self.tok[site] = (None, None, False) st["xg"], st["f"] = None, False + st["nz"] = False st["ua"] = st["u"] def scalar_def_since_exec(self, n, i, r): @@ -1374,6 +1390,7 @@ def new_exec(self, site, old, st, narrowing, mg, mt, violation, idiom): break self.tok[site] = (old if narrowing else None, xg, st["f"] and False) st["x"], st["xg"], st["f"] = site, xg, False + st["nz"] = False if not narrowing: st["ua"] = st["u"] @@ -1653,8 +1670,14 @@ def run(self, stale): while work: n = work.pop() out = self.block(n, entry[n], None) - for s in self.succ[n]: - new = out if entry[s] is None else self.join(entry[s], out, s) + last_op = base_op(self.blocks[n][-1]["op"]) if self.blocks[n] else "" + nz_idx = 1 if last_op == "s_cbranch_execz" else (0 if last_op == "s_cbranch_execnz" else -1) + for idx, s in enumerate(self.succ[n]): + edge_out = out + if idx == nz_idx and len(self.succ[n]) == 2 and not out["nz"]: + edge_out = dict(out) + edge_out["nz"] = True + new = edge_out if entry[s] is None else self.join(entry[s], edge_out, s) if entry[s] is None or new != entry[s]: entry[s] = new work.append(s) From 62e54722010c9057b6798ef7f02559b5f2037895 Mon Sep 17 00:00:00 2001 From: madhyn Date: Tue, 24 Nov 2026 11:12:27 -0300 Subject: [PATCH 2/2] wave64_halves: restore EXEC launch mask across s_wqm_b64 + s_and_b64 --- tests/semantic/test_wave64_halves.py | 7 +++++++ tools/wave64_halves.py | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/semantic/test_wave64_halves.py b/tests/semantic/test_wave64_halves.py index 92ba8a8c1a..618eb58dca 100644 --- a/tests/semantic/test_wave64_halves.py +++ b/tests/semantic/test_wave64_halves.py @@ -160,6 +160,13 @@ def test_a_uvote_cselect_from_uniform_scc(self): "s_and_b64 vcc, exec, s[24:25]", "s_cbranch_vccnz 1", "v_add_f32_e32 v6, 1.0, v6"]), "A", [], []) + def test_a_wqm_restore_keeps_uvote(self): + self.tier("ps_a_wqm_restore", ps([ + "s_mov_b64 s[12:13], exec", "s_wqm_b64 exec, exec", + "s_and_b64 exec, exec, s[12:13]", + "v_cmp_lt_f32_e64 s[14:15], 0.5, s4", "s_and_b64 vcc, exec, s[14:15]", + "s_cbranch_vccz 1", "v_add_f32_e32 v6, 1.0, v6"]), "A", [], []) + def test_a_loop_execnz_no_scalar_state(self): self.tier("ps_a_loop", ps([ "v_and_b32_e32 v4, 3, v0", "v_add_nc_u32_e32 v10, 1, v4", "v_mov_b32_e32 v9, 0", diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index fac5ae7fc1..354de1281b 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -336,6 +336,7 @@ def __init__(self, blocks, succ, masks, stage): 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.wqm_base = {} # exec token -> base token it is a WQM superset of self.stale = set() self.voted = {} # block -> mask register key the terminating branch votes on self.branch_class = {} @@ -1314,13 +1315,15 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ 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)): + if (op == "s_mov_b64" or (op == "s_or_b64" and any(_regs(x) == ["exec"] for x in srcs)) + or (op == "s_and_b64" and any(_regs(x) == ["exec"] for x in srcs) + and mg and mg[0] == "mask" and self.wqm_base.get(old) == mg[1])) \ + and mg and mg[0] == "mask" and (op != "s_or_b64" or self.ancestor(mg[1], old)): st["x"] = mg[1] st["xg"], st["f"] = self.tok.get(mg[1], (None, None, False))[1:] st["nz"] = False st["ua"] = st["u"] - if op == "s_or_b64": # s_mov_b64 leaves SCC as it was + if op in ("s_or_b64", "s_and_b64"): # s_mov_b64 leaves SCC as it was T["scc"] = X st["g"]["scc"] = ("vote", "exec") return @@ -1330,6 +1333,13 @@ def exec_write(self, n, i, ins, op, a, dests, srcs, st, ev, violation, idiom, t_ 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) + elif op == "s_wqm_b64" and len(srcs) == 1 and _regs(srcs[0]) == ["exec"]: + st["x"] = site + self.tok[site] = (None, None, False) + self.wqm_base[site] = self.wqm_base.get(old, old) + st["xg"], st["f"], st["q"] = None, False, quads + st["nz"] = False + st["ua"] = st["u"] else: st["x"] = site self.tok[site] = (None, None, False)