Skip to content

Read the first active Wave64 lane across both host halves in single-wave groups - #12

Open
MichaelSmithAI wants to merge 4 commits into
r4tedx:shader/wave-per-groupfrom
MichaelSmithAI:wave64-first-lane-single-group
Open

MichaelSmithAI wants to merge 4 commits into
r4tedx:shader/wave-per-groupfrom
MichaelSmithAI:wave64-first-lane-single-group

Conversation

@MichaelSmithAI

@MichaelSmithAI MichaelSmithAI commented Nov 24, 2026 •

Copy link
Copy Markdown

A Wave64 compute shader can read a different first active lane in each 32-lane host wave. On an RTX 4090, the upper half of a 64-thread group read 32 at entry where the console reads 0. Inside a branch starting at lane 10 it read 103 from lane 32 where the console reads 37.

This change lets an ordinary v_readfirstlane_b32 request the existing console-flow exchange on hosts that support 32-lane waves but cannot run 64-lane waves. The group must have 33–64 threads and at least one source must lack a uniformity proof. The proof follows straight-line scalar and vector arithmetic over constants and scalar inputs. It also follows scalar dword loads when every base or descriptor word and the scalar offset are uniform. Joins and partial writes remain conservative. Waterfall reads use their existing lowering.

The tests inspect the emitted DXIL wave size and exercise the public emitter's per-lane retry when console flow declines a joined descriptor. RTX 4090 readbacks under Wine/GE matched every expected word for entry and divergent reads at 32, 48 and 64 threads, and for a uniform scalar-to-vector chain in two 64-thread groups. The scalar-load fixture reads a descriptor in slot 1 and then two dwords at a group-dependent offset. Its first-lane read of the second dword stays undeclared at 48 and 64 threads and returns 37 throughout group 0 and 91 throughout group 1. An offset outside the proof still selects wave size 32. Larger groups and the retry can still produce per-half values. Hosts that support native Wave64 are outside this change.

The maintainer’s offline title-pack evaluation predicted that following these scalar loads would clear 66 of the 158 programs selected by the prior rule. We have not measured this head against the title pack or timed it in game.

Follow-up to Marvisak#439 and Marvisak#1132. AI-assisted.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you. The defect is real, the fix is right where it applies, and I would like to carry it with your authorship after the detection is narrowed. Reviewed, built and run on an RTX 5070 (32-lane waves) and a Radeon iGPU (64-lane waves), at 08e9089.

The defect, reproduced at the parent (bf0f951)

Your GPU test taken onto the base fails on the RTX 5070 at 48 threads: lanes 32-47 of each group read 32 where the console reads 0. A probe that reports every case, two groups each, lanes that differ from the console's result:

threads varying value (entry and divergent read) uniform value (v_mov of a scalar, then first lane)
32 0 of 64 0
48 32 of 96 0
64 64 of 128 0
128 128 of 256 0

With your change the RTX 5070 is exact at 32, 48 and 64 threads; 128 is unchanged, as your test comment says. So the design note's "as on main: its v_readfirstlane reads the host wave's first lane" described a wrong result, not a harmless one.

What checked out

  • Waterfall reads keep their own lowering (the same predicate the lowering uses); programs with LDS, ds_permute, DPP, whole-wave sections or groups over 64 behave as before.
  • The per-lane fallback is intact: a program under the new rule alone has no wave size and no refusal reason, so if the console flow declines it translates as before. The rule depends on neither pass (console_cf) nor on the deferred selects.
  • Nothing changes on a host that runs 64-lane waves.
  • Both CI clippy commands clean; cargo test --workspace --locked: 384 passed, 0 failed, 6 ignored.
  • Over the title's shader pack (local title data, offline, CPU only, counts only; translate --host-lanes 32-32 --validate, before and after): no program changes outcome. Emitted and validated counts are identical in every stage.

Why not as it is: the rule fires for 179 compute programs and most of them gain nothing

160 of the 179 move from the per-lane form to the console flow (the other 19 are refused before and after for unrelated reasons; none fell back). In a group of more than 32 threads every first-lane read and every EXEC or VCC branch then becomes an exchange, which is two group barriers, and the module declares WaveSize(32). Of the 160:

programs can the halves read differently?
group of 32 threads or fewer (14 of them one thread) 21 no: there is no upper half
more than 32 threads, every read of a value that is the same in every lane 66 no
more than 32 threads, uniform through joins or loads at uniform addresses 36 probably not
more than 32 threads, not shown uniform 37 possibly

"The same in every lane" comes from a conservative check written for this measurement (VALU and scalar chains over scalars and constants; no joins, guarded writes or loads); it is not in the tree. So at least 87 of 160 would pay barriers for nothing, and the real beneficiaries are at most 37.

What I am asking for

  1. Exclude first-lane reads whose source is the same in every lane, at least VALU and scalar chains over scalars and constants. Your uniform probe case should stay undeclared and per lane.
  2. Require a group of more than 32 and at most 64 threads.
  3. Tests that pin the form: wave size 32 for a varying read, none for a uniform read and for a group of 32; and one for the fallback on a 32-32 target when the console flow declines.
  4. docs/wave64-on-wave32.md: the sentence quoted above and the eligibility list.

One thing your test skips and that I will take separately: the Radeon (32-64) shows the same wrong lanes for the varying read, before and after, because these undeclared programs run there in 32-lane waves too. On a host that runs 64-lane waves the fix is to declare WaveSize(64) for a varying first-lane read. Groups over 64 on a 32-lane host stay open.

On where it goes: Marvisak#439 is not taking new behaviour while it waits for review, so this would land on the branch after it. You merged our newest head in already, which is fine; one commit of yours on top is what I would carry.

AI-assisted (Claude Code).

@MichaelSmithAI
MichaelSmithAI force-pushed the wave64-first-lane-single-group branch from 56d0af0 to b5f052b Compare November 24, 2026 17:30
@MichaelSmithAI

Copy link
Copy Markdown
Author

Updated to b5f052b, a single commit on your current 28aca3b base, with the eligibility list and design note revised.

An ordinary first-lane read can now request the exchange only for a 33–64-thread group on a host that runs 32-lane waves and cannot run 64-lane waves. The classifier follows straight-line scalar and vector arithmetic over constants and SGPR inputs. Loads, joins and partially executed writes remain conservative.

On the simulated 32–32 target, varying entry and divergent reads declare WaveSize(32) at 48 and 64 threads. The scalar-to-vector arithmetic control stays undeclared at both sizes, and all three stay undeclared at 32. A masked write that combines two constants still selects the exchange. The joined-descriptor fixture confirms that the ordinary read requests console flow, the console form declines the join, and the normal emitter returns validated DXIL with no declared wave size.

RTX 4090 readback matched every lane in the entry and divergent controls at 32, 48 and 64 threads. The uniform 64-thread control read 2 throughout group 0 and 3 throughout group 1. All seven focused tests passed. I don't have a revised title-pack count or timing measurement. The Radeon native-wave case remains open as discussed.

The Linux shader suite passed 99 tests with one ignored. The Win64 shader suite under Wine passed 163 with one ignored and the same stub_pipeline_is_created failure seen on the base. Win64 shader Clippy passed with all targets and features.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Second round, at b5f052b. It is sound and it loses nothing; one more step and I carry it.

Soundness of the uniformity check. No case found where it calls a value uniform that can differ between lanes. It only admits an SGPR input (user data, the group id or zero on the host: these can differ per group, never per lane) or the result of an unguarded listed operation in a block every lane reaches, all of whose operands are uniform. v0 and other VGPR inputs, loads and joins are never admitted; writes under a partial EXEC are skipped; v_cndmask, DPP and SDWA forms are not listed; a value that changes per loop iteration passes through a join, so what it admits in a loop is loop-invariant. Thirteen hand-assembled programs at 48 and 64 threads, two groups each, on an RTX 5070: all 26 runs match the console. Five shapes are called uniform and stay undeclared (a read at entry, inside a divergent if whose lower half is empty, under a narrowed EXEC, written and read inside a divergent loop, a s_cselect chain); eight are flagged and run with wave size 32.

The four requests. The 33-64 thread limit, the tests that pin the form (including the per-lane fallback after the console flow declines a joined descriptor) and the doc are done. Both CI clippy commands are clean; cargo test --workspace --locked: 387 passed, 0 failed, 6 ignored. Over the title's shader pack (local title data, offline, CPU only, counts only; --host-lanes 32-32 --validate) nothing changes outcome: emitted and validated counts are identical in every stage.

What is left: the check clears no title program yet.

first version this version
programs the rule fires for 179 158
moved to the console flow 160 139
refused before and after, for other reasons 19 19

The 21 fewer are exactly the groups of 32 threads or fewer. All 158 are in groups of 33 to 64 threads (156 at 64, 2 at 48). I ran one more pass with four candidate widenings measured per program:

widening fired programs it would clear
follow scalar loads (s_load_dword*, s_buffer_load_dword*) whose base and offset are uniform 66
drop the "block every lane reaches" condition, keeping "unguarded" 0 more
both, plus any lane-pure operation 0 more

So one change does it: a scalar load at a uniform address is uniform (it reads the same memory for the whole wave). That takes the rule from 158 programs to 92, and the 66 are the ones the first review counted as reading only uniform values. The other two widenings are sound as far as I can see but buy nothing on this title, so I would leave them out.

Why I hold it until then: each program the rule moves gets a group barrier pair at every first-lane read and every EXEC or VCC branch, and there is an open report of a 30-series card hanging in a barrier-heavy split program. 66 programs paying that for nothing is worth one more commit.

With the scalar loads in, I will take your commit, with your authorship, onto the branch after Marvisak#439 (shader/wave-per-group); this PR's base (shader/console-flow-lds) is not taking new behaviour while Marvisak#439 waits for review, so either retarget it or I cherry-pick. If you would rather I add the load case on top myself, say so.

Still open and mine: on a host that runs both wave sizes (the Radeon here) these programs stay undeclared and run in 32-lane waves, with the same wrong lanes; declaring WaveSize(64) there for a varying first-lane read is the fix.

AI-assisted (Claude Code).

swarm and others added 2 commits November 24, 2026 13:30
… halves

A Wave64 first-lane read at entry can return lane 32's value in the
upper half on a 32-lane host. In divergent flow the two host halves can
likewise select different active lanes.

Prefer the existing console-flow exchange for an ordinary first-lane
read in a 33–64-thread group when straight-line scalar and vector
arithmetic cannot prove its input uniform. Waterfalls and existing
wave-size requirements retain their own routing. Keep the per-lane retry
when the console form declines a joined descriptor.

Pin the DXIL wave sizes and fallback route, and check the entry,
divergent and uniform cases with two groups on a 32-lane adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MichaelSmithAI
MichaelSmithAI changed the base branch from shader/console-flow-lds to shader/wave-per-group November 24, 2026 18:32
@MichaelSmithAI
MichaelSmithAI force-pushed the wave64-first-lane-single-group branch from b5f052b to 512ce75 Compare November 24, 2026 18:32
@MichaelSmithAI

Copy link
Copy Markdown
Author

Retargeted to shader/wave-per-group and incorporated its mask-join changes through 88b38e8. The scalar-load change is 512ce75, and the combined PR head is f3f61f9.

The proof now follows s_load_dword* and s_buffer_load_dword* when every dword of the base or descriptor and the scalar offset are already uniform. The existing reach and guard checks still apply.

The new regression loads a four-dword descriptor from the second table slot, then a pair of dwords from its buffer at a group-dependent offset. It reads the second returned dword through a VGPR with v_readfirstlane_b32. On the prior rule the shader requested wave size 32. On this head it stays undeclared at 48 and 64 threads, and the RTX 4090 returned 37 in every lane of group 0 and 91 in every lane of group 1 at both sizes. An offset outside the proof still requests wave size 32.

All 16 focused first-lane and mask tests passed under Wine/GE, and both Windows-target workspace Clippy configurations passed. The 66-program reduction is your title-pack projection. I haven't rerun that pack or measured frame time.

MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
…ads across halves

Port of r4tedx/VIctory shader/wave-per-group plus r4tedx#12 (MichaelSmithAI, f3f61f9), the follow-ups to upstream JMAN730#439:

- Groups of several wave64 console waves without LDS are emitted as one
  host group of 64 threads per console wave (HostShader::group_waves);
  direct dispatches multiply x (within 65535), indirect ones of such
  pipelines are dropped until the clamp pass multiplies the records.
  These were 43 programs on a 32-lane host (16x16, 1024x1, 8x16 ...).
- A v_readfirstlane whose source may differ between the host halves
  takes the console flow in groups of 33-64 threads (a plain read in the
  upper half returned lane 32, not 0). Uniform scalar loads stay per-lane.
- A mask joined across divergent paths and read as a number takes the
  console flow (moey424); whole-wave sections in groups of 32 threads or
  fewer.

Co-authored-by: Swarm <swarm@users.noreply.github.com>
Co-authored-by: moey424 <mohammad.allami75@gmail.com>
Co-authored-by: CitizenCInema <fredericlaura.fl@pm.me>

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Owner

First a correction, and an apology: my advice last round was wrong. I told you that following scalar loads would clear 66 of the 158 programs. I had misread my own diagnostic: the bit pattern I took for "scalar loads alone" meant "scalar loads, plus dropping the reach condition, plus any lane-pure operation, together". You implemented exactly what I asked, correctly, and it clears none: the rule still fires for the same 158 programs. That one is on me, not on 512ce75.

What the final check of f3f61f9 found

  • Sound, again. The load rule admits scalar loads only (s_load_dword*, s_buffer_load_dword*; never a vector load, s_scratch_load_dword, s_memtime), requires every base or descriptor dword and the offset register to be uniform, marks exactly the dwords written, and still sits behind the reach and guard tests. Nine more hand-assembled programs at 48 and 64 threads on an RTX 5070 (overlapping destinations, register plus immediate offsets, an offset joined after a divergent branch, a load inside a branch only upper-half lanes take, a loaded dword plus the lane number, an offset from a loop counter, a proven read's result as the next load's offset): no wrong lane, and none called uniform that varies.
  • The merge with 88b38e8 composes: only the test file conflicted and both sides are kept; your rule (a wave64 program on a host without 64-lane waves) and the joined-mask rule (a host that runs the program's wave size) never fire for the same program; the per-lane fallback is intact.
  • Both CI clippy commands clean; cargo test --workspace --locked: 392 passed, 0 failed, 6 ignored. Over the title's shader pack (local title data, offline, CPU only, counts only) no program changes outcome.

So nothing is wrong with it. It still moves 139 programs into the barrier form, and this time I measured what actually stands between the proof and the 66, per program.

What blocks the proof (158 fired programs; each line counts programs in which that instruction is on the frontier of the read's sources):

blocker programs kind
v_mul_legacy_f32 89 operation not listed
v_cvt_u32_f32 87 operation not listed
v_rcp_iflag_f32 67 operation not listed
v_cvt_f32_u32 52 operation not listed
s_buffer_load_dword*, s_load_dwordx4, s_add_i32, s_and_b32, s_mov_b32, s_mul_i32, v_mov_b32, v_add_f32 ... 48 and fewer each listed, but in a block not every lane reaches
v_readfirstlane_b32 44 a first-lane read of a source that is itself uniform
v_cndmask_b32 33 operation not listed
v_cvt_i32_f32, v_cvt_f32_ubyte0, v_rcp_f32, v_fma_f32, float compares 25 and fewer each operation not listed
buffer_load_format_x, image_load, a VGPR input, a join 83 programs have at least one these can vary: the real candidates

What each widening would clear (projected from that listing, each program counted once):

widening programs cleared of 158
more operations, reach condition kept 19
more operations and the reach condition dropped (keeping "unguarded") 57
the same, plus the result of a first-lane read of a proven source 66
the same, plus v_cndmask_b32 when its selector and both operands are proven 75

"More operations" means any VALU or scalar operation whose result depends only on its operands in the same lane: the float conversions and arithmetic above are the bulk. A denylist would be shorter than the list: operations that read another lane (DPP forms, v_readlane, permutes, swizzles), that read EXEC or VCC as a lane's bit, v_cndmask unless its selector is proven, and anything with a side effect. Dropping the reach condition is sound for the reason the unguarded test already relies on: a value written in a block only some lanes reach is the same in every lane that is there, and a later read by the others goes through a join, which is never admitted.

That would leave the rule at 92 programs (83 with the select), all of them with a genuinely per-lane source. I will verify with one count pass, as before.

Because the last round's miss was mine: if you would rather not do another, say so and I will merge f3f61f9 as it is and widen it on top myself.

AI-assisted (Claude Code).

@MichaelSmithAI

Copy link
Copy Markdown
Author

Thanks for rerunning the corpus and correcting the count. Please go ahead and merge f3f61f9 into shader/wave-per-group as offered. Your nine additional GPU cases and the workspace run give us a good baseline for measuring the wider proof separately.

For the reach-condition follow-up, one regression worth keeping is a VGPR initialized to 0, overwritten with 42 only on lanes 10 and above, then read with v_readfirstlane_b32 after EXEC is restored. The expected result is 0 in both halves of a 64-lane console wave. We previously saw the upper half return 42 with a looser classifier on a related branch. We exercised both an explicit branch and a straight-line EXEC save/restore. That pair should help check the join assumption as the proof expands.

@r4tedx r4tedx left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the merge 6db13d8 (after #17 landed in shader/wave-per-group). Static review: I read the resolution and did not compile or run it.

  • Both sides' tests are kept, each once: 40 split tests and 8 waterfall tests. That covers this PR's first-lane cases (Wave32 readback, uniform scalar loads, wave-size declaration checks, larger-group admission, waterfall fallback) and #17's full- and partial-wave exit-mask cases.
  • The hardware readbacks now go through #17's hardware_adapters(), which returns no adapters under VICTORY_WARP=1. The partial-wave case still requires reported_target().runs(32).
  • lower.rs is identical to this PR's parent. host.rs and victory-gpu match the base (6551f22). The net diff against the base is lower.rs, the two test files and docs/wave64-on-wave32.md.

Approving the resolution. Not merging yet: CI was still running when I checked.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants