Skip to content
Merged
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
32 changes: 32 additions & 0 deletions tests/semantic/test_wave64_halves.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ 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_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",
Expand Down
67 changes: 50 additions & 17 deletions tools/wave64_halves.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -449,7 +450,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"])}

Expand All @@ -458,13 +459,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)
Expand Down Expand Up @@ -933,6 +934,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
Expand Down Expand Up @@ -961,7 +964,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
Expand All @@ -976,14 +981,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")
Expand Down Expand Up @@ -1217,7 +1231,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")
Expand Down Expand Up @@ -1301,12 +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
Expand All @@ -1316,10 +1333,18 @@ 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)
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():
Expand All @@ -1333,6 +1358,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):
Expand Down Expand Up @@ -1374,6 +1400,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"]

Expand Down Expand Up @@ -1653,8 +1680,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)
Expand Down