From bcd375c367dc0fadb5366c013cb73ec1318131ec Mon Sep 17 00:00:00 2001 From: xpatfussy69x <178718790+xpatfussy69x@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:43:23 +1030 Subject: [PATCH] Recompiler: the shader cache counts a dispatch chain when it goes in (#2454) ShaderCache::insert added the blob, cache dir, DXIL and samplers to its byte count, but evict_one subtracts ShaderEntry::bytes(), which also counts a split compute shader's dispatch_chain pieces. Evicting such an entry subtracted more than was added: a subtract-with-overflow panic in debug builds (poisoning the global shader_cache() mutex), and in release a count near usize::MAX, after which every insert evicts the whole cache. insert now builds the entry first and counts it with ShaderEntry::bytes(), so the two sides use one definition. Test: an entry with a 300- and a 200-byte piece. Without the fix insert counts 94 bytes where the entry holds 594; with it the count goes back to 0 on eviction. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/pso.rs | 41 +++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index da4b25e5fb..95d7554954 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -821,10 +821,14 @@ impl ShaderCache { return; } } - let bytes = blob.len() - + key.cache_dir.capacity() - + shader.dxil.capacity() - + shader.samplers.capacity() * std::mem::size_of::(); + // counted as `evict_one` subtracts it (a split compute shader's chain too, #2454) + let mut entry = ShaderEntry { + key, + blob: blob.to_vec(), + shader, + gen: 0, + }; + let bytes = entry.bytes(); if bytes > self.limit { return; } @@ -836,16 +840,9 @@ impl ShaderCache { self.next_id += 1; self.next_gen += 1; let (id, gen) = (self.next_id, self.next_gen); - self.by_key.entry(key.clone()).or_default().push(id); - self.entries.insert( - id, - ShaderEntry { - key, - blob: blob.to_vec(), - shader, - gen, - }, - ); + entry.gen = gen; + self.by_key.entry(entry.key.clone()).or_default().push(id); + self.entries.insert(id, entry); self.lru.push_back((id, gen)); self.bytes += bytes; } @@ -1880,6 +1877,22 @@ mod shader_cache_tests { assert!(cache.get(&key(0), b"a").is_none()); } + /// A split compute shader's dispatch chain counts in the cache's bytes when it goes in, as + /// when it is evicted (#2454: the count underflowed on its eviction) + #[test] + fn shader_cache_counts_a_dispatch_chain_on_insert_and_evict() { + let chained = Arc::new(Compiled { + dispatch_chain: Some(Arc::new(crate::gpu::dxil::kernel::DispatchChain { pieces: vec![vec![0; 300], vec![0; 200]], dwords_per_group: 4 })), + ..compiled(1) + }); + let mut cache = ShaderCache::new(1 << 20); + cache.insert(key(1), b"a", chained.clone()); + let entry = ShaderEntry { key: key(1), blob: b"a".to_vec(), shader: chained, gen: 0 }.bytes(); + assert_eq!(cache.bytes, entry); + assert!(cache.evict_one()); + assert_eq!(cache.bytes, 0); + } + #[test] fn shader_cache_coalesces_concurrent_same_key_compiles() { use std::sync::atomic::{AtomicUsize, Ordering};