Skip to content

Shaders: keep spilled compute root parameters from claiming s16+ system SGPRs - #3038

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/cs-spilled-root-param-of
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/cs-spilled-root-param-of

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Summary

In compute shaders (HwStage::Cs), hardware provides at most 16 user SGPRs (s0..s15); root parameters with user_data >= 16 spill to memory (loaded via s[0:1]), while s16+ hold system inputs (SV_GroupID, GroupInfo).

spill_loads, spilled_param, and param_word in crates/victory-shader/src/resource.rs already exclude user_data >= 16 for HwStage::Cs, but Tracer::param_of did not:

fn param_of(&self, reg: Reg) -> Option<(usize, u32)> {
    let Reg::S(code) = reg else { return None };
    self.layout.iter().enumerate().find_map(|(i, p)| {
        let first = self.stage.user_sgpr(p.user_data);
        ...

When a compute shader has any spilled root parameter (user_data >= 16) and reads s16+, Tracer::param_of falsely maps the system SGPR to that spilled root parameter. Depending on how s16+ is used, this either:

  1. Causes Lower::inputs / Lower::get to replace SV_GroupID with user_data[16+], or
  2. Causes resource tracking to reject valid dynamic offsets (e.g. s_load / global_load indexed by SV_GroupID) with "an offset from a root parameter".

Fix

  • Return None early in Tracer::param_of when self.stage == HwStage::Cs && code >= 16.
  • Add unit tests in crates/victory-shader/src/resource.rs:
    • compute_spilled_params_do_not_claim_system_sgprs: verifies track() accepts s16 as a dynamic s_load offset alongside a spilled root parameter at user_data = 16, without mapping s16 into r.params.
    • compute_group_id_in_s16_survives_spilled_root_constants: verifies full emit() + DXIL validation keeps s16 lowered as Input::GroupId(0) and never pulls spilled user_data[16] into shader.user_data unless loaded from s[0:1].

Verification

  • cargo test --locked -p victory-shader --lib resource::tests (37 passed)
  • cargo test --locked -p victory-shader --test resource_tracking (3 passed)
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (clean)
  • cargo xtask policy (191 files ok)

@kotekompotek kotekompotek 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.

LGTM — correct hardware fact (CS user data ends at s15; s16+ are system inputs). The test is precise: s16 stays unmapped while the spilled param still resolves through its memory load.

@pawrenity

Copy link
Copy Markdown

VERIFY-3038: verified aed7e97 on NVIDIA GeForce RTX 4070, Windows 11 (plus WARP where the tests use it).

Review (reading). The new guard in Tracer::param_of (crates/victory-shader/src/resource.rs:635) makes it agree with the three compute-spill paths beside it: spill_loads, spilled_param and param_word already treat user_data >= 16 on HwStage::Cs as memory reached through s[0:1], never as SGPRs. Without the guard, a Cs parameter at user_data >= 16 matched s16+ because HwStage::Cs.user_sgpr(i) == i. track() (line 172) and param_value (line 528) are its only callers, and both now leave s16+ to the system inputs (ComputeIo::system_sgprs). I found no defects.

Regression check. I put the PR's test hunk alone on origin/main 63bcca4, with the 6-line guard taken out. Both new tests fail for the reasons the description gives:

  • compute_spilled_params_do_not_claim_system_sgprs: track returns Unsupported { what: "an offset from a root parameter" }
  • compute_group_id_in_s16_survives_spilled_root_constants: panics on "s16 must not be claimed by the spilled root constant"

At head:

  • cargo test --locked -p victory-shader --lib resource::: 37 passed, 0 failed. Both new tests pass, including their DXIL validation step (dxil.dll from Windows Kits 10.0.26100.0).
  • cargo test --locked -p victory-shader --test resource_tracking: 3 passed, 1 ignored.
  • cargo test --locked -p victory-shader --lib (one test skipped because it crashes the process on NVIDIA): 300 passed, 3 failed. The 3 (stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware) also fail on main on this card.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: clean.
  • A trial merge onto current origin/main 63bcca4 is clean.

Not checked: whether any title shader is affected (no shader pack here).

AI-assisted (Claude Code).

@fyernald fyernald 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.

The two new tests check the translation (tracking, user_data, DXIL validation) but never run the shader, so I dispatched both of compute_group_id_in_s16_survives_spilled_root_constants's programs on an RTX 3080 (driver 617.42), Windows 10 22H2. Same NativePrograms, 8 groups, spilled constant (user_data[16]) = 1000, a 12-dword raw UAV at heap index 0. Base is main 63bcca4 + #486 (Agility opt-in). Windows 10 has no SM 6.6 WARP here, so these are hardware results only.

program expected UAV[0..8] main this PR (aed7e97)
s16 + user_data[16] 1000, 1001, …, 1007 all 0 1000, 1001, …, 1007
s16 alone 0, 1, …, 7 all 0 0, 1, …, 7

On main the "s16 alone" shader's user_data already includes {index: 16, root: Constants}, and nothing lands in the UAV. That's consistent with every group reading s16 as the spilled 1000 and storing past the buffer. With the PR, user_data has only the table and each group writes its own id.

Also on the PR, with #2596 so the suite gets past the #2571 crash: resource:: 37 passed, and the full victory-shader --lib run gives 300 passed, 4 failed, 3 ignored. The 4 are the NVIDIA failures that fail on the same base without this PR (the two stride_tests heap-buffer tests, gathers_at_a_level_match_the_hardware, pops_wait_loop_exits_immediately_for_inactive_lanes). The extra 2 passes are your new tests.

A dispatch like this might be worth keeping in the test, since it's the only thing that checks the group id reaches the shader.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Tracer::param_of in crates/victory-shader/src/resource.rs now returns None for s16+ on HwStage::Cs. This makes it agree with what main already does for compute programs in spill_loads, spilled_param and param_word: parameters at user_data >= 16 are read from memory through s[0:1], never from SGPRs. Without the guard, HwStage::Cs.user_sgpr(i) == i let a spilled parameter claim a system SGPR such as the group ID that ComputeIo::system_sgprs places after the user SGPRs. I checked param_of's callers (track and param_value). Both now leave s16+ to the system inputs.

The two new tests check what should happen, not only what the code currently does: the group ID is raised as Input::GroupId(0), and user_data[16] appears only when it is loaded through s[0:1]. I found no repo-rule issues. CI: check passing.


Generated by Claude Code

@Dukestar69 Dukestar69 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.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64:

  • Diff review: Returning None from Tracer::param_of when self.stage == HwStage::Cs && code >= 16 ensures spilled compute root parameters (user_data >= 16) never falsely claim s16+ system SGPRs (SV_GroupID, group info).
  • cargo test -p victory-shader resource::tests: 37 passed, 0 failed (including compute_spilled_params_do_not_claim_system_sgprs and compute_group_id_in_s16_survives_spilled_root_constants).
  • cargo test -p victory-shader --test resource_tracking: 3 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
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.

6 participants