ANYPS5_ENABLE_SPIRV_TOOLS: CCP turns a constant byte store into an 8-bit OpConstant that the module cannot declare #1276

Closed
opened 2026-10-07 21:22:02 +00:00 by arreina · 0 comments
arreina commented 2026-10-07 21:22:02 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by arreina on 2026-10-07T21:22:02Z.


What

With ANYPS5_ENABLE_SPIRV_TOOLS=ON, Recompile can reject a module that validated a moment earlier. A global store whose data is a compile-time constant ends with:

SPIR-V validation after optimization failed:
level=2 word=104: Cannot form constants of 8- or 16-bit types
  %uchar_0 = OpConstant %uchar 0

The module is valid before optimization, and APS5_SPIRV_OPT=none recompiles it. Running the passes of ValidateAndOptimizeSpirv one at a time, CCP is the first one after which an OpConstant of the 8-bit type exists. What it changes:

%874 = OpUConvert %uchar %873          ->  %874 = OpUConvert %uchar %uint_0
OpStore %879 %874 Volatile|Aligned 1   ->  OpStore %879 %uchar_0 Volatile|Aligned 1

These are the byte stores of the unaligned path of a guest memory write (get_bda_write_pointer, note_bda_write). The module declares StorageBuffer8BitAccess (BdaFeatures requires storageBuffer8BitAccess) but never Int8, and an 8-bit constant needs Int8. With the option on, the optimized module replaces the emitted one, so Recompile throws and the shader fails to recompile. Default builds do not run this step.

Found by fuzzing the recompiler with mutated synthetic shaders: 4 of 30000 cases, all with this message. No title was used.

Reproduction

Built with -DANYPS5_ENABLE_SPIRV_TOOLS=ON, registered like agc_pixel_position_export (it needs only shader_recompiler_agc_driver and libc), this recompiles a 14-word vertex shader: global_store_dword with an unwritten data register, then an export.

Int8ConstantFold.cpp
#include "BdaAbi.hpp"
#include "Recompiler.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <span>
#include <string_view>

namespace {

alignas(256) constexpr std::uint32_t Code[] = {
    0xbe801f00u, 0x800000ffu, 0x34u, 0xdc7519cbu, 0xb0020010u, 0x10005004u, 0xf4200100u,
    0xfa000000u, 0xbf8cc07fu, 0x7e0049c8u, 0xf80008cfu, 0u, 0xbf810000u, 0x3f800000u,
};

const std::uint32_t Capabilities[] = {11, 5347, 4448};

const std::string_view Extensions[] = {
    "SPV_EXT_descriptor_indexing", "SPV_KHR_8bit_storage", "SPV_KHR_float_controls", "SPV_KHR_physical_storage_buffer",
};

}

int main() {
    using namespace ShaderRecompiler;
    const std::span<const std::uint32_t> code(Code);
    const std::array<MemoryRegion, 1> memory{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}};
    const SpirvTarget target{0x00401000u, 0x00010300u, 64, BdaAbi::Version, Capabilities, Extensions, false, {1024, 1024, 64}, 1024, 32768, {}, {}};
    RecompileRequest request{{ShaderStage::Vertex, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}}, {}, target, {0, 0, 0, 128}};
    request.context.waveSize = 64;
    request.context.memory = memory;
    request.context.userDataBaseRegister = 8;
    request.context.vertex = ShaderVertexStageInfo{};
    request.useCache = false;
    try {
        static_cast<void>(Recompile(request));
        std::puts("recompiled");
        return 0;
    } catch (const std::exception& error) {
        std::printf("%s\n", error.what());
        return 1;
    }
}

It prints the message above. With APS5_SPIRV_OPT=none it prints recompiled.

What I checked on this input:

  • Without the store (its two words replaced by s_nop) it recompiles.
  • The alignment of the store's immediate offset does not matter: an odd and an aligned offset both fail.
  • The data register is never written, so the recompiler sees a constant zero. The fuzzer's four compute seeds, which store values derived from loaded data, compile. I could not build a variant with clearly run-time data in this setup (user data is known at recompile time), so I do not know whether such stores are affected, and I have not tried a real shader.

Directions

I am not sure which one fits the project:

  1. Declare Int8 when the device reports shaderInt8, and skip CCP when it does not.
  2. Leave CCP out of the pipeline.
  3. Emit the byte stores so they cannot fold into constants.

Which would you prefer? I can send a pull request for the one you choose.

AI-assisted: yes (Claude Code found and analysed this; the run is on my machine).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5752063710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1276). Originally posted by [arreina](https://github.com/arreina) on 2026-10-07T21:22:02Z. --- ### What With `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, `Recompile` can reject a module that validated a moment earlier. A global store whose data is a compile-time constant ends with: ``` SPIR-V validation after optimization failed: level=2 word=104: Cannot form constants of 8- or 16-bit types %uchar_0 = OpConstant %uchar 0 ``` The module is valid before optimization, and `APS5_SPIRV_OPT=none` recompiles it. Running the passes of `ValidateAndOptimizeSpirv` one at a time, `CCP` is the first one after which an `OpConstant` of the 8-bit type exists. What it changes: ``` %874 = OpUConvert %uchar %873 -> %874 = OpUConvert %uchar %uint_0 OpStore %879 %874 Volatile|Aligned 1 -> OpStore %879 %uchar_0 Volatile|Aligned 1 ``` These are the byte stores of the unaligned path of a guest memory write (`get_bda_write_pointer`, `note_bda_write`). The module declares `StorageBuffer8BitAccess` (`BdaFeatures` requires `storageBuffer8BitAccess`) but never `Int8`, and an 8-bit constant needs `Int8`. With the option on, the optimized module replaces the emitted one, so `Recompile` throws and the shader fails to recompile. Default builds do not run this step. Found by fuzzing the recompiler with mutated synthetic shaders: 4 of 30000 cases, all with this message. No title was used. ### Reproduction Built with `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`, registered like `agc_pixel_position_export` (it needs only `shader_recompiler_agc_driver` and `libc`), this recompiles a 14-word vertex shader: `global_store_dword` with an unwritten data register, then an export. <details><summary>Int8ConstantFold.cpp</summary> ```cpp #include "BdaAbi.hpp" #include "Recompiler.hpp" #include <array> #include <cstdint> #include <cstdio> #include <exception> #include <span> #include <string_view> namespace { alignas(256) constexpr std::uint32_t Code[] = { 0xbe801f00u, 0x800000ffu, 0x34u, 0xdc7519cbu, 0xb0020010u, 0x10005004u, 0xf4200100u, 0xfa000000u, 0xbf8cc07fu, 0x7e0049c8u, 0xf80008cfu, 0u, 0xbf810000u, 0x3f800000u, }; const std::uint32_t Capabilities[] = {11, 5347, 4448}; const std::string_view Extensions[] = { "SPV_EXT_descriptor_indexing", "SPV_KHR_8bit_storage", "SPV_KHR_float_controls", "SPV_KHR_physical_storage_buffer", }; } int main() { using namespace ShaderRecompiler; const std::span<const std::uint32_t> code(Code); const std::array<MemoryRegion, 1> memory{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}; const SpirvTarget target{0x00401000u, 0x00010300u, 64, BdaAbi::Version, Capabilities, Extensions, false, {1024, 1024, 64}, 1024, 32768, {}, {}}; RecompileRequest request{{ShaderStage::Vertex, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}}, {}, target, {0, 0, 0, 128}}; request.context.waveSize = 64; request.context.memory = memory; request.context.userDataBaseRegister = 8; request.context.vertex = ShaderVertexStageInfo{}; request.useCache = false; try { static_cast<void>(Recompile(request)); std::puts("recompiled"); return 0; } catch (const std::exception& error) { std::printf("%s\n", error.what()); return 1; } } ``` </details> It prints the message above. With `APS5_SPIRV_OPT=none` it prints `recompiled`. What I checked on this input: - Without the store (its two words replaced by `s_nop`) it recompiles. - The alignment of the store's immediate offset does not matter: an odd and an aligned offset both fail. - The data register is never written, so the recompiler sees a constant zero. The fuzzer's four compute seeds, which store values derived from loaded data, compile. I could not build a variant with clearly run-time data in this setup (user data is known at recompile time), so I do not know whether such stores are affected, and I have not tried a real shader. ### Directions I am not sure which one fits the project: 1. Declare `Int8` when the device reports `shaderInt8`, and skip `CCP` when it does not. 2. Leave `CCP` out of the pipeline. 3. Emit the byte stores so they cannot fold into constants. Which would you prefer? I can send a pull request for the one you choose. AI-assisted: yes (Claude Code found and analysed this; the run is on my machine).
Sign in to join this conversation.
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#1276
No description provided.