feat(shader): implement S_SCRATCH_LOAD_DWORD, S_SCRATCH_LOAD_DWORDX2 and S_SCRATCH_LOAD_DWORDX4 #766

Merged
Kikoarl merged 2 commits from upstream-pr/766 into main 2026-10-05 23:52:17 +00:00
Kikoarl commented 2026-10-05 22:16:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-05T22:16:54Z.


What

Implement RDNA 2 SMEM scratch load instructions: S_SCRATCH_LOAD_DWORD, S_SCRATCH_LOAD_DWORDX2 and S_SCRATCH_LOAD_DWORDX4.

Tested

Linux x86_64, unit test scalar_scratch_load_tests verifying decoding and IR translation for 1, 2, and 4 dwords.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4751412622 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/766). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-05T22:16:54Z. --- ### What Implement RDNA 2 SMEM scratch load instructions: `S_SCRATCH_LOAD_DWORD`, `S_SCRATCH_LOAD_DWORDX2` and `S_SCRATCH_LOAD_DWORDX4`. ### Tested Linux x86_64, unit test `scalar_scratch_load_tests` verifying decoding and IR translation for 1, 2, and 4 dwords. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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
oneandonlydean commented 2026-10-05 22:56:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T22:56:53Z.


Thanks for this. The decoding looks right to me: SMEM opcodes 5 to 7 are the scalar scratch loads in the GFX10.1 encoding. The translation has a semantic problem, though.

sScratchLoad emits LoadAddressU32 on the Scratch resource, which the SPIR-V backend lowers to the invocation's own Function variable. So each lane reads its own private copy at that byte offset, and the destination SGPR can end up with a different value in every lane. On the console a scalar load returns one dword for the whole wave, read from the wave's scratch memory. As far as I know, vector scratch accesses are swizzled per lane there (consecutive dwords of one lane are a lane stride apart), while a scalar access uses the plain address. So a dword written by a vector scratch_store_dword in lane L is not the dword that s_scratch_load_dword reads at the same offset, and a lane-private read can't model either layout.

Two smaller points:

  • Only the low dword of the SBASE pair is read (readU32(inst.source0)); the high dword is ignored without a check.
  • scalar_scratch_load checks the decode and counts the LoadAddressU32s, but nothing runs the result, so the test would pass with any address or layout.

Which title needs these instructions? If it only reads back values that its own s_scratch_store wrote, a wave-uniform scratch array would do; otherwise the swizzled layout has to be modelled. Until then, I think the loads should keep throwing, or the limitation should be listed in docs/dev/TechnicalDebt.md.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6004894216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/766#issuecomment-6004894216). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T22:56:53Z. --- Thanks for this. The decoding looks right to me: SMEM opcodes 5 to 7 are the scalar scratch loads in the GFX10.1 encoding. The translation has a semantic problem, though. `sScratchLoad` emits `LoadAddressU32` on the `Scratch` resource, which the SPIR-V backend lowers to the invocation's own `Function` variable. So each lane reads its own private copy at that byte offset, and the destination SGPR can end up with a different value in every lane. On the console a scalar load returns one dword for the whole wave, read from the wave's scratch memory. As far as I know, vector scratch accesses are swizzled per lane there (consecutive dwords of one lane are a lane stride apart), while a scalar access uses the plain address. So a dword written by a vector `scratch_store_dword` in lane L is not the dword that `s_scratch_load_dword` reads at the same offset, and a lane-private read can't model either layout. Two smaller points: - Only the low dword of the SBASE pair is read (`readU32(inst.source0)`); the high dword is ignored without a check. - `scalar_scratch_load` checks the decode and counts the `LoadAddressU32`s, but nothing runs the result, so the test would pass with any address or layout. Which title needs these instructions? If it only reads back values that its own `s_scratch_store` wrote, a wave-uniform scratch array would do; otherwise the swizzled layout has to be modelled. Until then, I think the loads should keep throwing, or the limitation should be listed in `docs/dev/TechnicalDebt.md`.
Kikoarl commented 2026-10-05 23:06:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-05T23:06:45Z.


Thanks for the detailed review and architectural explanation.

These instructions were implemented to complete SMEM coverage from the RDNA 2 ISA specification rather than from a specific title trace, so there is no immediate title demanding them yet.

Given that, I agree that having s_scratch_load throw an explicit exception during translation (while keeping the GFX10.1 decoding and opcode definitions in place) is the cleanest and safest approach. That way, we complete the decoder without risking silent data corruption across lanes until the wave-uniform/swizzled scratch layout is modelled.

I will update the PR to have the translation throw, adjust the test accordingly, and check the SBASE register. Thanks again.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6005053969 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/766#issuecomment-6005053969). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-05T23:06:45Z. --- Thanks for the detailed review and architectural explanation. These instructions were implemented to complete SMEM coverage from the RDNA 2 ISA specification rather than from a specific title trace, so there is no immediate title demanding them yet. Given that, I agree that having `s_scratch_load` throw an explicit exception during translation (while keeping the GFX10.1 decoding and opcode definitions in place) is the cleanest and safest approach. That way, we complete the decoder without risking silent data corruption across lanes until the wave-uniform/swizzled scratch layout is modelled. I will update the PR to have the translation throw, adjust the test accordingly, and check the `SBASE` register. Thanks again.
github-actions[bot] commented 2026-10-05 23:37:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T23:37:34Z.


Progress report (a4bc492 → 9ac108b)

📈 GPU shader instructions: 96.4% (+0.26%, +3 implemented instructions)

✅ 3 implemented
Encoding Name
SMEM S_SCRATCH_LOAD_DWORD
SMEM S_SCRATCH_LOAD_DWORDX2
SMEM S_SCRATCH_LOAD_DWORDX4
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6005563087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/766#issuecomment-6005563087). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T23:37:34Z. --- <!-- progress-report --> ### Progress report (a4bc492 → 9ac108b) 📈 **GPU shader instructions**: 96.4% (+0.26%, +3 implemented instructions) <details> <summary>✅ 3 implemented</summary> | Encoding | Name | | - | - | | SMEM | `S_SCRATCH_LOAD_DWORD` | | SMEM | `S_SCRATCH_LOAD_DWORDX2` | | SMEM | `S_SCRATCH_LOAD_DWORDX4` | </details>
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!766
No description provided.