upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/test_cfg_structure.py
diff options
context:
space:
mode:
authoredwin042331-hue <edwin042331@gmail.com>2026-11-22 09:39:52 -0400
committeredwin042331-hue <edwin042331@gmail.com>2026-11-22 10:51:34 -0400
commita827ff9f9695d1276f47b5f86f0fc05c38df96ff (patch)
treefa4ee2f69246079daf2500413245091ce61abc4c /tests/test_cfg_structure.py
parent6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff)
cfg_structure: make structuring iterative and flatten dominated tail continuations
Avoid O(N) OpSelectionMerge nesting and Python RecursionError on long sequential diamond/loop chains (e.g. unrolled ScreenProbes compute shaders): hoist each guarded member's dominated tail continuation out of its enclosing guard (exact by strict dominance) and make cfg_structure.structure, cfg_structure.run, and rdna2spirv.assemble's structured emitter iterative.
Diffstat (limited to 'tests/test_cfg_structure.py')
-rw-r--r--tests/test_cfg_structure.py68
1 files changed, 66 insertions, 2 deletions
diff --git a/tests/test_cfg_structure.py b/tests/test_cfg_structure.py
index 78c71f8de..47b073f8a 100644
--- a/tests/test_cfg_structure.py
+++ b/tests/test_cfg_structure.py
@@ -83,11 +83,30 @@ def random_graph(rng):
83 83
84def blocks_in(tree): 84def blocks_in(tree):
85 out = [] 85 out = []
86 for it in tree: 86 stack = [tree]
87 out += [it[1]] if it[0] == "block" else blocks_in(it[2]) 87 while stack:
88 for it in reversed(stack.pop()):
89 if it[0] == "block":
90 out.append(it[1])
91 else:
92 stack.append(it[2])
93 out.reverse()
88 return out 94 return out
89 95
90 96
97def max_depth(tree):
98 best = 0
99 stack = [(tree, 0)]
100 while stack:
101 items, depth = stack.pop()
102 if depth > best:
103 best = depth
104 for it in items:
105 if it[0] in ("guard", "loop"):
106 stack.append((it[2], depth + 1))
107 return best
108
109
91def _check_random_graphs(): 110def _check_random_graphs():
92 rng = random.Random(20261121) 111 rng = random.Random(20261121)
93 structured = refused = loops = split_ok = split_cap = 0 112 structured = refused = loops = split_ok = split_cap = 0
@@ -177,8 +196,53 @@ def test_shapes():
177 assert S.split_irreducible([[1, 2], [2], [1]], max_splits=0) is None 196 assert S.split_irreducible([[1, 2], [2], [1]], max_splits=0) is None
178 197
179 198
199def test_deep_sequential_chains_stay_flat_and_iterative():
200 """Long chains of sequential if/else diamonds, sequential loops, and early-exit chains must structure without
201 hitting Python's recursion limit and keep bounded OpSelectionMerge nesting depth."""
202 # 1. 2,000 sequential if/else diamonds (6,001 blocks): previously nested 4,000 levels deep and raised RecursionError.
203 n_diamonds = 2000
204 diamonds = []
205 for k in range(n_diamonds):
206 b = 3 * k
207 diamonds.extend([[b + 1, b + 2], [b + 3], [b + 3]])
208 diamonds.append([])
209 tree = S.structure(diamonds)
210 assert tree is not None
211 assert len(blocks_in(tree)) == len(diamonds)
212 assert max_depth(tree) <= 2, f"expected O(1) depth on sequential diamonds, got {max_depth(tree)}"
213 for seed in range(3):
214 want = walk(diamonds, chooser(diamonds, seed), len(diamonds) + 5)
215 got = S.run(tree, chooser(diamonds, seed), len(diamonds) + 5)
216 assert got == want
217
218 # 2. 1,200 sequential single-block loops: previously nested 1,200 levels deep.
219 n_loops = 1200
220 loops = [[k, k + 1] for k in range(n_loops)] + [[]]
221 ltree = S.structure(loops)
222 assert ltree is not None
223 assert len(blocks_in(ltree)) == len(loops)
224 assert max_depth(ltree) <= 2, f"expected O(1) depth on sequential loops, got {max_depth(ltree)}"
225 for seed in range(3):
226 want = walk(loops, chooser(loops, seed), 5000)
227 got = S.run(ltree, chooser(loops, seed), 5000)
228 assert got == want
229
230 # 3. 1,500 sequential early-exit guards (b -> {b+1, END}): stays bounded by max_guard_depth without recursion.
231 n_exits = 1500
232 exits = [[k + 1, n_exits] for k in range(n_exits)] + [[]]
233 etree = S.structure(exits)
234 assert etree is not None
235 assert len(blocks_in(etree)) == len(exits)
236 assert max_depth(etree) <= 17, f"expected bounded depth on early-exit chain, got {max_depth(etree)}"
237 for seed in range(3):
238 want = walk(exits, chooser(exits, seed), len(exits) + 5)
239 got = S.run(etree, chooser(exits, seed), len(exits) + 5)
240 assert got == want
241
242
180if __name__ == "__main__": 243if __name__ == "__main__":
181 test_shapes() 244 test_shapes()
245 test_deep_sequential_chains_stay_flat_and_iterative()
182 structured, refused, loops, split_ok, split_cap = _check_random_graphs() 246 structured, refused, loops, split_ok, split_cap = _check_random_graphs()
183 print(f"ok: {structured} graphs structured and run, {refused} irreducible " 247 print(f"ok: {structured} graphs structured and run, {refused} irreducible "
184 f"({split_ok} split-structured, {split_cap} capped), {loops} with loops") 248 f"({split_ok} split-structured, {split_cap} capped), {loops} with loops")