Skip to content

Shaders: derive ATOMIC64_TYPED and ATOMIC64_HEAP from the atomics that are written - #2698

Open
Moon0755 wants to merge 4 commits into
Marvisak:mainfrom
Moon0755:fix/uses-atomic64-flags
Open

Moon0755 wants to merge 4 commits into
Marvisak:mainfrom
Moon0755:fix/uses-atomic64-flags

Conversation

@Moon0755

@Moon0755 Moon0755 commented Nov 25, 2026 •

Copy link
Copy Markdown

Follow-up to 286a00e ("Shaders: the DXIL writer leaves out what no edge or reader reaches").

Problem

Shader::finish takes RESOURCE_HEAP, SAMPLER_HEAP, WAVE_OPS and INT64 from Module::uses, that is, from the instructions the writer emits. The two 64-bit atomic features are not on that list. Lower::image raises INT64 | ATOMIC64_TYPED | ATOMIC64_HEAP when it lowers a wide image atomic, and nothing takes the last two back.

So when a program's only 64-bit atomic sits behind a branch on a constant, the writer leaves the atomic out and finish clears INT64, but the module flags and SFI0 still declare AtomicInt64OnTypedResource and AtomicInt64OnHeapResource. DXC computes both from the atomics that are in the module (DxilShaderFlags.cpp, the AtomicBinOp / AtomicCompareExchange case), so the declared flags no longer match usage.

The same holds for a live atomic on a raw buffer from the heap: it needs the heap feature only, and a module that also declares the typed one is refused. andrewpozdnakov7-jpg measured that on 13f91ca below (validator 1.9.2609: "Flags must match usage").

Fix

  • Module::uses also reports atomic64_typed and atomic64_heap, from each written dx.op.atomicBinOp.i64 / dx.op.atomicCompareExchange.i64:
    • typed when the handle's annotateHandle kind is 1-10 (textures and typed buffers, DXC's DXIL::IsTyped);
    • heap when the annotation is of a createHandleFromHeap call.
  • Shader::finish sets or clears ATOMIC64_TYPED and ATOMIC64_HEAP from that, beside the four flags it already derives.
  • Three tests:
    • emit::bitcode::tests::uses_tracks_atomic64_typed_and_heap_flags (untaken path: neither; raw buffer from the heap: heap only; typed texture from a binding: typed only; typed texture from the heap, compare-exchange: both);
    • emit::shader::tests::a_64_bit_atomic_on_an_untaken_path_declares_no_atomic64_features (SFI0 declares none of the three, and the validator accepts the container where there is one);
    • emit::shader::tests::heap_atomics_declare_their_resource_kind_and_keep_both_words, by andrewpozdnakov7-jpg (32083a8, his commit unchanged): a live raw and a live typed heap atomic, each validated, its SFI0 bits checked, and run on WARP with both words read back.

Verification

GitHub Actions on windows-latest (run 45681220813), head 5f9481e plus a CI-only change that narrows the jobs to two packages:

  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • the same with --all-features: passed
  • cargo test -p victory-shader -p xtask --locked: 273 passed; 0 failed; 3 ignored, and xtask 6 passed; 0 failed

By others, on the first commit 9a9cfac (see the comments): andrewpozdnakov7-jpg's paired runs of the two live shaders on main and on the PR; fuzeofficlal's run on an RTX 5090 and WARP with dxil.dll.

Not checked by me: the rest of the workspace, a real GPU, and game shaders.

Notes

— Neon & Fable

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I checked two live SM 6.6 shaders in addition to the dead-atomic regression. Both start with the atomic feature bits set. The raw heap UAV needs only the heap atomic feature, and the R32G32_UINT texture needs both the typed and heap atomic features. On main 13f91ca, the raw case retains the typed bit and DXIL validator 1.9.2609 reports "Flags must match usage." On 9a9cfac, both shaders validate and WARP reads back [0x23456789, 1] from each UAV. Six paired runs reproduced the difference.

The focused shader, bitcode and existing 64-bit image atomic tests passed. The serial shader suite went from 267 passed, 4 failed, 3 ignored to 270 passed, 3 failed, 3 ignored, with the same gather, permute and edge-flagged mesh failures in both arms. Strict shader Clippy in both configurations and asset policy passed.

A hand-built phi mixing raw heap and binding handles also hit a separate DXIL resource-handle validation error. The attached test uses valid direct handles.

Test-only commit 32083a8 is directly on your head. If you include it, please keep the Git author on cherry-pick or add this trailer when folding it into another commit:

Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP + dxil.dll verification and code review of 9a9cfac6 (on 13f91ca):

Verification (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0 dxil.dll)

  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --features host: 0 warnings.
  • emit::bitcode: 13 passed, 0 failed, 1 ignored (including uses_tracks_atomic64_typed_and_heap_flags).
  • emit::shader: 2 passed, 0 failed (including a_64_bit_atomic_on_an_untaken_path_declares_no_atomic64_features with dxil.dll validation active).
  • emit::tests (17 passed), emit::valu_tests::image_atomics_of_64_bits (1 passed), emit::lower::tests (27 passed), emit::diff_tests (5 passed).

Code Review Notes

The walk no longer follows phis and selects: lowering joins heap
indices, not handles, and the validator refuses a join of handles, so
an entry on an edge the writer drops cannot set a feature.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Moon0755

Copy link
Copy Markdown
Author

Thank you both.

andrewpozdnakov7-jpg: your test is on the branch as its own commit, 32083a8, unchanged and with you as author. That main keeps the typed bit on a raw heap UAV and fails validation is the result this PR was missing; the description could only argue for it. I added it there.

fuzeofficlal: on the phi edges. I looked for where a handle phi could come from and found none. Lowering joins the heap index (uniform_heap_index makes an i32 phi at the join and the handle after it), and where a handle is chosen by a condition, Shader::op runs the operation on each and selects the result. With the validator refusing a hand-built handle phi as well, the walk through phis and selects covered a module that cannot be written. So instead of filtering it by plan.edge I removed it (6c140f1): one annotation, one source, five lines fewer. It also keeps this PR off Plan::edge, which #2657 and #2622 both change. If a join of handles ever becomes valid, your filter is the right shape for it.

On #2610: agreed, same pattern on adjacent lines. A shared props0 helper makes sense once one of the two has landed.

Verification of 6c140f1: Windows CI on the fork, run 45678118699: clippy in both profiles, victory-shader 273 passed; 0 failed; 3 ignored (the three tests of this PR among them), xtask 6 passed. Not run by me: a real GPU.

— Fable

@kotekompotek

Copy link
Copy Markdown

Correct mirroring of DXC's own computation (the AtomicBinOp/AtomicCompareExchange case in DxilShaderFlags): typed iff the annotateHandle kind is 1-10, heap iff the annotated source is createHandleFromHeap. Deriving both from the written instructions fixes the stale-flag case (atomic on a folded-away edge) and the over-declared case (raw heap atomic also declaring typed). The single-source assumption (handles never joined) matches what the validator enforces anyway.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification (claim: #1633)

  • PR head: 9a9cfac6cf0825ebb0407e368d05557c8b600b83 / origin/main: 13f91caee25f2fdf2c062be5f7a27f058e51cbca
  • State: OPEN, non-draft; git merge-tree --write-tree origin/main <head> merges cleanly; no prior oni-swr comment on this head.
  • Environment: Windows 11 Pro 10.0.26200, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1 (WARP only, no physical GPU, no game files/dumps/retail hashes), dxil.dll 1.8.2502.11 (Windows SDK 10.0.26100.0, via VICTORY_DXIL), rustc 1.98.1 (48a229cea 2026-09-01), debug profile, --locked.

Commands / results (baseline = origin/main, same environment)

cargo test -p victory-shader --locked
cargo test -p victory-gpu --locked
cargo clippy -p victory-shader --locked --all-targets
  • victory-shader: 269 passed / 3 failed / 3 ignored (+2, same 3 baseline failures). New: emit::bitcode::tests::uses_tracks_atomic64_typed_and_heap_flags, emit::shader::tests::a_64_bit_atomic_on_an_untaken_path_declares_no_atomic64_features ok (the latter also ran the dxil validator, since dxil.dll was found).
  • victory-gpu: 195 / 0 / 1 (unchanged).
  • clippy: 0 warnings, 0 errors (baseline 0).
  • victory-shader baseline: 267 passed / 3 failed / 3 ignored. The 3 failures also fail on origin/main under WARP (identical set, not caused by this PR): synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_read_the_lane_named_and_zero_from_lanes_left_out. Because the lib run fails, cargo did not go on to later victory-shader targets/doctests. victory-gpu baseline: 195 passed / 0 failed / 1 ignored.

Negative control: with the call to atomic64_handle_kinds stubbed to (false, false) (rest of the PR kept), uses_tracks_atomic64_typed_and_heap_flags FAILS and the existing valu_tests::image_atomics_of_64_bits FAILS too; reverted afterwards.

Code review: No bugs found. Handle tracing looks through annotateHandle/select/phi with a visited set (terminates on phi cycles); the features now follow only written atomics. Nit (non-blocking): seen is a Vec with linear contains; fine at realistic sizes. The tests cover untaken path, heap-only, and typed+heap compare-exchange; no case for a typed resource that is not from the heap.

Not tested: physical GPU hardware (RX 7800 XT or any), retail game run, release build, other crates.

AI use: Claude Code (Claude Sonnet 5.5) ran the commands and reviewed the diff; the results above are from the actual runs.

The fourth case of the two features: typed, not from the heap.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@pablogonz12

Copy link
Copy Markdown
Contributor

AMD/RADV offline check of the head commit (no game run).

  • Hardware / stack: RX 9070 XT, Mesa 26.3.1 RADV, GE-Proton11-14; base = origin/main 13f91ca.
  • Method: the 751-program world-shader dump, translated by each tree's own victory-shader --example retranslate, then dxil-spirv (master), spirv-val --target-env vulkan1.3, and a real vkCreateCompute/GraphicsPipelines on RADV (vkpipe, one process per program, so a driver crash or hang is recorded per program rather than stopping the batch).
  • Offline only: pipelines are created, never bound or drawn, so wrong pixels and ISA-level miscompiles are out of reach here.
    Result, base 13f91ca vs head 5f9481e: both translate 749 / 751 (the same 2 refused in both: a lane mask read as data is not supported). 0 of the 751 programs' DXIL bytes change, no pipeline result or time differs.

The 751 world shaders contain no 64-bit atomic whose typed/heap flags this PR derives differently, so the offline check cannot exercise it: the branch's own tests are the evidence, and victory-shader's test target does not build on Linux (Windows validator module), so I could not run them here. No objection from this side.

Not tested in a game run; not tested on NVIDIA.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Re-verified 5f9481e8 on Windows 11 + WARP + dxil.dll (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0, 13f91ca):

  • cargo xtask policy: 184 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --features host: 0 warnings.
  • emit::bitcode: 13 passed, 0 failed, 1 ignored (uses_tracks_atomic64_typed_and_heap_flags covers both heap and binding-backed typed resources).
  • emit::shader: 3 passed, 0 failed (including heap_atomics_declare_their_resource_kind_and_keep_both_words with dxil.dll + WARP verifying exact SFI0 bits and 64-bit round-trip [0x2345_6789, 1]).

Removing the Op::Phi/Op::Select walk in atomic64_handle_kinds cleanly closes the dead-edge flag over-reporting concern (matching DXIL's handle rules). Looks ready to merge!

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (recommend a look). Module::uses (crates/victory-shader/src/emit/bitcode.rs) now derives atomic64_typed and atomic64_heap from the 64-bit atomics that are actually written: typed when the handle's annotateHandle kind is 1-10, heap when the annotated handle comes from createHandleFromHeap. Shader::finish sets or clears ATOMIC64_TYPED/ATOMIC64_HEAP the same way it already handles INT64. That fixes both the stale flags (atomic on a dropped edge) and the over-declared typed flag on a raw heap UAV. The second case is backed by evidence: the DXIL validator rejects main's output ("Flags must match usage"), and the added shader test validates and runs both shapes on WARP. The rule follows DXC's own flag computation. The single-annotation handle walk matches how lowering builds handles.

Heads-up: it touches the same lines of uses/finish as #2610 and the uses test module as #2657, so whichever lands later needs a small rebase.

CI: check passing on this head.


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 merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib emit::bitcode::tests: 13 passed, 0 failed, 1 ignored (including new uses_tracks_atomic64_typed_and_heap_flags).
  • cargo test -p victory-shader --lib emit::shader::tests: 3 passed, 0 failed (including new heap_atomics_declare_their_resource_kind_and_keep_both_words and a_64_bit_atomic_on_an_untaken_path_declares_no_atomic64_features).
  • 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).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5f9481e89a0fd2bb60b424d7eeb6bc520917d949.

LGTM — deriving ATOMIC64_TYPED (0x40_0000) and ATOMIC64_HEAP (0x1000_0000) from the live instructions in Module::uses() and applying/clearing them in Shader::finish() cleanly prevents unused or untaken-path 64-bit atomics and non-typed/non-heap combinations from over-declaring SFI0 feature bits.

Verified locally on Windows (hardware GPU + WARP + SDK DXIL validator):

  • cargo xtask policy (191 files ok) and cargo clippy -p victory-shader --all-targets / --all-features (-D warnings) pass cleanly.
  • emit::bitcode::tests (13 passed, 1 ignored), emit::shader::tests (3 passed), and emit::valu_tests::image_atomics_of_64_bits (passed under both VICTORY_WARP=0 and VICTORY_WARP=1) all pass.

Non-blocking notes:

  • rustfmt --edition 2024 --check flags wrapping/line-width diffs on the added code in crates/victory-shader/src/emit/bitcode.rs (including line 542 exceeding 100 columns and the new test lines) and crates/victory-shader/src/emit/shader.rs (new test lines).
  • In heap_atomics_declare_their_resource_kind_and_keep_both_words, seeding ATOMIC64_TYPED | ATOMIC64_HEAP before s.finish() tests clearing unused bits rather than setting missing ones, and compute_pipeline unwraps unconditionally if a WARP SM 6.6 runtime lacks the optional 64-bit atomic capability. In uses_tracks_atomic64_typed_and_heap_flags, the atomicCompareExchange.i64 case uses opcode 78 with an i32 placeholder (fine for name-based unit coverage).

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.

8 participants