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:
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 /tests/test_cfg_structure.py
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 'tests/test_cfg_structure.py')
-rw-r--r--tests/test_cfg_structure.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/tests/test_cfg_structure.py b/tests/test_cfg_structure.py
index f6caa5bd8..761bb21ee 100644
--- a/tests/test_cfg_structure.py
+++ b/tests/test_cfg_structure.py
@@ -255,10 +255,11 @@ def test_deep_sequential_chains_stay_flat_and_iterative():
255 255
256 256
257def test_unwrap_known_pc_guards(): 257def test_unwrap_known_pc_guards():
258 def unwrap(succ): 258 def unwrap(succ, out_pcs=None):
259 tree = S.structure(succ) 259 if out_pcs is None:
260 out_pcs = {b: (set(succ[b]) if succ[b] else {S.END}) for b in range(len(succ))}
261 tree = S.structure(succ, out_pcs=out_pcs)
260 assert tree is not None 262 assert tree is not None
261 out_pcs = {b: (set(succ[b]) if succ[b] else {S.END}) for b in range(len(succ))}
262 return S.unwrap_known_pc_guards(tree, out_pcs) 263 return S.unwrap_known_pc_guards(tree, out_pcs)
263 264
264 # Single-arm if-then (ms_wave_info_s3 shape: 0 -> {2, 1}, 1 -> {2}, 2 -> []): join block 2 unwraps to top level. 265 # Single-arm if-then (ms_wave_info_s3 shape: 0 -> {2, 1}, 1 -> {2}, 2 -> []): join block 2 unwraps to top level.
@@ -270,6 +271,18 @@ def test_unwrap_known_pc_guards():
270 assert unwrap([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("block", 1)] 271 assert unwrap([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("block", 1)]
271 # Early exit in arm 1 (0 -> {1, 2}, 1 -> [], 2 -> []): both arms MUST remain guarded (pc may be END). 272 # Early exit in arm 1 (0 -> {1, 2}, 1 -> [], 2 -> []): both arms MUST remain guarded (pc may be END).
272 assert unwrap([[1, 2], [], []]) == [("block", 0), ("guard", 2, [("block", 2)]), ("guard", 1, [("block", 1)])] 273 assert unwrap([[1, 2], [], []]) == [("block", 0), ("guard", 2, [("block", 2)]), ("guard", 1, [("block", 1)])]
274 # Conditional branch in the last block with no s_endpgm after it (fallthrough writes END):
275 # loop body block 2 has succ [0] but out_pcs {0, END}, so exiting loop 0 leaves pc in {1, END};
276 # guard 1 MUST stay so falling off the end does not execute block 1.
277 assert unwrap([[2, 1], [], [0]], out_pcs={0: {2, 1}, 1: {S.END}, 2: {0, S.END}}) == [
278 ("loop", 0, [("block", 0), ("guard", 2, [("block", 2)])]),
279 ("guard", 1, [("block", 1)]),
280 ]
281 # Non-loop conditional branch falling off the end whose single explicit target is immediately dominated by it:
282 # block 2 has succ [1] and out_pcs {1, END}, so structure() and unwrap MUST both keep guard 1.
283 assert unwrap([[2], [], [1]], out_pcs={0: {2}, 1: {S.END}, 2: {1, S.END}}) == [
284 ("block", 0), ("block", 2), ("guard", 1, [("block", 1)]),
285 ]
273 286
274 287
275if __name__ == "__main__": 288if __name__ == "__main__":