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:
authoreIBart0 <eIBart0@users.noreply.github.com>2026-11-21 21:53:03 +0000
committereIBart0 <eIBart0@users.noreply.github.com>2026-11-21 19:05:14 -0300
commit338ff3354bc2c4c5922213404e7b4fcf11330dcc (patch)
treeaa4b1fea8546e52d1893d48b5c6a8a21c52d9bc4 /tests/test_cfg_structure.py
parentab3b0492f22f8f8f7ac4964f91b7907f4e4d027c (diff)
tests: verify closed-region node splitting on random irreducible CFGs and synthetic shaders
Diffstat (limited to 'tests/test_cfg_structure.py')
-rw-r--r--tests/test_cfg_structure.py60
1 files changed, 57 insertions, 3 deletions
diff --git a/tests/test_cfg_structure.py b/tests/test_cfg_structure.py
index 6185d6a81..11e59999a 100644
--- a/tests/test_cfg_structure.py
+++ b/tests/test_cfg_structure.py
@@ -81,13 +81,38 @@ def blocks_in(tree):
81 81
82def test_random_graphs(): 82def test_random_graphs():
83 rng = random.Random(20261121) 83 rng = random.Random(20261121)
84 structured = refused = loops = 0 84 structured = refused = loops = split_ok = split_cap = 0
85 for case in range(20000): 85 for case in range(20000):
86 succ = random_graph(rng) 86 succ = random_graph(rng)
87 tree = S.structure(succ) 87 tree = S.structure(succ)
88 assert (tree is not None) == reducible(succ), f"reducibility disagrees for {succ}" 88 assert (tree is not None) == reducible(succ), f"reducibility disagrees for {succ}"
89 if tree is None: 89 if tree is None:
90 refused += 1 90 refused += 1
91 res = S.split_irreducible(succ)
92 if res is not None:
93 new_succ, orig = res
94 assert len(new_succ) <= min(2 * len(succ), len(succ) + 64)
95 stree = S.structure(new_succ)
96 assert stree is not None, f"split_irreducible returned irreducible graph for {succ}"
97 splaced = blocks_in(stree)
98 assert len(splaced) == len(set(splaced)), f"a split block is placed twice: {succ}"
99 for seed in range(6):
100 want = walk(succ, chooser(succ, seed), 200)
101 visits = {}
102
103 def split_choose(b, _orig=orig, _ns=new_succ, _seed=seed, _v=visits):
104 ob = _orig[b]
105 k = _v[ob] = _v.get(ob, 0) + 1
106 opts = _ns[b]
107 if not opts:
108 return S.END
109 return opts[random.Random(f"{_seed}/{ob}/{k}").randrange(len(opts))]
110
111 got = [orig[b] for b in S.run(stree, split_choose, 200)]
112 assert got == want, f"split graph {succ} seed {seed}: tree ran {got}, graph runs {want}"
113 split_ok += 1
114 else:
115 split_cap += 1
91 continue 116 continue
92 structured += 1 117 structured += 1
93 loops += '"loop"' in repr(tree).replace("'", '"') 118 loops += '"loop"' in repr(tree).replace("'", '"')
@@ -98,7 +123,8 @@ def test_random_graphs():
98 got = S.run(tree, chooser(succ, seed), 200) 123 got = S.run(tree, chooser(succ, seed), 200)
99 assert got == want, f"graph {succ} seed {seed}: tree ran {got}, graph runs {want}" 124 assert got == want, f"graph {succ} seed {seed}: tree ran {got}, graph runs {want}"
100 assert structured > 5000 and refused > 1000 and loops > 2000, (structured, refused, loops) 125 assert structured > 5000 and refused > 1000 and loops > 2000, (structured, refused, loops)
101 return structured, refused, loops 126 assert split_ok > 1000 and split_ok + split_cap == refused, (split_ok, split_cap, refused)
127 return structured, refused, loops, split_ok, split_cap
102 128
103 129
104def test_shapes(): 130def test_shapes():
@@ -109,8 +135,36 @@ def test_shapes():
109 ("guard", 1, [("block", 1)]), ("guard", 3, [("block", 3)])] 135 ("guard", 1, [("block", 1)]), ("guard", 3, [("block", 3)])]
110 assert S.structure([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("guard", 1, [("block", 1)])] 136 assert S.structure([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("guard", 1, [("block", 1)])]
111 assert S.structure([[1, 2], [2], [1]]) is None # two-entry loop 137 assert S.structure([[1, 2], [2], [1]]) is None # two-entry loop
138 ns, orig = S.split_irreducible([[1, 2], [2], [1]])
139 assert S.structure(ns) is not None and len(ns) == 4 and orig == [0, 1, 2, 2]
140 # Nested irreducible cycle {2, 3} inside an outer reducible loop headed by 1.
141 nested = [[1], [2, 3], [3, 4], [2, 4], [1, 5], []]
142 assert S.structure(nested) is None
143 ns2, orig2 = S.split_irreducible(nested)
144 assert S.structure(ns2) is not None and len(ns2) == 7
145 # Regression for single-node infinite unrolling: SCC {1, 2, 3} with entries 1 (h) and 2 (e), plus an
146 # internal subcycle 2 <-> 3 that does not pass through 1. Single-node cloning bounces 2' -> 3 -> 3' -> 2 -> ...
147 # until hitting the cap; closed-region cloning duplicates R = {2, 3} once (+2 blocks, 5 -> 7).
148 subcycle = [[1, 2], [3, 4], [3], [1, 2], []]
149 assert S.structure(subcycle) is None
150 ns3, orig3 = S.split_irreducible(subcycle)
151 assert S.structure(ns3) is not None and len(ns3) == 7
152 for seed in range(6):
153 want = walk(subcycle, chooser(subcycle, seed), 200)
154 visits = {}
155
156 def sc_choose(b, _o=orig3, _ns=ns3, _s=seed, _v=visits):
157 ob = _o[b]
158 k = _v[ob] = _v.get(ob, 0) + 1
159 opts = _ns[b]
160 return S.END if not opts else opts[random.Random(f"{_s}/{ob}/{k}").randrange(len(opts))]
161
162 assert [orig3[b] for b in S.run(S.structure(ns3), sc_choose, 200)] == want
163 # Cap enforcement: max_splits=0 must refuse an irreducible graph cleanly.
164 assert S.split_irreducible([[1, 2], [2], [1]], max_splits=0) is None
112 165
113 166
114if __name__ == "__main__": 167if __name__ == "__main__":
115 test_shapes() 168 test_shapes()
116 print("ok: %d graphs structured and run, %d irreducible refused, %d with loops" % test_random_graphs()) 169 print("ok: %d graphs structured and run, %d irreducible (%d split-structured, %d capped), %d with loops" %
170 (lambda s, r, l, ok, cap: (s, r, ok, cap, l))(*test_random_graphs()))