feat: structured control flow in HLSL recompiler (#68) #116

Manually merged
github merged 3 commits from gh-pr/116/head into gh-pr/116/base 2026-11-26 05:32:18 +00:00
Owner

@⁠Lewandox on GitHub · 2026-11-23 23:56 UTC · original
Lewandox:feat/hlsl-structured-cfg → main · +241 −20 · 12 files · 3 commits · merged 2026-11-24 10:14 UTC

First part of #68: replaces the flat while (pc != END_PC) { switch (pc) ... } loop in the HLSL recompiler with structured control flow ([loop] while loops and if (pc == ...) guards) using third_party/leonida/cfg_structure.py (Leonida, GPL-3.0).

Changes

  • PrototypeScarlett/emu/rdna2_hlsl.py: splits decoded instructions into basic blocks (_build_blocks), structures the CFG via cfg_structure.structure(succ), and emits nested loops/guards (_emit_tree). Straight-line shaders no longer emit any loop or switch (pc); irreducible CFGs cleanly fall back to while + switch (pc).
  • PrototypeScarlett/emu/rdna2_hlsl_graphics.py: uses return o_out; / return; for s_endpgm in translate_vertex and translate_pixel instead of break; (which is invalid outside a loop).
  • tests/test_rdna2_hlsl_cfg.py and tests/test_rdna2_hlsl.py: tests covering straight-line code, if/else diamonds, nested loops, irreducible CFG fallback to switch (pc), VS/PS graphics stages, and GPU/WARP execution against the reference interpreter.
  • README.md and docs/i18n/README.*.md: updated test count (555 -> 561).

Local verification

  • .venv\Scripts\python.exe -m pytest tests/test_rdna2_hlsl_cfg.py tests/test_readme_sync.py -v: 9 passed, 2 skipped (dxc/anyxboxx_gpu_run.exe not installed locally; Windows CI runs the full suite including GPU/WARP tests).
  • .venv\Scripts\python.exe -m pytest tests/test_rdna2_hlsl.py tests/test_rdna2_graphics.py tests/test_rdna2_image.py tests/test_shader_cache.py -v: 22 passed (non-GPU tests).
  • .venv\Scripts\python.exe -m ruff check --select W605,F,E9 PrototypeScarlett tests tools: All checks passed!

No game, console, or SDK files used. AI-assisted contribution.

**@⁠Lewandox** on GitHub · 2026-11-23 23:56 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/116) `Lewandox:feat/hlsl-structured-cfg` → `main` · +241 −20 · 12 files · 3 commits · merged 2026-11-24 10:14 UTC First part of #68: replaces the flat `while (pc != END_PC) { switch (pc) ... }` loop in the HLSL recompiler with structured control flow (`[loop] while` loops and `if (pc == ...)` guards) using `third_party/leonida/cfg_structure.py` (Leonida, GPL-3.0). ### Changes - **`PrototypeScarlett/emu/rdna2_hlsl.py`**: splits decoded instructions into basic blocks (`_build_blocks`), structures the CFG via `cfg_structure.structure(succ)`, and emits nested loops/guards (`_emit_tree`). Straight-line shaders no longer emit any loop or `switch (pc)`; irreducible CFGs cleanly fall back to `while` + `switch (pc)`. - **`PrototypeScarlett/emu/rdna2_hlsl_graphics.py`**: uses `return o_out;` / `return;` for `s_endpgm` in `translate_vertex` and `translate_pixel` instead of `break;` (which is invalid outside a loop). - **`tests/test_rdna2_hlsl_cfg.py`** and **`tests/test_rdna2_hlsl.py`**: tests covering straight-line code, if/else diamonds, nested loops, irreducible CFG fallback to `switch (pc)`, VS/PS graphics stages, and GPU/WARP execution against the reference interpreter. - **`README.md` and `docs/i18n/README.*.md`**: updated test count (555 -> 561). ### Local verification - `.venv\Scripts\python.exe -m pytest tests/test_rdna2_hlsl_cfg.py tests/test_readme_sync.py -v`: **9 passed, 2 skipped** (`dxc`/`anyxboxx_gpu_run.exe` not installed locally; Windows CI runs the full suite including GPU/WARP tests). - `.venv\Scripts\python.exe -m pytest tests/test_rdna2_hlsl.py tests/test_rdna2_graphics.py tests/test_rdna2_image.py tests/test_shader_cache.py -v`: **22 passed** (non-GPU tests). - `.venv\Scripts\python.exe -m ruff check --select W605,F,E9 PrototypeScarlett tests tools`: **All checks passed!** No game, console, or SDK files used. AI-assisted contribution.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:32:17 +00:00
github manually merged commit 7e2b402246 into gh-pr/116/base 2026-11-26 05:32:18 +00:00
Author
Owner

@⁠Lewandox on GitHub · 2026-11-24 08:51 UTC · original

Rebased onto \main\ (with #120) and added @⁠gpu\ interpreter-vs-GPU comparisons for both the if/else diamond (\ est_if_else_diamond_matches_interpreter) and the irreducible-CFG \switch (pc)\ fallback (\ est_irreducible_cfg_fallback_matches_interpreter), with README counts synced (553 -> 561).

**@⁠Lewandox** on GitHub · 2026-11-24 08:51 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/116#issuecomment-7126655388) Rebased onto \main\ (with #120) and added \@⁠gpu\ interpreter-vs-GPU comparisons for both the if/else diamond (\ est_if_else_diamond_matches_interpreter\) and the irreducible-CFG \switch (pc)\ fallback (\ est_irreducible_cfg_fallback_matches_interpreter\), with README counts synced (553 -> 561).
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-24 08:31 UTC · original · reviewed

Thanks! Tested locally (full pytest with dxc + anyxboxx_gpu_run.exe, ruff): all new tests pass, GPU test included; the 5 failures in tests/test_native.py come from our local runtime build, they also fail on main.

One point for rule 4 (every HLSL translation checked against the reference interpreter on GPU/WARP): only the nested-loop case runs on the GPU. The if/else diamond and the irreducible-CFG fallback to switch (pc) are only checked by searching strings in the HLSL. Could you add GPU comparisons for those two shapes in tests/test_rdna2_hlsl_cfg.py?

Note: CI was failing for every PR because the Leonida submodule's upstream repo was deleted; this is fixed in #120. Once it is merged, a rebase will bring CI back.

🤖 Generated with Claude Code

**@⁠LooperSalty** on GitHub · 2026-11-24 08:31 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/116#pullrequestreview-6035947792) · **reviewed** Thanks! Tested locally (full pytest with dxc + anyxboxx_gpu_run.exe, ruff): all new tests pass, GPU test included; the 5 failures in `tests/test_native.py` come from our local runtime build, they also fail on main. One point for rule 4 (every HLSL translation checked against the reference interpreter on GPU/WARP): only the nested-loop case runs on the GPU. The if/else diamond and the irreducible-CFG fallback to `switch (pc)` are only checked by searching strings in the HLSL. Could you add GPU comparisons for those two shapes in `tests/test_rdna2_hlsl_cfg.py`? Note: CI was failing for every PR because the Leonida submodule's upstream repo was deleted; this is fixed in #120. Once it is merged, a rebase will bring CI back. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This discussion has been locked. Commenting is limited to contributors.
No description provided.