Shaders: lower waterfall loops over per-lane descriptors (and s_cselect-chosen descriptors) #307
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!307
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/307/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?
@AntoninMichelq on GitHub · 2026-11-24 02:13 UTC · original
AntoninMichelq:up/waterfall-descriptors→main· +1133 −14 · 5 files · 4 commits · merged 2026-11-24 09:55 UTCRequested by 4nto · project thread
Summary
Descriptors that a waterfall loop reads lane by lane are now traced and lowered, so the loop runs once with every lane using its own descriptor. Before this, tracing stopped with "a descriptor neither loaded nor built" and the whole program was dropped, along with every draw that used it. The branch also traces descriptors that are chosen without a branch, by
s_cselect/s_cmov.What changed
Two commits, both based on main:
Trace descriptors chosen by
s_cselectands_cmov. The compiler sometimes hoists both descriptor loads and replaces the if/else withs_cselect_b64per half, ors_cselect_b32/s_cmovper dword. Those words are now split by the SCC value they read, and each side is traced into a newSource::Select. Lowering picks the heap index of the two loaded descriptors by SCC and creates one heap handle. A choice that mixes two descriptors still stops, with "a descriptor changed after loading".Lower waterfall loops over per-lane descriptors in one pass. The
v_readfirstlane_b32reads of a VGPR descriptor are followed to where each lane gets its own dwords:global_load_dword*from a table or heap at a per-lane address, orv_cndmask_b32per-lane choice between such sources.Other sources fail and name what they are (LDS, buffer load, phi, input, computed VGPRs). The access gets each lane's heap index, with
createHandleFromHeap'snonUniformIndexset. The recorded readfirstlanes are lowered as the lane's own dword and theirv_cmp_eqas true.docs/architecture.mdis updated.Adapted to main:
lower_with/lower_wave/roottoemit::lower::tests.paramanddynamiconly. Theincludes_basefield it also compared is not on main.How it was tested
cargo test -p victory-shaderon Linux: all tests pass. New tests:resource::tests::descriptors_selected_by_scc_are_traced_per_outcomeresource::tests::a_selection_that_mixes_two_descriptors_failsresource::tests::descriptors_read_from_the_first_lane_of_per_lane_loads_are_traced_to_themresource::tests::descriptors_read_from_the_first_lane_of_a_per_lane_choice_are_traced_per_laneresource::tests::descriptors_read_from_the_first_lane_of_lds_fail_naming_itemit::lower::tests::a_descriptor_selected_by_scc_picks_its_heap_indexemit::lower::tests::waterfall_loops_over_per_lane_descriptors_sample_with_each_lanes_own(Wave32 and Wave64)cargo check -p victory-shader --target x86_64-pc-windows-msvcpasses.victory translateon the integration stack.Where 3D rendering stands on an RX 7600 (Windows 11)
These observations come from the integration stack (upstream main plus many open PRs), not from this branch alone.
🤖 Generated with Claude Code
https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
A descriptor held in VGPRs is used through a waterfall loop: each pass reads the first active lane's dwords with v_readfirstlane_b32, compares them with every lane's own (v_cmp_eq), runs the access with the lanes that match and repeats for the rest. Tracing stopped at the readfirstlane results ("a descriptor neither loaded nor built"). The tracer now follows the read lanes' dwords to where each lane gets its own: global_load_dword* from a table or heap at a per-lane address (an image descriptor's two x4 loads lined up), or a v_cndmask_b32 choice per lane between such sources (e.g. two s_load descriptors). Other sources fail naming them (LDS read, buffer load, phi, input, computed VGPRs). The loop's compares of those reads with their own dwords are recorded. Lowering gives the access each lane's heap index (its load address over the descriptor size, or a per-lane select of indices) with createHandleFromHeap's nonUniformIndex set, lowers the recorded readfirstlanes as the lane's own dword and their compares as true, so the loop runs once with all lanes. Wave32 and wave64 are tested. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66@ifBars on GitHub · 2026-11-24 02:22 UTC · original
I reproduced a case where the rewrite changes a first-lane selection that is outside a waterfall loop.
Starting from
WATERFALL_FETCHED_WAVE32, replace its only back edge,0xBF89_FFE6(s_cbranch_execnz), with0xBF80_0000(s_nop). The result is a single conditional sample. Only lanes whose descriptor matches the first active lane reach the sample, because the program never loops over the remaining lanes. On this patch,lower_wavestill reportsnonuniform_handles == 1andwave_ops == false. The first-lane reads become per-lane values, and the equality checks become true, so the emitted program samples for lanes the original program skipped.This seems to come from
Tracer::waterfallmatching the read and comparison operands without checking that they belong to a loop that removes the matched lanes and returns for the rest. The modified fixture would make a negative regression test. It should preserve the first-lane selection and EXEC gating or reject that form explicitly. If the first-lane values can have other uses, those also need to keep their original semantics.This was reproduced with the CPU lowering fixture on Windows. No game files or GPU were used for the counterexample. AI-assisted (Codex).
@tanishq-dubey on GitHub · 2026-11-24 02:33 UTC · original
Independent local check of head
ffa663571fd0a14b9beb0889d6c48468010601fbon the high-impact mesh shader910b4be059bfb28ffrom issue #293: main reportsa descriptor neither loaded nor builtat0x2830; this PR reports the more specifica descriptor read from the first lane of a phi is not supportedat the same offset. The shader still does not translate on this branch, consistent with the stated phi limitation. No game files or disassembly attached. AI-assisted.@r4tedx on GitHub · 2026-11-24 03:08 UTC · original
Review note from building this on current
main(d2a4bae), RTX 5070:cargo test -p victory-shaderpasses, but the CLI no longer builds, so a tester cannot producevictory.exefrom this branch as it is:tally_resourcesmatchesSourceexhaustively. CountingFetchedlikeLoaded, and handlingSelect/Lanes(not leaves) explicitly, fixes it;cargo check -p victorythen passes here.A smaller question:
classifytreats any descriptor read throughv_readfirstlaneas per-lane, also whenwaterfall()finds no matchingv_cmp_eq. For a plain "first lane's index" that differs between lanes, the console uses the first lane's descriptor for the whole wave. Would requiring the compare be safer?This is the PR aimed at the second row of #293 (
a descriptor neither loaded nor built, about 462 draws per frame), so it is worth getting into a tester's hands. From building and reading only; I have no copy of the title.@SpaghettiMaster545 on GitHub · 2026-11-24 03:11 UTC · original
cargo build -p victoryfails on this PR's headffa6635(based2a4bae, Rust 1.98.1 pinned, Windows 11), in both the release and the diag (--profile diag --features diagnostics) builds:The new
Sourcevariants fromcrates/victory-shader/src/resource.rsare not handled in thatmatchin thevictorybinary, so the PR could not be run on the title here.cargo test -p victory-shaderalone would not show it, since the binary crate is not built.(Claude Code, Claude Opus 5.5: hit this building the PR to test it on the title, and wrote this.)
@miniongk on GitHub · 2026-11-24 03:53 UTC · original
Tested
077ec8bcombined with #320 (a5ba4a4) on an RTX 5080, driver 621.24, Windows 11 26300, using a local Xbox Series 1.0.2.1 copy.The merge needed two compatibility adjustments: keep the waterfall
v_readfirstlanecase before the console-flow case, and add the new target/console-flow fields to the #307 test helper. With those changes,cargo test --workspacepassed (288 passed, 0 failed, 4 ignored).On the native title path, the
a descriptor neither loaded nor builtclass fell from 16 distinct shaders with #320 alone to 2 with this combination; host-pipeline refusals stayed at zero. The run still ended inDXGI_ERROR_DEVICE_HUNG, with DRED implicating indirect execution and mesh dispatch. So this narrows descriptor support as intended, but does not by itself resolve #97 here.Only aggregate counts and error classes shared, no game data or raw game logs. AI-assisted (Kimi Code).
@EduAzevedo on GitHub · 2026-11-24 04:03 UTC · original
Corpus-wide numbers for what was seen on one shader above:
077ec8b, retail shaders (v1.0.2.1), offlinevictory translate --listagainstmaind2a4bae, all 26,148 programs.a descriptor neither loaded nor built: 298 → 3.a descriptor read from the first lane of a phi, so only one more program translates on this branch alone.mainstops translating.So nearly all of what this PR unlocks goes through that phi case, which matches the #320 combination results above.
AI-assisted.
@r4tedx on GitHub · 2026-11-24 04:26 UTC · original
@AntoninMichelq are you working on
a descriptor read from the first lane of a phi? Going by @EduAzevedo's corpus numbers above it is now the largest single gap behind this PR: 294 programs (259 mesh, 35 vertex), and the second row of #293's ranking (about 462 draws per frame). I do not want to duplicate it, so if you have it in hand, say so and I will stop. Otherwise I am starting a branch on top of this PR's head (6285e19) and will post it for you to take or pick from.The approach I intend, so it can be shot down early:
resource.rs,per_lane). When the lane's dwords are VGPR phis of one block, trace each predecessor's values withper_laneagain (throughlane_term), exactly asphi()does for SGPR words, and giveSource::Phi { block, args }whose arguments are per-lane sources (Fetched,Lanes,Loaded). A phi of a block already being resolved stays refused (a descriptor that changes in a loop), and the join limit applies.lower.rs,handle).Source::Phicomputes each predecessor's heap index at that predecessor's end and joins them with a DXIL phi, as it does today forLoadedarguments, but throughlane_heap_indexso the arguments can be per-lane; the handle is non-uniform when any leaf is. Since structurisation makes each lane take its own path, the phi gives each lane its own descriptor.@EduAzevedo, if it is cheap for you: what the phi looks like in those 294 would tell me whether this is the right shape before I write tests against the wrong one. Yes/no or counts, no shader contents:
global_load_dword*results,v_mov_b32from SGPRs that ans_load_dword*filled, av_cndmask_b32, or something else (which instruction)?No game data used on my side.
@AnoHunted on GitHub · 2026-11-24 04:35 UTC · original
Tested
6285e19merged ontomaind2a4baeon NVIDIA GeForce RTX 5080, Windows 11, Rust 1.98.1, withdxil.dllvalidation on: pass.cargo test -p victory-shader -- --test-threads=1: main 69 passed / 0 failed; this PR 78 passed / 0 failed (new tests: 9, e.g.waterfall_loops_over_per_lane_descriptors_sample_with_each_lanes_own,a_descriptor_selected_by_scc_picks_its_heap_index,a_first_lane_read_outside_a_loop_is_no_waterfall).cargo test --workspace -- --test-threads=1: main 264 passed / 0 failed / 4 ignored; this PR 273 passed / 0 failed / 4 ignored.cargo build -p victory --releasealso succeeds on this head (thenon-exhaustive patternserror intranslate.rsreported onffa6635is gone, fixed by077ec8b).No game files used.
@EduAzevedo on GitHub · 2026-11-24 04:42 UTC · original
Yes, measuring it now
@miniongk on GitHub · 2026-11-24 04:45 UTC · original
Verified the updated head
6285e19on Windows 11 with the RTX 5080 setup.resource::tests::a_first_lane_read_outside_a_loop_is_no_waterfallpasses standalone.cargo test --workspace: 273 passed, 0 failed, 4 ignored; the new negative regression also passes in that run.No game data used for this check. AI-assisted (Kimi Code).
@marquezinii on GitHub · 2026-11-24 04:51 UTC · original
Ran
6285e19on an NVIDIA RTX 5070 (32-lane waves, driver 621.24), Windows 11:cargo test -p victory-shaderpasses (78). The PR's own lowering tests do not run on a GPU, so I dispatched a few synthetic programs through the testHost(all validated bydxil.dll), each sampling one of two descriptors (entry 0 samples 1.0, entry 1 samples 2.0):v_cndmask(lanes < 16 / >= 16), wave32global_load_dwordx4(entry = lane & 1), wave64s_cselect_b64x4 chosen by a root constant, k = 0 and k = 1One shape is accepted and comes out wrong: the same complete loop (readfirstlane x8, four
v_cmp_eq_u64,s_and_saveexec,s_xor exec,s_cbranch_execnz), but with theimage_sample_lzusings[12:19]placed afters_mov_b32 exec_lo, s20, outside the loop. On the console every lane samples with the last pass's descriptor (lane 16's, 2.0). Here it translates (nonuniform_handles = 1), validates, and returns1.0 x16, 2.0 x16, because each recorded first-lane read becomes the lane's own value wherever it is used. A compiler would not normally emit this (the waterfall SGPRs are dead after the loop), but nothing ties the use to thes_and_saveexecregion of the compare, so it is silently wrong rather than refused. Requiring every use of a recorded read to sit inside that region (or refusing reads used outside it) would close it.Not covered: the title (no game data here).
@EduAzevedo on GitHub · 2026-11-24 04:52 UTC · original
Measured on
6285e19with a local, uncommitted trace at thea descriptor read from the first lane of a phirefusal (retail v1.0.2.1 shaders,victory translate, all 26,148 programs). It fires once per program: 294 (259 mesh, 35 vertex). Counts only:s_load_dwordx4results (through VGPR copies of the SGPRs), i.e. one of two scalar-loaded descriptors. Every program also has other leaves in the same chains. Programs with each kind of other leaf:buffer_load_dwordx3149, input VGPRs 130,v_mul_legacy_f32110,v_sub_f3274,v_fmac_f3243, constants 17,v_rsq_f3214,v_cndmask_b327,v_mad_u32_u245,v_lshrrev_b324,v_cvt_f32_u324,buffer_load_dwordx43,v_xor_b321. Noglobal_load_dword*anywhere. They look like the VGPRs' contents on paths that do not reach the descriptor use, but I have not proved those edges are dead.So the shape is "one of two
s_load_dwordx4descriptors chosen by if/else joins, with unrelated values on the other edges", rather than per-lane loads.@r4tedx on GitHub · 2026-11-24 05:01 UTC · original
@EduAzevedo thank you, your answers changed the design. The phi case is up as #566, one commit (
6638098) on top of this PR's head6285e19.What your numbers showed: the "unrelated values" on other edges (
buffer_load_dwordx3, input VGPRs, arithmetic) are what an EXEC if/else leaves behind. Each dword gets two phis: one joins an arm's dwords with whatever the lanes that skipped the arm held before, the other picks per path after the other arm. Only lanes that never read the descriptor bring those values, so tracing every edge, as I first proposed above, cannot work. #566 traces only the edges that the lanes accessing the descriptor can take, using the per-lane conditions from structurisation, and joins the per-edge heap indices with a DXIL phi.@AntoninMichelq two things for you:
s_cbranch_execz(so the arm'sv_mov_b32s are guarded writes) is traced as the second descriptor for every lane. #566 refuses that (a per-lane descriptor written where some lanes may keep the old one) and has the test; the same check would fit here independently of the phi work.If you can rerun,
victory translate --liston #566 against this PR's head:a descriptor read from the first lane of a phishould go from 294 to 0, and what matters is where those 294 stop next. If the pruning cannot prove the unrelated edges dead in the real programs, they will stop at... of an input VGPR,... of a buffer loador... of computed VGPRs, and the counts per reason will say what is still missing. Also worth counting separately: programs that translated here and stop on #566 at the guarded-write reason.Not run against the title on my side.
@upipibi on GitHub · 2026-11-24 05:51 UTC · original
Independent check of head
6285e19onmaind2a4bae. I read the comments above first. The two open correctness points are already there (@marquezinii: a first-lane value used after the loop; @r4tedx: VGPR writes under a partly clear EXEC, refused in #566); I add one more input of the same family and otherwise found the operand orders right.Read against the RDNA2 ISA (30-November-2020)
s_cselect_b32/_b64(12.1, opcodes 10, 11, p. 103):D.u = SCC ? S0.u : S1.u.pickedinresource.rsreturns (set = src0, clear = src1), andSource::Selectis documented and lowered as "set, then clear" (lower.rs:select(cond, set, clear)). Correct.s_cmov_b32/_b64(12.3, opcodes 5, 6, p. 111):if(SCC) then D.u = S0.u; endif.pickedtakes clear = the destination's old dwords. Correct, and it works althoughlower.rshas no arm fors_cmov_*as plain data: the descriptor's words are never lowered as values. No test in the PR usess_cmov; a local one does (below).v_cndmask_b32(12.7, opcode 1, p. 131):D.u32 = VCC ? S1.u32 : S0.u32.lane_pickeddestructures[clear, set, mask]. Correct.fetched_indexmirrorsheap_index(address low dword plus offsets, over 32 or 16), and the handle is made withnonUniformIndex = true, which is what D3D12 asks of a heap index that differs between lanes.One more accepted shape that differs from the console (same family as the use-after-loop case; a compiler would not emit it, but it is accepted rather than refused):
WATERFALL_FETCHED_WAVE32with the lasts_and_b32 s21, s21, s22(0x8715_1615, the one after the fourthv_cmp_eq_u64) replaced bys_nop. The fourth compare is still computed, soTracer::waterfallrecords all 4 compares and 8 reads andtracksucceeds, but its result no longer reachess_and_saveexec. On the console, lanes that match the first lane in dwords 0..5 and differ in dwords 6..7 sample with the first lane's descriptor; translated, each samples with its own.waterfallchecks that the compare is in the loop and pairs the right operands, not that its mask is what narrows EXEC for the access. Tying the recorded compares to thes_and_saveexecoperand would close this and the use-after-loop case together.Run on hardware. NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1,
dxil.dllfrom the Windows SDK 10.0.26100 validating;cargo test -p victory-shader -- --test-threads=1:main: 78 passed, 0 failed (main: 69).s_cmov_b64 s[0:1], s[4:5]/s_cmov_b64 s[2:3], s[6:7]: traced asSelect { args: [second load, first load] }, i.e. SCC set picks the moved-in one. Correct.lower: lowers, 0 non-uniform handles. Correct.trackreturnsOk, 4 compares, 8 reads.I did not dispatch waterfall programs on this GPU; @marquezinii's RTX 5070 runs above cover that.
Not checked: the title (no game files here), AMD hardware, #566.
AI-assisted (Claude Code).
@LoukaAyariPignel on GitHub · 2026-11-24 08:04 UTC · original
I merged current
maininto this branch to clear the conflict:fix-307on my fork, merge commit36b8fdb. The only conflict was in theemit::lowertest module: this PR's new tests and imports against thehalf_shiftimport andscalar_bit_tests_lowertest frommain. I kept both sides. On Windows,cargo test -p victory-shaderpasses, including the new waterfall ands_cselecttests, and so doclippy -D warningsandfmt --check. It still merges cleanly with today'smain. Feel free to pull it in.@slint6614-dev on GitHub · 2026-11-24 08:44 UTC · original
Measured on the full retail shader pack (26,503 containers) with
victory translate --validate --pipelines(pipelines created on an AMD RX 9070, Adrenalin, Windows 11; dxil.dll from SDK 10.0.26100), against main1f858b0. Counts are emitted / validated / pipelines.With #566, of the 298: +8 VS/PS and +56 MS validated, the rest stop on "scalar memory read as data" (573 → 740), "a descriptor read from the first lane of computed VGPRs" (38), "a waterfall loop whose compares do not select the access's lanes" (15) and "a descriptor partly joined by control flow" (8). #307 alone gives no validated gain. All pipelines are created on the RX 9070.
@Vaximous on GitHub · 2026-11-24 10:12 UTC · original
A hang to know about before this lands: waterfall loops in pixel shaders never end on NVIDIA when only helper lanes are left.
On the console, whole-quad mode puts a pixel shader's helper lanes in EXEC, so a waterfall loop serves them too:
v_readfirstlanethe descriptor index,v_cmpx_eqthe lanes with that index, run them, take them out of EXEC withs_andn2_wrexec. On the host,WaveReadLaneFirst(like the other wave operations) leaves helper lanes out. Once every non-helper lane has been served, the remaining helper lanes read no lane, none of them matches, and each one runs to the translator's loop limit.Observed on an RTX 4070 Ti, GTA VI 1.0.2.1, local integration branch = main + many open PRs including this one and #566:
DEVICE_HUNG(0x887A0006) about 200 s into the world. DRED breadcrumbs stopped at the same instanced draws on every run.v_readfirstlanetakes its own value. It then matches in the first pass and leaves the loop. Its results only feed derivatives, and its stores are already dropped.The same applies to any
v_readfirstlane/WaveReadLaneFirstlowering in a pixel shader that a loop exits on, so it may matter for the NVIDIA world hangs in #625, #71 and #619 as well.@SpaghettiMaster545 on GitHub · 2026-11-24 10:15 UTC · original
@Vaximous could you push that local commit to a branch on your fork? Upstream PRs are paused (#712), so a branch is the only way NVIDIA testers on #71, #619 and #625 can try it.
I can run it on an RTX 5070 Ti (driver 621.24, Windows 11 26100, Ryzen 7 9800X3D). On
mainthe world there drops to about 1 fps and ends inDEVICE_HUNG59–111 s in. That is the same picture as yours: each world submission takes close to a second until one crosses the TDR limit (#400 has the per-frame breakdown). I'd compare it againstmainwith the same settings and post the result here.AI-assisted (Claude Code).
@claude-GTA6 on GitHub · 2026-11-24 12:20 UTC · original
From a tester working with Claude (AI-assisted); measured on my own machine.
The finding is @Vaximous's (#307 comment): pixel-shader waterfall loops never end on NVIDIA once only helper lanes are left. PR creation is closed right now, so here's the same idea as a branch you can merge or cherry-pick, with what I could check on my machine: claude-GTA6/VIctory
pr-helper-readfirstlane, one commit onmainffe310e.Problem
In a pixel shader, the console's whole-quad mode keeps helper lanes in EXEC. A waterfall loop (
v_readfirstlanean index or count,v_cmpx_eqthe lanes that hold it, run them, take them out of EXEC) therefore serves the helper lanes too. On the host,WaveReadLaneFirstleaves helper lanes out. Once every non-helper lane is served, the helpers read a value none of them holds, so none matches, and each runs to the translator's loop limit (2^20 iterations per lane).Change
v_readfirstlane_b32in a pixel shader: a helper lane (IsHelperLane, op 221) takes its own value. It then matches in its first pass and leaves. A helper lane's results only feed derivatives, and its stores are already dropped. Other stages are unchanged.Measured (RTX 5090, driver 621.24, Windows 11; GTA VI 1.0.2.1 continuing from a save; my branch with about 60 merged PRs)
1af4a50c7fb40c97has a light loop of this shape (s59 = v_readfirstlane(v75), exits when it matches). Before the fix, at the default loop limit, it took 1,163 ms for one 4-group mesh draw, and 4 to 6 of its draws per run took over 100 ms. With the fix, one of its draws took over 100 ms in the run.42683eebec081520at about 495 ms per dispatch, pixel6a3a8d3fe52b4eab). So this alone doesn't end the NVIDIA world hang on my card. WithVICTORY_SHADER_LOOP_LIMIT=256, #454 and a skip of three mesh pipelines, runs with and without this change both ran 15 minutes with no device removal, so no regression showed.mainffe310e+ this commit):cargo test -p victory-shader120 passed, 1 ignored. The two pixel shaders above emit and passdxil.dll. Whole dump--validate: 12,243 vertex and pixel programs emitted, 12,167 validated. No validator error mentions the new op. I have no count from before the change to compare against.Not verified
No game data attached.