Repository navigation
Recompiler: a lane loop form for small split wave64 compute programs - #3561
Royalflamejlh wants to merge 2 commits into
Conversation
|
Review (head 1216e70, diff read, not a merge-check: the
Fine: the AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point. |
|
No open PR adds a lane-loop form. #3562 (same author) attacks the same problem, the split wave64 exchange on 32-lane GPUs, by folding two console lanes into one host lane. This PR instead runs a whole console wave per thread for small programs (at most 512 code dwords by default). These are different approaches. If both merge, the lane loop runs first in AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
1216e70 to
659efb7
Compare
|
Edited: Thanks for the review. The lane loop is opt-in now (head On the other points: a lane-loop program gets no dispatch chain, as you say (one thread a wave has nothing to cut); and it does take single-wave programs with LDS when the switch is on. If both this and #3562 merge, the order between them needs one decision point; I would put the choice in
AI use: Claude Code with Claude Fable 5.2 made the change, ran the tests and wrote this. |
659efb7 to
c15bcb1
Compare
|
A correction to my comment above: head AI use: Claude Code (Claude Opus 5.5). |
|
Follow-up review (head c15bcb1, since 1216e70, diff read, not a merge-check; checks red):
New: none. Fine: the AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point. |
|
merge-check Default (switch unset): Opt-in ( verdict: safe to merge |
c15bcb1 to
4a65347
Compare
|
Follow-up review (head 4a65347, since c15bcb1, diff read, not a merge-check; checks red):
New: none. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point. |
bmjask
left a comment
There was a problem hiding this comment.
Review of 4a653476: 3 finding(s) (1 critical, 1 high, 1 medium), most severe first.
- Critical
crates/xcl/src/gpu/dxil/lane_loop.rs:138: The lane loop's CFG walk drops the per-dispatch loop-pass cap (loop_passes/ VAR_LOOP_PASSES from IO_LOOP_BUDGET) that kernel.rs applies to every plain-form compute program. (inline) - High
crates/xcl/src/gpu/dxil/lane_loop.rs:175: Not certain about this one, because it depends on scheduling the hardware doesn't guarantee: numthreads becomes[ceil(N/64),1,1], so all console waves of a group run as threads of one 32-lane hardware wave. (inline) - Medium
crates/xcl/src/gpu/dxil/lane_loop.rs:179: The lane loop always finishes withtry_finish(..)anddispatch_chain: None, bypassingfinish_dispatch'ssplit_compute/dispatch_piece_limit()(12,500 instructions). (inline)
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| for &b in &cfg.rpo { | ||
| let h = head(&mut lw, b); | ||
| lw.s.m.set_block(h); | ||
| if cfg.headers.contains(&b) { |
There was a problem hiding this comment.
Critical: The lane loop's CFG walk drops the per-dispatch loop-pass cap (loop_passes / VAR_LOOP_PASSES from IO_LOOP_BUDGET) that kernel.rs applies to every plain-form compute program. Loop headers only decrement the 65,536-pass VAR_BUDGET. A compute program whose loop runs away (a spin on memory that never changes, or an exit condition the translation gets wrong) runs up to 65,536 passes per console wave, where the plain form stops it at the dispatch's share (DISPATCH_LOOP_PASSES / threads, min 1024). Each pass also runs every vector instruction as a 64-iteration loop in one thread, so a dispatch the split form cuts short runs ~64x longer per pass: the GPU hang / TDR the form is meant to avoid. Applying the same per-dispatch cap at the lane loop's loop headers would keep it bounded.
There was a problem hiding this comment.
Right: the lane loop's loop heads counted only the lane's 65,536 budget. Fixed in cab682f3e: the head now goes through kernel::loop_head (taken out of translate_on_this_thread, so both forms share it), and loop_passes divides the dispatch's share by the console group's threads, so a console lane gets the passes it gets in the split form.
Tests: lane_loop_loops_have_the_passes_of_the_console_threads (red before the fix: the program never loaded IO_LOOP_BUDGET), and the lane loop is now a third case of gpu_direct_compute_loop_passes_follow_the_dispatch, with the same expected counts as the split form.
| return Err(e); | ||
| } | ||
| let static_samplers = lw.static_used.iter().map(|&(w, c, _)| (w, c)).collect(); | ||
| let dxil = lw.s.try_finish([waves, 1, 1], None).map_err(|e| format!("direct DXIL builder error: {e}"))?; |
There was a problem hiding this comment.
High: Not certain about this one, because it depends on scheduling the hardware doesn't guarantee: numthreads becomes [ceil(N/64),1,1], so all console waves of a group run as threads of one 32-lane hardware wave. Waits between console waves that don't use s_barrier (LDS-flag or atomic spin loops) then depend on divergent threads of one hardware wave making progress, which GPUs without independent thread scheduling don't guarantee. In a 256-thread group (4 console waves) where wave 0 polls an LDS or global flag that wave 1 sets with no s_barrier (accepted, since only S_BARRIER with waves > 1 is refused), thread 0 loops while thread 1 is masked at the reconvergence point on AMD. The flag is never written, so the loop spins until VAR_BUDGET runs out and exits with wrong results, or hangs the GPU. Refusing programs with cross-wave waits other than s_barrier (e.g. LDS/global loads inside loops whose exit depends on them) would keep them on the split form.
There was a problem hiding this comment.
Taken, though fixed another way than a refusal: each console wave now has a hardware wave of its own. The group is declared with 64 threads for each console wave; at the entry the first lane of each hardware wave takes the next console wave from a count in the LDS, and the other threads return (wave_by_arrival, cab682f3e; it does not assume the driver forms waves from consecutive thread indices). So on 32- and 64-lane hosts alike a wave that polls LDS or memory another wave writes is in a different hardware wave, as on the console. A group of one console wave is declared as one thread, so the s_barrier the form accepts there has nobody to wait for.
Not reproduced: we have no AMD card, and on NVIDIA the old layout may well have made progress anyway. Test: lane_loop_has_one_thread_a_wave_and_waits_only_at_its_entry expects numthreads 128 for a group of 96 (2 before the fix) and the entry's two group syncs, one waveIsFirstLane and one atomic add.
| if cfg!(any(test, debug_assertions)) { | ||
| super::validate::validate(&dxil).map_err(|e| format!("lane loop DXIL failed validation: {e}"))?; | ||
| } | ||
| Ok(Kernel { dxil, static_samplers, dispatch_chain: None, exchange_sites: lw.exchange_sites }) |
There was a problem hiding this comment.
Medium: The lane loop always finishes with try_finish(..) and dispatch_chain: None, bypassing finish_dispatch's split_compute / dispatch_piece_limit() (12,500 instructions). With XCL_WAVE64_LANE_LOOP=all, a large split-wave64 program the split form would run as a dispatch chain becomes one monolithic shader running 64 lanes per thread, and its lane-loop module is already much bigger than the split form's. That brings back the driver compile times, size limits and GPU timeouts the chain avoids. Routing through finish_dispatch, or refusing programs over the piece limit, would keep the chain.
There was a problem hiding this comment.
Right. A dispatch chain cannot carry the lane arrays across its cuts, so in cab682f3e the lane loop refuses a program whose module costs more than dispatch_piece_limit(), measured as split_compute measures it (Module::dispatch_cost, now shared). Such a program keeps the split form and its chain. A program of 512 code dwords (MAX_WORDS) still fits.
Test: lane_loop_refuses_a_program_larger_than_a_dispatch_piece (2,048 instructions; translated whole before the fix).
|
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any. |
4a65347 to
50f65e5
Compare
|
Follow-up review of In short: Since High
Nit
Checked and fine
Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point. |
bmjask
left a comment
There was a problem hiding this comment.
Review of 50f65e53: 2 finding(s) (1 high, 1 medium), most severe first.
- High
crates/xcl/src/gpu/dxil/lane_loop.rs:107: With one console wave (waves == 1),s_barrieris let through and lowered by the scalar path (lw.instat line 146) to a real GROUP_SYNC barrier, but only thread 0 of the 64-thread group reaches it: the other 63 have already left at th.... (inline) - Medium
crates/xcl/src/gpu/dxil/lane_loop.rs:364: The 'reads a VGPR it does not name' refusal only sees reads that fall through toinit.load(init.touched). (inline)
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| let waves = cs.threads.iter().product::<u32>().div_ceil(64).max(1); | ||
| for d in &cfg.insts { | ||
| let why = match d.inst.name { | ||
| "S_BARRIER" if waves > 1 => "s_barrier in a group of several waves", |
There was a problem hiding this comment.
High: With one console wave (waves == 1), s_barrier is let through and lowered by the scalar path (lw.inst at line 146) to a real GROUP_SYNC barrier, but only thread 0 of the 64-thread group reaches it: the other 63 have already left at the runs && leads gate (lines 135/258). A cs([64,1,1]) program with DS_WRITE; S_BARRIER; DS_READ under XCL_WAVE64_LANE_LOOP=all gets GroupMemoryBarrierWithGroupSync in non-uniform control flow after a whole 32-lane hardware wave returned. That's undefined behaviour in D3D12 and in vkd3d-proton's SPIR-V (OpControlBarrier), and can hang on drivers that don't count exited waves. The kernel path avoids exactly this (leave = !any S_BARRIER). Since the test calls it 'one wave: nothing to wait for', dropping the barrier in the lane loop would match.
There was a problem hiding this comment.
Right, and already fixed in f66d74c9f, the head this comment sits on: a group of one console wave is declared as one thread (lane_loop.rs ~174), so its s_barrier waits for nobody who left; groups of several waves still refuse s_barrier. Tests: lane_loop_is_chosen_by_size_and_refusals_keep_the_split_form checks numthreads 1 for the one-wave barrier program (64 at 50f65e53), and gpu_lane_loop_passes_the_barrier_of_a_group_of_one_wave runs one.
| }); | ||
| let touched = std::mem::replace(&mut self.init.touched, before); | ||
| r?; | ||
| if touched.iter().any(|v| (VAR_VGPR..VAR_BUDGET).contains(v) && !arrays.contains_key(&(v - VAR_VGPR))) { |
There was a problem hiding this comment.
Medium: The 'reads a VGPR it does not name' refusal only sees reads that fall through to init.load (init.touched). Once an earlier lane loop has written VAR_VGPR+m, a read of an unnamed VGPR resolves to that SSA definition, which is lane 63's value from the earlier loop, and nothing is refused. If instruction A names and writes v7, and later instruction B's lowering reads v7 without naming it (e.g. an implicit second dword of a 64-bit or NSA operand that rdna2::vgpr_uses doesn't list), every lane of B silently reads lane 63's v7. Checking the reads themselves, or clearing the VGPR SSA definitions after each lane loop, would make the refusal reliable.
There was a problem hiding this comment.
I don't think this one happens. Lw::rd puts every variable it reads into init.touched (lower.rs ~410-411), before the SSA looks for a definition, so a read that finds an earlier lane loop's VAR_VGPR + m is recorded the same as one that falls through to init.load. vreg reads through rd (lower.rs ~809). lane_loop_inst empties touched before the lanes run (lane_loop.rs ~311) and refuses after them if a VGPR outside the instruction's arrays is in it (~366). So an instruction whose lowering reads a VGPR that vgpr_uses does not list is refused, whatever wrote that VGPR before.
50f65e5 to
f66d74c
Compare
|
Follow-up review of In short: This push fixes the one-wave barrier problem from the last review: a group of one console wave is now declared as one thread ( Nit
Checked and fine
Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point. |
bmjask
left a comment
There was a problem hiding this comment.
Review of f66d74c9: 1 finding(s) (1 medium), most severe first.
Re-review at f66d74c9. Both findings from the 50f65e53 review look addressed: the single-wave s_barrier after the other threads exit isn't flagged any more, and unnamed VGPR reads are now refused (every read and write is recorded in touched). One new item below.
- Medium
crates/xcl/src/gpu/dxil/lane_loop.rs:258: Not certain about this one, because it depends on how drivers form hardware waves: giving each console wave its own hardware wave assumes the driver packs consecutive flat thread ids into hardware waves, which the codebase says elsewhere.... (inline)
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| let tid = self.s.thread_id_in_group(0); | ||
| let wave = self.lshrc(tid, 6); | ||
| let lane = self.andc(tid, 63); | ||
| let leads = self.cmpc(Pred::Eq, lane, 0); |
There was a problem hiding this comment.
Medium: Not certain about this one, because it depends on how drivers form hardware waves: giving each console wave its own hardware wave assumes the driver packs consecutive flat thread ids into hardware waves, which the codebase says elsewhere isn't guaranteed (setup_lanes: 'the PC need not form waves from consecutive thread indices'). If a driver puts leading threads 0 and 64 in the same lockstep hardware wave, and console wave 0 spins on memory console wave 1 writes (the cross-wave polling the module docs say now 'waits as on the console'), the spinning thread starves the other and the dispatch hangs, the GPU hang this form was meant to avoid. Leading with one thread per hardware wave derived from WaveGetLaneIndex()/wave id, rather than from flat ids, would remove the assumption.
There was a problem hiding this comment.
Right that it was an assumption: numthreads is 1-D there, but setup_lanes already counts on no particular packing, so the lane loop now does the same. In cab682f3e the first lane of each hardware wave takes the next console wave from a count in the LDS at the entry (wave_by_arrival: zero the count, group sync, WaveIsFirstLane adds 1, group sync), and only those up to the group's console waves run; every other thread returns. Whatever the driver packs, each running thread is the only one of its hardware wave. The count is the LDS's top word (or a word of its own when the program has no LDS), which the program finds as undefined as at any start, since the second sync comes before any of its code.
Test: lane_loop_has_one_thread_a_wave_and_waits_only_at_its_entry (two syncs, one waveIsFirstLane, one atomic add; none at f66d74c9). The GPU tests run on the RTX 2070 SUPER PC; their results go into the description.
One thread runs a whole console wave: a group has 64 threads for each console wave, the first lane of each hardware wave takes the next console wave from a count in the LDS at the entry (a driver need not form waves from consecutive thread indices) and the other threads leave, so each console wave is a hardware wave of its own. A group of one wave is declared as one thread, so an s_barrier there has nobody to wait for. Each VGPR is 64 dwords local to the thread, and a vector instruction is a loop over the lanes around its ordinary lowering (Lw::inst_guarded) with the lane's EXEC bit as the guard. Masks are gathered bit by bit and lane reads read the register's array, so nothing waits for another hardware wave. The SSA sees each loop body once: scalars read there are their values before the loop, and scalars written are taken from the last lane, where masks and lane reads are whole. Loops have the split form's caps: the head goes through kernel::loop_head, now shared, with the dispatch's share divided by the console group's threads. Refused (the caller keeps the split form): s_barrier in a group of several waves, v_movrel, scratch memory, image writes, a lane read of a computed value, and a program larger than a dispatch piece (Module::dispatch_cost over dispatch_piece_limit), which the split form cuts into a chain. XCL_WAVE64_LANE_LOOP tags the translated cache. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rograms the lane loop Off unless asked for: with =1 a split program of at most 512 code dwords (lane_loop::MAX_WORDS) takes the lane loop where the form supports it, with =all every program it supports. The 512 is a first guess; GPU time against the split form is not measured. The switch is part of the translated cache's identity. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f66d74c to
cab682f
Compare
|
Follow-up review of In short: This push changes how the lane loop gives out console waves: no longer thread 64 x to wave x, but by arrival ( Nit
Checked and fine
Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point. |
|
Thanks for the follow-ups (to 21:51). At
AI use: Claude Code (Claude Opus 5.5) wrote the fixes, the tests and the replies in this round. |
bmjask
left a comment
There was a problem hiding this comment.
Review of cab682f3: 1 finding(s) (1 medium), most severe first.
Re-review at cab682f3. The wave-packing assumption from the f66d74c9 review looks addressed: waves are now assigned by arrival (wave_by_arrival). One new item below.
- Medium
crates/xcl/src/gpu/dxil/lane_loop.rs:172: The only time limit on the lane loop is a check of static DXIL size (dispatch_cost() > dispatch_piece_limit()), but each lane-loop body runs 64 times in one thread, so GPU time per dispatch is roughly 64x what that size suggests and is.... (inline)
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| if let Some(e) = lw.exchange_error.take() { | ||
| return Err(e); | ||
| } | ||
| if lw.s.m.dispatch_cost() > dispatch_piece_limit() { |
There was a problem hiding this comment.
Medium: The only time limit on the lane loop is a check of static DXIL size (dispatch_cost() > dispatch_piece_limit()), but each lane-loop body runs 64 times in one thread, so GPU time per dispatch is roughly 64x what that size suggests and isn't bounded. With XCL_WAVE64_LANE_LOOP=all, a program just under 12 500 static instructions, or one with long loops (up to 65 536 passes, each now 64 lanes run one after another in one thread), runs about 64x longer per thread than the split form and is never cut into a dispatch chain. That can cause the same TDR/device hang the PR is trying to avoid (the 16 s dispatch at the title's world start). Counting the 64x in the cost estimate, or scaling the loop budget down for the lane loop, would keep it bounded.
There was a problem hiding this comment.
Not changed, on purpose; here is the reasoning. The loop cap already gives a console lane the passes it gets in the split form (loop_passes over the console group's threads), so the total work of a runaway dispatch is the split form's; what the lane loop gives up is parallelism, by design (one thread per console wave), and that is why it is opt-in and its GPU time is listed under Not tested. Scaling the cap by 64 would end a working loop after 16 passes (1024 / 64, against the floor that keeps working programs whole), and counting 64x in dispatch_cost would refuse nearly every program (about 195 DXIL instructions). The 16 s dispatch in the description is the split form's exchange, not a long loop. If GPU time on real programs shows the lane loop near a timeout, the size limit (MAX_WORDS) is the place to narrow it.
(Edited: head
cab682f3e, rebased ontomain5ad98ad, after three rounds of review: the lane loop now has the split form's per-dispatch loop caps (kernel::loop_head, shared), gives each console wave a hardware wave of its own, taken by arrival at the entry (a group of one wave stays one thread, for itss_barrier), and refuses a program larger than a dispatch piece; a test for each. Before: rebased ontomain38ad22d, after #3655; head4a6534767. The change itself: the lane loop now fills theexchange_sitesfield #3655 added toKernel(the form exchanges nothing, so 0), and its cache tag sits beside #3655'sXCL_WAVE64_IDIOMS=0tag.)A third form for wave64 compute programs on a GPU with 32-lane waves only: one host thread runs a whole console wave, all 64 lanes, in a loop. Nothing is exchanged and nothing waits, so it cannot spin. It is meant as the exact fallback for small programs that the split form runs badly: on Linux (RTX 4060 Ti) a split program of 64 threads a group stayed 16 s in one dispatch at the title's world start and the run ended in a GPU hang; #3114 measures the same exchange at 2.6x a program's GPU time.
The form is opt-in (
XCL_WAVE64_LANE_LOOP=1): without the switch nothing changes. Tests on other cards (Windows, AMD, other NVIDIA) are wanted. It has not run in a title from this branch.What is in it
crates/xcl/src/gpu/dxil/lane_loop.rs(new, about 430 lines of code and 200 of tests): the form. A vector instruction is a 64-pass loop around the existing per-lane lowering with the lane's EXEC bit as the guard; each VGPR is a 64-dword array; a mask is built lane by lane; lane reads (v_readlane,v_readfirstlane, DPP, swizzle, permlane,ds_bpermute) read the array. numthreads becomes[ceil(N/64) * 64, 1, 1]: at the entry the first lane of each hardware wave takes the next console wave from a count in the LDS (two group syncs) and the others return, so each console wave is a hardware wave of its own however the driver packs threads; a group of one wave is[1, 1, 1]. Loops have the split form's caps (kernel::loop_head,loop_passesover the console group's threads).XCL_X_LDS_ORDERdoes not affect it; withXCL_WAVE64_LANE_LOOPset it also takes the single-wave LDS programs thatXCL_X_LDS_ORDER=0sends to the HLSL path onmain.s_barrierin a group of more than one console wave,v_movrel*, scratch memory, image stores and atomics, a lane read of a computed value, a program larger than a dispatch piece (dispatch_piece_limit, which the split form cuts into a chain), and any instruction that touches per-lane state it does not track.gpu/recomp/pso.rs(direct_compute): one call,lane_loop::choose, before the split translation.lanes.rs: two early returns (ballot,rla).kernel.rs: the cache tag, and the loop head taken out intoloop_head.module/split.rs:Module::dispatch_cost, the sumsplit_computealready weighed.XCL_WAVE64_LANE_LOOP=1split programs of at most 512 code dwords (lane_loop::MAX_WORDS) take it, with=allevery program the form supports. The 512 is a first guess, and GPU time against the split form is not measured, which is why it is opt-in. The switch is part of the translated cache's identity.docs/wave64-on-wave32.md: a section on the form.817 added lines and 43 removed, 709 of them the new file with its tests; the form is not useful in smaller parts.
Tested
x86_64-pc-windows-gnu, release. Headc15bcb172. (Edited: the earlier head659efb76did not carry the opt-in its commit describes: the edits were left uncommitted, so659efb76ran the form by default on programs of at most 512 code dwords.c15bcb172has them, andXCL_WAVE64_LANE_LOOPis a warm pipeline library setting; the lane-loop andshaders::tests there, under Wine on the CPU: 103 passed, 0 failed.)cargo test -p xcl --release --lib -- gpu::dxil --test-threads=4: 311 passed, 1 failed, 3 ignored. SKIPPED:dispatch_chain_native_wave64_builds_whole(no wave64). The failure isround_tests::directed_reference_basics("Invalid bitcode signature"), which fails the same way here on a tree without this change; not rerun onmainalone.gpu_lane_loop_gives_the_console_waves_values(groups of 48, 64, 96, 256 and 8x8x4 threads; ballot, readlane, readfirstlane, mbcnt, a saveexec loop, LDS write then read; against the console's values and the split form's),gpu_lane_loop_reads_other_lanes_before_any_is_written,gpu_lane_loop_leaves_out_the_lanes_past_a_thread_limit.Regression run
RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, release builds by
scripts/smoke.ps1, the same retail title for 60 s each.main:this PR's head:
Not tested
AI use