Shaders: reads of uninitialized SGPRs read as zero, as VGPRs do #432

Manually merged
github merged 1 commit from gh-pr/432/head into gh-pr/432/base 2026-11-26 04:03:45 +00:00
Owner

@⁠Proudnoob4 on GitHub · 2026-11-24 03:17 UTC · original
Proudnoob4:fix/uninitialized-sgprs → main · +47 −1 · 3 files · 1 commits · merged 2026-11-24 08:14 UTC

What

Some compute programs read SGPRs that the hardware never initializes, because two branches make it look as if a read can happen before the write — but the branch that skips the write does not reach the read. VIctory stops at a hardware SGPR input is not supported and drops the program's dispatches.

ComputeIo::initialized_sgprs() counts the SGPRs the hardware does load: user SGPRs, enabled system ones and the scratch wave offset (rsrc2 bit 0). A read of a general SGPR (codes 0..=105, per ir.rs) at or above that count holds whatever the last wave left, so no correct program depends on its value. Read it as zero, as already done for unset VGPRs (lower.rs "Registers no one set read as zero"). VCC, trap temporaries, M0 and EXEC keep their present handling.

Evidence

Three real meshlet-culling programs fail on main at 0x3e0 with this reason. Instrumented io.rs/lower.rs locally (temporary prints, not in the diff):

rsrc2=0x00000090 scratch_en=0 user_sgprs=8 ids=[Some(8), None, None] info=None threads=[64,1,1]
missing: S(37), S(38)

They are written at 0xec/0xf4 on the path from s_cbranch_vccz label_00CC; the fall-through at 0xb0 writes them and branches to 0x100, while label_00CC reaches the 0x3c8 read. With this change all three translate, validate and get pipelines on an RTX 4080 (driver 621.01):

program main d2a4bae this change
3b471c122be79b3e meshlet culling not translated pipeline created
fc5e66f8f873d3bb meshlet culling, prev not translated pipeline created
764ca63daec9c616 meshlet culling, paraboloid shadows not translated pipeline created

A local build combining this with #136's image_gather4_l hunks took the seven known meshlet-culling programs from 0/7 → 4/7 → 7/7 emitted, validated and pipelined.

Tests

  • cargo test -p victory-shader -- --skip emit::valu_tests: 63 passed, 0 failed (the 7 skipped ones are the known Windows 10 "no SM 6.6 device" failures in this test binary, discussed in #84).
  • New test initialized_sgprs_covers_user_system_and_scratch covers user + group ids, the info SGPR, and the scratch offset.
  • rustfmt --check on the three changed files and git diff --check pass.

Caveat

This does not fix the NVIDIA world hang (#71): with all seven programs enabled the device was still removed, and a little earlier than without them. It only restores the dropped dispatches. I posted that correction on #71.

AI-assisted (Claude Code). I ran the tests on Windows 10, RTX 4080; the reasoning and code are mine.

**@⁠Proudnoob4** on GitHub · 2026-11-24 03:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/432) `Proudnoob4:fix/uninitialized-sgprs` → `main` · +47 −1 · 3 files · 1 commits · merged 2026-11-24 08:14 UTC ## What Some compute programs read SGPRs that the hardware never initializes, because two branches make it look as if a read can happen before the write — but the branch that skips the write does not reach the read. VIctory stops at `a hardware SGPR input is not supported` and drops the program's dispatches. `ComputeIo::initialized_sgprs()` counts the SGPRs the hardware does load: user SGPRs, enabled system ones and the scratch wave offset (rsrc2 bit 0). A read of a **general** SGPR (codes 0..=105, per `ir.rs`) at or above that count holds whatever the last wave left, so no correct program depends on its value. Read it as zero, as already done for unset VGPRs (`lower.rs` "Registers no one set read as zero"). VCC, trap temporaries, M0 and EXEC keep their present handling. ## Evidence Three real meshlet-culling programs fail on `main` at 0x3e0 with this reason. Instrumented `io.rs`/`lower.rs` locally (temporary prints, not in the diff): ``` rsrc2=0x00000090 scratch_en=0 user_sgprs=8 ids=[Some(8), None, None] info=None threads=[64,1,1] missing: S(37), S(38) ``` They are written at 0xec/0xf4 on the path from `s_cbranch_vccz label_00CC`; the fall-through at 0xb0 writes them and branches to 0x100, while `label_00CC` reaches the 0x3c8 read. With this change all three translate, validate and get pipelines on an RTX 4080 (driver 621.01): | program | `main d2a4bae` | this change | |---|---|---| | 3b471c122be79b3e meshlet culling | not translated | pipeline created | | fc5e66f8f873d3bb meshlet culling, prev | not translated | pipeline created | | 764ca63daec9c616 meshlet culling, paraboloid shadows | not translated | pipeline created | A local build combining this with #136's `image_gather4_l` hunks took the seven known meshlet-culling programs from 0/7 → 4/7 → 7/7 emitted, validated and pipelined. ## Tests - `cargo test -p victory-shader -- --skip emit::valu_tests`: 63 passed, 0 failed (the 7 skipped ones are the known Windows 10 "no SM 6.6 device" failures in this test binary, discussed in #84). - New test `initialized_sgprs_covers_user_system_and_scratch` covers user + group ids, the info SGPR, and the scratch offset. - `rustfmt --check` on the three changed files and `git diff --check` pass. ## Caveat This does **not** fix the NVIDIA world hang (#71): with all seven programs enabled the device was still removed, and a little earlier than without them. It only restores the dropped dispatches. I posted that correction on #71. AI-assisted (Claude Code). I ran the tests on Windows 10, RTX 4080; the reasoning and code are mine.
Compute programs read SGPRs the hardware does not initialize when two
branches make the paths look like they allow a read before a write, even
though the branch that skips the write does not reach the read. That stops
translation with 'a hardware SGPR input is not supported' and drops the
program's dispatches.

ComputeIo::initialized_sgprs() counts the user SGPRs, the enabled system
ones and the scratch wave offset. A read of a general SGPR (0..=105) at or
above that count holds whatever the previous wave left, so no program can
depend on its value: read it as zero, as unset VGPRs already are. Reserved
registers (VCC, trap temporaries, M0, EXEC) keep their current handling.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:03:42 +00:00
github manually merged commit f4438b38f4 into gh-pr/432/base 2026-11-26 04:03:45 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:37 UTC · original

Verified f4438b3 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 57 passed, 0 failed, 0 ignored.

Not covered: GPU execution of the new zero-read paths on Windows/Wine and title behaviour (no game data here).

**@⁠DaviTostes** on GitHub · 2026-11-24 03:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/432#issuecomment-7121264694) Verified `f4438b3` on **Arch Linux x86_64** (rustc 1.98.1, `cargo test -p victory-shader`): **57 passed, 0 failed, 0 ignored**. Not covered: GPU execution of the new zero-read paths on Windows/Wine and title behaviour (no game data here).
Author
Owner

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

A check after the merge, on main ffe310e (which has f4438b3) with #521 merged in for its test fixture that takes COMPUTE_PGM_RSRC2, and one thing that may be worth a follow-up.

Run on hardware (RTX 3070, driver 616.56, and WARP; the compute fixture of valu_tests.rs with 6 user SGPRs, storing the SGPR it reads):

COMPUTE_PGM_RSRC2 read result
6 user SGPRs s6 (first above the count) 0
6 user SGPRs s20 0
+ scratch (bit 0) s6 (the scratch wave offset) refused: a hardware SGPR input
+ scratch s7 0
+ group id X + scratch s6 (the group id) 0 (one group is dispatched)
+ group id X + scratch s7 (the scratch wave offset) refused
+ thread-group info s6 (the info SGPR) refused (with #521's reason for it)

So the boundary is where initialized_sgprs says, and the initialized system SGPRs without a value are still refused. The order it assumes (group ids, group info, scratch wave offset last) is the one in LLVM's AMDGPUUsage, "Initial Kernel Execution State".

A read with no write anywhere now also reads zero. The case in the description has a write on another path (s37/s38 written at 0xec/0xf4). The rule is wider: v_mov_b32 v0, s20 in a program that never writes s20 translates and gives 0 (second row). A register that is read and written nowhere is not a path artefact; it is more likely an input the translator does not know about (a system SGPR in a position we have wrong, or a count decoded short), and before this change that showed up as a refusal in the census. Two ways to keep that signal:

  • apply the zero only to a register some instruction in the program writes, and keep refusing the others; or
  • keep the rule and log the program and register once when it is used, so #293-style tables can still count them.

I have not seen a title program of the second kind (no game files here); this is from the code and the probe.

Not verified: user_sgprs() takes five bits (rsrc2 >> 1 & 0x1F). If the console sets a high bit for more than 31 user SGPRs, the count comes out 32 short. Reads through the root layout are loaded before this arm, so they would not be zeroed, but the system SGPR positions would be off. I could not find a reference for where that bit is in COMPUTE_PGM_RSRC2 on GFX10, so treat this as a question.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-24 10:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/432#issuecomment-7128622386) A check after the merge, on `main` `ffe310e` (which has `f4438b3`) with #521 merged in for its test fixture that takes `COMPUTE_PGM_RSRC2`, and one thing that may be worth a follow-up. **Run on hardware** (RTX 3070, driver 616.56, and WARP; the compute fixture of `valu_tests.rs` with 6 user SGPRs, storing the SGPR it reads): | `COMPUTE_PGM_RSRC2` | read | result | |---|---|---| | 6 user SGPRs | `s6` (first above the count) | 0 | | 6 user SGPRs | `s20` | 0 | | + scratch (bit 0) | `s6` (the scratch wave offset) | refused: `a hardware SGPR input` | | + scratch | `s7` | 0 | | + group id X + scratch | `s6` (the group id) | 0 (one group is dispatched) | | + group id X + scratch | `s7` (the scratch wave offset) | refused | | + thread-group info | `s6` (the info SGPR) | refused (with #521's reason for it) | So the boundary is where `initialized_sgprs` says, and the initialized system SGPRs without a value are still refused. The order it assumes (group ids, group info, scratch wave offset last) is the one in LLVM's `AMDGPUUsage`, "Initial Kernel Execution State". **A read with no write anywhere now also reads zero.** The case in the description has a write on another path (`s37`/`s38` written at 0xec/0xf4). The rule is wider: `v_mov_b32 v0, s20` in a program that never writes `s20` translates and gives 0 (second row). A register that is read and written nowhere is not a path artefact; it is more likely an input the translator does not know about (a system SGPR in a position we have wrong, or a count decoded short), and before this change that showed up as a refusal in the census. Two ways to keep that signal: - apply the zero only to a register some instruction in the program writes, and keep refusing the others; or - keep the rule and log the program and register once when it is used, so #293-style tables can still count them. I have not seen a title program of the second kind (no game files here); this is from the code and the probe. **Not verified:** `user_sgprs()` takes five bits (`rsrc2 >> 1 & 0x1F`). If the console sets a high bit for more than 31 user SGPRs, the count comes out 32 short. Reads through the root layout are loaded before this arm, so they would not be zeroed, but the system SGPR positions would be off. I could not find a reference for where that bit is in `COMPUTE_PGM_RSRC2` on GFX10, so treat this as a question. AI-assisted (Claude Code).
Author
Owner

@⁠Proudnoob4 on GitHub · 2026-11-24 12:07 UTC · original

@⁠upipibi thanks for checking after the merge. I have the title's files here, so I measured both points on its own programs.

1. Reads of SGPRs no instruction writes. I ran victory translate --list on main ffe310e over the title's shaders: shaders.rpf plus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch in inputs():

Rule Programs whose outcome differs from main
your first option (zero only if some instruction writes the register; refuse the rest) 0
refuse every read above the initialized SGPRs (the behaviour before #432) 4

The 4 are the meshlet_culling_CS_BatchCullMeshlets* variants, reading s37 with 9 SGPRs initialized.

I also logged every live-in read above the initialized SGPRs, whether or not the value is used. 12 distinct programs have one:

Register Initialized SGPRs Programs
s37, s38 9 4
s46 14 7
s83, s84 14 1

Each of these registers is written elsewhere in its program. No title program reads an SGPR that nothing writes. So your first option costs nothing here and keeps the signal.

I have that change ready with two CPU-only tests: branch fix/sgpr-zero-only-written on my fork (d493a13). It refuses the never-written case as an SGPR no instruction writes, above the initialized ones, so tables can tell it apart from a hardware SGPR input. I'll open it as a PR once external PRs reopen (#712).

2. user_sgprs() taking five bits. For compute it can't come out short. GFX10 has 16 compute user-data registers (COMPUTE_USER_DATA_0 to _15), and COMPUTE_PGM_RSRC2 has no MSB field. Its fields are:

Bits Field
0 SCRATCH_EN
5:1 USER_SGPR
6 TRAP_PRESENT
9:7 TGID_X/Y/Z_EN
10 TG_SIZE_EN
12:11 TIDIG_COMP_CNT
14:13 EXCP_EN_MSB
23:15 LDS_SIZE
30:24 EXCP_EN

USER_SGPR_MSB (bit 27) exists only in the graphics registers SPI_SHADER_PGM_RSRC2_{VS,PS,GS,GS_VS,HS}. This is from Mesa's register database, src/amd/registers/gfx10.json. LLVM's AMDGPUUsage agrees: the user SGPRs requested "must not exceed 16".

On the title, compute user-SGPR counts range from 4 to 14. In VIctory only compute decodes the count from rsrc2 (native.rs:304). Graphics stages map user data through the root layout, so bit 27 is never involved.

Counts and program names only; no game data attached. AI-assisted (Claude Code); I ran the measurements on my machine.

**@⁠Proudnoob4** on GitHub · 2026-11-24 12:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/432#issuecomment-7130989282) @⁠upipibi thanks for checking after the merge. I have the title's files here, so I measured both points on its own programs. **1. Reads of SGPRs no instruction writes.** I ran `victory translate --list` on `main` `ffe310e` over the title's shaders: `shaders.rpf` plus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch in `inputs()`: | Rule | Programs whose outcome differs from `main` | |---|---:| | your first option (zero only if some instruction writes the register; refuse the rest) | **0** | | refuse every read above the initialized SGPRs (the behaviour before #432) | 4 | The 4 are the `meshlet_culling_CS_BatchCullMeshlets*` variants, reading `s37` with 9 SGPRs initialized. I also logged every live-in read above the initialized SGPRs, whether or not the value is used. 12 distinct programs have one: | Register | Initialized SGPRs | Programs | |---|---:|---:| | `s37`, `s38` | 9 | 4 | | `s46` | 14 | 7 | | `s83`, `s84` | 14 | 1 | **Each of these registers is written elsewhere in its program.** No title program reads an SGPR that nothing writes. So your first option costs nothing here and keeps the signal. I have that change ready with two CPU-only tests: branch `fix/sgpr-zero-only-written` on my fork ([`d493a13`](https://github.com/Proudnoob4/VIctory/commit/d493a131df47dc8519d5a766a955698c362c0459)). It refuses the never-written case as `an SGPR no instruction writes, above the initialized ones`, so tables can tell it apart from `a hardware SGPR input`. I'll open it as a PR once external PRs reopen (#712). **2. `user_sgprs()` taking five bits.** For compute it can't come out short. GFX10 has 16 compute user-data registers (`COMPUTE_USER_DATA_0` to `_15`), and `COMPUTE_PGM_RSRC2` has no MSB field. Its fields are: | Bits | Field | |---|---| | 0 | `SCRATCH_EN` | | 5:1 | `USER_SGPR` | | 6 | `TRAP_PRESENT` | | 9:7 | `TGID_X/Y/Z_EN` | | 10 | `TG_SIZE_EN` | | 12:11 | `TIDIG_COMP_CNT` | | 14:13 | `EXCP_EN_MSB` | | 23:15 | `LDS_SIZE` | | 30:24 | `EXCP_EN` | `USER_SGPR_MSB` (bit 27) exists only in the graphics registers `SPI_SHADER_PGM_RSRC2_{VS,PS,GS,GS_VS,HS}`. This is from Mesa's register database, [`src/amd/registers/gfx10.json`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/registers/gfx10.json). LLVM's [AMDGPUUsage](https://llvm.org/docs/AMDGPUUsage.html) agrees: the user SGPRs requested "must not exceed 16". On the title, compute user-SGPR counts range from 4 to 14. In VIctory only compute decodes the count from `rsrc2` (`native.rs:304`). Graphics stages map user data through the root layout, so bit 27 is never involved. Counts and program names only; no game data attached. AI-assisted (Claude Code); I ran the measurements on my machine.
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!432
No description provided.