perf(shader): store buffer bytes and shorts with 8- and 16-bit views #4085
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4085"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T22:58:36Z.
What
buffer_store_byteandbuffer_store_shortinto descriptor-bound buffers become plain 8- and 16-bit stores instead of compare-and-swap loops on the containing dword. This also fixes a dropped byte in one short-store case.Background. The storage buffers are declared as
u32arrays (buffers), sowriteBuffermerged a byte or a short into its dword withAtomicUpdate, a compare-and-swap loop. When neighbouring lanes write bytes of the same dword, which is the usual pattern, the loop retries. The device already requiresstorageBuffer8BitAccess, andbuffers_u64already shows the pattern of an aliased typed view for 64-bit atomics.How.
AnalyzeProgramRequirementsnotes raw (not formatted, not BDA) byte and short buffer stores.u8viewbuffers_u8(StorageBuffer8BitAccess) and, for shorts, au16viewbuffers_u16(StorageBuffer16BitAccess) of the same descriptor array. They are aliased with the others and getCoherentlikebuffers_u64when the program needs it.VulkanDeviceenablesstorageBuffer16BitAccesswhere the device has it. The capability is also added to the graphics shader validation list.writeBuffer, a store goes to byte(byteOffset & ~3) + misalignment + address. That is where the word path puts it: dword(address >> 2) + (byteOffset >> 2), shifted by the V#'s misalignment and the address's low bits.u8store. A short is oneu16store at an even byte, and twou8stores at an odd one; the second is skipped if its dword is out of the bound range.Dropped byte. The word path shifted the short's 16-bit mask by
(address & 3) * 8first and added the V#'s misalignment afterwards. At an address that is 3 mod 4, the high byte was shifted past bit 31 and never written.u8stores write both bytes in both cases. Withmain's code, the new test givesbuffer_store_short (word updates, base +1): byte 5 is 0x000000a5, expected 0x00000060.Before / after on an RTX 4060 Laptop (Ada, driver 591.86):
buffer_store_bytebuffer_store_shortSASS per instruction is the
APS5_PIPELINE_STATSbinary-size difference over 16 stores; the rest is the V# decode, bounds check and address math. No other memory instruction in the sweep changes.Run time, 16,384 groups of 32 lanes, each lane storing to its own byte or short of consecutive dwords. Median of 15 interleaved dispatches in one process, with and without the views:
buffer_store_bytebuffer_store_shortWhy results don't change otherwise. The views alias the same buffer and the byte position is the word path's. A plain
u8/u16store writes exactly the bytes the masked compare-and-swap replaced, without touching the neighbours. AMD'sbuffer_store_byte/shortare plain stores too, not read-modify-writes of the dword. The bound check on the dword is unchanged; the extra check only guards the second byte of an odd short.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainat7c8df1f0with #4021 applied locally (maindoesn't compilelibkernelon Windows without it). RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build,GALLIUM_OVERRIDE_CPU_CAPS=avx).agc_driver_buffer_subword_store:StorageBuffer16BitAccessremoved (shorts as two bytes).buffer_store_byte (device target, base +1): byte 3 is 0x000000a5, expected 0x00000040.agc_driver_buffer_unaligned_base,agc_driver_buffer_load_store,agc_driver_d16_memory,agc_driver_formatted_storeandagc_driver_format_d16_storepass on all three devices.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 320 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out.agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way (expected preparation failure) on a build without this change.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping), which fail the same way withmain's recompiler.agc_driver_gated_dispatch_capturefailed during the suite and passed twice on its own.registered_workgroup_syncfailure and the intermittentagc_driver_depth_fast_clear_submitsegfault that also happens withmain's recompiler.Limits.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:29:16Z.
The dropped-byte claim checks out exactly, with a concrete number: at address 3 with a V# misalignment of 1, the old code shifts the 16-bit mask by the address-derived offset before adding the misalignment, so the high byte's bits end up shifted past bit 31 and are simply gone — not wrapped, not corrupted, just never written. The new path can't inherit that bug by construction: it computes the same byte position once and writes it directly (two single-byte stores, or one aligned 16-bit store), with no left-shift of the value that could push a byte out of range.
The capability gating is real and correctly structured both ways — the 8-bit and 16-bit SPIR-V views are only declared when the corresponding capability is present, and the device only advertises
storageBuffer16BitAccesswhen the physical device actually reports it, so there's no path where this emits for an unsupported target. The word path genuinely survives as a fallback for devices without 8-bit storage and for formatted stores that don't also need raw access.One thing worth adding to the PR's own race disclosure, since it's real but goes further than what's written: the body correctly flags that a byte store racing a 32-bit atomic on the same dword is now a plain-store-vs-atomic race that wasn't one before. What it doesn't mention is that the two individual stores of an odd-aligned short are also no longer atomic with each other, and neither is a short-store against a byte-store landing on one of its bytes — the old single compare-and-swap covered both bytes of a short at once and gave overlapping writers a defined winner; two independent plain stores don't. Worth a line in the PR acknowledging this is a second, distinct untested race, not just the one case named.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T02:31:58Z.
Thanks @Dextroid17, that's a second case, and I've added it to the limits. A short at an odd address is now two byte stores, so two racing stores that overlap (two shorts, or a short and a byte on one of its bytes) can leave bytes from both writers, where the compare-and-swap wrote both bytes at once. Like the atomic case, it isn't tested.
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-12T18:14:29Z.
Rechecked head
4bcdb818on Linux using a synthetic pixel-shader validation probe, againstmain3d8ffeb2.Base accepts all 10 cases. With this PR, all 8 byte/short-store cases fail
ValidateShaderswithAGC graphics: unsupported SPIR-V extension; both dword controls pass. The cases cover offsets 0 and 3 with requested BDA ABI versions 0 and 1. The relevant validator file is unchanged through currentmain2a5eca11.The generated shaders declare
SPV_KHR_8bit_storage, which the graphics validator refuses on this path. Compute tests do not exercise that validator. Could this PR include graphics validation coverage and acceptance of the emitted u8/u16 views?This checks shader generation and validation, not a real draw. Ubuntu 24.04, GCC 13.3, Release, SPIRV-Tools enabled.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:42:35Z.
Graphics shaders with a raw byte or short store no longer pass validation with this. The module now declares
SPV_KHR_8bit_storage, andShaderValidation.cpp:310accepts that extension only with the BDA ABI, so they fail withunsupported SPIR-V extension(simonbbby's probe above, 8 of 8 cases). Please accept the u8/u16 views there, add a graphics validation case, and rebase:SpirvRuntimeBuffer.cppandSpirvEmitterHelpers.cpphave changed on main since.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:50:01Z.
Review: CHANGES_REQUESTED
Agreeing with the points above; the blocker is confirmed on the current code:
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp:332acceptsSPV_KHR_8bit_storageonly whenshader.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version, and the diff only addsCapabilityStorageBuffer16BitAccessto the capability list (line 280). Any graphics shader with a rawbuffer_store_byte/buffer_store_shortnow declares the extension and fails withunsupported SPIR-V extension, which compute tests don't exercise. Accept the extension(s) the new views need (includingSPV_KHR_16bit_storageif the module declares it) when the device has the features, not only with the BDA ABI.SpirvRuntimeBuffer.cppandSpirvEmitterHelpers.cppchanged on main since. Rerun the new test after the rebase.I'll measure the store semantics on hardware once this is updated.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:55:07Z.
Thanks @simonbbby for the probe, and @oneandonlydean and @adriadam10 for confirming it. You're right: I only ran the byte and short views through compute, and
ValidateShadersdoesn't check compute. Next push:main, which now has #4250'sbuffers_u8view for formatted 8- and 16-bit stores in compute. This PR will reuse that view forbuffer_store_byteand the odd-address short, so there is one u8 declaration, and addbuffers_u16next to it.ShaderValidation.cppacceptsSPV_KHR_8bit_storage,SPV_KHR_16bit_storageandStorageBuffer16BitAccesswhen the device hasstorageBuffer8BitAccess/storageBuffer16BitAccess, not only with the BDA ABI. It also accepts u8 and u16 runtime arrays in aliased guest-buffer views, next to the u64 view from #4381.#4250 keeps the compare-exchange path for formatted stores outside compute. Once the validator accepts the views, those stores could use them in graphics as well, but I'd leave that to a separate PR.
I'll post here when it's up.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:39:09Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed