From 800f9ae91d81e387a7e1febe5a9ab93ff452c7c3 Mon Sep 17 00:00:00 2001 From: Oli Larkin Date: Mon, 9 Feb 2026 23:42:44 +0100 Subject: [PATCH] Add GPU.setMetallibPath() for plugin bundle support 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 --- Source/Cmlx/include/mlx/c/metal.h | 1 + Source/Cmlx/mlx | 2 +- Source/Cmlx/mlx-c | 2 +- Source/MLX/GPU+Metal.swift | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Source/Cmlx/include/mlx/c/metal.h b/Source/Cmlx/include/mlx/c/metal.h index 5877b224b..c9619567f 100644 --- a/Source/Cmlx/include/mlx/c/metal.h +++ b/Source/Cmlx/include/mlx/c/metal.h @@ -29,6 +29,7 @@ extern "C" { /**@{*/ int mlx_metal_is_available(bool* res); +int mlx_metal_set_metallib_path(const char* path); int mlx_metal_start_capture(const char* path); int mlx_metal_stop_capture(void); diff --git a/Source/Cmlx/mlx b/Source/Cmlx/mlx index ce45c5250..248f1790e 160000 --- a/Source/Cmlx/mlx +++ b/Source/Cmlx/mlx @@ -1 +1 @@ -Subproject commit ce45c52505c8158ea48d2a54e8caae05efd86bfe +Subproject commit 248f1790e97344342d1d73e13213aff7b37a28ae diff --git a/Source/Cmlx/mlx-c b/Source/Cmlx/mlx-c index 0726ca922..71371f2ff 160000 --- a/Source/Cmlx/mlx-c +++ b/Source/Cmlx/mlx-c @@ -1 +1 @@ -Subproject commit 0726ca922fc902c4c61ef9c27d94132be418e945 +Subproject commit 71371f2ffd6036b210f782abe4f4cf32d3e6299a diff --git a/Source/MLX/GPU+Metal.swift b/Source/MLX/GPU+Metal.swift index c46440817..b89eeea95 100644 --- a/Source/MLX/GPU+Metal.swift +++ b/Source/MLX/GPU+Metal.swift @@ -171,6 +171,23 @@ public enum GPU { Memory.clearCache() } + /// Set a custom path to the mlx.metallib file. + /// + /// Must be called before any MLX operation (e.g., before `Pipeline.load()`). + /// Use this when running inside a plugin bundle where the default search paths + /// don't apply. + /// + /// The override is stored as process-global state inside a single MLX image. If + /// the process ends up with two copies of MLX linked in (see the "XCode (2)" + /// note in the README about diamond dependencies), this may set the path on a + /// different copy than the one that loads the device, and the override will be + /// silently ignored. Ensure there is exactly one MLX instance in the process. + /// + /// - Parameter path: Absolute path to `mlx.metallib` + public static func setMetallibPath(_ path: String) { + mlx_metal_set_metallib_path(path) + } + /// Start capturing a metal trace into the given file. /// /// > There are several requirements for this to be used.