fix: check mlx_detail_compile and mlx_closure_apply return values in CompiledFunction#398
Open
vernonstinebaker wants to merge 1 commit intoml-explore:mainfrom
Conversation
mlx_detail_compile and mlx_closure_apply both return int (0=success, 1=failure) but their return values were silently ignored. When an error fires inside a withError scope the MLX error handler stores the error in an ErrorBox instead of calling fatalError; execution then continues past the failed call, innerCall returns an empty result vector, and the single/two/three-array compile overloads crash with a Swift 'Index out of range' trap — bypassing withError entirely. Fix: capture both return values and early-return [] from innerCall on failure. The placeholder return from the compile overloads is never observed by the caller because withError throws before the value is used. Adds three regression tests covering the single-array, two-array, and [MLXArray]->[MLXArray] compile overloads.
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 #397.
CompiledFunction.innerCall()(Source/MLX/Transforms+Compile.swift) silently ignored theintreturn values ofmlx_detail_compileandmlx_closure_apply. Both return1on failure and callmlx_error(). When called inside awithErrorscope the Swift error handler stores the error in anErrorBoxand execution continues — leavingresultVectorempty. The single/two/three-arraycompileoverloads then subscript into an empty array (result[0]), triggering a Swift trap that kills the process beforewithErrorcan throw.Fix:
mlx_detail_compile;return []frominnerCallif it fails.mlx_closure_apply;return []frominnerCallif it fails.compileState.call(...)[0]with a safelet r = ...; return r.isEmpty ? MLXArray(0) : r[0].The placeholder
MLXArray(0)returned by the overloads on the error path is never observed by the caller —withErrorthrowsMLXErrorbefore the value is used.MLXCustomFunction.swift:112already uses the same pattern (precondition(status == 0, ...)) for its ownmlx_closure_applycall; this PR makesTransforms+Compile.swiftconsistent.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes