[PR #2183] loop guard: one total of loop visits per invocation, on top of each loop's limit (SHADER-LOOP-TOTAL) #2186

Open
opened 2026-11-26 21:20:20 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2183
Author: @QemmHD
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: claude/loop-total


📝 Commits (1)

  • 88abd16 loop guard: one total of loop visits an invocation, on top of each loop's limit

📊 Changes

8 files changed (+110 additions, -11 deletions)

View changed files

📝 docs/design.md (+1 -0)
📝 make_game.py (+9 -1)
📝 tests/semantic/test_control_flow_branches.py (+6 -0)
📝 tests/semantic/test_loop_ballot_barrier.py (+4 -1)
📝 tests/semantic/test_loop_iteration_guard.py (+38 -0)
📝 tests/semantic/test_wave64_emulate_opt.py (+2 -0)
📝 tools/rdna2spirv.py (+8 -2)
📝 tools/spirv_loop_guard.py (+42 -7)

📄 Description

tools/spirv_loop_guard.py caps each loop at 65,536 visits, so nested loops multiply their caps. A loop bound computed by a float divide whose divisor is 0 becomes +inf, converts to 0xFFFFFFFF, and an inclusive exit test against it never fails once the counter wraps. A compute program with nested loops of that shape ran at the caps for seconds per dispatch, and the device was lost with no faulting address.

  • --loop-total N (default 1,048,576; 0 = none) adds one count of every guarded back edge per invocation, in a Private variable listed by the entry point. No back edge passes once it reaches N. Each loop keeps its own limit, so a loop that ends by itself is only cut after the whole invocation made N visits.
  • make_game.py passes the flag through and records it with the other translator flags.
  • The default changes the translation of every program with a loop, so a shader library picks it up when it is translated again.

Tests

  • tests/semantic/test_loop_iteration_guard.py: two nested loops that never end stop at the total, in wave32 and emulated wave64; loops that end by themselves are untouched; without a total the per-loop counts multiply.
  • Three test-local SPIR-V interpreters (test_loop_ballot_barrier, test_control_flow_branches, test_wave64_emulate_opt) now give each invocation its Private globals, which every module with a loop has from now on.
  • Full run_semantic_tests suite green on this branch. On an RTX 4070 SUPER, the GPU loop-cap ctests pass, including an inner loop that never ends stopping at the total.

AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2183 **Author:** [@QemmHD](https://github.com/QemmHD) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `claude/loop-total` --- ### 📝 Commits (1) - [`88abd16`](https://github.com/ubiquitarianism/leonida/commit/88abd160991ff7954896752ec76bed18995df14e) loop guard: one total of loop visits an invocation, on top of each loop's limit ### 📊 Changes **8 files changed** (+110 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -0) 📝 `make_game.py` (+9 -1) 📝 `tests/semantic/test_control_flow_branches.py` (+6 -0) 📝 `tests/semantic/test_loop_ballot_barrier.py` (+4 -1) 📝 `tests/semantic/test_loop_iteration_guard.py` (+38 -0) 📝 `tests/semantic/test_wave64_emulate_opt.py` (+2 -0) 📝 `tools/rdna2spirv.py` (+8 -2) 📝 `tools/spirv_loop_guard.py` (+42 -7) </details> ### 📄 Description `tools/spirv_loop_guard.py` caps each loop at 65,536 visits, so nested loops multiply their caps. A loop bound computed by a float divide whose divisor is 0 becomes +inf, converts to 0xFFFFFFFF, and an inclusive exit test against it never fails once the counter wraps. A compute program with nested loops of that shape ran at the caps for seconds per dispatch, and the device was lost with no faulting address. - `--loop-total N` (default 1,048,576; 0 = none) adds one count of every guarded back edge per invocation, in a Private variable listed by the entry point. No back edge passes once it reaches N. Each loop keeps its own limit, so a loop that ends by itself is only cut after the whole invocation made N visits. - `make_game.py` passes the flag through and records it with the other translator flags. - The default changes the translation of every program with a loop, so a shader library picks it up when it is translated again. **Tests** - `tests/semantic/test_loop_iteration_guard.py`: two nested loops that never end stop at the total, in wave32 and emulated wave64; loops that end by themselves are untouched; without a total the per-loop counts multiply. - Three test-local SPIR-V interpreters (`test_loop_ballot_barrier`, `test_control_flow_branches`, `test_wave64_emulate_opt`) now give each invocation its Private globals, which every module with a loop has from now on. - Full `run_semantic_tests` suite green on this branch. On an RTX 4070 SUPER, the GPU loop-cap ctests pass, including an inner loop that never ends stopping at the total. AI-assisted (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2186
No description provided.