Skip to content

Add metal::set_metallib_path() to override the metallib search path#3597

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

Add metal::set_metallib_path() to override the metallib search path#3597
olilarkin wants to merge 1 commit into
ml-explore:mainfrom
olilarkin:metallib-path

Conversation

@olilarkin
Copy link
Copy Markdown

@olilarkin olilarkin commented May 28, 2026

What

Adds mlx::core::metal::set_metallib_path(const std::string&), allowing a caller to specify an explicit path to mlx.metallib before the Metal device initializes. When set, load_default_library() tries this path first and falls back to the existing search logic if it fails to load.

Why

When MLX is embedded inside a plugin bundle (e.g. Audio Unit / VST3), the host application — not MLX — owns the main bundle, so the default colocated / SwiftPM / bundle search paths don't resolve mlx.metallib and Metal init fails. There is currently no public way to point MLX at the library.

Notes

  • The override is only used if the library loads successfully; otherwise behavior is unchanged.
  • Must be called before any MLX operation (before the device is created).
  • No change to default behavior when the override is unset.

Companion PRs

This is the root of a three-PR chain (C++ → C → Swift):

  1. Add metal::set_metallib_path() to override the metallib search path #3597 — this PR (C++ set_metallib_path)
  2. Add mlx_metal_set_metallib_path() C API mlx-c#117 — C API wrapper
  3. Add GPU.setMetallibPath() for plugin bundle support mlx-swift#416 — Swift GPU.setMetallibPath()

Tracking issue: ml-explore/mlx-swift#415.

Allows overriding the default metallib search paths before Device
initialization. Needed when MLX is embedded in plugin bundles (AU,
VST3) where the standard search locations don't apply.
Copy link
Copy Markdown
Collaborator

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good API to have, better than adding numerous fallbacks for searching. But I would like another review from maintainers before merging.

static std::string g_metallib_override_path;

namespace mlx::core::metal {
void set_metallib_path(const std::string& path) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to mlx/backend/metal/metal.cpp, and add a stub in mlx/backend/metal/no_metal.cpp? Probably also needs to add a get_metallib_path.

namespace mlx::core::metal {

} // namespace std
static std::string g_metallib_override_path;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The globals are usually put in anonymous namespace rather than using static.

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.

2 participants