shader: implement 32-bit RDNA2 IMAGE_ATOMIC_CMPSWAP #952

Manually merged
github merged 3 commits from gh-pr/952/head into gh-pr/952/base 2026-11-26 09:37:59 +00:00
Owner

@⁠tototomate123 on GitHub · 2026-10-01 08:21 UTC · original
tototomate123:fix/rdna2-image-cmpswap → main · +163 −102 · 12 files · 3 commits · merged 2026-10-01 21:15 UTC

This is the first in a planned series of small, focused PRs adding shader translation and runtime fixes needed to improve support for Horizon Forbidden West and other AAA games; this PR implements 32-bit RDNA2 image compare-and-swap.

MIMG opcode 0x10 currently decodes as unsupported and stops shader compilation. This change decodes the 32-bit form, passes the replacement and comparison operands through a dedicated IR operation, and emits SPIR-V OpAtomicCompareExchange.

The translation reads replacement from VDATA and comparison from VDATA+1. GLC writes the previous texel into VDATA, including on a failed comparison. The existing EXEC guard suppresses both memory and register changes for inactive lanes. DMASK values other than 0x3 remain unsupported, including the 64-bit form (0xf). Existing image atomic format and memory-barrier policies are retained.

References

  • AMD machine-readable ISA documentation, specifically amdgpu_isa_rdna2.xml, IMAGE_ATOMIC_CMPSWAP, in the August 2026 archive: identifies opcode 16 and describes conditional replacement and GLC returning the original value. The XML was downloaded and inspected. It does not explicitly establish the consecutive input register order.
  • LLVM AMDGPU intrinsic definition: names the two inputs src, then cmp.
  • LLVM AMDGPU image lowering: packs the first and second atomic inputs in that order and selects DMASK 0x3 for two 32-bit inputs versus 0xf for two 64-bit inputs. Combining this with the intrinsic definition supports the VDATA replacement / VDATA+1 comparison ordering used here. These links are pinned to a specific LLVM commit.
  • Khronos OpAtomicCompareExchange: specifies Value before Comparator, replacement only on equality, and return of the original value on either outcome.

Validation

Built kyty_emulator, shader_cfg_tests, and shader_recompiler_compute_tests on Linux with Clang in RelWithDebInfo.

  • shader_cfg_tests: passed, including all 16 DMASK values for the new opcode.
  • shader_recompiler_compute_tests --image-cmpswap-only: passed; SPIR-V validation and GPU readback cover successful/failed comparisons, GLC on/off, unchanged comparison registers, inactive EXEC, and existing image swap/atomic variants.
  • Full compute suite: passed, including the compute and graphics cases supported by this device

Windows/macOS have not been tested locally

Implemented with the help of AI.

**@⁠tototomate123** on GitHub · 2026-10-01 08:21 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/952) `tototomate123:fix/rdna2-image-cmpswap` → `main` · +163 −102 · 12 files · 3 commits · merged 2026-10-01 21:15 UTC This is the first in a planned series of small, focused PRs adding shader translation and runtime fixes needed to improve support for Horizon Forbidden West and other AAA games; this PR implements 32-bit RDNA2 image `compare-and-swap`. MIMG opcode `0x10` currently decodes as unsupported and stops shader compilation. This change decodes the 32-bit form, passes the replacement and comparison operands through a dedicated IR operation, and emits SPIR-V `OpAtomicCompareExchange`. The translation reads replacement from VDATA and comparison from VDATA+1. GLC writes the previous texel into VDATA, including on a failed comparison. The existing EXEC guard suppresses both memory and register changes for inactive lanes. DMASK values other than 0x3 remain unsupported, including the 64-bit form (0xf). Existing image atomic format and memory-barrier policies are retained. ## References - [AMD machine-readable ISA documentation](https://gpuopen.com/machine-readable-isa/), specifically `amdgpu_isa_rdna2.xml`, `IMAGE_ATOMIC_CMPSWAP`, in the [August 2026 archive](https://gpuopen.com/download/AMD_GPU_MR_ISA_XML_2026_08_06.zip): identifies opcode 16 and describes conditional replacement and GLC returning the original value. The XML was downloaded and inspected. It does not explicitly establish the consecutive input register order. - [LLVM AMDGPU intrinsic definition](https://github.com/llvm/llvm-project/blob/d8c62b730f84536805d7a4a837a1f81ceac207b5/llvm/include/llvm/IR/IntrinsicsAMDGPU.td#L1529-L1531): names the two inputs `src`, then `cmp`. - [LLVM AMDGPU image lowering](https://github.com/llvm/llvm-project/blob/d8c62b730f84536805d7a4a837a1f81ceac207b5/llvm/lib/Target/AMDGPU/SIISelLowering.cpp#L10520-L10547): packs the first and second atomic inputs in that order and selects DMASK 0x3 for two 32-bit inputs versus 0xf for two 64-bit inputs. Combining this with the intrinsic definition supports the VDATA replacement / VDATA+1 comparison ordering used here. These links are pinned to a specific LLVM commit. - [Khronos OpAtomicCompareExchange](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpAtomicCompareExchange): specifies Value before Comparator, replacement only on equality, and return of the original value on either outcome. ## Validation Built `kyty_emulator`, `shader_cfg_tests`, and `shader_recompiler_compute_tests` on Linux with Clang in RelWithDebInfo. - `shader_cfg_tests`: passed, including all 16 DMASK values for the new opcode. - `shader_recompiler_compute_tests --image-cmpswap-only`: passed; SPIR-V validation and GPU readback cover successful/failed comparisons, GLC on/off, unchanged comparison registers, inactive EXEC, and existing image swap/atomic variants. - Full compute suite: passed, including the compute and graphics cases supported by this device Windows/macOS have not been tested locally Implemented with the help of AI.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:37:57 +00:00
github manually merged commit e71b50dedf into gh-pr/952/base 2026-11-26 09:37:59 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-01 08:31 UTC · original · edited 2026-10-01 09:33 UTC

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 80682c2b-603d-4e27-a7c9-dd22a06a859c

📥 Commits

Reviewing files that changed from the base of the PR and between b7a1fac898 and 6591be8eab.

📒 Files selected for processing (10)
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.h
  • src/graphics/shader/recompiler/frontend/decode/ImageOps.cpp
  • src/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cpp
  • src/graphics/shader/recompiler/frontend/decode/ShaderDecoder.h
  • src/graphics/shader/recompiler/frontend/translate/Memory.cpp
  • src/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cpp
  • src/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.inc
  • tests/ShaderRecompilerComputeTests.cpp
  • tests/shaderCfgTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The change adds image atomic compare-and-swap decoding for DMASK 3, translates the instruction into an IR operation, and emits it as SPIR-V OpAtomicCompareExchange. The emission uses replacement and comparison operands and the final instruction argument as the execution condition. New tests cover decoding, compare outcomes, GLC behavior, and disabled EXEC. Other edits adjust instruction formatting and decoder declarations.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Feature

Merge Risk: ⚪ Minimal · up to 6591b

The compare-and-swap implementation and its tests are consistent with the intended behavior. No merge-blocking issue was identified; proceed subject to normal build and test checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 6591b

The new operation is narrowly constrained and retains existing resource-selection and execution controls. No introduced security defect was established, but broader runtime isolation and hardware failure behavior remain incompletely verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The newly reachable mutation is a guest-instruction-driven conditional update of the addressed uint32 texel in a resolved storage image. Compared with existing image atomics, the inspected emitter adds an operation rather than a new descriptor-selection or resource-authority path. Effective exposure still depends on which images runtime metadata makes available.

Trust Boundaries and Controls

  • observed — The atomic operation and its barrier remain inside the EXEC conditional branch. The emitter now reads EXEC from the final argument, accommodating the additional comparator while preserving the existing four-argument atomic layout. An inactive branch does not execute the texel mutation.

Resilience and Maintainability Implications

  • inferred — Using a single native compare-exchange avoids an intermediate non-atomic load/compare/store state. Success conditionally replaces the texel; failure returns the old value without replacing it. Repetition and concurrency follow device-scope atomic semantics, with the existing post-atomic barrier retained; hardware interruption behavior was not validated.
🚥 Pre-merge checks | ✅ 4 | ❓ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ❓ Inconclusive Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 7 files. (3 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely identifies the implementation of 32-bit RDNA2 IMAGE_ATOMIC_CMPSWAP, which is the main change.
Description check ✅ Passed The description directly explains the opcode support, translation flow, operand behavior, GLC semantics, EXEC handling, validation, and platform test scope.
Full details: Docstring Coverage

Explanation

Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 7 files. (3 skipped: 1 unsupported, 2 too large.)

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-01 08:31 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/952#issuecomment-5927734757) · edited 2026-10-01 09:33 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/952?cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> Navigate logical layers of code changes, visualize relationships, and explore their blast radius. <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml **Review profile**: CHILL **Plan**: Advanced **Run ID**: `80682c2b-603d-4e27-a7c9-dd22a06a859c` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between b7a1fac898be93bfe0c282752a7a386fd5202486 and 6591be8eab78b11a9dd74a017619af41b0eb97ef. </details> <details> <summary>📒 Files selected for processing (10)</summary> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp` * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.h` * `src/graphics/shader/recompiler/frontend/decode/ImageOps.cpp` * `src/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cpp` * `src/graphics/shader/recompiler/frontend/decode/ShaderDecoder.h` * `src/graphics/shader/recompiler/frontend/translate/Memory.cpp` * `src/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cpp` * `src/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.inc` * `tests/ShaderRecompilerComputeTests.cpp` * `tests/shaderCfgTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough The change adds image atomic compare-and-swap decoding for DMASK 3, translates the instruction into an IR operation, and emits it as SPIR-V `OpAtomicCompareExchange`. The emission uses replacement and comparison operands and the final instruction argument as the execution condition. New tests cover decoding, compare outcomes, GLC behavior, and disabled EXEC. Other edits adjust instruction formatting and decoder declarations. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"6591be8eab78b11a9dd74a017619af41b0eb97ef" --> **Change:** Feature <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** _⚪ Minimal_ · up to `6591b` <!-- final_review_risk_coverage:{"sourceCommitId":"6591be8eab78b11a9dd74a017619af41b0eb97ef","coveredCommitId":"6591be8eab78b11a9dd74a017619af41b0eb97ef","kind":"reviewed"} --> The compare-and-swap implementation and its tests are consistent with the intended behavior. No merge-blocking issue was identified; proceed subject to normal build and test checks. <!-- final_review_risk_end --> <!-- architecture_review_start --> ### Security Architecture Review **Security architecture risk:** _🔵 Low_ · up to `6591b` The new operation is narrowly constrained and retains existing resource-selection and execution controls. No introduced security defect was established, but broader runtime isolation and hardware failure behavior remain incompletely verified. **Retained concerns** No architecture-level concerns identified. <details> <summary>Security review details</summary> **Security Blast Radius** - _inferred_ — The newly reachable mutation is a guest-instruction-driven conditional update of the addressed uint32 texel in a resolved storage image. Compared with existing image atomics, the inspected emitter adds an operation rather than a new descriptor-selection or resource-authority path. Effective exposure still depends on which images runtime metadata makes available. **Trust Boundaries and Controls** - _observed_ — The atomic operation and its barrier remain inside the EXEC conditional branch. The emitter now reads EXEC from the final argument, accommodating the additional comparator while preserving the existing four-argument atomic layout. An inactive branch does not execute the texel mutation. **Resilience and Maintainability Implications** - _inferred_ — Using a single native compare-exchange avoids an intermediate non-atomic load/compare/store state. Success conditionally replaces the texel; failure returns the old value without replacing it. Repetition and concurrency follow device-scope atomic semantics, with the existing post-atomic barrier retained; hardware interruption behavior was not validated. </details> <!-- architecture_review_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❓ 1</summary> ### ❌ Failed checks (1 inconclusive) | Check name | Status | Explanation | Resolution | | :----------------: | :------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ❓ Inconclusive | Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 7 files. (3 skipped: 1… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------: | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Title check | ✅ Passed | The title clearly and concisely identifies the implementation of 32-bit RDNA2 IMAGE_ATOMIC_CMPSWAP, which is the main change. | | Description check | ✅ Passed | The description directly explains the opcode support, translation flow, operand behavior, GLC semantics, EXEC handling, validation, and platform test scope. | </details> <details> <summary>Full details: Docstring Coverage</summary> **Explanation** Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 7 files. (3 skipped: 1 unsupported, 2 too large.) </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts > Autopilot is currently an internal CodeRabbit preview. <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=952)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠tototomate123 on GitHub · 2026-10-01 09:56 UTC · original

@⁠nmzik Quick question for future contributions: do you generally prefer smaller, focused PRs like this one over larger "game-fixing" PRs that can easily end up touching 5k+ lines?

Im mostly trying to understand what tends to be easiest for you to review and merge without wasting maintainer time. For example, would you rather have individual missing shader/runtime features split into separate PRs, even if several of them are ultimately needed to get one game further, or is a larger game-focused PR sometimes preferable?

Just want to structure future contributions in the most useful way for the project.

**@⁠tototomate123** on GitHub · 2026-10-01 09:56 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/952#issuecomment-5929062041) @⁠nmzik Quick question for future contributions: do you generally prefer smaller, focused PRs like this one over larger "game-fixing" PRs that can easily end up touching 5k+ lines? Im mostly trying to understand what tends to be easiest for you to review and merge without wasting maintainer time. For example, would you rather have individual missing shader/runtime features split into separate PRs, even if several of them are ultimately needed to get one game further, or is a larger game-focused PR sometimes preferable? Just want to structure future contributions in the most useful way for the project.
Author
Owner

@⁠Nmzik on GitHub · 2026-10-01 10:41 UTC · original

@⁠tototomate123 I would generally prefer one PR that gets the game in-game, with separate commits describing what each change does.

You can also make PRs for specific things, but please group related changes together. For example, don’t make PR for each new opcode you encounter. Group them/Do something like: new opcodes: ***, ***, ***.

**@⁠Nmzik** on GitHub · 2026-10-01 10:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/952#issuecomment-5929706543) @⁠tototomate123 I would generally prefer one PR that gets the game in-game, with separate commits describing what each change does. You can also make PRs for specific things, but please group related changes together. For example, don’t make PR for each new opcode you encounter. Group them/Do something like: new opcodes: ***, ***, ***.
Author
Owner

@⁠Nmzik on GitHub · 2026-10-01 10:42 UTC · original

@⁠tototomate123 This PR is fine. This is just for the future :)

**@⁠Nmzik** on GitHub · 2026-10-01 10:42 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/952#issuecomment-5929721307) @⁠tototomate123 This PR is fine. This is just for the future :)
This discussion has been locked. Commenting is limited to contributors.
No description provided.