Repository navigation
metal: cheap conversion-cache hits and per-draw lookups (-51% worker instructions on the live map) - #507
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configuration
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ac5fd0617e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (submit->count != 0) std::memcpy(host(submit->address), submit->words, std::size_t{submit->count} * 4); | ||
| driver.SubmitCommandBuffer(submit->address, submit->count, 0, submit->queue); |
There was a problem hiding this comment.
Apply page deltas before overwriting command-buffer pages
When two queued submissions use command buffers in the same 64 KiB page, this loop can copy the later submission's words before the first worker executes its replay hook. The first hook then sees a page matching neither its recorded previous nor hash, treats it as GPU-modified, and skips the entire recorded CPU delta, silently losing unrelated writes on that page and corrupting replay results. Restore each submission's command words at the same serialized boundary as its delta, rather than mutating guest memory ahead of worker execution.
Useful? React with 👍 / 👎.
| Range range{in.Get<std::uint64_t>(), in.Get<std::uint64_t>(), in.Get<std::uint32_t>() != 0, {}}; | ||
| range.chunks.assign((range.bytes + Capture::ChunkBytes - 1) / Capture::ChunkBytes, nullptr); | ||
| file.ranges.push_back(std::move(range)); |
There was a problem hiding this comment.
Coalesce remapped guest ranges before configuring replay
If the title unmaps and later reuses a guest address during the captured frame, SnapshotRange emits another Range because the host pointer changed, and the loader appends both historical mappings here. Replay subsequently passes both overlapping guest ranges to MetalDriver::Configure, where NativeGuestMemory::BorrowedRangesScope rejects them as overlapping, so captures containing address reuse cannot be replayed. Range records need lifetime-aware replacement/coalescing instead of accumulating every mapping into one simultaneous range set.
Useful? React with 👍 / 👎.
ac5fd06 to
1fa21a1
Compare
Profiled the live-map replay (sample, worker thread, waits excluded): inside MetalDraw::Draw, ConvertToMetal took 25% of CPU even on hits. Draws with per-draw push constants never repeated their exact key, so each built three keys (one over all SPIR-V words), probed three caches and inserted two entries. Returned() then deep-copied the MSL text. - ModuleConversionCache is keyed by SPIR-V identity plus the stable key and holds the shared converted shader; a hit restores the draw's own push constants (as a StableConversionCache hit already did). - Result::source is a SharedSource: copies share the immutable text. - CachedConvertedShaderFunction finds the MTLFunction by the shared text's identity instead of comparing the whole source on every pipeline. - ConvertToMetal records storesThroughBda, so draws stop searching the source. - MetalShaderResources::mirror and the compute-buffer lookup index the snapshot's mirrors instead of building an NSArray per call. Live map, 10 interleaved A/B processes: worker instructions 8673 M -> 4192 M (paired median -51%), worker CPU 836 -> 467 ms; pixel hash unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DgW4n3qYgi5jAAohEmXRQj
1fa21a1 to
5d4bbed
Compare
Depends on #505 (the AGC frame-replay tool; this branch is stacked on it).
What
Makes the Metal per-draw hit path cheap. Profiled first (
/usr/bin/sampleon the live-map replay,agc-queue-0thread, blocking waits excluded), then fixed the top items:Returned()deep-copied the MSL textModuleConversionCachekeyed by SPIR-V identity + stable key, holding the shared converted shader; hit =RestorePushConstants(what aStableConversionCachehit already did).Result::sourceis aSharedSource(copies share the text)mirror()and the compute-buffer lookup index the snapshot's mirrors instead of building anNSArrayper callCachedConvertedShaderFunction: by the identity of the shared text firstStoresThroughBdasearched the whole source twice per drawstoresThroughBda)After: ConvertToMetal is 1.2% of Draw CPU.
Texture lookup (the "25-30%" from the earlier profile): about 80% of its wall time is
textureKeys->BeforeCpuReaddraining the GPU timeline before reading DCC keys the GPU just wrote (precise already:MayHaveWritten), not CPU lookup. Its CPU part isGuestImageCache::Find(~6% of Draw CPU: re-hashing whole surfaces after every Expire, ~22 per frame; 870 MiB hashed per frame incl. inserts) and real uploads. Not changed here: a cheaper safe proof needs the guest write tracker (GuestMemory::UnchangedSince), which the Metal path does not use yet.Measurements (live map
captures/agc-live-map, 10 interleaved A/B pairs, separate processes,--repeat 2, 2 s pauses)--expect-hash 5e5670079f1ae58aPaired deltas: instructions median -51.3% (range -53.0..-49.5%), worker CPU -44.5%. The one baseline failure is the known 1 px GPU-order flake (pixel 508,770, hash 87c73baf34838f3f); a 14-repeat profiling run of this PR hit it once too.
Menu (
captures/agc-menu,--expect-png replay-ref.png --max-diff-pixels 8): pass on both builds (0-3 px), same flickering hash set.Tests:
anyps5_metal_shader_bridge_test(conversion-cache expectations updated: push-constant values now hit and must return the caller's push constants),anyps5_metal_conversion_disk_cache_test,metal_utility_tests: pass.Note: the shader disk-cache source version changes with the bridge sources, so the first launch after this change converts again.
Review findings (chatgpt-codex-connector, commit ac5fd06)
No findings on the draw-path commit (ac5fd06's own files).
🤖 Generated with Claude Code
https://claude.ai/code/session_01DgW4n3qYgi5jAAohEmXRQj