Skip to content

Shaders: compute threads that read other lanes keep unique ids in 2-D groups (NVIDIA) - #2126

Open
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shader/host-wave-lane-probe
Open

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shader/host-wave-lane-probe

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

The problem

Some console compute programs read values from other lanes of their wave (v_mbcnt, v_readlane, DPP, ds_swizzle and similar). For those to work on a PC GPU, each thread's lane on the host must be its lane on the console. VIctory arranges that by renumbering the threads of such programs (Lower::thread_id): a thread's flat id becomes

(first flat id in my host wave, rounded down to 32) + my host lane

That gives every thread a different id only if each host wave holds 32 consecutive flat ids, starting at a multiple of 32, which is how the console fills its waves.

NVIDIA does not always do that. Probed on an RTX 5070 Ti (driver 621.24), for 2-D groups 24, 32 or 64 threads wide, or not a power of two wide, the driver fills a wave with a 16x2 tile of the group instead of a row:

group 32x2:   wave 0 holds ids 0-15 and 32-47
              wave 1 holds ids 16-31 and 48-63

Both waves round down to the same base (0 and 16 both round to 0), so two threads end up with every id and half the ids are never used. The program then computes and stores the same results twice and never writes the other half. Measured with a translated program that stores 1 at its own thread id:

group shape ids stored on main
64x1, 8x8, 16x16 all (these shapes are laid out in rows)
32x2 32 of 64
32x8 128 of 256
64x4 128 of 256
24x8 128 of 192
12x12 96 of 144
10x10 68 of 100

XCL's public notes report that an AMD RX 9070 lays 2-D groups out as 2x2 quads, which would break 8x8 groups there too.

The fix

The renumbering no longer guesses from where a wave starts. It hands each wave its block of 32 ids explicitly:

  • 1-D groups ([n, 1, 1]) keep the old rounding. Every host measured fills them in order, and the new test checks that it does.
  • 2-D and 3-D groups take their ids by ticket:
    1. Every thread writes 0 to a counter in group-shared memory, then waits at a group barrier.
    2. In each full wave, the first lane adds 32 to the counter; the value it read is the wave's base, and the wave shares it (WaveReadLaneFirst).
    3. The group's one partial wave (its last threads, which hosts put in lanes 0 and up) takes the last block.
    4. A thread's id is its wave's base plus its host lane.

Each wave gets a different block, so every id is used exactly once however the GPU arranges the threads. It is computed once, at the start of the program; the cost is one barrier and one group-shared atomic per wave, and only in 2-D and 3-D programs that read other lanes.

Testing

Two new tests in emit::lane_order_tests:

  • relabelled_threads_each_get_their_own_id: translates a console wave32 program that reads lanes (v_mbcnt) and stores 1 at its thread id, runs it in the nine shapes above, and checks every id is stored exactly once. It fails on main (the table above) and passes with the change.
  • host_waves_keep_what_the_relabelling_relies_on: probes the GPU itself with a plain HLSL program (built with the Windows SDK's dxc.exe; VICTORY_PC_DXC points elsewhere) and prints each shape's wave layout. It checks the two things the fix relies on: 1-D groups fill waves in order, and each group has at most one partial wave, filled from lane 0. Skipped without dxc; WARP refuses WaveSize(32) and is skipped.

cargo test -p victory-shader --lib on the RTX 5070 Ti passes except the three tests that fail on main there too (two stride_tests and gathers_at_a_level_match_the_hardware, fixed by #1887 and #1777). Clippy is clean with and without diagnostics.

Not checked on AMD hardware or in a title run.

Related

#2117 renumbers the threads of the programs it lets drop their WaveSize using the same "waves hold aligned runs" assumption, so the shapes above break there too. Whichever of the two lands second should take its ids the same way.

…yout

A compute program that reads other lanes is relabelled so that each
thread's host lane is its console lane (`Lower::thread_id`): its flat id
was its wave's first flat id rounded down to the wave size, plus its lane
index. That is a bijection only while each host wave holds one aligned
run of flat ids. Measured on an RTX 5070 Ti (driver 621.24), it does not
for 2-D groups 24, 32 or 64 wide or not a power of two wide: the driver
fills waves with 16 by 2 tiles (ids 0-15 and 32-47 in one wave), so two
waves round to one base. A 32 by 2 group gave its 64 threads 32 ids;
32 by 8, 64 by 4, 24 by 8, 12 by 12 and 10 by 10 lost a quarter to a half
of theirs, threads computing and storing for the same console thread.
(XCL's public notes report 2 by 2 quads on an RX 9070, which would break
8 by 8 groups too.)

1-D groups keep the rounding: hosts put them into waves in order. Other
groups take ids by ticket: every thread zeroes a group-shared counter,
a barrier, then each full wave's first lane adds the wave size to it and
the wave takes the old value as its base; the one partial wave (the
group's last threads, its lanes from 0) takes the last run. Computed
once, in the entry block.

New tests (`emit::lane_order_tests`):
- `relabelled_threads_each_get_their_own_id` translates a console wave32
  program that reads lanes and stores 1 at its thread's flat id, in nine
  group shapes, and checks every id is stored once. On main six shapes
  fail (32 by 2: 32 of 64); with the change all pass.
- `host_waves_keep_what_the_relabelling_relies_on` probes the host with a
  plain HLSL program (the Windows SDK's dxc), printing each shape's wave
  layout, and checks 1-D groups are aligned runs and each group has at
  most one partial wave, its lanes from 0. Skipped without dxc.
@0xkn0wh3r3 0xkn0wh3r3 changed the title Shaders: relabelled compute threads keep their own ids in any wave layout Shaders: compute threads that read other lanes keep unique ids in 2-D groups (NVIDIA) Nov 25, 2026
@DrDarkDK

Copy link
Copy Markdown

I ran the two lane_order_tests on native Windows with a Ryzen 9 7900X Radeon iGPU (32.0.11024.2) and an RTX 4070 Ti (621.01). I changed only the local test adapter selector to choose each GPU explicitly and verified the D3D12 device’s adapter LUID.

Retaining the PR’s tests but using the parent 940d32f lowering, both GPUs missed the same six of nine group shapes. For example, 32×2 marked 32/64 IDs, 24×8 marked 128/192, and 10×10 marked 68/100. With e214ce4, both tests passed on each GPU, and all nine shapes marked every ID.

Synthetic tests only. WARP refused WaveSize(32) and was skipped.

AI-assisted with Codex.

@fiestorra

Copy link
Copy Markdown
Contributor

This and #2126 rewrite the same arithmetic, they conflict, and one may be a precondition of the other

Two pull requests in the same window are changing how a thread's lane is arranged for cross-lane reads, and they are not independent.

Measured: they conflict, in the one file where it matters:

#2126 + #2117  → conflict in crates/victory-shader/src/emit/lower.rs

#2126 (console_flat, "compute threads that read other lanes keep unique ids in 2-D groups") changes how a thread's console flat id is derived, using a group-shared ticket, because a host may put threads of several rows into one wave; #2117 changes whether a wave size is declared at all, adding min_lanes and a Stop::Wide re-lowering for programs that only read within aligned groups of 4, 8, 16 or 32.

The question that decides how they land is about your premise for narrow groups. The argument for "an aligned group of k lanes never straddles a host wave" needs the host to fill its waves from consecutive flat ids in group order. #2126's description says that is not what some hosts do: an RTX 5070 Ti fills 2-D groups in 16 × 2 tiles ("24, 32 or 64 wide or not a power of two wide"), and XCL's notes report 2 × 2 quads on an RX 9070. If threads of one aligned group land in different host waves, then a narrow-group read is not within one wave after all, and #2126's ticket is what makes the ids line up again.

So:

I am not asking for both to be merged together; I am asking for the sentence that says which mechanism owns the 2-D case. At the moment two of them claim it and they disagree by construction.

A note on the evidence, since it decides what a reviewer can check: translate --validate will not show this. A declared or missing WaveSize is refused by the host when the pipeline is created, so the number that moves is the --pipelines count (or a boot), not the validated count. If either description has a before/after of that count on a host whose lane count differs from the console's, it would settle the review in one line; on this machine (NVIDIA, 32 lanes, 32.0.16.2124) I can measure it for whichever of the two lands first, and for the pair once they agree.

No game data is involved: file names, hosts and counts.

Written with an AI assistant.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. Compute threads reading other lanes keep unique ids — lane-id uniqueness fix for cross-lane reads.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib emit::lane_order_tests:: — 2 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (184 files ok)

Clean NVIDIA-verified fix keeping unique thread IDs in 2-D compute groups that read other lanes. Looks great.

AI-assisted (Gemini 4 Neon).

@0xkn0wh3r3

Copy link
Copy Markdown
Author

@DrDarkDK thanks. A Radeon iGPU missing the same six shapes as the 4070 Ti means this isn't NVIDIA-only.

@fiestorra, on which mechanism owns the 2-D case: the ticket does, and it covers #2117's narrow groups too, whatever G is.

The failure isn't a lane group straddling host waves. Once ids are relabelled, a console lane is a host lane: a thread's id is its wave's base plus waveGetLaneIndex. So an aligned group of G ≤ host-lanes console lanes is always G lanes of one host wave, on any tiling. What breaks on a tiled host is the base. With 16×2 tiles, wave 0 starts at flat id 0 and wave 1 at flat id 16. Rounding both down to the wave width gives both the same base, so every id appears twice and half are never used. That happens for every G. It even happens with no cross-lane read beyond v_mbcnt, which is why the test here stores at its own id and reads nothing else.

So no size is safe without the ticket on a host that tiles 2-D groups. #2117's relabelling ("a host wave holds an aligned run of flat thread ids") makes the same assumption as thread_id on main, and shares the failure. On the conflict: keep the ticket, and in #2117's narrow path take the block size from waveGetLaneCount() instead of the console's N. Each host wave then takes one block of host-lane-count ids, and #2117's G argument holds as written. 1-D groups can keep the rounding, since every host probed fills them in order. Either order works; whichever lands second makes that change.

On the evidence: #2126 doesn't change whether WaveSize is declared, so --pipelines won't move for it. Its before/after is the stored-id count in relabelled_threads_each_get_their_own_id, which you can measure on any host that tiles. So far it fails on main and passes here on an RTX 5070 Ti, an RTX 4070 Ti and a Radeon iGPU, and it passes here on an RTX 5090.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The fix looks sound. For 2-D and 3-D groups, Lower::console_flat (crates/victory-shader/src/emit/lower.rs) gives each full host wave a wave-sized block of ids from a group-shared ticket, and the one partial wave gets the last block. 1-D groups keep the old rounding. An independent run reproduced the duplicate ids on main and the fix on an RTX 4070 Ti and a Radeon iGPU.

This needs the maintainer's decision: the new test host_waves_keep_what_the_relabelling_relies_on runs an external dxc.exe, taken from VICTORY_PC_DXC or the newest Windows SDK. diff_tests.rs already does the same, but the PR still needs the maintainer's sign-off on it. Also worth confirming: the ticket adds one dword of groupshared memory. A 2-D program that reads lanes and already uses the full 32 KiB would then fail validation where it translates today, so in that case falling back to the rounding, or reporting it, would keep such programs translating. Conflicts with #2117 in lower.rs. CI has not run on this head.


Generated by Claude Code

@KainanP

KainanP commented Nov 25, 2026

Copy link
Copy Markdown

On an Intel UHD (Xe-LP, 16-lane waves), head e214ce40 merged onto main 940d32f makes relabelled_threads_each_get_their_own_id panic with [64, 1]: The parameter is incorrect instead of skipping, because the WaveSize(32) pipeline is refused on that host. Your probe's hardware leg ran there and shows most 2-D groups in 4×4 tiles. The layouts are in item 5 of #1829 (comment)

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants