Repository navigation
Fix asMTLBuffer crash on zero-size arrays - #522
Merged
davidkoski merged 1 commit intoNov 17, 2026
Merged
Conversation
An array with a logical size of 0 has no backing data pointer, so asMTLBuffer(device:noCopy:) crashed by force-unwrapping nil on the noCopy path. The copy path force-unwrapped the buffer base address the same way. Metal cannot create a zero-length buffer in any case. asMTLBuffer now returns nil for empty arrays, which its optional return type already allows, and the copy path skips a nil base address. This follows the asArray / asData fix in ml-explore#486. Adds a test for the empty-constructed, empty-slice, and zero-dimension cases on both the copy and noCopy paths. Fixes ml-explore#520
davidkoski
approved these changes
Nov 9, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Looks good, thank you! Justing waiting on CI.
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 #520.
An array with a logical size of 0 has no backing data pointer, so
asMTLBuffer(device:noCopy:)crashed by force-unwrapping nil on the noCopy path (MLXArray+Metal.swift:27). The copy path force-unwrapped the buffer's base address the same way. Metal cannot create a zero-length buffer in any case.asMTLBuffernow returnsnilfor zero-size arrays, which its optional return type already allows, and the copy path skips a nil base address. The doc comment says so. This follows theasArray/asDatafix in #486.The new test
asMTLBufferEmpty()covers an empty-constructed array, an empty slice (ones([1, 200])[128...]) and a zero-length dimension, on both the copy and noCopy paths.Tested on macOS arm64 (Xcode 27.2, Swift 6.4.2):
Fatal error: Unexpectedly found nil while unwrapping an Optional valueatMLXArray+Metal.swift:27MLXTestssuite passes (311 tests in 28 suites)Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes