SrtWalker wrongly SRT-flattens a plain S_LOAD, dropping the real memory read #606

Closed
opened 2026-11-26 09:27:09 +00:00 by github-bot · 2 comments
Owner

@⁠defektu on GitHub · 2026-09-13 15:32 UTC · original

Summary

A plain scalar memory load (S_LOAD_DWORD / LoadAddressU32, i.e. a raw guest-address read, not S_BUFFER_LOAD) whose base address is compile-time-uniform gets incorrectly treated by SrtWalker as an SRT-flattenable compile-time constant. The recompiler bakes a stale/wrong value into flattened_srt instead of emitting a real runtime buffer read, so the shader never actually reads guest memory for that load.

Root cause

In src/graphics/shader/recompiler/ir/passes/SrtWalker.cpp, the validator that decides whether a value can be flattened into the compile-time SRT constant buffer accepts LoadAddressU32\/ReadConstBuffer reads as long as the read pattern matches (IsRawRead) and its handle resolves to GetAddressResource/GetBufferResource:

if (op == ValueOpcode::LoadAddressU32 || op == ValueOpcode::ReadConstBuffer) {
    const auto  expected = op == ValueOpcode::LoadAddressU32
                               ? ValueOpcode::GetAddressResource
                               : ValueOpcode::GetBufferResource;
    const auto* handle = inst->NumArgs() != 0 ? inst->Arg(0).ResolveInstruction() : nullptr;
    if (!IsRawRead(m_program, *inst) || handle == nullptr ||
        handle->GetOpcode() != expected) {
        return finish(false);
    }
}
...
} else if (op != ValueOpcode::ReadConst && op != ValueOpcode::ReadConstBuffer &&
           op != ValueOpcode::LoadAddressU32 && !IsRuntimeUniformOp(op)) {
    return finish(false);
}

This conflates two different things:

  • "the address is compile-time-uniform" — true and useful for flattening genuine SRT/user-data descriptor fields, which are set up once per draw on the CPU side.
  • "the memory contents at that address are a compile-time constant" — categorically false for an arbitrary guest-memory read via S_LOAD. The address being constant says nothing about what the game has written there; it can be genuine, dynamic runtime data.

Repro

shader_recompiler_compute_tests has two cases that trigger this directly:

  • ScalarMemoryLoadVariants
  • ScalarLoadSignedImmediateOffsetAddsSoffset

Both fail at readback with the loaded dwords coming back as zero instead of the buffer's actual contents.

I disassembled the compiled SPIR-V for ScalarLoadSignedImmediateOffsetAddsSoffset (`spirv-dis`) to confirm: the value written to the output buffer is loaded directly from flattened_srt at compile time — there is no OpAccessChain/OpLoad against the actual input storage buffer anywhere in the module for that S_LOAD.

Where I found this

Found while merging origin/main into PR #558 (astrobot-raytracing) — see that PR for context. Confirmed this is pre-existing in main's own SrtWalker/ResourceMaterialization code and unrelated to anything changed in that PR (verified the affected files aren't touched by any commit on the branch).

A related but separate FMASK crash in the same neighborhood (ResourceMaterialization.cpp:1198, NativeImageKeyArg indexing past program.info.images for the FMASK remap sentinel) was also found and already fixed on that branch; happy to open a small standalone PR for that fix against main if useful.

Co-Authored-By: Claude Sonnet 5 [email protected]

**@⁠defektu** on GitHub · 2026-09-13 15:32 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/606) ## Summary A plain scalar memory load (`S_LOAD_DWORD` / `LoadAddressU32`, i.e. a raw guest-address read, not `S_BUFFER_LOAD`) whose base address is compile-time-uniform gets incorrectly treated by `SrtWalker` as an SRT-flattenable compile-time constant. The recompiler bakes a stale/wrong value into `flattened_srt` instead of emitting a real runtime buffer read, so the shader never actually reads guest memory for that load. ## Root cause In `src/graphics/shader/recompiler/ir/passes/SrtWalker.cpp`, the validator that decides whether a value can be flattened into the compile-time SRT constant buffer accepts `LoadAddressU32\`/`ReadConstBuffer` reads as long as the read pattern matches (`IsRawRead`) and its handle resolves to `GetAddressResource`/`GetBufferResource`: ```cpp if (op == ValueOpcode::LoadAddressU32 || op == ValueOpcode::ReadConstBuffer) { const auto expected = op == ValueOpcode::LoadAddressU32 ? ValueOpcode::GetAddressResource : ValueOpcode::GetBufferResource; const auto* handle = inst->NumArgs() != 0 ? inst->Arg(0).ResolveInstruction() : nullptr; if (!IsRawRead(m_program, *inst) || handle == nullptr || handle->GetOpcode() != expected) { return finish(false); } } ... } else if (op != ValueOpcode::ReadConst && op != ValueOpcode::ReadConstBuffer && op != ValueOpcode::LoadAddressU32 && !IsRuntimeUniformOp(op)) { return finish(false); } ``` This conflates two different things: - **"the address is compile-time-uniform"** — true and useful for flattening genuine SRT/user-data descriptor fields, which are set up once per draw on the CPU side. - **"the memory contents at that address are a compile-time constant"** — categorically false for an arbitrary guest-memory read via `S_LOAD`. The address being constant says nothing about what the game has written there; it can be genuine, dynamic runtime data. ## Repro `shader_recompiler_compute_tests` has two cases that trigger this directly: - `ScalarMemoryLoadVariants` - `ScalarLoadSignedImmediateOffsetAddsSoffset` Both fail at readback with the loaded dwords coming back as zero instead of the buffer's actual contents. I disassembled the compiled SPIR-V for `ScalarLoadSignedImmediateOffsetAddsSoffset` (\`spirv-dis\`) to confirm: the value written to the output buffer is loaded directly from `flattened_srt` at compile time — there is no `OpAccessChain`/`OpLoad` against the actual input storage buffer anywhere in the module for that S_LOAD. ## Where I found this Found while merging `origin/main` into PR #558 (astrobot-raytracing) — see [that PR](https://git.gtahub.st/github/KytyPS5--KytyPS5/pulls/558) for context. Confirmed this is pre-existing in `main`'s own `SrtWalker`/`ResourceMaterialization` code and unrelated to anything changed in that PR (verified the affected files aren't touched by any commit on the branch). A related but separate FMASK crash in the same neighborhood (`ResourceMaterialization.cpp:1198`, `NativeImageKeyArg` indexing past `program.info.images` for the FMASK remap sentinel) was also found and already fixed on that branch; happy to open a small standalone PR for that fix against `main` if useful. Co-Authored-By: Claude Sonnet 5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:27:09 +00:00
Author
Owner

@⁠francoisatt on GitHub · 2026-09-13 17:34 UTC · original

Data point, and it disagrees with mine, so it's worth pinning down which of us is looking at what.

On clean main at d3d7bd3, Windows, clang-cl 19.1.5, Ninja, full clean build:
both ScalarMemoryLoadVariants and ScalarLoadSignedImmediateOffsetAddsSoffset return ok. The whole shader_recompiler_compute suite exits 0 with 396 cases passing. Both cases are defined and registered through AddCase in tests/ShaderRecompilerComputeTests.cpp at d3d7bd3, so they ran rather than being skipped.

The case count may be the key: you report 328, I see 396 on main. If the merge brought across a different version of the test file, the two cases could exist in both trees while asserting different things — which would explain a real failure on the branch and a pass on main without either of us being wrong about what we ran.

Could you check what your suite reports as its total, and which commit your ShaderRecompilerComputeTests.cpp comes from? Your spirv-dis finding looks solid on its own terms; I'm only questioning whether it reproduces on main as it stands today.

**@⁠francoisatt** on GitHub · 2026-09-13 17:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/606#issuecomment-5654911228) Data point, and it disagrees with mine, so it's worth pinning down which of us is looking at what. On clean main at d3d7bd3, Windows, clang-cl 19.1.5, Ninja, full clean build: both ScalarMemoryLoadVariants and ScalarLoadSignedImmediateOffsetAddsSoffset return ok. The whole shader_recompiler_compute suite exits 0 with 396 cases passing. Both cases are defined and registered through AddCase in tests/ShaderRecompilerComputeTests.cpp at d3d7bd3, so they ran rather than being skipped. The case count may be the key: you report 328, I see 396 on main. If the merge brought across a different version of the test file, the two cases could exist in both trees while asserting different things — which would explain a real failure on the branch and a pass on main without either of us being wrong about what we ran. Could you check what your suite reports as its total, and which commit your ShaderRecompilerComputeTests.cpp comes from? Your spirv-dis finding looks solid on its own terms; I'm only questioning whether it reproduces on main as it stands today.
Author
Owner

@⁠defektu on GitHub · 2026-09-13 18:02 UTC · original

I ran this over stale changes so closed this issue. Sorry for the inconvenience.

**@⁠defektu** on GitHub · 2026-09-13 18:02 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/606#issuecomment-5655060372) I ran this over stale changes so closed this issue. Sorry for the inconvenience.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#606
No description provided.