Skip to content

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

Open
prosperkartik wants to merge 2 commits into
ProjectLeonida:mainfrom
prosperkartik:amd-rdna4-size-handle
Open

prosperkartik wants to merge 2 commits into
ProjectLeonida:mainfrom
prosperkartik:amd-rdna4-size-handle

Conversation

@prosperkartik

Copy link
Copy Markdown

Claim: #2570 (comment) (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 GPU test: NVIDIA GeForce RTX 3060 Ti (Vulkan 1.4, wave64-emulate) — 26497/26501 pass, 4 cs modules crash driver #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.

…niform handle

On AMD RDNA 4 (RX 9070 GRE, driver 32.0.32015.7029, Windows 11) the driver's
shader compiler crashes the title (0xc0000005 in amdxc64.dll+0x85fd0f) while
building compute pipelines at the first story cutscene. These programs ask a
buffer's size through the same uniform heap handle the buffer's loads use.

In descriptor_data the size query now gets a heap handle of its own, marked
non-uniform and not counted in nonuniform_handles (emit/amd_size.rs). Waterfall
loads and images keep the shared handle as before.

Port of trustno1-coder/VIctory-NVIDIA ProjectLeonida#559 (66da8aaa), the size-query part of
DexNom/VIctory PR 62 (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 unchanged.
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown

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).

…of its own

Adds a_buffers_size_is_asked_through_a_handle_of_its_own (emit/size_tests.rs) and a
test-only count of the heap handles marked non-uniform (nonuniform_marked): the size
handle is marked non-uniform but on purpose not counted in nonuniform_handles, so that
count alone cannot tell the change.

The program of the_record_count_of_a_structured_buffer_read_by_index asks two buffers'
sizes (descriptor_data: the structured buffer's at s[8:11], the raw UAV's at s[20:23]).
With the change both get a handle of their own: marked 2, counted 0. With the change's
call in descriptor_data reverted the test fails (marked 0). It only lowers the program,
so it runs on any host and needs no RDNA 4 card. size_tests' build is split into
lower_cs (the lowering) and build (the host shader) for it.
@prosperkartik

Copy link
Copy Markdown
Author

Thanks @OreoFR and @KelboLive for checking.

Added a red/green test in bcb6087: 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

Copy link
Copy Markdown
Contributor

RTX 3090 / 621.40, validation on, bcb6087 merged onto main 0f86bd5: 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

Copy link
Copy Markdown
Contributor

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 KelboLive 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants