Fix fence tracking for donated dynamic slice offsets - #4099
Merged
zcbenz merged 4 commits intoAug 11, 2026
Merged
Conversation
zcbenz
reviewed
Aug 10, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Adding a test relying on internal platform-dependent API is quite tricky, I think using your test in the original issue should be enough.
zcbenz
force-pushed
the
fix/3880-dynamic-offset-fence-tracking
branch
from
August 11, 2026 03:46
b448941 to
d0b6177
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Fixes #3880.
Metal's
compute_dynamic_offsetcan donate the dynamic-start input buffer toits one-element offset output. The donated alias is currently registered as
an encoder temporary. During
CommandEncoder::end_encoding(), temporarycleanup removes that shared pointer from the input and output dependency sets
before producer fences are resolved, so a dynamic slice can use a stale start
after an encoder boundary.
Register the offset as a temporary only when its storage was newly allocated.
The donated path remains enabled, and the primitive input retains the shared
storage through command-buffer completion. This matches the CPU path and the
temporary-lifetime pattern fixed for vector SDPA in #3121.
Per review, the committed regression is now the public
slice_updatereproducer from #3880 (no internal APIs).
Testing, on an M3 Pro (36 GB, macOS 26.2), base
8c28c38:
mainit fails 10/10 fresh processeswith default buffer limits (the 512 MB reduction crosses the per-buffer
memory limit, so the command-buffer boundary lands between the start's
producer and the slice) and 10/10 with
MLX_MAX_OPS_PER_BUFFER=0; withthe fix, 0/20 in both modes.
slicereproduction from [BUG] Metal slice_update can use a stale array-valued start across command buffers #3880, fresh processes withMLX_MAX_OPS_PER_BUFFER=0: pristine 20/20 wrong; fixed 0/50 wrong.METAL_DEVICE_WRAPPER_TYPE=1METAL_DEBUG_ERROR_MODE=0: pristine 20/20 wrong; fixed 0/20 wrong.Metal validation wrapper.
pre-commit run --all-files: passing.Checklist
CONTRIBUTING
document
pre-commit run --all-filesto format my code / installedpre-commit prior to committing changes
works
change)