shader: lower a conditional branch with identical targets to OpBranch #1012

Open
github-bot wants to merge 2 commits from github-bot/gh-1012 into main AGit
Owner

@⁠wuboo on GitHub · 2026-10-03 17:21 UTC · original
wuboo:pr/degenerate-branch → main · +85 −0 · 2 files · 2 commits · open

The SPIR-V emitter can produce OpSelectionMerge %m followed by OpBranchConditional %c %m %m: an if
whose arm was emptied, so both edges of the conditional go to the merge block. The SPIR-V is valid, but
SPIRV-Cross (used by MoltenVK for MSL) discards all code that follows such a selection.

Impact

In Astro Bot, 2 of 666 shaders contain this shape. Compute shader 01d6… has two such branches and its MSL
shrank from about 30k lines (370 stores) to about 1k lines with no stores, so on macOS the shader compiled
fine and silently did nothing.

Fix

In EmitStructuredTerminator, when a non-loop ConditionalBranch has true_block == false_block, emit a plain
OpBranch to that block (no OpSelectionMerge). The result is semantically identical and always valid.

Testing

  • New TestNewShaderRecompilerCfgIdenticalBranchTargets in shader_cfg_tests: a hand-built IR program whose
    entry block ends in a conditional branch with true == false == merge; asserts the emitted SPIR-V has no
    OpBranchConditional with equal targets and passes spirv-val. Without the fix it aborts with
    "SPIR-V has OpBranchConditional with identical targets"; with it the test passes. (The IR is built by hand
    because I could not produce this shape from a small guest shader through the CFG builder.)
  • Full CPU ctest identical to main 19a9464b: 34 pass, 16 need a Vulkan device and were not run here.
**@⁠wuboo** on GitHub · 2026-10-03 17:21 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1012) `wuboo:pr/degenerate-branch` → `main` · +85 −0 · 2 files · 2 commits · open The SPIR-V emitter can produce `OpSelectionMerge %m` followed by `OpBranchConditional %c %m %m`: an `if` whose arm was emptied, so both edges of the conditional go to the merge block. The SPIR-V is valid, but SPIRV-Cross (used by MoltenVK for MSL) discards all code that follows such a selection. ## Impact In Astro Bot, 2 of 666 shaders contain this shape. Compute shader `01d6…` has two such branches and its MSL shrank from about 30k lines (370 stores) to about 1k lines with no stores, so on macOS the shader compiled fine and silently did nothing. ## Fix In `EmitStructuredTerminator`, when a non-loop `ConditionalBranch` has `true_block == false_block`, emit a plain `OpBranch` to that block (no `OpSelectionMerge`). The result is semantically identical and always valid. ## Testing * New `TestNewShaderRecompilerCfgIdenticalBranchTargets` in `shader_cfg_tests`: a hand-built IR program whose entry block ends in a conditional branch with `true == false == merge`; asserts the emitted SPIR-V has no `OpBranchConditional` with equal targets and passes `spirv-val`. Without the fix it aborts with "SPIR-V has OpBranchConditional with identical targets"; with it the test passes. (The IR is built by hand because I could not produce this shape from a small guest shader through the CFG builder.) * Full CPU `ctest` identical to `main` 19a9464b: 34 pass, 16 need a Vulkan device and were not run here.
SPIRV-Cross discards all code after OpSelectionMerge %m followed by
OpBranchConditional %c %m %m. A CS in Astro Bot had two such branches and
its MSL shrank from ~30k lines (370 stores) to 1k lines with no stores.
Check that no OpBranchConditional is emitted and that the entry and merge
edges produce at least two OpBranch.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:40:17 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-03 17:28 UTC · original · edited 2026-10-03 19:18 UTC

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3f5a20c8-06f6-4287-a9e7-964c039a3b38
📥 Commits

Reviewing files that changed from the base of the PR and between 3e1d888458 and ca04dbdb4a.

📒 Files selected for processing (1)
  • tests/shaderCfgTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The SPIR-V emitter now emits an unconditional branch when a non-loop conditional has identical true and false targets. The regression test checks the emitted instructions and validates the resulting SPIR-V.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to ca04d

The change appears mergeable: the targeted branch-lowering regression is covered, and no actionable risk remains in the supplied evidence.

🚥 Pre-merge checks | ✅ 4 | ❓ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ❓ Inconclusive Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: lowering a conditional branch with identical targets to OpBranch.
Description check ✅ Passed The description explains the SPIR-V issue, the emitter fix, its impact, and the regression test. It is directly related to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 too large.)

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-03 17:28 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1012#issuecomment-5971653396) · edited 2026-10-03 19:18 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1012?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> Navigate logical layers of code changes, visualize relationships, and explore their blast radius. <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `3f5a20c8-06f6-4287-a9e7-964c039a3b38` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 3e1d888458d4bc770243e7521587717e9ff40a0d and ca04dbdb4affa5c0dd94f4316ddaa577e3b246fc. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `tests/shaderCfgTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough The SPIR-V emitter now emits an unconditional branch when a non-loop conditional has identical true and false targets. The regression test checks the emitted instructions and validates the resulting SPIR-V. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"ca04dbdb4affa5c0dd94f4316ddaa577e3b246fc" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** _⚪ Minimal_ · up to `ca04d` <!-- final_review_risk_coverage:{"sourceCommitId":"ca04dbdb4affa5c0dd94f4316ddaa577e3b246fc","coveredCommitId":"ca04dbdb4affa5c0dd94f4316ddaa577e3b246fc","kind":"reviewed"} --> The change appears mergeable: the targeted branch-lowering regression is covered, and no actionable risk remains in the supplied evidence. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❓ 1</summary> ### ❌ Failed checks (1 inconclusive) | Check name | Status | Explanation | Resolution | | :----------------: | :------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ❓ Inconclusive | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 … | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------: | :------- | :---------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ✅ Passed | The title clearly summarizes the main change: lowering a conditional branch with identical targets to `OpBranch`. | | Description check | ✅ Passed | The description explains the SPIR-V issue, the emitter fix, its impact, and the regression test. It is directly related to the changeset. | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | </details> <details> <summary>Full details: Docstring Coverage</summary> **Explanation** Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 too large.) </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1012)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-03 17:28 UTC · original · reviewed

🧹 Nitpick comments (1)
tests/shaderCfgTests.cpp (1)

7839-7840: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert that the direct branch is present.

The current check rejects only a same-target OpBranchConditional; it does not assert that the emitter produced the required OpBranch. Add both opcode checks. The fixture emits one branch to its first IR block and a second from that block to the merge.

Suggested fix
   Check(!SpirvHasDegenerateBranchConditional(spirv),
         "SPIR-V has OpBranchConditional with identical targets");
+  Check(!SpirvContainsOpcode(spirv, 250),
+        "identical-target branch was not lowered to OpBranch");
+  Check(SpirvInstructionOpcodeCount(spirv, 249) >= 2,
+        "identical-target branch did not emit an OpBranch to the shared target");

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5740ae0f-544f-43f1-88ce-f18d05f5cd0c
📥 Commits

Reviewing files that changed from the base of the PR and between e7f6327068 and 3e1d888458.

📒 Files selected for processing (2)
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterProgram.cpp
  • tests/shaderCfgTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-03 17:28 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1012#pullrequestreview-5401869487) · **reviewed** <details> <summary>🧹 Nitpick comments (1)</summary><blockquote> <details> <summary>tests/shaderCfgTests.cpp (1)</summary><blockquote> `7839-7840`: _📐 Maintainability & Code Quality_ | _🔵 Trivial_ | _⚡ Quick win_ **Assert that the direct branch is present.** The current check rejects only a same-target `OpBranchConditional`; it does not assert that the emitter produced the required `OpBranch`. Add both opcode checks. The fixture emits one branch to its first IR block and a second from that block to the merge. <details> <summary>Suggested fix</summary> ```diff Check(!SpirvHasDegenerateBranchConditional(spirv), "SPIR-V has OpBranchConditional with identical targets"); + Check(!SpirvContainsOpcode(spirv, 250), + "identical-target branch was not lowered to OpBranch"); + Check(SpirvInstructionOpcodeCount(spirv, 249) >= 2, + "identical-target branch did not emit an OpBranch to the shared target"); ``` </details> <!-- cr-comment:v1:b0e959002696339cb39344c0 --> </blockquote></details> </blockquote></details> --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `5740ae0f-544f-43f1-88ce-f18d05f5cd0c` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between e7f6327068ff565d42b55641a11c30a6be5a01f6 and 3e1d888458d4bc770243e7521587717e9ff40a0d. </details> <details> <summary>📒 Files selected for processing (2)</summary> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterProgram.cpp` * `tests/shaderCfgTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
Author
Owner

@⁠wuboo on GitHub · 2026-10-03 19:05 UTC · original

Added in ca04dbdb: the test now also checks that no OpBranchConditional (250) is emitted and that there are at least two OpBranch (249) — the entry->merge edge and the lowered conditional. Checked against the fixture; with the emitter fix reverted the test fails.

**@⁠wuboo** on GitHub · 2026-10-03 19:05 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1012#issuecomment-5972532758) Added in ca04dbdb: the test now also checks that no OpBranchConditional (250) is emitted and that there are at least two OpBranch (249) — the entry->merge edge and the lowered conditional. Checked against the fixture; with the emitter fix reverted the test fails.
This pull request has changes conflicting with the target branch.
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterProgram.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1012/head:github-bot/gh-1012'
git switch 'github-bot/gh-1012'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1012'
git switch 'github-bot/gh-1012'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1012'
git switch 'github-bot/gh-1012'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1012'
git switch 'main'
git merge --squash 'github-bot/gh-1012'
git switch 'main'
git merge --ff-only 'github-bot/gh-1012'
git switch 'main'
git merge 'github-bot/gh-1012'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.