fix(shader): scope descriptor length reuse to SPIR-V labels #7747
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7747"
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 MarcZX10 on 2026-10-19T05:55:01Z.
What
Reuses a storage-buffer runtime length query only when the cached definition and the next access are in the same SPIR-V label. This prevents an OpArrayLength emitted in a conditional path from being reused after its merge, where it may not dominate the access. Reads across generated labels emit separate queries. No runtime speedup is expected or claimed; this change constrains the structural reuse reported in #7138 to cases where the SPIR-V definition is valid.
Tested
Linux x86_64;
agc_formatted_buffer_oncepasses withANYPS5_ENABLE_SPIRV_TOOLS=OFF(structural counts) andON(each generated shader is validated before optimization). With the original cache, the new atomic-then-read case fails SPIRV-Tools validation because an ID defined in block 274 does not dominate its use in block 405; the label-scoped cache passes. I also built and attemptedagc_driver_buffer_load_storeandagc_driver_stream_copywith lavapipe and Vulkan validation enabled; both abort before Vulkan initialization because this host's Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2 and FMA. No game or homebrew run. GPUs tested: n/a (compile and SPIR-V validation only).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T08:12:35Z.
Review: COMMENTED
Device run of
d1e172b9f, since the description has no GPU run. With this change the recompiler emits invalid SPIR-V for a set of buffer shaders, and 28 tests fail on NVIDIA (mostly segfaults) and 17 on lavapipe.Machine: Ubuntu 26.04, i7-8700, GCC 15.2, Release,
BUILD_TESTING=ON, every execution test built. NVIDIA GeForce RTX 3070 (driver 595.99.02) and lavapipe (Mesa 26.0.3, LLVM 21.1.8) on the same host. PR merged ontomain6b8228b2b(clean).ANYPS5_NO_SHADER_CACHE=1,-j1 --timeout 30,^agcwithout display/flip/lds_atomics64(763 tests).main6b8228b2bagc_driver_bda_device, #7584)agc_formatted_buffer_oncepasses. The new failures (largely the same set on both devices; lavapipe skips the mesh ones and addsagc_driver_oversized_dispatch) includeagc_driver_buffer_load_store,agc_driver_buffer_format,agc_driver_stream_copy,agc_driver_dword_pattern_fill,agc_driver_constant_dword_fill,agc_driver_memory_value_fill,agc_driver_vertex_fetch,agc_driver_image_table,agc_driver_address_passesand theagc_driver_mesh*tests. On NVIDIA most are segfaults. On lavapipe they fail withvkCreateComputePipelines: Vulkan result -13or abort.The same head built with
-DANYPS5_ENABLE_SPIRV_TOOLS=ONstops at the recompiler's validator inagc_driver_buffer_load_storeandagc_driver_stream_copyon both devices:mainpasses both tests in that build. So a cachedOpArrayLengthgets reused from a SPIR-V block that doesn't dominate the later access.bufferLengthsis cleared once per IR block inEmitControlFlow, but one IR block can turn into several SPIR-V blocks (the access paths branch), so the firstOpArrayLengthcan sit in a branch that the second access doesn't pass through. The structural test only countsOpArrayLength, so it can't see this. Running the module through the validator in the test, or one of the failing execution tests above, would catch it.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T09:00:42Z.
Review: COMMENTED
Follow-up to my NVIDIA/lavapipe run, with the Apple GPU / MoltenVK result for the same head. It breaks the same way on Metal: 15
^agctests start failing on both MoltenVK drivers, and in every one of them SPIRV-Cross refuses the module withSPIR-V to MSL conversion error: Cannot resolve expression type, followed byvkCreateComputePipelines/vkCreateGraphicsPipelines: Vulkan result -3. That fits theOpArrayLengththat doesn't dominate its use: SPIRV-Cross can't resolve an ID used outside the block that defines it.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704. Head d1e172b9f, PR diff applied cleanly, compared with the same build without it.
Tested: the tree is
maina373e85e5+ mugurc's macOS series (ci/macos-series-2) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plainmain(it doesn't build on macOS yet). Under this PR the build also had #7371, #7535, #7684, #7746, #7759 and a test-only patch that lets nine AGC fixtures load the Vulkan loader on macOS (branchjotitadev:fix/agc-tests-vulkan-loader-macos). The comparison is that same build with and without this PR.agc_formatted_buffer_oncepasses.agc_driver_address_passes,agc_driver_address_passes_targets_split,agc_driver_buffer_format,agc_driver_buffer_load_store,agc_driver_buffer_resource_vcc,agc_driver_constant_dword_fill,agc_driver_depth_fast_clear_submit,agc_driver_dword_pattern_fill,agc_driver_format_d16,agc_driver_format_d16_store,agc_driver_memory_value_fill,agc_driver_pattern_buffer_fill,agc_driver_stream_copy,agc_driver_vertex_fetch,agc_driver_zero_position_vertex. The mesh tests that fail on NVIDIA don't run on Apple (no mesh shaders), andagc_driver_image_tablealready fails on MoltenVK for lack of descriptor indexing features.GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T16:09:11Z.
Thanks for the detailed NVIDIA, lavapipe, and MoltenVK runs. I confirmed the cache can reuse an
OpArrayLengthafter control flow has changed blocks. The fix on current head4792d104f6scopes each cache lookup to its defining SPIR-V label while retaining per-IR-block lifetime. The existingagc_formatted_buffer_once_testsbuilds and passes on Linux (the CTest project has no registered tests, so I ran the executable directly). This environment hasANYPS5_ENABLE_SPIRV_TOOLS=OFF, so I could not runspirv-valhere; hosted checks are queued.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:23:49Z.
Review: CHANGES_REQUESTED
Thanks for the follow-up, but 4792d104f does not fix the problem jotitadev reported.
SpirvRuntimeBuffer.cpp:149: the addedentry.length != 0uis always true (AllocateId()never returns 0), so the lookup is unchanged. The entry still has no SPIR-V label, and theif (access.length == 0u)fallback at:157is dead code.directAccessis called from inside conditional regions (e.g.:826, withinEmitValueOrDefaultIfCondition) as well as outside them (:761), and one IR block spans several SPIR-V labels. AnOpArrayLengthemitted inside such a region is then reused after the merge, where it does not dominate its use. That is the invalid SPIR-V (Cannot resolve expression typein SPIRV-Cross, crashes on NVIDIA) from the earlier reports.state.currentLabelin the entry and match on it, or drop entries whenever a label is emitted (a reuse is only valid if the defining label dominates the current one; same label is the simple safe case).ANYPS5_ENABLE_SPIRV_TOOLS=ONso spirv-val checks the module.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T18:01:38Z.
I tested the suggested strict state.currentLabel cache key locally. It avoids reusing a value across labels, but it makes the existing three-read scalar-buffer regression emit three OpArrayLength instructions instead of one, so the PR’s current reuse behavior disappears on this path. I reverted the experiment and have not pushed it. Would you prefer a correctness-only label-scoped change with the test and PR claim updated, or should I investigate a dominance-aware cache that preserves reuse when the definition dominates the later access?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T18:20:22Z.
Implemented in two commits:
43ca2784adds the regression and87300c7scopes cached lengths to their defining SPIR-V label.With
ANYPS5_ENABLE_SPIRV_TOOLS=ON, the atomic-then-read fixture fails on the original cache with the same dominance validation error reported above (an ID defined in block 274 does not dominate block 405), and passes with the fix. The focused CTest passes with SPIRV-Tools both ON and OFF. With Tools OFF, the structural assertions confirm that queries are not reused across generated labels. The PR title and description now state that no runtime speedup is expected.python3 tools/check_conventions.py --base upstream/mainpasses. I did not run a game or GPU test.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T18:25:15Z.
I installed the Khronos validation layer and built the two runtime tests named in the review. I attempted both on lavapipe with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. Both abort before Vulkan initialization: this host's Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2 and FMA, which the test harness requires to run the guest instructions. No Vulkan VUID comparison is possible here. I added this limitation to the PR description; the dedicated SPIRV-Tools regression still passes with validation enabled.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T20:24:05Z.
Thanks for the careful review. The current head
87300c7cbnow storesstate.currentLabelin each buffer-length cache entry and only reuses anOpArrayLengthwhen the defining label matches the current label. This is the same-label safe case you suggested; it intentionally gives up reuse across generated labels while retaining reuse within one label. The regression added in43ca2784bemits a buffer query in the 64-bit atomic path and reads the buffer after its merge. I reranagc_formatted_buffer_oncelocally withANYPS5_ENABLE_SPIRV_TOOLS=ON; CTest passes (1/1), so the module is validated by SPIRV-Tools. The executable also reports the structural checks passing, andpython3 tools/check_conventions.py --base upstream/mainpasses. I have not rerun the full GPU suite on this host.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T07:56:58Z.
The current head
87300c7cb259bfa69d36ad768ee5394b55e776daaddresses the label/dominance concern: the cache key now includesstate.currentLabel, so anOpArrayLengthis reused only within the label where it was defined. The addedFormattedBufferOnceregression compiles a 64-bit buffer atomic followed by a buffer read, asserts the post-merge read emits a new array-length query, and passes the focused CTest withANYPS5_ENABLE_SPIRV_TOOLS=ON; the validator checks the generated module for dominance errors. The correctness-only behavior and no-speedup claim are stated in the PR description.The NVIDIA, lavapipe and MoltenVK failures in the earlier reports were against
d1e172b9for4792d104f, before this fix. My Xeon host cannot run the AGC execution tests past its CPU feature guard, so I cannot provide the requested current-head driver matrix. Could you rerun the affected buffer tests against87300c7cband currentmainand reassess the change request?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T09:03:42Z.
Current-head verification for
87300c7cb259bfa69d36ad768ee5394b55e776da:ANYPS5_ENABLE_SPIRV_TOOLS=ONis set in the build configuration,ninja -C build-scalar agc_formatted_buffer_once_testsreports no work to do, andctest --test-dir build-scalar -R '^agc_formatted_buffer_once$' --output-on-failurepasses 1/1 (2.66 s). This is the focused dominance regression and SPIRV-Tools validates the generated module. The unrelated runtime Vulkan tests remain unrun here because the host CPU guard stops before Vulkan initialization.The current PR description now states that reuse is label-scoped and no runtime speedup is expected. Could you re-review this exact head and update the review state if the requested correctness and test changes address your findings?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T10:31:05Z.
I reran the focused dominance regression on the exact PR head
87300c7cb259bfa69d36ad768ee5394b55e776da. The existing Release build hasANYPS5_ENABLE_SPIRV_TOOLS=ON;ninja -C build-scalar agc_formatted_buffer_once_testsreports no work, andctest --test-dir build-scalar -R "^agc_formatted_buffer_once$" --output-on-failurepasses 1/1 (2.64 s). This validates the generated SPIR-V in the atomic-then-read case; it is not a Vulkan driver-suite run. The earlier GPU failures were reported on pre-fix heads. @adriadam10, could you reassess the label-dominance finding against this exact-head regression?Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T01:30:48Z.
Review: COMMENTED
Re-ran this after the rework to
label-scoped reuse (head87300c7cb). The^agcsuite regression I reported ond1e172b9f(28 failures on NVIDIA, 17 on lavapipe, and the MoltenVKCannot resolve expression typefrom theOpArrayLengththat didn't dominate its use) is gone: the guarded suite is back tomainon both devices.Tested
Ubuntu 26.04, GCC 15.2.0, Release,
BUILD_TESTING=ON,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, device selected withVK_ICD_FILENAMES,ctest -R "^agc"without display/flip/lds_atomics64,-j1 --timeout 30, onmaina6dcea927(this PR applies cleanly on it; head87300c7cb). Build hasANYPS5_ENABLE_SPIRV_TOOLS=OFF, soagc_formatted_buffer_onceruns its structural-count branch.maina6dcea92787300c7cbSame counts on both: the only failures left are the ones
mainalready has here (sample_orderfamily on NVIDIA,multisample_retirementon lavapipe), none of them this PR's. No segfaults, no pipeline-creation failures.agc_formatted_buffer_oncepasses on both.One note on the structural test: with the three SPIR-V backend files reverted to current
mainand the new test kept,agc_formatted_buffer_oncestill passes, so against today'smainit does not reproduce the earlier counting difference the way it did against the older base — the headline for me here is the suite being clean again rather than a red→green of that test.GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02, Linux) and lavapipe (Mesa 26.0.3, LLVM 21.1.8), Ubuntu 26.04.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-22T02:14:02Z.
Device run of
87300c7cbon lavapipe, since the rerun against87300c7cband currentmainthat @MarcZX10 asked for on 10-20 had not happened yet (the 10-19 device failures were all againstd1e172b9f).Setup:
mainata6dcea927+ this PR (merge6bb3e5c6e); CI's Linux configure line (-DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON, gcc 16.2.1, no ccache); i9-11900K, llvmpipe from Mesa 26.2.4 (LLVM 23.1.1, 256 bits) viaVK_ICD_FILENAMES; scrubbed env (env -i) withANYPS5_NO_SHADER_CACHE=1; @jotitadev's 10-19 filter:ctest -R '^agc' -E 'display|flip|lds_atomics64' -j1 --timeout 30→ 869 tests.Results (same 869 tests, same host, both arms):
SpirvBackendfiles reset toa6dcea927, PR test and CMake kept, rebuilt): 809 passed, 3 failed, 57 skipped. Identical skip set.agc_driver_buffer_load_store,agc_driver_buffer_format,agc_driver_stream_copy,agc_driver_dword_pattern_fill,agc_driver_constant_dword_fill,agc_driver_memory_value_fill,agc_driver_vertex_fetch,agc_driver_image_table,agc_driver_address_passes,agc_driver_oversized_dispatch, and the MoltenVK set (agc_driver_address_passes_targets_split,agc_driver_buffer_resource_vcc,agc_driver_depth_fast_clear_submit,agc_driver_format_d16,agc_driver_format_d16_store,agc_driver_pattern_buffer_fill,agc_driver_zero_position_vertex). Theagc_driver_mesh*tests are skipped on lavapipe here, as in jotitadev's run.agc_driver_multisample_retirement("MSAA storage promotion did not retain its source image") andagc_compute_pipeline_replay("replay did not export a pipeline cache header"). Both fail with the same message on both arms, so they are not from this PR. I have not checked them on another driver or host.agc_driver_sgpr_pattern_fill_keys("the dispatch completed 17 times without a pending write over the keys"), is a timing flake on this host, not an arm difference: rerun alone 10× per arm it passed 5/10 on the PR build and 6/10 on the control build. Its code path does not touch the buffer-length emitter.does not dominateor any other spirv-val message appears in either ctest log (SPIRV-Tools validation was on for every generated shader).One note on the regression test.
agc_formatted_buffer_oncepasses with the three source files ata6dcea927too (3/3 withANYPS5_ENABLE_SPIRV_TOOLS=ONand 3/3 with itOFF), becausemainata6dcea927has nobufferLengthscache at all: each access emits its ownOpArrayLength(core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp:146onmain). The "fails on the original cache" result in the description is against this PR's earlier revision, not againstmain, so the new atomic-then-read case guards the label-scoped cache this PR adds rather than catching a bugmainhas today. That matches what the PR now claims (correctness-only, label-scoped reuse); noting it so the test is not read as amainrepro.Logs:
ctest-head.log,ctest-main.log(869 tests each) and the 20sgprrerun logs attached on request.AI-assisted: yes