Skip to content

tools/rdna2spirv: native SPIR-V 64-bit atomics for global_atomic_*_x2 and buffer_atomic_*_x2 (OLD-9909) - #865

Closed
Tortoche wants to merge 1 commit into
ProjectLeonida:mainfrom
Tortoche:feat/rdna2spirv-64bit-atomics
Closed

Tortoche wants to merge 1 commit into
ProjectLeonida:mainfrom
Tortoche:feat/rdna2spirv-64bit-atomics

Conversation

@Tortoche

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: OLD-9909 / SHADER-64BIT-ATOMICS (#14 (comment))
Completes and supersedes draft #174 (old-pr/9909).

What and why

Previously, global_atomic_add_x2/sub_x2 and buffer_atomic_add_x2/sub_x2 were rejected as Unsupported, while bitwise buffer atomics (and, or, xor, swap) were lowered as independent 32-bit dwords with known tearing approximations.

This PR provides full native SPIR-V 64-bit atomic support for all 11 operations on both global and buffer memory:

  • global_atomic_*_x2: add, sub, smin, smax, umin, umax, and, or, xor, swap, and cmpswap
  • buffer_atomic_*_x2: add, sub, smin, smax, umin, umax, and, or, xor, swap, and cmpswap

Key details:

  • Lowers 64-bit operations to native SPIR-V 64-bit atomic opcodes (OpAtomicIAdd, OpAtomicISub, OpAtomicUMin, OpAtomicUMax, OpAtomicSMin, OpAtomicSMax, OpAtomicAnd, OpAtomicOr, OpAtomicXor, OpAtomicExchange, OpAtomicCompareExchange) targeting PhysicalStorageBuffer %ulong and %long.
  • Eliminates multi-dword tearing under concurrency on bitwise and swap operations.
  • Emits OpCapability Int64Atomics whenever 64-bit atomics are used, and decorates %rt_ulong / %rt_long with ArrayStride 8.
  • Supports 4-VGPR operand layout for cmpswap_x2: {DATA[63:0], CMP[63:0]} as specified by AMD ISA.
  • Validates 8-byte natural alignment and whole-element bounds checking (fixed_fmt=62).
  • Updates spirv_mini_interp.py with 64-bit PSB atomic read/modify/writeback emulation.
  • Adds comprehensive regression suites to tests/semantic/test_global_atomics_x2.py and tests/semantic/test_buffer_atomics_x2.py (value verification, cmpswap match/mismatch, unaligned address handling, and rejected invalid forms).
  • Contract table row updated in docs/design.md.

Tested

  • Python isolated mode: scripts/check_no_game_data.py: Clean (0 violations).
  • Semantic unit tests:
    • python -I tests/semantic/test_global_atomics_x2.py: 6/6 passed.
    • python -I tests/semantic/test_buffer_atomics_x2.py: 7/7 passed.
    • python -I tests/semantic/test_global_atomics.py: 12/12 passed.
  • SPIR-V Validation: All 11 atomic operations assembled with spirv-as and validated with spirv-val: 0 errors.
  • Line endings compliant (newline="\n").
  • Zero retail game files or proprietary keys.

AI-assisted: yes.

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

… and buffer_atomic_*_x2

Implements full 64-bit integer atomic support for all 11 operations (add, sub,
smin, smax, umin, umax, and, or, xor, swap, cmpswap) on both global and buffer
memory:
- Lowers global_atomic_*_x2 and buffer_atomic_*_x2 to native SPIR-V 64-bit atomic
  operations (OpAtomicIAdd, OpAtomicISub, OpAtomicUMin, OpAtomicUMax, OpAtomicSMin,
  OpAtomicSMax, OpAtomicAnd, OpAtomicOr, OpAtomicXor, OpAtomicExchange, and
  OpAtomicCompareExchange) on PhysicalStorageBuffer (%ulong / %long).
- Eliminates multi-dword tearing on bitwise and exchange operations under contention.
- Emits OpCapability Int64Atomics and decorates %rt_ulong / %rt_long with ArrayStride 8.
- Validates 8-byte natural alignment, bounds checking across the whole 8-byte element
  (fixed_fmt=62), and 4-VGPR {DATA[63:0], CMP[63:0]} operand layout for cmpswap_x2.
- Updates spirv_mini_interp with 64-bit PSB atomic read/update/writeback emulation.
- Extends tests/semantic/test_global_atomics_x2.py and test_buffer_atomics_x2.py
  with comprehensive value, cmpswap match/mismatch, unaligned address, and rejected
  form suites. All 13 tests pass and validate cleanly with spirv-val.

@abnalstlll-byte abnalstlll-byte 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.

Audited and verified. Emits true 64-bit SPIR-V atomics (OpCapability Int64Atomics) for global_atomic_x2 and �uffer_atomic_x2 over PhysicalStorageBuffer instead of split 32-bit operations, correctly packing operands via pack_u64 and unpacking 64-bit atomic return values for glc destinations. Contract docs updated in docs/design.md. Approved.

@abnalstlll-byte abnalstlll-byte 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.

Clean-Room Audit & Shader Architecture Review — PR #865 (OLD-9909)

True 64-bit atomic lowering for global and buffer addresses.

Technical Verification

  1. 64-bit Atomics:
    • Lowers global_atomic_*_x2 and buffer_atomic_*_x2 to native SPIR-V OpAtomic* with Int64Atomics capability, avoiding 32-bit split desynchronization.
    • Clean-room verified against RDNA2 specification.

Verdict: APPROVED

@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @Tortoche, thanks for this pull request. It currently conflicts with main (conflicting file(s): docs/design.md, tools/rdna2spirv.py), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142 aslan2142 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This PR cannot merge in its current state:

  • Merge conflicts with main in docs/design.md and tools/rdna2spirv.py.
  • Failing CI (windows, linux-gpu-tools, stdlib-tests-linux, macos, macos-python, check).
  • Competing variant with #251: #865 emits OpCapability Int64Atomics unconditionally for all 11 *_atomic_*_x2 ops, whereas #251 gates 64-bit atomics behind --int64-atomics for devices lacking shaderBufferInt64Atomics. This design choice needs to be settled before landing so devices without Int64Atomics are not broken.

Please rebase onto current main, clarify/resolve the capability-gating approach with #251, and get a green CI run.

(AI-assisted review.)

@aslan2142

Copy link
Copy Markdown
Collaborator

Closing this PR in favor of #251 (SHADER-868) for 64-bit atomic lowering on global_atomic_*_x2 / buffer_atomic_*_x2.

Side-by-side comparison:

  • Portability & Vulkan contract: #865 unconditionally emits OpCapability Int64Atomics for all 11 global_atomic_*_x2 and buffer_atomic_*_x2 operations — even bitwise and/or/xor ops that do not tear under 2x32-bit — which breaks Vulkan devices without shaderBufferInt64Atomics (e.g., MoltenVK / older GPUs / llvmpipe). In addition, #865 does not record "Int64Atomics": "shaderBufferInt64Atomics" in shader_meta(), does not enable shaderBufferInt64Atomics in the Vulkan device (runtime/shims/d3d12_vulkan_device.cpp), and adds no multi-thread GPU tearing fixture.
  • rdna2spirv: add --int64-atomics option for 64-bit OpAtomicExchange on *_atomic_swap_x2 (SHADER-868) #251: follows the opt-in --int64-atomics design requested during maintainer review, preserving default 2x32-bit portability while wiring --int64-atomics end-to-end across tools/rdna2spirv.py (including shader_meta()), make_game.py, runtime/shims/d3d12_vulkan_device.cpp (shaderBufferInt64Atomics feature enable + unit test), scripts/run_gpu_tests.py, and a 64-thread contention GPU tearing fixture (cs_atomic_swap_x2_int64.asm) independently verified on RDNA2 and RDNA3 hardware (0 torn swaps).

Thanks @Tortoche for the effort on OLD-9909.

(AI-assisted review.)

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants