Shaders: model the compute thread-group info SGPR's wave count #521

Open
github-bot wants to merge 3 commits from github-bot/gh-521 into main AGit
Owner

@⁠AnoHunted on GitHub · 2026-11-24 04:26 UTC · original
AnoHunted:thread-group-info-sgpr → main · +145 −2 · 2 files · 3 commits · open

What

A compute program with COMPUTE_PGM_RSRC2.TG_SIZE_EN (bit 10) gets the thread-group info SGPR right after the group ids. Lowering refused every such program (the thread-group info SGPR). It now supplies the value's low field, the waves in the group (ceil(threads / wave size), bits 5:0), when every reader of the SGPR is an s_and_b32 with a mask inside bits 5:0 or an s_bfe_u32 field inside bits 5:0.

Anything else keeps refusing, with a more precise reason: the thread-group info SGPR above the wave count. The rest of the register is not derived: per LLVM's AMDGPU docs it is {first_wavefront, 14'b0, ordered_append_term[10:0], threadgroup_size_in_wavefronts[5:0]}, and the first-wave flag and the ordered-append term cannot be produced correctly from the DXIL group model. Nothing else changes.

The change is in emit/lower.rs (group_info). valu_tests.rs gets a small refactor (lower_valu_cs takes rsrc2 and the thread counts and returns the lowering error; build_valu_cs calls it unchanged).

Why

#293 ranks a hardware SGPR input at ~37 lost draws/dispatches (2 shaders). The reason string is the fallback for any system SGPR the translator has no value for; I do not have those shaders and cannot say which one they read. The thread-group info SGPR is the one compute system SGPR that was recognised but refused, so this removes it from the unmodelled set where its use is the wave count. Other candidates (scratch wave offset, which needs scratch support, #265; the first-wave flag / ordered-append bits) are not touched. Honest caveat: this may not be what those 2 shaders read.

Sources: LLVM AMDGPUUsage, "Initial Kernel Execution State" (SGPR order: user, work-group id X/Y/Z, work-group info, scratch wavefront offset; work-group info layout above); AMDHSAKernelDescriptor.h (COMPUTE_PGM_RSRC2 bit 10 = workgroup info enable).

Tested

Windows 11, NVIDIA GeForce RTX 5080 and WARP, Rust 1.98.1, VICTORY_DXIL set so DXIL validation runs, --test-threads=1.

  • New thread_group_info_wave_count_matches_the_group: synthetic gfx10 compute programs (s_and_b32 s7, s6, 63 and s_bfe_u32 of bits 5:0), 1/32/33/64/96/256 threads, wave32, translated and run on both backends; results 1/1/2/2/3/8 as expected. thread_group_info_bits_above_the_wave_count_are_refused: a raw read and a 0xFFFF mask are refused.
  • cargo test -p victory-shader thread_group_info: branch 2 passed; main has no such tests (0 run). On main the same programs hit the unconditional refusal in Input::GroupInfo (by reading the code; the tests were not run against main).
  • cargo test --workspace -- --test-threads=1: main 264 passed, 0 failed, 4 ignored; branch 266 passed, 0 failed, 4 ignored.

Not tested

AMD hardware. wave64 programs (count formula uses the program's wave size, not exercised). The 2 shaders of #293 (not available).

No game files were used.

**@⁠AnoHunted** on GitHub · 2026-11-24 04:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/521) `AnoHunted:thread-group-info-sgpr` → `main` · +145 −2 · 2 files · 3 commits · open ## What A compute program with `COMPUTE_PGM_RSRC2.TG_SIZE_EN` (bit 10) gets the thread-group info SGPR right after the group ids. Lowering refused every such program (`the thread-group info SGPR`). It now supplies the value's low field, the waves in the group (`ceil(threads / wave size)`, bits 5:0), when every reader of the SGPR is an `s_and_b32` with a mask inside bits 5:0 or an `s_bfe_u32` field inside bits 5:0. Anything else keeps refusing, with a more precise reason: `the thread-group info SGPR above the wave count`. The rest of the register is not derived: per LLVM's AMDGPU docs it is `{first_wavefront, 14'b0, ordered_append_term[10:0], threadgroup_size_in_wavefronts[5:0]}`, and the first-wave flag and the ordered-append term cannot be produced correctly from the DXIL group model. Nothing else changes. The change is in `emit/lower.rs` (`group_info`). `valu_tests.rs` gets a small refactor (`lower_valu_cs` takes `rsrc2` and the thread counts and returns the lowering error; `build_valu_cs` calls it unchanged). ## Why #293 ranks `a hardware SGPR input` at ~37 lost draws/dispatches (2 shaders). The reason string is the fallback for any system SGPR the translator has no value for; I do not have those shaders and cannot say which one they read. The thread-group info SGPR is the one compute system SGPR that was recognised but refused, so this removes it from the unmodelled set where its use is the wave count. Other candidates (scratch wave offset, which needs scratch support, #265; the first-wave flag / ordered-append bits) are not touched. Honest caveat: this may not be what those 2 shaders read. Sources: LLVM AMDGPUUsage, "Initial Kernel Execution State" (SGPR order: user, work-group id X/Y/Z, work-group info, scratch wavefront offset; work-group info layout above); `AMDHSAKernelDescriptor.h` (`COMPUTE_PGM_RSRC2` bit 10 = workgroup info enable). ## Tested Windows 11, NVIDIA GeForce RTX 5080 and WARP, Rust 1.98.1, `VICTORY_DXIL` set so DXIL validation runs, `--test-threads=1`. - New `thread_group_info_wave_count_matches_the_group`: synthetic gfx10 compute programs (`s_and_b32 s7, s6, 63` and `s_bfe_u32` of bits 5:0), 1/32/33/64/96/256 threads, wave32, translated and run on both backends; results 1/1/2/2/3/8 as expected. `thread_group_info_bits_above_the_wave_count_are_refused`: a raw read and a 0xFFFF mask are refused. - `cargo test -p victory-shader thread_group_info`: branch 2 passed; main has no such tests (0 run). On main the same programs hit the unconditional refusal in `Input::GroupInfo` (by reading the code; the tests were not run against main). - `cargo test --workspace -- --test-threads=1`: main 264 passed, 0 failed, 4 ignored; branch 266 passed, 0 failed, 4 ignored. ## Not tested AMD hardware. wave64 programs (count formula uses the program's wave size, not exercised). The 2 shaders of #293 (not available). No game files were used.
A compute program with COMPUTE_PGM_RSRC2.TG_SIZE_EN reads the thread-group
info SGPR after the group ids; lowering refused it outright. Bits 5:0 hold
the waves in the group (LLVM AMDGPUUsage, "Initial Kernel Execution State":
{first_wavefront, 14'b0, ordered_append_term[10:0],
threadgroup_size_in_wavefronts[5:0]}), which is ceil(threads / wave size).

The value is given only when every reader is an s_and_b32 mask or an
s_bfe_u32 field confined to bits 5:0; the first-wave flag and the
ordered-append term are not derived, so anything else still refuses, now
with "the thread-group info SGPR above the wave count".
The use check looked only at instructions that read the entry value, so a phi (or a copy) carrying it to a later full-width read passed. Follow phi results and s_mov_b32 copies transitively and accept only s_and_b32 masks (on either operand side) and s_bfe_u32 fields within bits 5:0.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:07:01 +00:00
Author
Owner

@⁠Raxstreams on GitHub · 2026-11-24 04:41 UTC · original

I ported the group-info change and both synthetic tests into a Windows integration. Both tests passed, including the GPU wave-count cases and the refusal of high-field reads. I haven't found a matching refusal in the captured first-scene shaders on this build, so I can't attribute a title pipeline or rendering change to this PR.

**@⁠Raxstreams** on GitHub · 2026-11-24 04:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/521#issuecomment-7122251389) I ported the group-info change and both synthetic tests into a Windows integration. Both tests passed, including the GPU wave-count cases and the refusal of high-field reads. I haven't found a matching refusal in the captured first-scene shaders on this build, so I can't attribute a title pipeline or rendering change to this PR.
Author
Owner

@⁠Dathinel on GitHub · 2026-11-24 05:02 UTC · original

I found a full-width read that group_info accepts on 6ff46736. Its use check walks instruction reads. When the entry value feeds a phi, the next instruction reads the phi result, so it passes that check.

In the existing lower_valu_cs fixture, set TG_INFO_RSRC2 and [64, 1, 1]. With these four words as the body, the helper emits a shader:

0xBF06_0100,             // s_cmp_eq_u32 s0, s1
0xBF85_0001,             // s_cbranch_scc1 join
s_mov_b32(6, 128),       // s6 = 0 on the fallthrough path
vop1(0x001, 0, sgpr(6)), // join: v0 = s6; the fixture stores all 32 bits

On the taken path the store can observe bits above 5:0, while the lowering supplies only the wave count. Could the use check follow phi results before accepting the input? There is a positive control worth preserving: insert s_and_b32 s7, s6, 63 at the join and read s7 in the move. The current PR accepts that masked case. A local guard that rejected every phi input rejected it as well.

Tested with synthetic inputs on Windows using cargo test --frozen -p victory-shader --features host. The observation concerns what the translator accepts. I have no native RDNA2 comparison for this case. AI assistance was used to prepare and review the reproducer.

**@⁠Dathinel** on GitHub · 2026-11-24 05:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/521#issuecomment-7122572153) I found a full-width read that `group_info` accepts on `6ff46736`. Its use check walks instruction reads. When the entry value feeds a phi, the next instruction reads the phi result, so it passes that check. In the existing `lower_valu_cs` fixture, set `TG_INFO_RSRC2` and `[64, 1, 1]`. With these four words as the body, the helper emits a shader: ```rust 0xBF06_0100, // s_cmp_eq_u32 s0, s1 0xBF85_0001, // s_cbranch_scc1 join s_mov_b32(6, 128), // s6 = 0 on the fallthrough path vop1(0x001, 0, sgpr(6)), // join: v0 = s6; the fixture stores all 32 bits ``` On the taken path the store can observe bits above 5:0, while the lowering supplies only the wave count. Could the use check follow phi results before accepting the input? There is a positive control worth preserving: insert `s_and_b32 s7, s6, 63` at the join and read `s7` in the move. The current PR accepts that masked case. A local guard that rejected every phi input rejected it as well. Tested with synthetic inputs on Windows using `cargo test --frozen -p victory-shader --features host`. The observation concerns what the translator accepts. I have no native RDNA2 comparison for this case. AI assistance was used to prepare and review the reproducer.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 10:19 UTC · original

Independent check of head 6ff4673, on its own base and merged onto main ffe310e. I read the comments above first. @⁠Dathinel's phi finding reproduces here on hardware; the rest adds a reference and some edge cases.

It no longer merges. main now has #432 in the same match arm of lower.rs and new tests at the end of valu_tests.rs; both files conflict. The resolution is mechanical (keep Reg::S(n) => match io_inputs.get(&v) from main and put the Some(Input::GroupInfo) arm first; keep both sets of tests), and everything below was run on that resolution.

The phi case, run. @⁠Dathinel's four words in lower_valu_cs with TG_INFO_RSRC2 and [64, 1, 1]:

body s0 == s1 (info reaches the store) s0 != s1
v_mov_b32 v0, s6 at the join lowers; stores 2 on RTX 3070 and WARP 0
s_and_b32 s7, s6, 63 at the join, v0 = s7 2 0

So the full-width read is accepted and gets the bare wave count; on the console the same store has the first-wave flag and the bits above 5:0 in it. The control is fine. Following phi results in the use check (or refusing an info value that reaches any phi) closes it.

References

  • LLVM AMDGPUUsage, "Initial Kernel Execution State" (release/18.x): Work-Group Info ... {first_wavefront, 14'b0000, ordered_append_term[10:0], threadgroup_size_in_wavefronts[5:0]}, placed after Work-Group Id X/Y/Z and before the scratch wavefront offset. The field layout and the SGPR's position in the PR match it.
  • Mesa ACO (aco_instruction_selection.cpp, tag mesa-24.0.0, lines 8328-8341) reads this register in one place only, for the local invocation index: "The tg_size bits [6:11] contain the subgroup id", taken with s_bfe_u32 ..., 0x6 | 0x6 << 16 on wave32 and s_and_b32 0xfc0, tg_size on wave64. It never reads bits 5:0. That is a PC driver, not the console compiler, but it suggests the two #293 programs, if this is the SGPR they read, want the wave index rather than the wave count, and those reads are still refused here (probe below). Nobody has the two shaders' reason at instruction level yet, so this stays a guess.

Edge cases (RTX 3070 and WARP agree on every row that runs):

program result
s_and_b32 s7, s6, 63, threads [8, 8, 4] 8
same, [1024, 1, 1] and [32, 32, 1] 32
s_and_b32 s7, s6, 7, 256 threads 0
s_bfe_u32 offset 2 width 4, 256 threads 2
s_bfe_u32 offset 3 width 4 (reaches bit 6) refused
s_bfe_u32 offset 6 width 6 (ACO's subgroup id) refused
s_and_b32 s7, 63, s6 (mask as the first operand, the order ACO writes) refused
s_mov_b32 s7, s6 then s_and_b32 s7, s7, 63 refused
v_mov_b32 v0, s6, no join refused

The last four refusals are conservative, not wrong. The two middle ones are cheap to accept if a real program turns out to need them: the mask on either side, and a plain copy followed like the phi.

Not covered: wave64. group_info divides by self.wave(), so a wave64 program gets ceil(threads / 64), which is what the field means, but both tests and my probes are wave32.

Suite. NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1, SDK 10.0.26100 dxil.dll validating, cargo test -p victory-shader -- --test-threads=1: main ffe310e + this PR, 122 passed, 0 failed, 1 ignored (the bitcode benchmark), including the PR's two tests.

Not checked: the title (no game files here), AMD hardware.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-24 10:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/521#issuecomment-7128621169) Independent check of head `6ff4673`, on its own base and merged onto `main` `ffe310e`. I read the comments above first. @⁠Dathinel's phi finding reproduces here on hardware; the rest adds a reference and some edge cases. **It no longer merges.** `main` now has #432 in the same `match` arm of `lower.rs` and new tests at the end of `valu_tests.rs`; both files conflict. The resolution is mechanical (keep `Reg::S(n) => match io_inputs.get(&v)` from `main` and put the `Some(Input::GroupInfo)` arm first; keep both sets of tests), and everything below was run on that resolution. **The phi case, run.** @⁠Dathinel's four words in `lower_valu_cs` with `TG_INFO_RSRC2` and `[64, 1, 1]`: | body | `s0 == s1` (info reaches the store) | `s0 != s1` | |---|---|---| | `v_mov_b32 v0, s6` at the join | lowers; stores `2` on RTX 3070 and WARP | `0` | | `s_and_b32 s7, s6, 63` at the join, `v0 = s7` | `2` | `0` | So the full-width read is accepted and gets the bare wave count; on the console the same store has the first-wave flag and the bits above 5:0 in it. The control is fine. Following phi results in the use check (or refusing an info value that reaches any phi) closes it. **References** - LLVM `AMDGPUUsage`, "Initial Kernel Execution State" (release/18.x): `Work-Group Info ... {first_wavefront, 14'b0000, ordered_append_term[10:0], threadgroup_size_in_wavefronts[5:0]}`, placed after Work-Group Id X/Y/Z and before the scratch wavefront offset. The field layout and the SGPR's position in the PR match it. - Mesa ACO (`aco_instruction_selection.cpp`, tag `mesa-24.0.0`, lines 8328-8341) reads this register in one place only, for the local invocation index: "The tg_size bits [6:11] contain the subgroup id", taken with `s_bfe_u32 ..., 0x6 | 0x6 << 16` on wave32 and `s_and_b32 0xfc0, tg_size` on wave64. It never reads bits 5:0. That is a PC driver, not the console compiler, but it suggests the two #293 programs, if this is the SGPR they read, want the wave index rather than the wave count, and those reads are still refused here (probe below). Nobody has the two shaders' reason at instruction level yet, so this stays a guess. **Edge cases** (RTX 3070 and WARP agree on every row that runs): | program | result | |---|---| | `s_and_b32 s7, s6, 63`, threads `[8, 8, 4]` | 8 | | same, `[1024, 1, 1]` and `[32, 32, 1]` | 32 | | `s_and_b32 s7, s6, 7`, 256 threads | 0 | | `s_bfe_u32` offset 2 width 4, 256 threads | 2 | | `s_bfe_u32` offset 3 width 4 (reaches bit 6) | refused | | `s_bfe_u32` offset 6 width 6 (ACO's subgroup id) | refused | | `s_and_b32 s7, 63, s6` (mask as the first operand, the order ACO writes) | refused | | `s_mov_b32 s7, s6` then `s_and_b32 s7, s7, 63` | refused | | `v_mov_b32 v0, s6`, no join | refused | The last four refusals are conservative, not wrong. The two middle ones are cheap to accept if a real program turns out to need them: the mask on either side, and a plain copy followed like the phi. **Not covered:** wave64. `group_info` divides by `self.wave()`, so a wave64 program gets `ceil(threads / 64)`, which is what the field means, but both tests and my probes are wave32. **Suite.** NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1, SDK 10.0.26100 `dxil.dll` validating, `cargo test -p victory-shader -- --test-threads=1`: `main` `ffe310e` + this PR, **122 passed, 0 failed, 1 ignored** (the bitcode benchmark), including the PR's two tests. Not checked: the title (no game files here), AMD hardware. AI-assisted (Claude Code).
Author
Owner

@⁠AnoHunted on GitHub · 2026-11-24 10:40 UTC · original · edited 2026-11-24 10:45 UTC

Thanks @⁠Dathinel for the reproducer, and @⁠upipibi for the independent check and the rebase notes.

Fixed in a new commit on top of the rebased branch:

  • The use check now follows the entry value. It goes through phi results and plain s_mov_b32 copies before accepting it. Every reader it reaches must be an s_and_b32 whose mask fits in bits 5:0, or an s_bfe_u32 field within bits 5:0. The mask may be in either operand, so ACO's s_and_b32 s7, 63, s6 order is accepted. Everything else is refused, including the full-width read after the join.
  • New tests.
    • Refused: Dathinel's phi case, and a read through a copy.
    • Accepted: the masked-at-join control, a copy followed by the mask, and the mask-first order.
    • The two existing tests are unchanged.
  • Rebased onto current main. It keeps #432's Reg::S(n) arm, with the GroupInfo arm first.

Tested on NVIDIA GeForce RTX 5080, Windows 11, Rust 1.98.1, main and branch side by side: cargo test --workspace -- --test-threads=1 goes from 342 passed on main to 347 here, 0 failed. Wave64 is still untested.

As @⁠upipibi points out, the two #293 programs may read the wave index (bits 11:6 in ACO's use) rather than the wave count. This PR still refuses that, since nobody has the instruction-level reason yet.

**@⁠AnoHunted** on GitHub · 2026-11-24 10:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/521#issuecomment-7129099086) · edited 2026-11-24 10:45 UTC Thanks @⁠Dathinel for the reproducer, and @⁠upipibi for the independent check and the rebase notes. Fixed in a new commit on top of the rebased branch: - **The use check now follows the entry value.** It goes through phi results and plain `s_mov_b32` copies before accepting it. Every reader it reaches must be an `s_and_b32` whose mask fits in bits 5:0, or an `s_bfe_u32` field within bits 5:0. The mask may be in either operand, so ACO's `s_and_b32 s7, 63, s6` order is accepted. Everything else is refused, including the full-width read after the join. - **New tests.** - Refused: Dathinel's phi case, and a read through a copy. - Accepted: the masked-at-join control, a copy followed by the mask, and the mask-first order. - The two existing tests are unchanged. - **Rebased onto current `main`.** It keeps #432's `Reg::S(n)` arm, with the `GroupInfo` arm first. Tested on NVIDIA GeForce RTX 5080, Windows 11, Rust 1.98.1, main and branch side by side: `cargo test --workspace -- --test-threads=1` goes from 342 passed on `main` to 347 here, 0 failed. Wave64 is still untested. As @⁠upipibi points out, the two #293 programs may read the wave index (bits 11:6 in ACO's use) rather than the wave count. This PR still refuses that, since nobody has the instruction-level reason yet.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:23 UTC · original · edited 2026-11-25 22:28 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-shader/src/emit/valu_tests.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/521#issuecomment-7148462268) · edited 2026-11-25 22:28 UTC <!-- victory-triage v1 head=a69019a5ae1b0d5fe2718251e6d0208d1f78deb5 verdict=needs-rebase at=2026-11-25T22:32:00Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main`: `main` has moved since the last triage, and the branch now conflicts in `crates/victory-shader/src/emit/valu_tests.rs`. It will be reviewed again once it is rebased onto current `main`. Previous verdict for this head: merge candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/521/head:github-bot/gh-521'
git switch 'github-bot/gh-521'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-521'
git switch 'github-bot/gh-521'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-521'
git switch 'github-bot/gh-521'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-521'
git switch 'main'
git merge --squash 'github-bot/gh-521'
git switch 'main'
git merge --ff-only 'github-bot/gh-521'
git switch 'main'
git merge 'github-bot/gh-521'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!521
No description provided.