Conversation
…upports The Metal runtime compiles every textual MSL module with MTLLanguageVersion2_3 unless the device supports Metal 4. MSL 2.3 has no bfloat type (added in MSL 3.1) and no device atomic<float> (added in MSL 3.0), so any kernel that uses them fails to compile at load time on macOS 13-15, even though the OS supports a newer language version. The Metal codegen prints bfloat16 as `bfloat` (including `simdgroup_bfloat8x8` fragments), and MSL supplied through tvm_callback_metal_compile or by downstream projects hits the same limit. Select the highest MSL version below 4.0 that the running OS supports: MSL 3.1 on macOS 14 / iOS 17 and later, MSL 3.0 on macOS 13 / iOS 16, and MSL 2.3 otherwise. Each newer version is guarded by both the SDK version macros and @available, so builds against older SDKs and older deployment targets keep the current behavior. The MSL 4.0 selection for Metal 4 devices is unchanged. Add a test that compiles a kernel through tvm_callback_metal_compile using `bfloat` and __METAL_VERSION__, and checks that the runtime compiled it as MSL 3.1 or newer on macOS 14+.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MetalModuleNode::GetPipelineStatecompiles every textual MSL module (fmt == "metal") withMTLLanguageVersion2_3, unless the device supports Metal 4 (macOS/iOS 26 SDK). MSL 2.3 has nobfloattype (added in MSL 3.1) and no deviceatomic<float>(added in MSL 3.0). Any kernel that uses either fails to compile at load time on macOS 13-15, even though the OS supports a newer MSL version:The Metal codegen prints bfloat16 as
bfloat(includingsimdgroup_bfloat8x8fragments). MSL supplied throughtvm_callback_metal_compile, or by downstream projects that reuse the TVM Metal runtime, runs into the same limit.Change
src/backend/metal/runtime/metal_module.mmnow picks the highest MSL version below 4.0 that the running OS supports. The Metal 4 path is unchanged:bfloat)atomic<float>)Each newer version is selected inside a matching
@availablecheck, and the__MAC_OS_X_VERSION_MAX_ALLOWED/__IPHONE_OS_VERSION_MAX_ALLOWEDguards follow the existingTVM_METAL_HAS_MSL_4_0pattern. Builds against older SDKs and older deployment targets keep the current behavior.A new test,
test_metal_source_compiled_with_msl_3_1intests/python/codegen/test_target_codegen_metal.py, usestvm_callback_metal_compileto compile a kernel that usesbfloatand writes out__METAL_VERSION__. It then checks that the runtime compiled it as MSL 3.1 or newer. The test is skipped below macOS 14 and when there is no Metal device.Testing
Run on an Apple M2 with macOS 15.6.1 (Command Line Tools SDK 15.5, AppleClang 17). This is not a Metal 4 setup, so the MSL 4.0 path was not exercised.
USE_METAL=ON,USE_LLVM=OFF, Release, Ninja:libtvm_compiler,libtvm_runtimeandlibtvm_runtime_metalall built. The changed file adds no warnings. It also compiles warning-free with-mmacosx-version-min=11.0 -Wunguarded-availability.Target("metal", host="c"), exported withexport_libraryand loaded back withload_module.__METAL_VERSION__ == 310.main(onlylibtvm_runtime_metalrebuilt): fails withuse of undeclared identifier 'bfloat'.test_unaligned_vectorizestill passes.test_metal_inf_nanandtest_metal_erfcould not run with the C host: the generated host C code useshalf. This is a limitation of the C host only. These tests run under the LLVM host in the macOS CI job.pre-commit run --from-ref origin/main --to-ref HEADpasses, including clang-format and ruff.The same runtime change was proposed to the TileLang TVM fork as tile-ai#78.