This is an automated email from the ASF dual-hosted git repository.
tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new b64cb5b2c3 [Metal] Enable Metal 4 shader compilation (#19595)
b64cb5b2c3 is described below
commit b64cb5b2c356109b0543c8c3928f8b935450ba90
Author: Yichen Yan <[email protected]>
AuthorDate: Mon Jun 22 10:29:57 2026 +0800
[Metal] Enable Metal 4 shader compilation (#19595)
Use the latest SDK language version when available so generated MPP
tensor_ops shaders can compile on M5+ while preserving the existing
fallback for older SDKs.
---
src/backend/metal/runtime/metal_module.mm | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/src/backend/metal/runtime/metal_module.mm
b/src/backend/metal/runtime/metal_module.mm
index aff2152a13..a283c27fd8 100644
--- a/src/backend/metal/runtime/metal_module.mm
+++ b/src/backend/metal/runtime/metal_module.mm
@@ -44,12 +44,26 @@
#include "../../../support/bytes_io.h"
#include "metal_common.h"
+#if (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED
>= 260000) || \
+ (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) &&
__IPHONE_OS_VERSION_MAX_ALLOWED >= 260000)
+#define TVM_METAL_HAS_MSL_4_0 1
+#endif
+
namespace tvm {
namespace runtime {
/*! \brief Maximum number of GPU supported in MetalModule. */
static constexpr const int kMetalMaxNumDevice = 32;
+static bool MetalDeviceSupportsMetal4(id<MTLDevice> device) {
+#if defined(TVM_METAL_HAS_MSL_4_0)
+ if (@available(macOS 26.0, iOS 26.0, *)) {
+ return [device supportsFamily:MTLGPUFamilyMetal4];
+ }
+#endif
+ return false;
+}
+
// Module to support thread-safe multi-GPU execution.
// The runtime will contain a per-device module table
// The modules will be lazily loaded
@@ -123,17 +137,22 @@ class MetalModuleNode final : public ffi::ModuleObj {
const ffi::Bytes& source = (*kernel).second;
if (fmt_ == "metal") {
- MTLCompileOptions* opts = [MTLCompileOptions alloc];
- opts.languageVersion = MTLLanguageVersion2_3;
+ MTLCompileOptions* opts = [[MTLCompileOptions alloc] init];
+ MTLLanguageVersion language_version = MTLLanguageVersion2_3;
+#if defined(TVM_METAL_HAS_MSL_4_0)
+ if (MetalDeviceSupportsMetal4(w->devices[device_id])) {
+ language_version = MTLLanguageVersion4_0;
+ }
+#endif
+ opts.languageVersion = language_version;
opts.fastMathEnabled = YES;
- // opts = nil;
// Per-kernel payload is bytes; treat as UTF-8 MSL source.
std::string source_str(source.data(), source.size());
lib = [w->devices[device_id]
newLibraryWithSource:[NSString
stringWithUTF8String:source_str.c_str()]
options:opts
error:&err_msg];
- [opts dealloc];
+ [opts release];
if (lib == nil) {
LOG(FATAL) << "Fail to compile metal source:"
<< [[err_msg localizedDescription] UTF8String];