feat(shader): implement buffer_atomic_csub #594

Merged
DotDebian merged 1 commit from upstream-pr/594 into main 2026-10-05 01:25:57 +00:00
DotDebian commented 2026-10-05 00:17:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-05T00:17:24Z.


What

MUBUF opcode 0x34, buffer_atomic_csub, had no entry in the decode table. It is now decoded and translated: the instruction subtracts the data register from a buffer dword, clamps the result to zero when the subtrahend is larger than the stored value, and returns the previous value in the data register.

  • RdnaOpcode::BufferAtomicCsub, decoded like the other 32-bit buffer atomics
  • new IrOpcode::BufferAtomicUSubSat32, emitted through the existing EmitAtomicUpdate loop with old >= value ? old - value : 0
  • the form without glc throws: LLVM defines only the returning variant of this instruction (BUFInstructions.td, BUFFER_ATOMIC_CSUB under the GFX10 B encoding), so there is nothing to base the other one on

Only the MUBUF instruction is covered. global_atomic_csub belongs with the flat and global atomics of #491, #495 and #506 and is left to them. Those three PRs and #573 add lines next to the ones added here, so whichever lands second needs a small textual rebase; none of them touches MUBUF 0x34.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA RTX 5080. No title run.

New execution test agc_driver_buffer_atomic_csub: a compute shader runs the instruction on wave32 with 32 and 64 threads and on wave64 with 64 threads. Each thread has its own dword and subtrahend (16 edge rows such as 0 - 1, 1 - 0xffffffff, 0x80000000 - 0x7fffffff, then hashed values), once with a full exec mask and once with 0x55555555. It checks the stored value, the returned value, and that inactive lanes leave both memory and the data register alone. Two more dwords are shared by all lanes: one that stays above zero and one that every lane drives to zero. The form without glc is checked to throw.

ctest (Release): 222/222 passed.

Not tested on AMD hardware, and not compared against a console.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4738688176 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/594). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-05T00:17:24Z. --- ### What MUBUF opcode `0x34`, `buffer_atomic_csub`, had no entry in the decode table. It is now decoded and translated: the instruction subtracts the data register from a buffer dword, clamps the result to zero when the subtrahend is larger than the stored value, and returns the previous value in the data register. - `RdnaOpcode::BufferAtomicCsub`, decoded like the other 32-bit buffer atomics - new `IrOpcode::BufferAtomicUSubSat32`, emitted through the existing `EmitAtomicUpdate` loop with `old >= value ? old - value : 0` - the form without `glc` throws: LLVM defines only the returning variant of this instruction (`BUFInstructions.td`, `BUFFER_ATOMIC_CSUB` under the GFX10 B encoding), so there is nothing to base the other one on Only the MUBUF instruction is covered. `global_atomic_csub` belongs with the flat and global atomics of #491, #495 and #506 and is left to them. Those three PRs and #573 add lines next to the ones added here, so whichever lands second needs a small textual rebase; none of them touches MUBUF `0x34`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA RTX 5080. No title run. New execution test `agc_driver_buffer_atomic_csub`: a compute shader runs the instruction on wave32 with 32 and 64 threads and on wave64 with 64 threads. Each thread has its own dword and subtrahend (16 edge rows such as `0 - 1`, `1 - 0xffffffff`, `0x80000000 - 0x7fffffff`, then hashed values), once with a full exec mask and once with `0x55555555`. It checks the stored value, the returned value, and that inactive lanes leave both memory and the data register alone. Two more dwords are shared by all lanes: one that stays above zero and one that every lane drives to zero. The form without `glc` is checked to throw. `ctest` (Release): 222/222 passed. Not tested on AMD hardware, and not compared against a console. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-05 00:17:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T00:17:45Z.


Progress report (9651ec9 → da9752b)

📈 GPU shader instructions: 83.96% (+0.08%, +1 implemented instructions)

✅ 1 implemented
Encoding Name
MUBUF BUFFER_ATOMIC_CSUB
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985967228 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/594#issuecomment-5985967228). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T00:17:45Z. --- <!-- progress-report --> ### Progress report (9651ec9 → da9752b) 📈 **GPU shader instructions**: 83.96% (+0.08%, +1 implemented instructions) <details> <summary>✅ 1 implemented</summary> | Encoding | Name | | - | - | | MUBUF | `BUFFER_ATOMIC_CSUB` | </details>
oneandonlydean commented 2026-10-05 01:18:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:41Z.


Thanks. I checked the test kernel with llvm-mc for gfx1030: it disassembles to the three loads, buffer_atomic_csub ... offen glc with the full exec mask and again at offset 4 under exec_lo = 0x55555555, the two shared dwords at offsets 2048 and 2052, and the stores, which matches what Check expects. The clamp (old >= value ? old - value : 0, returning the old value) is the ISA's definition, and going through EmitAtomicUpdate keeps it atomic like inc and dec. Throwing for the form without glc is fine.

One note for the record: LLVM only has this instruction for gfx10.3. For gfx1013 llvm-mc says "instruction not supported on this GPU", so I can't confirm from LLVM that the PS5 has it. On gfx10.1 MUBUF 0x34 is unused, though, so decoding it can't hide another instruction, and a title that never uses it is unaffected.

agc_driver_buffer_atomic_csub passes here and fails on main's code ("MUBUF opcode is not supported"). PPSA21564 has no buffer_atomic_csub in its shaders, so this doesn't change my runs. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986483944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/594#issuecomment-5986483944). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T01:18:41Z. --- Thanks. I checked the test kernel with llvm-mc for gfx1030: it disassembles to the three loads, `buffer_atomic_csub ... offen glc` with the full exec mask and again at offset 4 under `exec_lo = 0x55555555`, the two shared dwords at offsets 2048 and 2052, and the stores, which matches what `Check` expects. The clamp (`old >= value ? old - value : 0`, returning the old value) is the ISA's definition, and going through `EmitAtomicUpdate` keeps it atomic like inc and dec. Throwing for the form without `glc` is fine. One note for the record: LLVM only has this instruction for gfx10.3. For gfx1013 llvm-mc says "instruction not supported on this GPU", so I can't confirm from LLVM that the PS5 has it. On gfx10.1 MUBUF 0x34 is unused, though, so decoding it can't hide another instruction, and a title that never uses it is unaffected. `agc_driver_buffer_atomic_csub` passes here and fails on main's code ("MUBUF opcode is not supported"). PPSA21564 has no `buffer_atomic_csub` in its shaders, so this doesn't change my runs. Ready to merge from my side.
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
boykopovar/AnyPS5!594
No description provided.