Skip to content

Commit 895217f

Browse files
authored
optionally load metallib from framework (#2702)
* optionally load metallib from framework * pre-commit * adjust logic
1 parent 0cfeeb6 commit 895217f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

mlx/backend/metal/device.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ MTL::Library* try_load_bundle(
7272
}
7373
return nullptr;
7474
}
75+
76+
MTL::Library* try_load_framework(
77+
MTL::Device* device,
78+
NS::URL* url,
79+
const std::string& lib_name) {
80+
std::string resource_path = std::string(url->fileSystemRepresentation()) +
81+
"/" + lib_name + ".metallib";
82+
auto [lib, error] = load_library_from_path(device, resource_path.c_str());
83+
if (lib) {
84+
return lib;
85+
}
86+
return nullptr;
87+
}
7588
#endif
7689

7790
// Firstly, search for the metallib in the same path as this binary
@@ -103,6 +116,17 @@ std::pair<MTL::Library*, NS::Error*> load_swiftpm_library(
103116
return {library, nullptr};
104117
}
105118
}
119+
// if SWIFTPM_BUNDLE is a framework identifier, try loading from that
120+
auto frameworks = NS::Bundle::allFrameworks();
121+
for (int i = 0, c = (int)frameworks->count(); i < c; i++) {
122+
auto bundle = reinterpret_cast<NS::Bundle*>(frameworks->object(i));
123+
if (!strcmp(bundle->bundleIdentifier()->utf8String(), SWIFTPM_BUNDLE)) {
124+
library = try_load_framework(device, bundle->resourceURL(), lib_name);
125+
if (library != nullptr) {
126+
return {library, nullptr};
127+
}
128+
}
129+
}
106130
#endif
107131
return {nullptr, nullptr};
108132
}

0 commit comments

Comments
 (0)