Skip to content

st/mesa: harden the shader cache paths of the locked link - #366

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
djdev/glsl-link-lock-followups
Nov 21, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
djdev/glsl-link-lock-followups

Conversation

@DjDeveloperr

Copy link
Copy Markdown

Follow-up to #356 (per-shader link locks). These are two hardening items for the shader cache paths; behaviour does not change in any case that occurs today.

Change

  • Copied cache keys carry their count. The metadata write used to size the key copy (taken while the attached shaders were locked) from the program's current shader count. It now gets the number of copied keys with them, so it can't read past the copy.
  • Cache-hit invariant. A link that hits the disk cache releases the shader locks before it rebuilds the program from the cache. That relies on the rebuild always succeeding, which is true today because st_load_nir_from_disk_cache() cannot fail once the program metadata was restored.
    • Debug builds now assert this.
    • If it ever fails, the link takes the shader locks again, starts over with fresh program data, compiles the shaders the cache hit skipped (as on a cache miss) and links from source. Before, it would have run the link front end without the locks.
  • shader_cache's recompile of skipped shaders is exported as shader_cache_compile_shaders() for that fallback.

Testing

  • meson test --suite asahi: 137 ok, 0 fail.
  • meson test --suite glsl --suite glcpp --suite mesa --suite st_mesa: 185 ok, 0 fail.
  • Hardware: the same re-gate as mesa: don't let a compile free shader state another context is linking #356 (the multithread link race case alone, sharing.gles2.multithread.*, a multi-process programs.link stress, the GLES2/3/31 shader query/linkage/negative groups and KHR-GL46.gl_spirv), queued.

🤖 Generated with Claude Code

@DjDeveloperr

Copy link
Copy Markdown
Author

Merge train (batch 1): independent review

Verdict: CLEAN WITH NITS. Independent code-only review (Opus 5.5, xhigh), no blocker or major.

  • MINOR (st_glsl_to_nir.cpp:444-465, st_relink_uncached): the fallback relink repeats only part of st_link_shader's front end. It recompiles every attached shader after an unlocked window without the CompileStatus/SPIR-V-mix checks, so a concurrent glShaderBinary(SPIR-V) on an attached shader would reach _mesa_glsl_compile_shader with a NULL source. Unreachable today (the fallback only runs if st_load_nir_from_disk_cache fails after metadata was restored), and debug builds assert first. A "retry" status that reruns st_link_shader's own front end with the cache bypassed would avoid a second copy of it.
  • NIT: if the fallback ever fires, the bad cache entry is not removed (blake3 is zero after the reset), so every later link of the same sources pays a full relink.
  • NIT: the key-count hardening reads prog->NumShaders twice (st_glsl_to_nir.cpp:886-888); read it once.
  • NIT: the quoted meson suites never reach the cache-hit path.

Reach: GL common code only (st/mesa + GLSL), every chip's GL; no Vulkan change. Default behaviour unchanged.

Train plan: batch 1 = #366, #564, #565, #446 on aurora-wip 4596393. Gates: host build + meson test --suite asahi, M5 Pro (G17S) dEQP-VK gate + surfaceless GL smoke + gpubench/Geekbench A/B against the pre-batch tip, and Neo (G17P) / M4 (G16G) rows. These nits can go in a follow-up after the merge.

DjDeveloperr added a commit that referenced this pull request Nov 20, 2026
DjDeveloperr added a commit that referenced this pull request Nov 20, 2026
DjDeveloperr added a commit that referenced this pull request Nov 20, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merge-train status (02:4x): held for its Neo (G17P) row. The independent review is clean with nits, posted above. This PR reaches the A18 Pro (G17P), so per the train's rule it merges only after both of these:

The Neo has been offline since about 02:04 (it idle-suspended and needs a physical wake), so its row can't run until it is back. Everything else for this PR will be ready by then. G17P owners' rows (A18 Pro / M4 Pro / M4 Max) on the head above would unblock it sooner.

@DjDeveloperr

Copy link
Copy Markdown
Author

Merge train: rebased onto aurora-wip afcc08b (conflict-free; authorship kept). Head b4e2c39 -> 81be996.

  • Why: so this exact head carries the current .github/workflows/asahi.yml and the policy-file copies that ci: sync the assistant-file copies with .github/aurora/AGENTS.md #580 synced, so its CI run and agent-policy.sh pr both apply to it. On the old base agent-policy.sh pr failed on the stale copies.
  • What changed: nothing in the PR's own commits (git range-diff: every commit identical). The train's batch stack built from the rebased heads has the same tree as the one already reviewed and host-tested.
  • CI: an asahi workflow run on this exact head is in progress on the DjDeveloperr/mesa mirror (branch ci/train-pr366), because aurora-silicon/mesa has Actions off. agent-policy.sh pr passes locally (rc=0).

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers. I read the change on 81be996 together with the lock helpers in shaderobj.c and the caller in shaderapi.c. The key-count fix is correct: the key copy and its count are now taken together while the shaders are locked, so the metadata write can no longer read past the copy. In the fallback, the lock handling holds up. _mesa_unlock_attached_shaders() is safe to call twice, and _mesa_link_program always unlocks after st_link_shader(). So a failed relock (count 0) or a relink that fails while holding the locks leaves nothing locked. link_shaders_init() sets LinkStatus back to success before the compile checks, so st_relink_uncached() does return the real status. The earlier independent review's minor point and nits still apply to this head unchanged. I have two things to add:

  1. src/mesa/state_tracker/st_glsl_to_nir.cpp:444-465: the SPIR-V mixing check from that review costs only one line here. After the relock, mark the link failed if any attached shader has spirv_data set, as st_link_shader() does: linker_error(prog, "not all attached shaders have the same SPIR_V_BINARY_ARB state"). That also keeps _mesa_glsl_compile_shader() away from a shader with no GLSL source. It closes the one race the unlocked window opens, without restructuring the front end.

  2. Evidence: no hardware run of this head is listed yet (the re-gate is "queued"). The meson suites never reach the cache-hit path. The assert at :488 also means debug builds stop before the fallback, so only the release-build cache-hit unlock is tested. To exercise the changed path, run the re-gate twice on one fresh MESA_SHADER_CACHE_DIR (cold, then warm) with a release build. In the warm run every link takes the LINKING_SKIPPED branch, which this PR rewrote.

Hardware row: M1 Pro (G13S) or M2 (G14G) GL, release build, base afcc08b against head. Run dEQP-GLES2.functional.shaders.*, dEQP-GLES3.functional.shaders.* and KHR-GL46.gl_spirv.* twice each with MESA_SHADER_CACHE_DIR pointing at a new empty directory and MESA_GLSL_CACHE_DISABLE unset, then dEQP-EGL.functional.sharing.gles2.multithread.* 50 times on the warm cache. Pass: identical results to base on both runs, the second run reports cache hits (MESA_GLSL=cache_info), and there are no hangs or crashes.

Assisted-by: LLM (Claude:Opus 5.5)

Two follow-ups to the per-shader link locks:

- The cache metadata write took its key copy, made while the attached
  shaders were locked, but sized it from the program's current shader
  count. Pass the number of copied keys along with them.

- A link that hits the disk cache releases the shader locks before it
  rebuilds the program from the cache, which relies on that rebuild
  always succeeding. Assert that, and if it ever fails, lock the
  shaders again and link from source as on a cache miss instead of
  running the link front end without the locks.

shader_cache's recompile of the shaders a cache hit skipped is now
exported as shader_cache_compile_shaders() for that fallback.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/glsl-link-lock-followups branch from 81be996 to 1dbbb94 Compare November 21, 2026 02:49
DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merge train: rebased onto aurora-wip 7ba2957 (#595, the CI fix that installs the GLVND EGL dispatcher, so the asahi job's EGL priority tests pass), conflict-free, every commit identical, authorship kept. New head 1dbbb94. A mirror asahi run on this exact head is in progress (ci/train-pr366 on DjDeveloperr/mesa), and agent-policy.sh pr is clean.

DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

T8140 (MacBook Neo J700, A18 Pro, G17P B1) merge-train batch 2 row (#366, #565, #446): CLEAN.

Setup:

  • Tested commit pair: 0c681ea13cd7 (tree 5ac8c22d9a80, identical to aurora-wip b8762aab9bdf) → 4a78ed4588e4 (tree f635329f6ef9, identical to restacked merge-train/b2 f30dcc5a9ff8 merging #366 1dbbb94cc312, #565 d8e392f5296d, #446 d56b317ac29b).
  • Both arms ran on one boot (25cbd202), kernel 7.3.0-41.fc44.aurora.aarch64, private prefixes (Borealis + Gallium/EGL pinned), VK-GL-CTS 1.4.7.1, Fedora 44 aarch64 package Meson options.

Results:

  • vulkaninfo (UUIDs, live memory budget/usage, and git id masked): identical apart from the expected shaderBinaryVersion 43 → 44 (#446).
  • CTS (18 parts, 113,500 unique cases across vk-default 1/100, full vk-060-mesh-shader, full dEQP-VK.memory.*, GLES2/3/31, KHR-GLES2/3/31/32, KHR-GL46, and EGL): 92,546 Pass / 20,932 NotSupported / 19 QW / 3 CW / 0 Fail / 0 Crash on both arms; 0 per-case differences.
  • gpubench (2 ABBA rounds, 32 rows; desktop GL 4.3 --verify ok on all 4 arms): 0 of 32 rows flagged (|W − 1| median 0.6 %).
  • Kernel GPU errors: 0 across CTS and perf windows.

Assisted-by: LLM (Google:Gemini 4 Neon)

@DjDeveloperr
DjDeveloperr merged commit 372ba30 into aurora-wip Nov 21, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged into aurora-wip (#366 372ba3050950, #565 7a151cbd3e46, #446 0815451be910; resulting aurora-wip head 0815451be910638dab29f4e7ea400b5b8a086873 has exact verified tree f635329f6ef9).

M5 Pro (J714s, G17S B1) verification row

  • Head vs base: combined tree f635329f6ef9 (#366 1dbbb94cc312 + #565 d8e392f5296d + #446 d56b317ac29b) vs aurora-wip b8762aab9bdf, native release builds of Borealis (libvulkan_asahi.so) and gallium asahi, same boot on kernel 7.3.0-m5-sship7 (default command line, ASAHI_M5_BRINGUP=1).
  • dEQP-VK 1.4.7.1 (322,238 cases total, 0 regressions, 0 flakes):
    • synchronization2.*, synchronization.timeline_semaphore.*, synchronization.basic.*, glsl.*, clipping.*, rasterization.*, memory.*, api.command_buffers.*, binding_model.mutable_descriptor.* (149,057 cases): 94,944 Pass / 54,016 Skip / 97 Fail; all 97 head failures reproduced on base in exact 200-test chunk-context replay (89 chunks / 17,800 results, 0 base Flake, 0 regressions).
    • draw.*, query_pool.*, sparse_resources.*, synchronization.signal_order.*, memory.binding.*, robustness.robustness2.*, ubo.*, robustness.buffer_access.* (173,181 cases): 114,180 Pass / 59,001 Skip / 0 Fail.
  • GL surfaceless smoke (index-coverage): 14/14 Pass on both head and base.
  • CI: fork mirror asahi run 44858062813 succeeded on exact head 1dbbb94cc312cc4b478e8ff5acfde55cd6e5aa02; agent-policy.sh pr passed.
  • Health: GPU liveness (vulkaninfo + compute dispatch) OK before and after; 0 kernel GPU faults/errors.

Assisted-by: LLM (Google:Gemini 4 Neon)

@RyanTheTide
RyanTheTide deleted the djdev/glsl-link-lock-followups branch November 27, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants