Repository navigation
fix(shader): throw for v_readlane_b32 of a register-selected lane on one-lane waves - #14068
Conversation
…one-lane waves Vertex and local programs on a device without vertex subgroups run as one-lane waves, where a shuffle returns the invocation's own value. v_readlane_b32 checked its lane only when a constant named it: a lane held in an SGPR read the invocation's value whatever lane it named, where the console reads that lane's VGPR (RDNA 2 ISA 12.12, V_READLANE_B32: the lane is S1 % 64 in wave64). On a one-lane wave a register-selected lane must now be the s_ff1 of a mask built from EXEC and ballots, as in a waterfall loop. Such a mask holds no lane but the invocation's own, so the read is its own value; the -1 of an empty mask (RDNA 2 ISA 12.3, S_FF1_I32_B64) reads it too. Any other selector throws through FailOutsideHostSubgroup. Accepted programs emit the same SPIR-V as before. The one-lane rules leave the vertex entry debt line for an entry of their own, which also lists the cross-lane reads still unchecked there: DPP moves, ds_swizzle_b32, ds_bpermute_b32 and v_permlane16_b32.
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
I found an unsound acceptance in the new register-selector proof: it can treat a lane-63 read as a lane-0 read on a one-lane host.
What I ran
Reviewed head 5e086eaac896fb794a8a59c947e3427ca6ebde43 against base 0f302813cebfc09e3e0b6f8fced15262d186879e; the merge preview is clean. Environment: Debian Linux 6.1, Python 3.11.2, GCC 12.2.0; I did not build or run CTest.
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14068 --output-dir /tmp/anyps5-14068-review
git diff --check 0f302813cebfc09e3e0b6f8fced15262d186879e...5e086eaac896fb794a8a59c947e3427ca6ebde43
python3 tools/check_conventions.py --base 0f302813cebfc09e3e0b6f8fced15262d186879e --head 5e086eaac896fb794a8a59c947e3427ca6ebde43 --title "fix(shader): throw for v_readlane_b32 of a register-selected lane on one-lane waves" --body /tmp/anyps5-14068-review/pr-body.md
python3 tools/pr_stack_check.py 14068 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-14068-review/stackResults
| Check | Result |
|---|---|
| Patch and conventions | Passed. |
| Dependency and merge preview | Passed; no declared dependencies and a clean preview against current main. |
| Register-selector proof | Failed source review: FindILsb32 is accepted when its proven-own-word is zero, and the SelectU32 fast path does not validate its condition. Both can admit a selector of 0xffffffff, which becomes lane 63. |
| Author-reported focused tests | The PR description reports 34 CTest entries passing three times on the branch and a prior main ancestor, plus validation-layer runs; I did not independently run these. The listed negative control removes the guard, so it does not cover either false-acceptance case below. |
| Hosted CI | Windows and relinker jobs passed; Linux was skipped. These results do not exercise the focused shader test reported by the author. |
Raw outputs and source excerpts
git diff --check output (empty):
Conventions output:
All conventions checks passed.
Stack report:
- PR #14068: **clean** against current base; head `5e086eaac896fb794a8a59c947e3427ca6ebde43`
No declared PR dependencies were found in the target PR chain.
Negative control
| Assertion | Mutation or input | Did the new test fail? |
|---|---|---|
| A one-lane host must not silently substitute lane 0 when the guest selector can be lane 63. | FindILsb32(0) as the v_readlane_b32 selector, or SelectU32(unrelated_false, FindILsb32(1), 0xffffffff). |
Not exercised by the added tests; not run locally. Both shapes pass the current source proof. |
Findings
Blocking
selector() accepts FindILsb32 whenever ownWord() proves the input contains no bits except bit 0, but ownWord() also accepts zero. The ISA’s s_ff1 result for an empty word is -1; EmitReadLane masks the selector with waveSize - 1, making it lane 63 for wave64. The one-lane host implementation then copies the current value regardless of the selector. A guest lane 0 that remains active while a separate ballot-derived mask is empty can therefore read lane 63 on the console but receive lane 0’s value here.
The SelectU32 fast path has a second gap: it checks that the true branch is FindILsb32 and the false branch matches noLane(), but it never proves the select condition tests that same low word. For example, an unrelated false condition can select 0xffffffff while the low mask contains lane 0. Please restrict acceptance to selector expressions that prove lane 0 for every executing invocation; in particular, prove non-emptiness for FindILsb32 and validate the select condition and its relation to the branches. Add regression cases for an empty low mask with lane 0 still active and for an unrelated condition selecting the -1 branch.
Non-blocking
None.
Sources
- Selector proof and empty-word acceptance at this head
- Read-lane masking and shuffle at this head
- Single-lane shuffle copies its input at the current base
- AMD RDNA 2 ISA:
S_FF1_I32_B32returns -1 for a zero source
Not verified
No build, CTest run, GPU run, or console run was performed in this review. The test and validation results in the PR description are author-reported.
To get this to a good state
Make the selector proof fail closed for these cases and add the two regression cases above. I will rerun the same source and no-build checks on the new head.
✅ NVIDIA GPU test: passedPR head
Failing on main too (4, not counted against this PR)Validation IDs already present on main (3 IDs in 387 tests)Skipped (48)ReproduceAutomated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
|
@frankea the refusal itself is the right call and the walk reads sound at The negative side of the walk is only pinned for immediates and |
…ane_b32 lane on one-lane waves The selector proof accepted the s_ff1 of a mask built from EXEC and ballots whenever that mask held no lane but the invocation's, empty masks included. s_ff1 of an empty mask is -1 (RDNA 2 ISA 12.3, S_FF1_I32_B64), which v_readlane_b32 reads as lane 63, the read a constant lane 63 already refuses. It also took Select(c, FindILsb32(mask), -1) without checking that c tests that mask. The mask must now hold the invocation's bit wherever the read runs: it is the entry EXEC, or a branch on the way to the read tested it for zero, which covers the back edge of an s_ff1 waterfall and a guard before one. The select must test mask != 0. Selects and phis of selectors are no longer accepted. New cases: a waterfall over an unguarded compare result and an s_cselect_b32 of s_ff1_i32_b32 of EXEC or -1 throw; the same waterfall behind s_cbranch_scc0 and a plain s_ff1_i32_b32 of EXEC build. Both rejected cases build with the previous selector proof.
…e ds_bpermute_b32 address The review of boykopovar#14072 found the sum and OR rules of the address proof unpinned: weakening either let a ds_bpermute_b32 whose address added the vertex index to a lane-0 term build and read the invocation's own value. Guest code reaches neither rule. Its lane id comes from v_mbcnt as a BitCount, which the proof never accepts, so the only register address it needs is the one the translator builds for image_sample_*_cd. The proof now accepts just that: the lane id, an AND with it, or the quad's first active lane, shifted left by 2. Sums, ORs, selects and offsets throw. Four new cases pin this: the v_mbcnt lane id plus the vertex index, the vertex index plus 32, the vertex index OR 32, and the vertex index at offset 128. Merged hull programs never reach these checks, because TessellationLowering's requireOwnLanes already throws for every cross-lane opcode before emission. The lane id is therefore accepted in every stage, and the v_readlane_b32 call site is back to boykopovar#14068's.
|
@MarcZX10 Thanks, both cases are real. I pushed
New cases in
On the RTX 4090, the 34 ctest entries listed in the description pass 3 times at |
MarcZX10
left a comment
There was a problem hiding this comment.
Re-checked — new head, looks good
The updated selector walk and added negative cases address the prior test-coverage concern, and I found no blocking issue in the exact-head re-review.
What I ran
Reviewed head 283e3e773aecc5d9d744b223ff2d11331b11dd7e against PR base 0f302813cebfc09e3e0b6f8fced15262d186879e; the no-build precheck recorded a clean merge preview against current main ca073ec3ad0d766ea9f4658e418a5c977001a353. I inspected the selector proof, its control-flow and mask analysis, and the added verifySingleLaneVertexWave cases. I did not build or run the C++ tests locally.
python3 /root/.codex/skills/anyps5-pr-review/scripts/pr_snapshot.py 14068 --repo boykopovar/AnyPS5
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14068 --output-dir /tmp/anyps5-14068-review
gh api repos/boykopovar/AnyPS5/commits/283e3e773aecc5d9d744b223ff2d11331b11dd7e/check-runsResults
| Check | Result |
|---|---|
| Diff and conventions | Passed |
| Current-main merge preview | Clean |
| Hosted Windows product job | Passed |
| Hosted Ubuntu and Windows relinker jobs; ASan | Passed |
| Author-reported focused Windows and macOS tests | Passed, including the new refusal cases and reported negative controls; not independently reproduced |
| Hosted Linux job | Skipped |
| Hosted macOS relinker jobs | Queued at review time |
Findings
Blocking
None found in this re-review.
Non-blocking
The host-side tests on Windows and macOS are author-reported; I did not independently reproduce them. The queued macOS relinker jobs and skipped Linux product job do not change the reviewed selector proof or the reported focused test evidence.
Sources
Not verified
I did not run a local build, Linux runtime or GPU test, or console behavior. The repository's hosted Linux product job was skipped.
✅ NVIDIA GPU test: passedPR head
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (4, not counted against this PR)Validation IDs already present on main (3 IDs in 387 tests)Skipped (48)ReproduceAutomated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
…-register-selector # Conflicts: # docs/dev/debt/shader.md
Tessellation evaluation programs without subgroups now take the one-lane path from boykopovar#13807, so verifySingleLaneDomainWave checks that a v_readlane_b32 at the lane v_readfirstlane_b32 takes from v5 throws there and builds on a 32-lane tessellation evaluation subgroup.
|
Linux / AMD RDNA 4 (RADV) paired validation run for current head Host & GPU: CachyOS Linux ( Environment:
AI-assisted hardware test run (Antigravity). |
✅ NVIDIA GPU test: passedPR head
Unstable on main (2, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (3, not counted against this PR)Validation IDs already present on main (5 IDs in 480 tests)Skipped (44)ReproduceAutomated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
|
Hi @frankea, following up on the earlier request about the negative side of the walk. The new head
Only dropping the dominating-branch proof in |
…with a word that is not the invocation's own
|
@oneandonlydean thanks, each of those rules now has a rejected case, in
The I weakened each rule in turn (the AND accepting any word, |
✅ NVIDIA GPU test: passedPR head
Failing on main too (4, not counted against this PR)Validation IDs already present on main (9 IDs in 587 tests)Skipped (52)ReproduceAutomated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
|
The RTX 4090 rerun is done: on |
What
On a device whose
subgroupSupportedStageslacks the vertex stage (lavapipe, MoltenVK), vertex and local programs run as one-lane waves (#4470), where a shuffle returns the invocation's own value.EmitReadLanechecked the lane of av_readlane_b32only when a constant named it. A lane held in an SGPR passedReadLaneEliminator, which skips non-immediate selectors, and reachedEmitSubgroupShuffle, whose one-lane branch copies the invocation's value whatever lane is named. The console reads that lane's VGPR: the lane isS1 % 64in wave64 (RDNA 2 ISA, 12.12, V_READLANE_B32, page 183). The review of #13807, which puts tessellation evaluation programs on the same path, pointed this out; this is the follow-up I said I would open there.HostSubgroupNarrowerThanWavewith a host width of 1),EmitReadLaneaccepts a register-selected lane only whenOwnLaneSelectorWalkproves it is the invocation's own. The selector must be theFindILsb32ofs_ff1_i32_b32, or thes_ff1_i32_b64lowering ofsFfI32B64(Select(lo != 0, FindILsb32(lo), Select(hi != 0, FindILsb32(hi) + 32, -1))) with its condition testing that samelo. The mask must be built from EXEC and ballots: a ballot word, 0 (or 1 in its low word), an AND with such a word, ORs, XORs and selects of such words, or a phi of them, which coverss_mov_b64from EXEC,s_and_b64with a compare result,s_bitset0_b64and the loop phis of a waterfall. On one lane a ballot is the invocation's own bit, so such a mask holds at most bit 0 in its low word and nothing in its high word. The mask must also hold that bit wherever the read runs: it is the entry EXEC (Ballot(true)), or a branch on the way to the read tested it for zero. The walk follows unique predecessors up from the read and the incoming edges of phis, and takes each conditional branch's condition fromBlockInfo:LogicalNot,LogicalAndandLogicalOrof tests, and!= 0or== 0of the word, ofword | highor of the 64-bit pair, whose high word is zero on one lane. That covers the back edge of ans_ff1waterfall (s_cmp_lg_u64,s_cbranch_scc1) and a guard before one (s_cbranch_scc0on the SCC ofs_and_b64).s_ff1of an empty mask is -1 (RDNA 2 ISA, 12.3, S_FF1_I32_B64, page 114), which reads lane 63 in wave64, so a mask that may be empty throws, as a constant lane 63 does. Any other selector throws throughFailOutsideHostSubgroup:v_readlane_b32 of a register-selected lane is outside the 1-lane host subgroup that runs this wave64 program at one lane per invocation. Accepted programs emit the same SPIR-V as before, and devices with vertex subgroups are not affected.AppendConsumeshuffles from the first active lane, which on one lane is the invocation's own, so it needs no check.EmitDsMaskedLaneRead(ds_swizzle_b32,ds_bpermute_b32), DPP moves andv_permlane16_b32are not checked here: on one lane they read another lane as inactive (0 or the old value), or with fetch-inactive as the invocation's value. The debt entry lists them; they are a separate change.docs/dev/debt/shader.mdwas 1,254 characters, over the 1,000 thatcheck_conventions.pyallows for a changed entry, so its one-lane rules move to a new "One-lane vertex waves" entry, which now states thev_readlane_b32rule and the unchecked shuffles. Thes3layout stays in the old entry.mainnow, so tessellation evaluation programs without subgroups take the one-lane path and this check with no further code. The merge ofmaininto this branch drops the sentence of its debt entry that said a register-selected lane is not checked, andverifySingleLaneDomainWavegets the same register-selector case as the vertex test.Title: none. In my Gran Turismo 7 (PPSA01317) shader disk cache from MoltenVK runs on an Apple M5 Max (16,739 entries), the 56 entries (7 distinct programs) that hold a
v_readlane_b32with an SGPR lane are all compute programs (thread ids fromv_lshl_add_u32 vN, sM, 6, v0, no exports), so none of the vertex or tessellation evaluation programs GT7 reached there starts throwing.Tested
Windows 11 on an RTX 4090 (driver 616.92, Vulkan 1.4.341), winlibs GCC 15.2.0, Release with
BUILD_TESTING=ON. The 4090 has vertex subgroups, so its draws keep the subgroup path; the one-lane path runs there only inverifySingleLaneVertexWave, which compiles for explicit targets.c999c741ac, withmain029463c merged in) and on thatmain:request_serializer, the 21 entries ofagc_shader_memory_tests,agc_driver_read_lane,agc_driver_write_lane,agc_driver_lds_mask_waterfall,agc_driver_exec_host_lanes,agc_driver_ngg_vertex_wave_reduction,agc_driver_pixel_wave_reduction,agc_driver_pixel_row_reduction,agc_driver_wave32_subgroup,agc_driver_window_lane_spill,agc_driver_scalar_sop1,agc_driver_tessellation,agc_driver_tessellation_depth_range_remap,agc_tessellation_pipeline_cache,agc_tessellation_factor_ring,agc_tessellation_loweringandagc_driver_vertex_elected_atomic_max(skipped: the 4090 has vertex subgroups). They passed the same way ondcd3021fea, and before the merge, the first 34 of them passed the same way on283e3e773aand5e086eaac8againstmaine8c25dd. They pass once more underVK_LAYER_KHRONOS_validationwithANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1(ctest -V) on both, with noVUID-lines and nowas not found but was requested by env var VK_INSTANCE_LAYERS; the loader log foragc_driver_read_lanein that environment shows the layer inserted.Apple M5 Max, macOS 27.0.1, x86-64 under Rosetta 2:
request_serializerand the 21agc_shader_memory_testsentries pass 3 times in a row on this branch (dcd3021fea), built natively, andagc_shader_memory_testspasses 3 times in a row onc999c741ac. These compile for explicit targets and run no GPU work. One local build aid that is not part of this pull request was needed for the build: theGuestMemory.cpppart of #11447.New checks, in
verifySingleLaneVertexWave(tests/ShaderMemory.cpp):v_readlane_b32ofv5(gl_VertexIndex) at the lanev_readfirstlane_b32takes fromv5throws without vertex subgroups and builds on a 32-lane vertex subgroup.s_ff1_i32_b64waterfall overs_mov_b64 s[0:1], -1instead of EXEC throws without vertex subgroups.s_ff1_i32_b64waterfall that readsv5overexec & vccafterv_cmp_gt_u32throws without vertex subgroups, since it can enter with an empty mask; behinds_cbranch_scc0on the SCC of thats_and_b64it builds without subgroup operations and still uses them with vertex subgroups, as the existing waterfall over EXEC and a plains_ff1_i32_b32of EXEC do.s_cselect_b32that pickss_ff1_i32_b32of EXEC or -1 ons_cmp_eq_u32ofv_readfirstlane_b32of the vertex index throws without vertex subgroups.verifySingleLaneDomainWave, av_readlane_b32ofv5at the lanev_readfirstlane_b32takes fromv5throws without tessellation evaluation subgroups and builds on a 32-lane tessellation evaluation subgroup; the existing domain waterfall over EXEC still builds without them.s_ff1reads throws without vertex subgroups when a ballot word is combined with a wordv_readfirstlane_b32took from the vertex index, one case per rule of the walk: an AND of two such words, EXEC ORed, XORed ors_cselect_b32-selected with one, each tested for zero first; EXEC ANDed with one, ands_cselect_b32of EXEC or 0, both untested; an OR of two compare results; a compare result with EXEC in the high word, tested bys_cmp_lg_u64and by the SCC ofs_and_b64; a compare result read only whens_cmp_lg_u32found it zero; and EXEC behind ans_cselect_b32on a test of another word or of that copy against 0. A compare result tested for zero, a copy of EXEC, and a copy of EXEC ORed with a compare result build without subgroup operations.&&changed to||for OR, XOR or select of mask words, or for AND or select of words that must hold the invocation's bit, an OR holding that bit accepting any words, any ballot word index accepted in the high word, or the dominating-branch test accepted without a branch), the test fails at the case for that rule.a vertex program without vertex subgroups read the lane v_readfirstlane_b32 took from the vertex index. With the selector proof of5e086eaac8, it fails on the unguarded waterfall and, with that case taken out, on thes_cselect_b32case.tools/check_conventions.py --base origin/mainpasses.main029463c is merged into the branch; its only conflict was the debt entries next to #13807's.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)