fix(shader): skip unused image atomic descriptor reconstruction #589

Closed
jeremygold02 wants to merge 1 commit from upstream-pr/589 into main
jeremygold02 commented 2026-11-24 05:14:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:14:52Z.


A scalar load of eight image-descriptor words currently reconstructs descriptor fields with a float-annotated GetDimensions. When those SGPRs feed only an image atomic, the atomic builds its heap index directly from the load address. The extra query also lets the cached float4 annotation reach an unsigned atomic on current main.

Track this single-consumer case during lowering and omit the field reconstruction. The check requires the same eight SGPR definitions to reach the image resource operand directly. A scalar data use, a PHI edge or another instruction reading any of those values keeps the existing path.

Verification

  • Added a public-translator regression with synthetic 32-bit unsigned image max, a live scalar field, a PHI input, and a second atomic consumer. The positive fails on main. Mutations that ignored each of the three additional uses also failed the test.
  • Validated the synthetic DXIL with the Windows SDK. The single-consumer case has one atomicBinOp.i32 UMax on a RWTexture2D<U32> handle and zero dimension queries. The scalar and PHI controls still feed the queried dimension into the atomic.
  • Compared 25,694 inputs on d2a4bae and this change. All 21,971 previously SDK-valid inputs passed. 21,931 outputs were byte-identical. The 40 changed shaders (27 pixel and 13 compute) retained all 167 atomic sites and dropped 83 redundant dimension queries. Exactly 78 atomic handle annotations changed from float4 to U32. All 3,538 translator refusals and 185 SDK rejections retained their previous diagnostic.
  • On a physical RTX 5070, three one-thread dispatches on a correctly typed R32_UINT UAV returned the expected unsigned maxima across the sign bit and within the high unsigned range. All three fences completed, and the adjacent texel and control descriptor retained their initial values. This check used the standalone change on main.
  • The CPU shader library suite passed 58/58. Strict library Clippy and rustfmt passed.

Related work includes #177 distinguishes cached handle annotations, #259 checks use of the reconstructed mip-count field, and #48 adds 64-bit image atomics. In a separate comparison stacked on #48, all 14 unsigned 64-bit maximum sites in seven SDK-valid pixel shaders lost their redundant float query. Five bounded synthetic comparisons on a physical RTX 5070 produced the expected unsigned 64-bit maxima with fence-complete readbacks.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5466543586 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T05:14:52Z. --- A scalar load of eight image-descriptor words currently reconstructs descriptor fields with a float-annotated `GetDimensions`. When those SGPRs feed only an image atomic, the atomic builds its heap index directly from the load address. The extra query also lets the cached float4 annotation reach an unsigned atomic on current main. Track this single-consumer case during lowering and omit the field reconstruction. The check requires the same eight SGPR definitions to reach the image resource operand directly. A scalar data use, a PHI edge or another instruction reading any of those values keeps the existing path. ### Verification - Added a public-translator regression with synthetic 32-bit unsigned image max, a live scalar field, a PHI input, and a second atomic consumer. The positive fails on main. Mutations that ignored each of the three additional uses also failed the test. - Validated the synthetic DXIL with the Windows SDK. The single-consumer case has one `atomicBinOp.i32` UMax on a `RWTexture2D<U32>` handle and zero dimension queries. The scalar and PHI controls still feed the queried dimension into the atomic. - Compared 25,694 inputs on `d2a4bae` and this change. All 21,971 previously SDK-valid inputs passed. 21,931 outputs were byte-identical. The 40 changed shaders (27 pixel and 13 compute) retained all 167 atomic sites and dropped 83 redundant dimension queries. Exactly 78 atomic handle annotations changed from float4 to U32. All 3,538 translator refusals and 185 SDK rejections retained their previous diagnostic. - On a physical RTX 5070, three one-thread dispatches on a correctly typed `R32_UINT` UAV returned the expected unsigned maxima across the sign bit and within the high unsigned range. All three fences completed, and the adjacent texel and control descriptor retained their initial values. This check used the standalone change on main. - The CPU shader library suite passed 58/58. Strict library Clippy and rustfmt passed. Related work includes #177 distinguishes cached handle annotations, #259 checks use of the reconstructed mip-count field, and #48 adds 64-bit image atomics. In a separate comparison stacked on #48, all 14 unsigned 64-bit maximum sites in seven SDK-valid pixel shaders lost their redundant float query. Five bounded synthetic comparisons on a physical RTX 5070 produced the expected unsigned 64-bit maxima with fence-complete readbacks.
slint6614-dev commented 2026-11-24 09:06:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T08:43:46Z.


Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines created on an AMD RX 9070, Adrenalin, Windows 11; dxil.dll from SDK 10.0.26100), against main 1f858b0. Counts are emitted / validated / pipelines.

VS/PS CS 64-bit image atomic refusals
main 12272 / 12172 / 12272 1693 / 1670 / 1693 653
#539 12790 / 12690 / 12790 1695 / 1672 / 1695 0
#305 12790 / 12172 / 12790 1695 / 1670 / 1695 0
#589 12272 / 12172 / 12272 1693 / 1670 / 1693 653
  • #539 and #305 clear all 653 and emit the same 518 extra pixel programs. With #539 they validate and the RX 9070 creates every pipeline.
  • With #305 those 520 programs all fail the validator with "Flags must match usage." (the AMD driver still creates the pipelines, but they are not valid DXIL). So there is no validated gain.
  • #589 changes nothing on this pack.
  • In both #539 and #305 about 100 programs move on to other refusals ("scalar memory read as data" 573 → 677, "barycentrics read directly" 6 → 29).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7126489447 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589#issuecomment-7126489447). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T08:43:46Z. --- Measured on the full retail shader pack (26,503 containers) with `victory translate --validate --pipelines` (pipelines created on an AMD RX 9070, Adrenalin, Windows 11; dxil.dll from SDK 10.0.26100), against main 1f858b0. Counts are emitted / validated / pipelines. | | VS/PS | CS | 64-bit image atomic refusals | |---|---|---|---| | main | 12272 / 12172 / 12272 | 1693 / 1670 / 1693 | 653 | | #539 | 12790 / **12690** / 12790 | 1695 / 1672 / 1695 | 0 | | #305 | 12790 / 12172 / 12790 | 1695 / 1670 / 1695 | 0 | | #589 | 12272 / 12172 / 12272 | 1693 / 1670 / 1693 | 653 | - #539 and #305 clear all 653 and emit the same 518 extra pixel programs. With #539 they validate and the RX 9070 creates every pipeline. - With #305 those 520 programs all fail the validator with "Flags must match usage." (the AMD driver still creates the pipelines, but they are not valid DXIL). So there is no validated gain. - #589 changes nothing on this pack. - In both #539 and #305 about 100 programs move on to other refusals ("scalar memory read as data" 573 → 677, "barycentrics read directly" 6 → 29).
catminou4 commented 2026-11-24 11:24:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T11:24:05Z.

Review: APPROVED


Sound and conservative. atomic_descriptor_loads only marks an s_load_dwordx8 when all 8 written SGPRs are consumed by exactly one image_atomic_*'s rsrc (checked via atomic.read(reg) == Some(v)), the SGPR window stays below VCC (code > VCC - 8 rejected), and — critically — any other reader (phi args or any other instruction's reads, excluding the atomic itself) disqualifies the load. So the skip can never hide a descriptor word that real code observes.

descriptor_data early-returning Ok(()) for those sites removes the reconstruction DXIL for loads whose words are provably unused, and the new native.rs test pins the behavior: x8 load yields smaller DXIL than x16 when only the atomic consumes it, a data read of a descriptor dword forces the reconstruction path (x8 == x16 output), and the phi-joined branch case stays conservative. The #[cfg(windows)] validator block is properly gated.

Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) → 97 passed / 0 failed / 1 ignored, including the new test (it runs on macOS; only the DXIL validator is Windows-gated).

<!-- greyfinch-sync:Marvisak/VIctory:review:6038329793 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589#pullrequestreview-6038329793). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T11:24:05Z. **Review:** APPROVED --- Sound and conservative. `atomic_descriptor_loads` only marks an `s_load_dwordx8` when all 8 written SGPRs are consumed by exactly one `image_atomic_*`'s `rsrc` (checked via `atomic.read(reg) == Some(v)`), the SGPR window stays below VCC (`code > VCC - 8` rejected), and — critically — any other reader (phi args or any other instruction's reads, excluding the atomic itself) disqualifies the load. So the skip can never hide a descriptor word that real code observes. `descriptor_data` early-returning `Ok(())` for those sites removes the reconstruction DXIL for loads whose words are provably unused, and the new `native.rs` test pins the behavior: x8 load yields smaller DXIL than x16 when only the atomic consumes it, a data read of a descriptor dword forces the reconstruction path (x8 == x16 output), and the phi-joined branch case stays conservative. The `#[cfg(windows)]` validator block is properly gated. Verified locally: test-merge with current main (`ffe310e`) clean; `cargo test -p victory-shader --locked` (macOS arm64, rustc 1.98.1) → **97 passed / 0 failed / 1 ignored**, including the new test (it runs on macOS; only the DXIL validator is Windows-gated).
mercuryVM commented 2026-11-24 11:33:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T11:33:43Z.

Review: APPROVED


Tested ab2e7cc on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 70 passed, including reconstructs_an_atomic_descriptor_only_when_its_words_are_read, which validates through dxil.dll here. cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 emitted, validated and built into pipelines.

  • The skip applies only when all eight written SGPR values flow straight into the one image_atomic_* rsrc operand, with no phi use and no other reader. Any scalar data use keeps the reconstruction, so a program that reads a descriptor field still gets it.
  • code > VCC - 8 keeps the range inside the SGPR file. The x16 contrast case in the test is a good guard that the skip doesn't widen by accident.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6038452809 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589#pullrequestreview-6038452809). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T11:33:43Z. **Review:** APPROVED --- Tested ab2e7cc on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` 70 passed, including `reconstructs_an_atomic_descriptor_only_when_its_words_are_read`, which validates through dxil.dll here. `cargo clippy -p victory-shader --all-targets` is clean. `victory translate --validate --pipelines` on the local title executable still reports 12 of 12 emitted, validated and built into pipelines. - The skip applies only when all eight written SGPR values flow straight into the one `image_atomic_*` rsrc operand, with no phi use and no other reader. Any scalar data use keeps the reconstruction, so a program that reads a descriptor field still gets it. - `code > VCC - 8` keeps the range inside the SGPR file. The x16 contrast case in the test is a good guard that the skip doesn't widen by accident. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 02:05:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:23:27Z.


Automated triage for the maintainers.

Proposed close (unchanged): a narrower duplicate of #1756, which is still open. The head has not changed since the previous verdict. Your follow-up confirms #1756 fixes every correctness case you could find for the float4-annotated handle reaching an unsigned image atomic (Lower::handle in crates/victory-shader/src/emit/lower.rs). What this PR adds on top is a smaller synthetic fixture (1,968 vs 2,160 bytes). On the retail pack it gives no extra validated shaders or created pipelines, so it does not justify a second code path. Thank you for checking this carefully and for saying so plainly.

CI: not run on this head.

This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149495630 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589#issuecomment-7149495630). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:23:27Z. --- <!-- victory-triage v1 head=ab2e7cc3f648fe78e851ff1769d50774728edc1f verdict=proposed-close at=2026-11-26T00:21:39Z --> Automated triage for the maintainers. **Proposed close (unchanged): a narrower duplicate of #1756, which is still open.** The head has not changed since the previous verdict. Your follow-up confirms #1756 fixes every correctness case you could find for the float4-annotated handle reaching an unsigned image atomic (`Lower::handle` in `crates/victory-shader/src/emit/lower.rs`). What this PR adds on top is a smaller synthetic fixture (1,968 vs 2,160 bytes). On the retail pack it gives no extra validated shaders or created pipelines, so it does not justify a second code path. Thank you for checking this carefully and for saying so plainly. CI: not run on this head. This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
jeremygold02 commented 2026-11-25 03:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T02:58:05Z.


@Marvisak I checked the overlap with #1756 on main at 940d32f6. With #1756 applied, I ran this PR’s synthetic fixture with Windows SDK DXIL validation required and the code-size assertion replaced by a measurement. The atomic-only x8 and x16 cases both validated at 2,224 bytes, and the descriptor-word and control-flow checks passed. I found no additional correctness case that #1756 misses.

This PR does have a separate code-size effect. With #589 alone on the same base, the original regression passed and the atomic-only x8 case produced 1,968 bytes versus 2,160 for x16. That measures the optimization in the fixture; the retail-pack comparison above found no increase in validated shaders or created pipelines. I agree with using #1756 for the correctness fix. I’ve left this branch as-is for the proposed-close decision.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151434888 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/589#issuecomment-7151434888). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T02:58:05Z. --- @Marvisak I checked the overlap with #1756 on `main` at `940d32f6`. With #1756 applied, I ran this PR’s synthetic fixture with Windows SDK DXIL validation required and the code-size assertion replaced by a measurement. The atomic-only x8 and x16 cases both validated at 2,224 bytes, and the descriptor-word and control-flow checks passed. I found no additional correctness case that #1756 misses. This PR does have a separate code-size effect. With #589 alone on the same base, the original regression passed and the atomic-only x8 case produced 1,968 bytes versus 2,160 for x16. That measures the optimization in the fixture; the retail-pack comparison above found no increase in validated shaders or created pipelines. I agree with using #1756 for the correctness fix. I’ve left this branch as-is for the proposed-close decision.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!589
No description provided.