Repository navigation
fix(shader): scope descriptor length reuse to SPIR-V labels - #7747
Conversation
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
jotitadev
left a comment
There was a problem hiding this comment.
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 onto main 6b8228b2b (clean). ANYPS5_NO_SHADER_CACHE=1, -j1 --timeout 30, ^agc without display/flip/lds_atomics64 (763 tests).
| RTX 3070 | lavapipe | |
|---|---|---|
main 6b8228b2b |
762/763 (agc_driver_bda_device, #7584) |
763/763 |
| this PR | 734/763 | 746/763 |
agc_formatted_buffer_once passes. The new failures (largely the same set on both devices; lavapipe skips the mesh ones and adds agc_driver_oversized_dispatch) include 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 and the agc_driver_mesh* tests. On NVIDIA most are segfaults. On lavapipe they fail with vkCreateComputePipelines: Vulkan result -13 or abort.
The same head built with -DANYPS5_ENABLE_SPIRV_TOOLS=ON stops at the recompiler's validator in agc_driver_buffer_load_store and agc_driver_stream_copy on both devices:
SPIR-V validation before optimization failed:
ID '509[%509]' defined in block '506[%506]' does not dominate its use in block '609[%609]'
main passes both tests in that build. So a cached OpArrayLength gets reused from a SPIR-V block that doesn't dominate the later access. bufferLengths is cleared once per IR block in EmitControlFlow, but one IR block can turn into several SPIR-V blocks (the access paths branch), so the first OpArrayLength can sit in a branch that the second access doesn't pass through. The structural test only counts OpArrayLength, 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.
jotitadev
left a comment
There was a problem hiding this comment.
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 ^agc tests start failing on both MoltenVK drivers, and in every one of them SPIRV-Cross refuses the module with SPIR-V to MSL conversion error: Cannot resolve expression type, followed by vkCreateComputePipelines / vkCreateGraphicsPipelines: Vulkan result -3. That fits the OpArrayLength that 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 d1e172b, PR diff applied cleanly, compared with the same build without it.
Tested: the tree is main a373e85 + 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 plain main (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 (branch jotitadev:fix/agc-tests-vulkan-loader-macos). The comparison is that same build with and without this PR.
agc_formatted_buffer_oncepasses.- New failures, the same 15 on both drivers:
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. - No failure goes away with the PR.
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.
d1e172b to
4792d10
Compare
|
Thanks for the detailed NVIDIA, lavapipe, and MoltenVK runs. I confirmed the cache can reuse an |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks for the follow-up, but 4792d10 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.- To fix it, store
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). - The test only covers straight-line scalar reads (one label). Please add a case where the first access is inside a conditional (formatted/atomic buffer op in a branch) and a second one follows after the merge, and run it with
ANYPS5_ENABLE_SPIRV_TOOLS=ONso spirv-val checks the module. - Please state the speedup or say it is not expected: #7138 is about frame time, and the description only claims structure.
|
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? |
4792d10 to
87300c7
Compare
|
Implemented in two commits: With
|
|
I installed the Khronos validation layer and built the two runtime tests named in the review. I attempted both on lavapipe with |
|
Thanks for the careful review. The current head |
|
The current head The NVIDIA, lavapipe and MoltenVK failures in the earlier reports were against |
|
Current-head verification for 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? |
|
I reran the focused dominance regression on the exact PR head |
jotitadev
left a comment
There was a problem hiding this comment.
Re-ran this after the rework to label-scoped reuse (head 87300c7cb). The ^agc suite regression I reported on d1e172b9f (28 failures on NVIDIA, 17 on lavapipe, and the MoltenVK Cannot resolve expression type from the OpArrayLength that didn't dominate its use) is gone: the guarded suite is back to main on both devices.
Tested
Ubuntu 26.04, GCC 15.2.0, Release, BUILD_TESTING=ON, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, device selected with VK_ICD_FILENAMES, ctest -R "^agc" without display/flip/lds_atomics64, -j1 --timeout 30, on main a6dcea927 (this PR applies cleanly on it; head 87300c7cb). Build has ANYPS5_ENABLE_SPIRV_TOOLS=OFF, so agc_formatted_buffer_once runs its structural-count branch.
| NVIDIA RTX 3070 (595.99.02) | lavapipe (Mesa 26.0.3, LLVM 21.1.8) | |
|---|---|---|
main a6dcea927 |
858/861 | 860/861 |
this PR 87300c7cb |
858/861 | 860/861 |
Same counts on both: the only failures left are the ones main already has here (sample_order family on NVIDIA, multisample_retirement on lavapipe), none of them this PR's. No segfaults, no pipeline-creation failures. agc_formatted_buffer_once passes on both.
One note on the structural test: with the three SPIR-V backend files reverted to current main and the new test kept, agc_formatted_buffer_once still passes, so against today's main it 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.
|
Device run of Setup: Results (same 869 tests, same host, both arms):
One note on the regression test. Logs: AI-assisted: yes |
|
AMD Windows data with the validation layer: Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, AMD Radeon RX 9070 XT (
With only this PR's AI-assisted: yes (Claude Code) |
|
Checked the label-scoped length cache ( |
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)