spirv: guard pixel shader cross-lane reads from lanes the host subgroup lacks #1515
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1515
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1515/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@overlordxrz-source on GitHub · 2026-10-14 19:35 UTC · original
overlordxrz-source:nhl-ps-cross-lane-missing-lanes→main· +128 −5 · 4 files · 1 commits · closed without mergingFixes the NHL 27 (PPSA34063, #1101) GPU hang in PS
0x1da1fd2871174330. On fozter's RTX 3060 Ti it ends as a device loss about 15 s into the loading screen after START GAME:That line is
waitSemaphores(..., UINT64_MAX)inMasterSemaphore::Wait. With an infinite timeout, its only non-success results are out-of-memory orVK_ERROR_DEVICE_LOST. The game's ownSubmissionManagerPs5::waitForFrametimeouts (0x8002003C) come right before it in the log. fozter's builds with VK_NV_device_diagnostic_checkpoints and VK_EXT_device_fault stop at the same draw every time: RectListDrawIndexAuto, 3 vertices × 3108 instances, VS0x126d611593f8bf08+ PS0x1da1fd2871174330. The fault has instruction pointers only, so it's a shader hang.Cause. The PS is wave64. It walks a per-tile light list with the WaveActiveMin idiom (
s_orn2_saveexec,v_cndmask-1, DPProw_shrv_min_u32with bound_ctrl=0,v_permlanex16,v_readlane31 and 63). It loops until every lane has consumed its list. The host subgroup is a fixed 32 wide (size_control=false), so the PS runs without split halves, and two things go wrong:v_readlane63 names a lane the subgroup doesn't have, so the shuffle is undefined.The min then picks a value no lane holds, no lane advances, and the loop never ends.
Change (spirvEmitterFlow.cpp, spirvEmitterHelpers.cpp):
V_READLANE_B32in a wave64 program without split halves wraps the lane bygl_SubgroupSize. This is a no-op on 64-wide subgroups.EmitSubgroupSizedeclares the builtin on first use (Flat in pixel shaders).ballot(true).v_permlane16/v_permlanex16return the lane's own value, and read the source EXEC bit from a ballot instead of shuffling it.v_readlanereads the highest lane present.The fallbacks are identities for min/max scans: an unwritten DPP destination keeps the running min/max, and
min(x, x) = x. They aren't a general answer. The own-value fallback in permlane would double-count an add reduction. The presence check reuses main'sEmitBallotLaneActiveBoolonctx.Ballot(true).EmitSubgroupLaneActiveBoolisn't used, because it ballots only the host subgroup, so with split wave64 halves it would report lanes 32-63 as missing.Which part fixes the hang. fozter bisected his original patch in the game. Each variant was a full rebuild, run to the first gameplay frame with the device-fault diagnostics on:
This PR is the "readlane + PS missing-lane handling" row. The DPP gate (no write from an EXEC-inactive source with FI=0/BOUND_CTRL=0) matches the ISA, but it doesn't stop this hang, so I left it out. I'm keeping it for a separate PR with its own test.
Test.
CheckPixelCrossLaneMissingLanesin ShaderRecompilerComputeTests.cpp compiles a wave64 pixel shader withv_mov_b32_dpp row_shr:1,v_permlanex16_b32andv_readlane_b32 s4, v1, 63. It checks:CompileFragmentCase)BuiltIn SubgroupSizeOpGroupNonUniformBallotFindMSBand at least fiveOpGroupNonUniformBallotVectorReadlaneSelectsTwoKeysWithinWave) gets no FindMSBIt doesn't need a device.
Related open PRs:
e1580f9bb"spirv: DPP16 leaves the destination alone for missing or inactive sources", plus a V_READLANE fallback (highest active lane at or below the target in its 16-lane row, else 0) and native reductions for DPP scans. It applies to all stages. This PR is narrower: pixel shaders only, plus the wave64 wrap, and it's the version tested on NHL. They conflict textually in spirvEmitterFlow.cpp.ctx.Ballotexclude helper invocations in pixel shaders. With both merged, helpers would count as missing lanes here, which is consistent with #761's model, but that combination is untested. #761 also realigns theEmitterStatefields, so the field this PR adds conflicts textually in spirvEmitterInternal.h.Testing
df5b9b92+ #1446), as part of his larger patch.TranslateProgram/CompileProgramin a native harness and ran spirv-val (SPIRV-Tools at the pinned submodule, Vulkan 1.2 env):SubgroupSizedeclared, 1 FindMSB, 5 ballots.SubgroupSize, 0 FindMSB, 1 ballot, so the new test fails on main.ballot(true)between the presence check and FindMSB, which gives the same values.-Wall -Wextra) of spirvEmitterFlow.cpp and spirvEmitterHelpers.cpp: no new warnings. The test file builds with-fsyntax-only.shader_recompiler_compute_testsbinary (it needs a Vulkan device), and nothing on MoltenVK.threads_per_simdgroupshould be allowed in Metal fragment functions (vertex functions reject it, see #983), but I haven't tried it.fozter found the hang, wrote the patch and did the bisect (#1101). He's said others may use his findings (#1101), so he's the commit author. I split out the part his bisect shows is needed, rebased it onto current main, reused the existing ballot helper, ran clang-format and added the test.
AI-assisted: yes. fozter's investigation, patch and bisect were done with Claude. I used AI to split and rebase the patch, write the test and the harness, and draft this text. I reviewed the change and ran the checks above.
@coderabbitai[bot] on GitHub · 2026-10-14 19:43 UTC · original
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
c12ad26a-05d9-46dc-b63a-1a1d598ad1d3📥 Commits
Reviewing files that changed from the base of the PR and between
9e41d8320aand6ab5269936.📒 Files selected for processing: 4
src/graphics/shader/recompiler/backend/spirv/spirvEmitterFlow.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterHelpers.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
The SPIR-V emitter now handles missing host-subgroup lanes in pixel-shader DPP updates, lane reads, and permutes. It adds a cached subgroup-size input. Non-pixel shader paths retain their existing behavior. A regression test checks emitted SPIR-V patterns for pixel shaders and verifies that the missing-lane fallback is absent for a compute shader.
Suggested reviewers:
nmzikPriority: ⬆️ High
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
6ab52No confirmed issue currently prevents merging, subject to normal validation.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 3 files. (1 skipped: 1 too large.)
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 helpto get the list of available commands.@Nawid3333 on GitHub · 2026-10-15 22:17 UTC · original
I ran the idiom from this PR's description as a GPU test on an AMD RX 9070 XT (Windows 11, AMD driver 26.8.1, LLPC, subgroup size 64), on
9e41d832(plus #1504's test-build fix) and on the same tree with this PR's commit6ab52699cherry-picked (it applied cleanly):The test draws into a 1x1 RGBA32F target, so the host fragment subgroup has a single invocation. On a console all 64 lanes of the wave exist:
s_orn2_saveexecturns them all on, and thev_cndmaskgives the lanes that weren't launched -1.row_shrscan leaves each 16-lane row's minimum in its lane 15.v_permlanex16with -1/-1 passes that value to the other row of the same half.So lanes 31 and 63 hold the minimum of their halves, and the result is X.
9e41d832, 5 runsSo on main, reads of lanes the host subgroup doesn't have return undefined values on AMD too, not only on 32-lane NVIDIA, when a wave64 pixel shader runs with fewer invocations than the guest wave has lanes (here a single pixel). With this PR the result here is correct and stable. The SPIR-V on main has no
SubgroupSizeand noOpGroupNonUniformBallotFindMSB; with the PR it hasSubgroupSizeand twoOpGroupNonUniformBallotFindMSB.The PR's own test checks only the disassembly, and CI doesn't run
shader_recompiler_compute_tests, so I thought a GPU run on a non-NVIDIA card would help. A few notes from reading the change (not measured):MayLackLanes), so vertex, hull and domain shaders and partial compute waves still read missing lanes as undefined; only the readlane wrap applies to every wave64 stage. #1503 handles the same idiom family for OR with a proof of which lanes were launched; extended to min/max/add, that approach would also cover these cases.Written with Claude Code (AI); the runs above were made on my PC with Claude Code, and I checked the raw outputs.
@overlordxrz-source on GitHub · 2026-10-16 18:52 UTC · original
@Nawid3333 thanks for running this on a 9070 XT. That shows the problem isn't NVIDIA-only. You're right on the limits: a lane missing mid-row still cuts the row_shr scan, and the fallbacks are only right for min/max. #1503's launched-lane proof, extended to min/max/add, would be the general fix. I'd rather keep this one narrow and fold it into that if Nmzik prefers.
AI-assisted: yes
@Nawid3333 on GitHub · 2026-10-16 19:15 UTC · original
Agreed, keeping this one narrow makes sense. For the general fix: I measured the add form of the same idiom at
5dec5231, which includes nmzik's302240da(it lowers only the UMin form), on an RX 9070 XT (driver 26.8.1, LLPC). A pixel shader counts its wave withv_readlane31 + 63 (and a row_xmask variant with lanes 0 + 32); the expected count iss_bcnt1of the entry EXEC.With 32 lanes (
requiredSubgroupSize32), LLPC returns lane 31 or 0 for a read of lane 63 or 32, so each half is counted twice. With a half-filled wave at 64 lanes those reads return stale values. DPP,v_permlanex16and constant-lanev_writelane/v_readlane(folded at compile time) were correct. Extending the matching of302240daand #1503 to add, the other min/max and and/or/xor, and to the row_xmask + lanes 0/32 shape should cover these (not tried). The tests are below; on main they fail on this GPU in the two half-wave cases, and with 32 lanes in all four.Tests: tests/ShaderRecompilerComputeTests.cpp, applies to
5dec5231Written with Claude Code (AI): it wrote the tests and ran them on my PC. I read this comment before posting it.
Pull request closed