upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorwhoalin1 <207920491+whoalin1@users.noreply.github.com>2026-11-24 23:56:36 +0200
committerwhoalin1 <207920491+whoalin1@users.noreply.github.com>2026-11-25 03:41:00 +0200
commit8e5cf5aef1942811bc6e377077b8dc8f388ff8fc (patch)
treeb71201c0323af882a783ad0504a12cd2e7f7e569 /tools
parent53ef382d7097290ecb1387f2400eaa7d134585b6 (diff)
fix(rdna2spirv): include END in out_pcs for trailing conditional branches
A conditional branch in the last basic block (no fallthrough block, so len(raw_edges[b]) == 1) emits OpSelect(%cond, tgt, len(blocks)) and can leave %pc at either tgt or cfg_structure.END. Compute exact per-block out_pcs in rdna2spirv._block_out_pcs (matching wave64_emulate_opt), pass it through capped_tree() into both cfg_structure.structure() (so single-target fall-off-the-end blocks also keep their dominated target guard) and unwrap_known_pc_guards(), and pass compact_out_pcs into cfg_structure.structure() in wave64_emulate_opt. Add unit and semantic regression tests covering both the loop back-edge repro reported in review and the non-loop dominated-target variant. Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/cfg_structure.py5
-rw-r--r--tools/rdna2spirv.py30
-rw-r--r--tools/wave64_emulate_opt.py17
3 files changed, 38 insertions, 14 deletions
diff --git a/tools/cfg_structure.py b/tools/cfg_structure.py
index 68522ec8b..9b7b41212 100644
--- a/tools/cfg_structure.py
+++ b/tools/cfg_structure.py
@@ -80,7 +80,7 @@ def _dominators(rpo, preds):
80 return idom, pos 80 return idom, pos
81 81
82 82
83def structure(succ): 83def structure(succ, out_pcs=None):
84 rpo, back, preds = _order(succ) 84 rpo, back, preds = _order(succ)
85 idom, pos = _dominators(rpo, preds) 85 idom, pos = _dominators(rpo, preds)
86 86
@@ -158,7 +158,8 @@ def structure(succ):
158 out_list.append(("loop", x, lbody)) 158 out_list.append(("loop", x, lbody))
159 else: 159 else:
160 out_list.append(("block", x)) 160 out_list.append(("block", x))
161 if kind == "b" and len(below) == 1 and list(succ[x]) == [below[0][1]]: 161 if (kind == "b" and len(below) == 1 and list(succ[x]) == [below[0][1]]
162 and (out_pcs is None or set(out_pcs[x]) == {below[0][1]})):
162 stack.append(("emit", out_list, parent_list, below[0], depth)) 163 stack.append(("emit", out_list, parent_list, below[0], depth))
163 else: 164 else:
164 hoist = (parent_list is not None and below 165 hoist = (parent_list is not None and below
diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py
index 1e5e50f7c..202a95c37 100644
--- a/tools/rdna2spirv.py
+++ b/tools/rdna2spirv.py
@@ -398,6 +398,7 @@ class Gen:
398 # single defining plain v_mov of it); valid I/J sources (SHADER-177) 398 # single defining plain v_mov of it); valid I/J sources (SHADER-177)
399 self.blocks = [] # per basic block: list of SPIR-V lines (dispatch-loop form), empty = straight-line 399 self.blocks = [] # per basic block: list of SPIR-V lines (dispatch-loop form), empty = straight-line
400 self.succ = [] # per basic block: the blocks it can go to next (none = the program ends there) 400 self.succ = [] # per basic block: the blocks it can go to next (none = the program ends there)
401 self.out_pcs = None # per basic block: exact set of %pc values written (including cfg_structure.END)
401 self.desc_fn = None # lines of the descriptor-lookup function, once an instruction needs it 402 self.desc_fn = None # lines of the descriptor-lookup function, once an instruction needs it
402 self.tbuffer_norm_fns: dict[int, list[str]] = {} # one exact quotient per used magnitude width 403 self.tbuffer_norm_fns: dict[int, list[str]] = {} # one exact quotient per used magnitude width
403 self.fmt_fn = None # lines of the typed-buffer format-decode function, once a dynamic-format load needs it 404 self.fmt_fn = None # lines of the typed-buffer format-decode function, once a dynamic-format load needs it
@@ -16276,7 +16277,7 @@ def assemble(gen):
16276 helpers.extend(function) 16277 helpers.extend(function)
16277 for function in gen.x_round_fns.values(): 16278 for function in gen.x_round_fns.values():
16278 helpers.extend(function) 16279 helpers.extend(function)
16279 tree, nesting = (capped_tree(gen.succ) if gen.blocks and not gen.emulate and not gen.bar_rounds 16280 tree, nesting = (capped_tree(gen.succ, gen.out_pcs) if gen.blocks and not gen.emulate and not gen.bar_rounds
16280 else (None, None)) 16281 else (None, None))
16281 if nesting is not None: 16282 if nesting is not None:
16282 gen.nesting = nesting 16283 gen.nesting = nesting
@@ -18055,6 +18056,7 @@ def translate(path):
18055 g.cu(n) 18056 g.cu(n)
18056 raw_edges = split_edges 18057 raw_edges = split_edges
18057 succ = [list(dict.fromkeys(e)) for e in raw_edges] 18058 succ = [list(dict.fromkeys(e)) for e in raw_edges]
18059 g.out_pcs = _block_out_pcs(blocks, raw_edges, succ)
18058 g.vm_latch = stage == "ps" and exec_changes_after_vm(blocks, succ) 18060 g.vm_latch = stage == "ps" and exec_changes_after_vm(blocks, succ)
18059 g.park_masks((blocks, succ)) 18061 g.park_masks((blocks, succ))
18060 halves_analysis(g, blocks, succ) 18062 halves_analysis(g, blocks, succ)
@@ -18334,15 +18336,33 @@ def finish_module(g):
18334 return spirv_loop_guard.add_guards(text, LOOP_LIMIT) 18336 return spirv_loop_guard.add_guards(text, LOOP_LIMIT)
18335 18337
18336 18338
18337def capped_tree(succ): 18339def _block_out_pcs(blocks, raw_edges, succ):
18340 """Possible `%pc` values each basic block can store (`cfg_structure.END` when the block can end the shader).
18341 A conditional branch with no fallthrough block (`len(raw_edges[b]) == 1`, i.e. the last block or a split clone
18342 of it) emits `OpSelect %uint cond tgt len(blocks)` and therefore writes either `tgt` or `END`."""
18343 out_pcs = {}
18344 for b, blk in enumerate(blocks):
18345 pcs = set(succ[b])
18346 if not pcs:
18347 pcs.add(cfg_structure.END)
18348 elif len(raw_edges[b]) == 1:
18349 last = next((i for i in blk if base_op(i["op"]) == "s_endpgm" or base_op(i["op"]) in BRANCHES), None)
18350 if last is not None and base_op(last["op"]) in BRANCHES and base_op(last["op"]) != "s_branch":
18351 pcs.add(cfg_structure.END)
18352 out_pcs[b] = frozenset(pcs)
18353 return out_pcs
18354
18355
18356def capped_tree(succ, out_pcs=None):
18338 """(tree, nesting): the structured tree for `succ` and its depth, or (None, None) under --dispatch-loop or when 18357 """(tree, nesting): the structured tree for `succ` and its depth, or (None, None) under --dispatch-loop or when
18339 cfg_structure can't structure it; tree is None (nesting still the measured depth) when it nests deeper than 18358 cfg_structure can't structure it; tree is None (nesting still the measured depth) when it nests deeper than
18340 MAX_STRUCTURED_DEPTH. assemble() emits exactly this tree, and needs_barrier_rounds() decides from the same one 18359 MAX_STRUCTURED_DEPTH. assemble() emits exactly this tree, and needs_barrier_rounds() decides from the same one
18341 (SHADER-427 review): a barrier in a tree that falls back to the dispatch loop isn't at a structured top level.""" 18360 (SHADER-427 review): a barrier in a tree that falls back to the dispatch loop isn't at a structured top level."""
18342 tree = cfg_structure.structure(succ) if STRUCTURED else None 18361 if out_pcs is None:
18362 out_pcs = {b: (set(succ[b]) if succ[b] else {cfg_structure.END}) for b in range(len(succ))}
18363 tree = cfg_structure.structure(succ, out_pcs=out_pcs) if STRUCTURED else None
18343 if tree is None: 18364 if tree is None:
18344 return None, None 18365 return None, None
18345 out_pcs = {b: (set(succ[b]) if succ[b] else {cfg_structure.END}) for b in range(len(succ))}
18346 tree = cfg_structure.unwrap_known_pc_guards(tree, out_pcs) 18366 tree = cfg_structure.unwrap_known_pc_guards(tree, out_pcs)
18347 nesting = cfg_structure.depth(tree) 18367 nesting = cfg_structure.depth(tree)
18348 if MAX_STRUCTURED_DEPTH and nesting > MAX_STRUCTURED_DEPTH: 18368 if MAX_STRUCTURED_DEPTH and nesting > MAX_STRUCTURED_DEPTH:
@@ -18370,7 +18390,7 @@ def needs_barrier_rounds(g, blocks, succ):
18370 break 18390 break
18371 if not with_barrier: 18391 if not with_barrier:
18372 return False 18392 return False
18373 tree, _ = capped_tree(succ) # the tree assemble() emits: over MAX_STRUCTURED_DEPTH it's the dispatch loop 18393 tree, _ = capped_tree(succ, g.out_pcs) # the tree assemble() emits: over MAX_STRUCTURED_DEPTH it's the dispatch loop
18374 top = {it[1] for it in tree if it[0] == "block"} if tree is not None else set() 18394 top = {it[1] for it in tree if it[0] == "block"} if tree is not None else set()
18375 return not with_barrier <= top 18395 return not with_barrier <= top
18376 18396
diff --git a/tools/wave64_emulate_opt.py b/tools/wave64_emulate_opt.py
index d41f5575c..4b5212721 100644
--- a/tools/wave64_emulate_opt.py
+++ b/tools/wave64_emulate_opt.py
@@ -1549,23 +1549,26 @@ def plan_structured_emulation(blocks, raw_edges, succ, folds, fold_chain, absorb
1549 eff_succ[b] = nxt 1549 eff_succ[b] = nxt
1550 eff_succ_compact.append([eff_idx[s] for s in nxt]) 1550 eff_succ_compact.append([eff_idx[s] for s in nxt])
1551 1551
1552 compact_tree = cfg_structure.structure(eff_succ_compact)
1553 if compact_tree is None:
1554 return None
1555 tree = _map_tree_ids(compact_tree, eff_blocks)
1556
1557 out_pcs = {} 1552 out_pcs = {}
1558 for b in eff_blocks: 1553 for b in eff_blocks:
1559 tail = chain_of[b][-1] 1554 tail = chain_of[b][-1]
1560 s = set(eff_succ[b]) 1555 s = set(eff_succ[b])
1561 if not s: 1556 if not s:
1562 s.add(-1) 1557 s.add(cfg_structure.END)
1563 elif tail not in folds and len(raw_edges[tail]) == 1: 1558 elif tail not in folds and len(raw_edges[tail]) == 1:
1564 last = active_blocks[tail][-1] if active_blocks[tail] else None 1559 last = active_blocks[tail][-1] if active_blocks[tail] else None
1565 op = base_op(last["op"]) if last is not None else None 1560 op = base_op(last["op"]) if last is not None else None
1566 if op in BRANCHES and op != "s_branch": 1561 if op in BRANCHES and op != "s_branch":
1567 s.add(-1) 1562 s.add(cfg_structure.END)
1568 out_pcs[b] = frozenset(s) 1563 out_pcs[b] = frozenset(s)
1564 compact_out_pcs = {
1565 eff_idx[b]: frozenset(eff_idx[p] if p != cfg_structure.END else cfg_structure.END for p in pcs)
1566 for b, pcs in out_pcs.items()
1567 }
1568 compact_tree = cfg_structure.structure(eff_succ_compact, out_pcs=compact_out_pcs)
1569 if compact_tree is None:
1570 return None
1571 tree = _map_tree_ids(compact_tree, eff_blocks)
1569 tree = _unwrap_known_pc_guards(tree, out_pcs, eff_blocks[0]) 1572 tree = _unwrap_known_pc_guards(tree, out_pcs, eff_blocks[0])
1570 1573
1571 # Precompute per-superblock instruction summaries (for dirty transfer, sync-free check, s_barrier, s_endpgm). 1574 # Precompute per-superblock instruction summaries (for dirty transfer, sync-free check, s_barrier, s_endpgm).