D3D12-AMD-RDNA4-SIZE-HANDLE: give a buffer's size query its own non-uniform handle (RDNA 4 compiler crash) #2778

Closed
prosperkartik wants to merge 2 commits from upstream-pr/2778 into main
prosperkartik commented 2026-11-30 18:22:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by prosperkartik on 2026-11-30T18:22:07Z.


Claim: https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7287739557 (D3D12-AMD-RDNA4-SIZE-HANDLE)

Problem

On AMD RDNA 4 the driver's shader compiler crashes the title while it builds compute pipelines. Here it happens every time just as the first story cutscene (the kitchen) starts:

Leonida: first-chance exception 0xc0000005 at amdxc64.dll+0x85fd0f (read 0x8: nothing mapped there)

The programs involved ask a buffer's size (dx.op.getDimensions on a raw/structured view) through the same uniform heap handle that the buffer's loads use, and the compiler reads a null pointer when it merges the two. This is the crash VIctory tracked as RDNA 4 (amdxc64.dll+0x861b9f on an RX 9070 XT), fixed there by trustno1-coder/VIctory-NVIDIA#559.

This is a different crash from #2694. That one serializes pipeline creation for a concurrency crash on RDNA 3. This one is deterministic for these shaders, with any number of workers.

Fix

In descriptor_data (emit/lower.rs), a buffer's size query now gets a heap handle of its own (size_view_handle, new emit/amd_size.rs). The handle is marked non-uniform and not counted in nonuniform_handles, because its index is the same in every lane. It uses the loads' index, raw twin included, so the size is still the one of the view the loads read. Waterfall loads and images keep the shared handle as before.

It's a port of trustno1-coder/VIctory-NVIDIA#559 (66da8aaa), the size-query part of DexNom/VIctory PR 62 by MrToucann; the approach is IxMxAMAR's (fxceless/victory-fork#1213). Mandy's twin_handle stands in for VIctory's raw_twin + heap_handle_at pair; otherwise it's unchanged. The shader cache keys on leonida-shader's sources, so stale translations aren't served.

Tested

RX 9070 GRE (12 GB), driver 32.0.32015.7029, i7-14700F, 32 GB, Windows 11; Series X profile, -Resolution 1080, no frame generation.

  • Before: ad25611 closed at the start of the first story cutscene, on two of two starts.
  • After: 30+ minutes of play through the cutscene and into the swamp mission (RV park, kennels), with 0 exceptions. The 8 compute shaders that were being built when it crashed before (0ab92d33b2800cac, 2cc6decf9a73aedd, 4c573a76d2eeba04, 5ddd2ca380fd74d8, 5ececdeb8cd235c2, b2ff2ef0d6c98dd7, e5742545a3591526, ffa23aec3187f880) all built. About 21.6 rendered FPS on average.
  • cargo test -p leonida-shader size_tests -- --test-threads=1: 9 passed on this branch, rebased on 6cdc0a0d.
  • size_tests::the_record_count_of_a_structured_buffer_read_by_index crashes the test process (STATUS_ACCESS_VIOLATION) on this card both with and without this change, so it was skipped above. VIctory's #559 notes and skips the same test.

Not tested on NVIDIA or older AMD cards. The change only affects how the size query's handle is made.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5571185158 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778). Originally posted by [prosperkartik](https://github.com/prosperkartik) on 2026-11-30T18:22:07Z. --- Claim: https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7287739557 (`D3D12-AMD-RDNA4-SIZE-HANDLE`) ### Problem On AMD RDNA 4 the driver's shader compiler crashes the title while it builds compute pipelines. Here it happens every time just as the first story cutscene (the kitchen) starts: ``` Leonida: first-chance exception 0xc0000005 at amdxc64.dll+0x85fd0f (read 0x8: nothing mapped there) ``` The programs involved ask a buffer's size (`dx.op.getDimensions` on a raw/structured view) through the **same uniform heap handle** that the buffer's loads use, and the compiler reads a null pointer when it merges the two. This is the crash VIctory tracked as RDNA 4 (`amdxc64.dll+0x861b9f` on an RX 9070 XT), fixed there by trustno1-coder/VIctory-NVIDIA#559. This is a different crash from #2694. That one serializes pipeline creation for a concurrency crash on RDNA 3. This one is deterministic for these shaders, with any number of workers. ### Fix In `descriptor_data` (`emit/lower.rs`), a buffer's size query now gets a heap handle of its own (`size_view_handle`, new `emit/amd_size.rs`). The handle is marked non-uniform and not counted in `nonuniform_handles`, because its index is the same in every lane. It uses the loads' index, raw twin included, so the size is still the one of the view the loads read. Waterfall loads and images keep the shared handle as before. It's a port of trustno1-coder/VIctory-NVIDIA#559 (`66da8aaa`), the size-query part of DexNom/VIctory PR 62 by MrToucann; the approach is IxMxAMAR's (fxceless/victory-fork#1213). Mandy's `twin_handle` stands in for VIctory's `raw_twin` + `heap_handle_at` pair; otherwise it's unchanged. The shader cache keys on `leonida-shader`'s sources, so stale translations aren't served. ### Tested RX 9070 GRE (12 GB), driver 32.0.32015.7029, i7-14700F, 32 GB, Windows 11; Series X profile, `-Resolution 1080`, no frame generation. - **Before:** `ad25611` closed at the start of the first story cutscene, on two of two starts. - **After:** 30+ minutes of play through the cutscene and into the swamp mission (RV park, kennels), with **0 exceptions**. The 8 compute shaders that were being built when it crashed before (`0ab92d33b2800cac`, `2cc6decf9a73aedd`, `4c573a76d2eeba04`, `5ddd2ca380fd74d8`, `5ececdeb8cd235c2`, `b2ff2ef0d6c98dd7`, `e5742545a3591526`, `ffa23aec3187f880`) all built. About 21.6 rendered FPS on average. - `cargo test -p leonida-shader size_tests -- --test-threads=1`: 9 passed on this branch, rebased on `6cdc0a0d`. - `size_tests::the_record_count_of_a_structured_buffer_read_by_index` crashes the test process (`STATUS_ACCESS_VIOLATION`) on this card **both with and without this change**, so it was skipped above. VIctory's #559 notes and skips the same test. Not tested on NVIDIA or older AMD cards. The change only affects how the size query's handle is made.
OreoFR commented 2026-11-30 18:32:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T18:32:10Z.


Checked by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup), main 8f41d7d01 merged with head 6a2b44992. A partial check.

  • MEASURED: cargo test --release -p leonida-shader: 232 passed, 0 failed, 2 ignored, the same on main and on main + this PR. No regression in the tests that build on macOS.
  • READ IN CODE: the PR adds no test (emit/amd_size.rs has none), so there is no red/green for the change itself.
  • Not covered: the crate's Windows-only test modules, DXIL validation of the new handle, and the RDNA 4 compiler crash the PR fixes (needs that card and driver).

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287992156 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7287992156). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T18:32:10Z. --- Checked by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup), `main` `8f41d7d01` merged with head `6a2b44992`. A partial check. - MEASURED: `cargo test --release -p leonida-shader`: 232 passed, 0 failed, 2 ignored, the same on `main` and on `main` + this PR. No regression in the tests that build on macOS. - READ IN CODE: the PR adds no test (`emit/amd_size.rs` has none), so there is no red/green for the change itself. - Not covered: the crate's Windows-only test modules, DXIL validation of the new handle, and the RDNA 4 compiler crash the PR fixes (needs that card and driver). AI-assisted: Claude Code
KelboLive commented 2026-11-30 18:35:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T18:35:52Z.


Independent check on Windows (not the author), at 6a2b44992.

  • cargo test --locked -p leonida-shader --lib (run alone): 387 passed, 0 failed, 5 ignored in 284 s, including the new AMD size-handle cases in emit/valu_tests.rs.
  • A first attempt alongside another shader suite hit a shared-target linker lock (LNK1104), not a code error; the clean rerun is above.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. The AMD-specific behaviour itself needs RDNA4 hardware; this covers compile + unit tests.

AI-assisted: yes (opencode).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7288085068 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7288085068). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T18:35:52Z. --- Independent check on Windows (not the author), at `6a2b44992`. - `cargo test --locked -p leonida-shader --lib` (run alone): **387 passed, 0 failed, 5 ignored** in 284 s, including the new AMD size-handle cases in `emit/valu_tests.rs`. - A first attempt alongside another shader suite hit a shared-target linker lock (`LNK1104`), not a code error; the clean rerun is above. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. The AMD-specific behaviour itself needs RDNA4 hardware; this covers compile + unit tests. AI-assisted: yes (opencode).
prosperkartik commented 2026-11-30 19:07:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by prosperkartik on 2026-11-30T19:07:09Z.


Thanks @OreoFR and @KelboLive for checking.

Added a red/green test in bcb6087c: size_tests::a_buffers_size_is_asked_through_a_handle_of_its_own.

  • It lowers the program of the_record_count_of_a_structured_buffer_read_by_index, which asks two buffers' sizes (the structured buffer at s[8:11] and the raw UAV at s[20:23]). It then checks that both sizes get a handle of their own, marked non-uniform but not counted: nonuniform_marked == 2, nonuniform_handles == 0.
  • nonuniform_marked is a new test-only count. The existing nonuniform_handles can't show the change, because the size handle is deliberately left out of it.
  • Red: with the size_view_handle call in descriptor_data reverted to self.handle(...), the test fails with left: 0, right: 2. Green with the change.
  • It only lowers, so it runs on any host; no RDNA 4 card needed.

Also on the RX 9070 GRE at bcb6087c: size_tests 10 passed (the old ..._read_by_index skipped as before; it crashes this card's driver with or without the change), and the waterfall tests 22 passed. cargo fmt --check reports no new spots.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7288870960 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7288870960). Originally posted by [prosperkartik](https://github.com/prosperkartik) on 2026-11-30T19:07:09Z. --- Thanks @OreoFR and @KelboLive for checking. Added a red/green test in bcb6087c: `size_tests::a_buffers_size_is_asked_through_a_handle_of_its_own`. - It lowers the program of `the_record_count_of_a_structured_buffer_read_by_index`, which asks two buffers' sizes (the structured buffer at `s[8:11]` and the raw UAV at `s[20:23]`). It then checks that both sizes get a handle of their own, marked non-uniform but not counted: `nonuniform_marked == 2`, `nonuniform_handles == 0`. - `nonuniform_marked` is a new test-only count. The existing `nonuniform_handles` can't show the change, because the size handle is deliberately left out of it. - **Red:** with the `size_view_handle` call in `descriptor_data` reverted to `self.handle(...)`, the test fails with `left: 0, right: 2`. **Green** with the change. - It only lowers, so it runs on any host; no RDNA 4 card needed. Also on the RX 9070 GRE at `bcb6087c`: `size_tests` 10 passed (the old `..._read_by_index` skipped as before; it crashes this card's driver with or without the change), and the `waterfall` tests 22 passed. `cargo fmt --check` reports no new spots.
ammonium231 commented 2026-11-30 21:09:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ammonium231 on 2026-11-30T21:09:05Z.


RTX 3090 / 621.40, validation on, bcb6087c6 merged onto main 0f86bd594: leonida-shader size_tests 11 passed, 0 failed, incl. a_buffers_size_is_asked_through_a_handle_of_its_own and the_record_count_of_a_structured_buffer_read_by_index, dxil.dll validation ran (main 10 passed; the PR's size_tests.rs doesn't compile there, no field nonuniform_marked on Lowered). No validation errors.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7291986796 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7291986796). Originally posted by [ammonium231](https://github.com/ammonium231) on 2026-11-30T21:09:05Z. --- RTX 3090 / 621.40, validation on, bcb6087c6 merged onto main 0f86bd594: leonida-shader size_tests 11 passed, 0 failed, incl. a_buffers_size_is_asked_through_a_handle_of_its_own and the_record_count_of_a_structured_buffer_read_by_index, dxil.dll validation ran (main 10 passed; the PR's size_tests.rs doesn't compile there, no field nonuniform_marked on Lowered). No validation errors.
merzleckeier commented 2026-12-01 12:29:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T12:29:03Z.


Independent A/B check of bcb6087c6 on c60ce461a with the Windows MSVC shader tests under Wine and the DXIL validator available. The added independent-size-handle test passed, as did the structured-buffer record-count test, on AMD Raphael with RADV 26.3.1 and RTX 4070 SUPER with NVIDIA 620.73.4.

The same existing integer image-store test failed for an identical bit-pattern comparison on each device both before and after the PR. Totals were 9 passed and 1 failed on the base, and 10 passed and 1 failed with the PR, on each device. This is coverage of lowering and offscreen behavior on these Wine drivers. Native Windows RDNA4 coverage remains open.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311196166 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7311196166). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-01T12:29:03Z. --- Independent A/B check of `bcb6087c6` on `c60ce461a` with the Windows MSVC shader tests under Wine and the DXIL validator available. The added independent-size-handle test passed, as did the structured-buffer record-count test, on AMD Raphael with RADV 26.3.1 and RTX 4070 SUPER with NVIDIA 620.73.4. The same existing integer image-store test failed for an identical bit-pattern comparison on each device both before and after the PR. Totals were 9 passed and 1 failed on the base, and 10 passed and 1 failed with the PR, on each device. This is coverage of lowering and offscreen behavior on these Wine drivers. Native Windows RDNA4 coverage remains open. AI-assisted with OpenAI Codex.
KelboLive commented 2026-12-01 16:16:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T16:16:54Z.

Review: COMMENTED


Ran it: full leonida-shader --lib is 388 passed / 5 ignored, size_tests 11/11, and the same tests pass on a merged tree with current main. The dedicated handle is built from the same heap_index(site, first, false), Source::Loaded{load:site,first}, raw_twin/bias and props as handle() (amd_size.rs:29-37 vs lower.rs:9533-9553), so it still names the loads' view — only the nonuniform argument differs. No different-view bug.

The mechanism is sound, and the reason no REUSE tag is needed here is worth writing down: Module::key uses Key::Call(f, args) with the constant non-uniform operand included, so the size handle and the uniform access handle cannot fold together. That is also why #2698's Tag::SizeHandle matters only because that one does not skip waterfall loads (where both handles are non-uniform).

Overlap: #2698 is the same class at the same call site — gated on LEONIDA_SEPARATE_SIZE_HANDLES=1, while this is always on — and the two conflict textually in lower.rs and shader.rs (both add a counter next to nonuniform_handles and edit the same descriptor_data line). Land one and rebase the other; for the non-waterfall case they are redundant, not complementary.

One nit worth fixing before merge: self.s.nonuniform_handles -= 1 (amd_size.rs:36) is a production decrement of a test counter, safe only while twin_handle is hard-coded nonuniform = true. Mutating that flag while keeping the line underflows and panics every size test in debug — I hit it. A counted/separate argument or a #[cfg(test)] guard would remove the trap.

Merge is clean against current main and the merged tree passes; the branch is ~510 commits behind, but main refactored around the handle(...) line without touching it, so the clean merge is genuine. GitHub still says mergeable: UNKNOWN — worth a re-check after the next push.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6135262077 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#pullrequestreview-6135262077). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T16:16:54Z. **Review:** COMMENTED --- Ran it: full `leonida-shader --lib` is **388 passed / 5 ignored**, `size_tests` 11/11, and the same tests pass on a merged tree with current main. The dedicated handle is built from the same `heap_index(site, first, false)`, `Source::Loaded{load:site,first}`, `raw_twin`/bias and `props` as `handle()` (`amd_size.rs:29-37` vs `lower.rs:9533-9553`), so it still names the loads' view — only the `nonuniform` argument differs. No different-view bug. The mechanism is sound, and the reason no REUSE tag is needed here is worth writing down: `Module::key` uses `Key::Call(f, args)` with the constant non-uniform operand included, so the size handle and the uniform access handle cannot fold together. That is also why #2698's `Tag::SizeHandle` matters only because that one does not skip waterfall loads (where both handles are non-uniform). **Overlap:** #2698 is the same class at the same call site — gated on `LEONIDA_SEPARATE_SIZE_HANDLES=1`, while this is always on — and the two **conflict textually** in `lower.rs` and `shader.rs` (both add a counter next to `nonuniform_handles` and edit the same `descriptor_data` line). Land one and rebase the other; for the non-waterfall case they are redundant, not complementary. One nit worth fixing before merge: `self.s.nonuniform_handles -= 1` (`amd_size.rs:36`) is a **production decrement of a test counter**, safe only while `twin_handle` is hard-coded `nonuniform = true`. Mutating that flag while keeping the line underflows and panics every size test in debug — I hit it. A `counted`/`separate` argument or a `#[cfg(test)]` guard would remove the trap. Merge is clean against current main and the merged tree passes; the branch is ~510 commits behind, but main refactored around the `handle(...)` line without touching it, so the clean merge is genuine. GitHub still says `mergeable: UNKNOWN` — worth a re-check after the next push.
MoonEyes commented 2026-12-02 11:50:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T11:50:46Z.


Closing: this pull request no longer applies to current main (merge conflict in d3d12/crates/leonida-shader/src/emit/lower.rs), and open pull requests that have fallen out of step with main are being closed so the list stays workable. If you still want this change, rebase it on current main and reopen it (or open a new pull request); it will be looked at again.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7344486194 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7344486194). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T11:50:46Z. --- Closing: this pull request no longer applies to current `main` (merge conflict in `d3d12/crates/leonida-shader/src/emit/lower.rs`), and open pull requests that have fallen out of step with `main` are being closed so the list stays workable. If you still want this change, rebase it on current `main` and reopen it (or open a new pull request); it will be looked at again. AI-assisted (Claude Code).
prosperkartik commented 2026-12-03 14:13:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by prosperkartik on 2026-12-03T14:13:58Z.


Rebased on current main (b053a181) and opened again as #3334, since this one can't be reopened from my side. Same two commits; the only conflict was the module list at the top of emit/lower.rs.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381104610 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2778#issuecomment-7381104610). Originally posted by [prosperkartik](https://github.com/prosperkartik) on 2026-12-03T14:13:58Z. --- Rebased on current `main` (`b053a181`) and opened again as #3334, since this one can't be reopened from my side. Same two commits; the only conflict was the module list at the top of `emit/lower.rs`.

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
ProjectLeonida/mandy!2778
No description provided.