GPU: Apple Metal backend, off by default - #15859
Merged
Merged
Conversation
davidrohr
reviewed
Sep 28, 2026
davidrohr
left a comment
Collaborator
There was a problem hiding this comment.
In general, nothing scary for me. In any case, it will be compiled only for MacOS.
I didn't read it in detail enough to understand anything, and I have no way of testing it anyway.
I saw that a comment still mentions OpenCL, so perhaps one should go through the code once and do some cleanup. I guess a lot was copy and pasted from the OpenCL host code.
| int32_t GPUReconstructionMetal::InitDevice_Runtime() | ||
| { | ||
| // Propagate processing settings to PoCL runtime. | ||
| // Won't affect other OpenCL runtimes. |
Collaborator
There was a problem hiding this comment.
Might need some cleanup, this is for metal, not OpenCL
Member
Author
|
Yes, this is indeed my old copy pastes from last year plus further corrections from my friends for metal 4.1. I will ask for a cleanup of this commit. |
The backend itself: the Objective-C++ host side, the .metal kernel source and its build rules, plus the CMake to enable them. macOS had no usable GPU backend before this. It ships OpenCL 1.2, below the 2.x the OpenCL backend requires, so find_package(OpenCL) there could never produce one; the version check dropped it again a few lines later. That lookup is now skipped on Apple, which leaves CUDA_ENABLED, OPENCL_ENABLED and HIP_ENABLED all necessarily off, so the Darwin arm of the backend dispatch was dead code and goes with it. Metal takes its place. The build rules also give Metal its entry in the no-fast-math table, so that once GPUCA_DETERMINISTIC_MODE reaches GPUCA_DETERMINISTIC_MODE_MAP_NO_FAST_MATH it drops its fast math flags like the other backends rather than keeping whatever the build type gave it. Off unless asked for. FindO2GPU.cmake leaves ENABLE_METAL=OFF on macOS and the subdirectory is gated on METAL_ENABLED, so macOS keeps running on the CPU until the whole chain is validated. Apple toolchain only: the source goes .metal -> AIR through xcrun metal and nothing else, with no SPIR-V translation step in between. Requires -std=metal4.1, the first MSL version with a generic address space. Earlier versions reject an unannotated pointer with 'pointer type must have explicit address space qualifier' and an unannotated 'this' with 'cannot initialize object parameter', both of which GPUCommonDefAPI.h relies on for GPUgeneric() and GPUdDefault(). Verified against Xcode 27, which ships metal4.1; Xcode 26 and earlier stop at metal4.0. The Metal frameworks ship with every macOS, so finding them says nothing about whether the backend can be built; the configure compiles a three-line kernel as MSL 4.1 to answer that directly. The deployment target has no say either: -std= is what picks the target OS, and MACOSX_DEPLOYMENT_TARGET and -mmacosx-version-min are both ignored by the Metal compiler. AUTO therefore turns Metal off on an older toolchain instead of failing somewhere in the middle of the build, and an explicit ENABLE_METAL=ON says why it cannot be honoured.
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.
The backend itself: the Objective-C++ host side, the .metal kernel source and
its build rules, plus the CMake to enable them.
macOS had no usable GPU backend before this. It ships OpenCL 1.2, below the 2.x
the OpenCL backend requires, so find_package(OpenCL) there could never produce
one; the version check dropped it again a few lines later. That lookup is now
skipped on Apple, which leaves CUDA_ENABLED, OPENCL_ENABLED and HIP_ENABLED all
necessarily off, so the Darwin arm of the backend dispatch was dead code and
goes with it. Metal takes its place.
The build rules also give Metal its entry in the no-fast-math table, so that once
GPUCA_DETERMINISTIC_MODE reaches GPUCA_DETERMINISTIC_MODE_MAP_NO_FAST_MATH it
drops its fast math flags like the other backends rather than keeping whatever
the build type gave it.
Off unless asked for. FindO2GPU.cmake leaves ENABLE_METAL=OFF on macOS and the
subdirectory is gated on METAL_ENABLED, so macOS keeps running on the CPU until
the whole chain is validated.
Apple toolchain only: the source goes .metal -> AIR through xcrun metal and
nothing else, with no SPIR-V translation step in between.
Requires -std=metal4.1, the first MSL version with a generic address space.
Earlier versions reject an unannotated pointer with 'pointer type must have
explicit address space qualifier' and an unannotated 'this' with 'cannot
initialize object parameter', both of which GPUCommonDefAPI.h relies on for
GPUgeneric() and GPUdDefault(). Verified against Xcode 27, which ships
metal4.1; Xcode 26 and earlier stop at metal4.0.
The Metal frameworks ship with every macOS, so finding them says nothing about
whether the backend can be built; the configure compiles a three-line kernel as
MSL 4.1 to answer that directly. The deployment target has no say either: -std=
is what picks the target OS, and MACOSX_DEPLOYMENT_TARGET and
-mmacosx-version-min are both ignored by the Metal compiler. AUTO therefore
turns Metal off on an older toolchain instead of failing somewhere in the middle
of the build, and an explicit ENABLE_METAL=ON says why it cannot be honoured.