metal: detach replaced and dropped heap allocations from MTLResidencySet #32

Manually merged
github merged 2 commits from gh-pr/32/head into gh-pr/32/base 2026-11-26 14:43:14 +00:00
Owner

@⁠claytonblythe on GitHub · 2026-11-26 10:46 UTC · original
claytonblythe:fix/metal-residency-set-lifecycle → main · +76 −5 · 2 files · 2 commits · merged 2026-11-26 10:59 UTC

Summary

During a long headed Story Mode streaming run on macOS 26 (Darwin 25.3.0, Apple M4), the host hit an Apple Silicon IOGPU kernel panic:

panic: "Memory object unexpectedly not found in fPendingMemorySet" @IOGPUGroupMemory.cpp:219

IOGPUGroupMemory backs MTLResidencySet. Inspecting crates/victory-metal/src/gfx.rs shows two residency-set lifecycle bugs on Heap:

  1. Metal::heap_entry overwrote heap.refs[index] (dropping the old MTLResource) without ever calling set.removeAllocation + set.commit(), and called set.addAllocation redundantly on every write even when the same resource was already in the set.
  2. Heap had no Drop implementation, so Rust dropped buf and refs in field order before dropping set, leaving dangling allocations in the MTLResidencySet (which does not retain its allocations).

This fix:

  • Tracks per-resource reference counts (alloc_counts) on Heap so each allocation is added to heap.set once on 0 -> 1 and detached (removeAllocation + commit) when its last descriptor slot is overwritten or cleared, before the old Res is dropped.
  • Implements Drop for Heap to call set.removeAllAllocations() and set.commit() before buf and refs are dropped.
  • Adds heap_overwrite_detaches_replaced_residency_allocations in crates/victory-metal/tests/resources.rs exercising multi-slot sharing, overwrite, and drop in both MTLResidencySet and useResource modes.

Verification

  • cargo test -p victory-metal --target x86_64-apple-darwin --test resources heap_overwrite_detaches_replaced_residency_allocations — passed.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings — 0 warnings.
  • cargo xtask policy — 363 files ok.
**@⁠claytonblythe** on GitHub · 2026-11-26 10:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/32) `claytonblythe:fix/metal-residency-set-lifecycle` → `main` · +76 −5 · 2 files · 2 commits · merged 2026-11-26 10:59 UTC ## Summary During a long headed Story Mode streaming run on macOS 26 (Darwin 25.3.0, Apple M4), the host hit an Apple Silicon `IOGPU` kernel panic: ``` panic: "Memory object unexpectedly not found in fPendingMemorySet" @IOGPUGroupMemory.cpp:219 ``` `IOGPUGroupMemory` backs `MTLResidencySet`. Inspecting `crates/victory-metal/src/gfx.rs` shows two residency-set lifecycle bugs on `Heap`: 1. `Metal::heap_entry` overwrote `heap.refs[index]` (dropping the old `MTLResource`) without ever calling `set.removeAllocation` + `set.commit()`, and called `set.addAllocation` redundantly on every write even when the same resource was already in the set. 2. `Heap` had no `Drop` implementation, so Rust dropped `buf` and `refs` in field order *before* dropping `set`, leaving dangling allocations in the `MTLResidencySet` (which does not retain its allocations). This fix: - Tracks per-resource reference counts (`alloc_counts`) on `Heap` so each allocation is added to `heap.set` once on `0 -> 1` and detached (`removeAllocation` + `commit`) when its last descriptor slot is overwritten or cleared, before the old `Res` is dropped. - Implements `Drop for Heap` to call `set.removeAllAllocations()` and `set.commit()` before `buf` and `refs` are dropped. - Adds `heap_overwrite_detaches_replaced_residency_allocations` in `crates/victory-metal/tests/resources.rs` exercising multi-slot sharing, overwrite, and drop in both `MTLResidencySet` and `useResource` modes. ## Verification - `cargo test -p victory-metal --target x86_64-apple-darwin --test resources heap_overwrite_detaches_replaced_residency_allocations` — passed. - `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` — 0 warnings. - `cargo xtask policy` — 363 files ok.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:13 +00:00
github manually merged commit 40da4fbca4 into gh-pr/32/base 2026-11-26 14:43:14 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 10:56 UTC · original · approved these changes

Reviewed and tested merged with current main: victory-metal (incl. the new test), victory-gpu/hle/victory/xtask and Windows clippy all pass. Good find on the residency set not retaining allocations.

Follow-ups, not blocking:

  1. Every last-reference overwrite now calls set.commit() immediately. During heavy streaming that can mean many commits per frame. Consider batching the removals (mark dirty, commit once before the next submit) if it shows up in profiles.
  2. Unrelated, but you might like it: victory-metal's srv_and_uav_through_a_descriptor_table fails on main under x86_64 Rosetta (tests/common/mod.rs:48, write past region.len). It looks like the same >=16 KiB page-size sizing issue you fixed for the gpu tests in #23.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 10:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/32#pullrequestreview-6072889206) · **approved these changes** Reviewed and tested merged with current main: victory-metal (incl. the new test), victory-gpu/hle/victory/xtask and Windows clippy all pass. Good find on the residency set not retaining allocations. Follow-ups, not blocking: 1. Every last-reference overwrite now calls `set.commit()` immediately. During heavy streaming that can mean many commits per frame. Consider batching the removals (mark dirty, commit once before the next submit) if it shows up in profiles. 2. Unrelated, but you might like it: `victory-metal`'s `srv_and_uav_through_a_descriptor_table` fails on main under x86_64 Rosetta (`tests/common/mod.rs:48`, write past region.len). It looks like the same >=16 KiB page-size sizing issue you fixed for the gpu tests in #23.
This discussion has been locked. Commenting is limited to contributors.
No description provided.