Skip to content

Add GPU.setMetallibPath() for plugin bundle support#416

Draft
olilarkin wants to merge 1 commit into
ml-explore:mainfrom
olilarkin:metallib-path
Draft

Add GPU.setMetallibPath() for plugin bundle support#416
olilarkin wants to merge 1 commit into
ml-explore:mainfrom
olilarkin:metallib-path

Conversation

@olilarkin

@olilarkin olilarkin commented May 28, 2026

Copy link
Copy Markdown

What

Adds GPU.setMetallibPath(_ path: String), a Swift wrapper over the new mlx_metal_set_metallib_path() C API. Lets callers point MLX at a specific mlx.metallib before initialization. Also updates the vendored header Source/Cmlx/include/mlx/c/metal.h to match the mlx-c change.

Why

When MLX-Swift is embedded in a plugin bundle (AU/VST3), the host owns the main bundle and the default metallib search paths don't resolve. This provides a way to set the path explicitly. Must be called before the first MLX operation (e.g. before Pipeline.load()).

Caveat: single MLX instance required

The override is stored as process-global state inside a single MLX image. As noted in the README "XCode (2)" section, a diamond dependency (YourApp -> MLX and YourApp -> YourFramework -> MLX) can put two copies of MLX in one process. In that case setMetallibPath() may set the path on a different copy than the one that loads the device, and the override is silently ignored. Callers must ensure there is exactly one MLX instance in the process (single Library, or the Framework build via xcode/MLX.xcodeproj).

Note also that building MLX as a Framework may resolve the metallib via the colocated / dyld-relative search path without needing this override at all — this API is the explicit escape hatch for setups where that doesn't hold.

Depends on

The Source/Cmlx/mlx and Source/Cmlx/mlx-c submodule pointers in this PR currently reference my forks. Once the two PRs above merge, the pointers should be re-pointed to the merged upstream commits (happy to do this, or for a maintainer to bump them).

Context

Tracking issue: #415.

Adds API to set a custom mlx.metallib path before MLX initializes.
Plumbed through C++ -> C -> Swift layers. Required when running
inside plugin bundles where default search paths fail since main
bundle is the host
@davidkoski

Copy link
Copy Markdown
Collaborator

This would have to come through mlx and mlx-c rather than modify the files directly like this.

@RNT56

RNT56 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

I tested this API shape while working on the SwiftPM metallib resource PR.

The local proof branch is here:

RNT56/mlx-swift:prep/swiftpm-metallib-via-path-api

It uses GPU.setMetallibPath(...) for the SwiftPM resource case, finds mlx-swift_Cmlx.bundle/default.metallib, and sets the path before the first GPU stream.

Validation passed:

  • swift test --filter SwiftPMMetallibResourceTests
  • swift test --filter StreamTests/testDeviceType

So once the mlx-c wrapper lands, this should unblock the SwiftPM packaging PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants