Repository navigation
Preprocess the CPU JIT's preamble with the compiler the JIT runs - #13
Merged
Merged
Conversation
On Linux, CMake preprocessed the CPU JIT's prebuilt preamble with CMAKE_CXX_COMPILER, but jit_compiler.cpp compiles every kernel with a hard-coded g++. In a Clang build the preamble holds libstdc++ as clang++ preprocessed it, with Clang builtins, and g++ rejects them: error: '__make_integer_seq' does not name a type error: '__builtin_operator_new' was not declared in this scope So every case of tests/compile_tests.cpp that compiles a kernel failed in a Clang build (clang 21 and g++ 12, aarch64): [doctest] test cases: 16 | 0 passed | 16 failed | 261 skipped Add the cache variable MLX_CPU_JIT_COMPILER (default g++), the compiler that the JIT runs on Linux. CMake preprocesses the preamble with it and passes it to jit_compiler.cpp, where available() and build_command() run it. If CMake does not find it, CMake warns and keeps the old behaviour. Cross builds also keep the old behaviour. Without the definition (macOS, Windows, builds without CMake), jit_compiler.cpp compiles as before. A Clang build now makes the same preamble as a GCC build, byte for byte, and passes every case on aarch64 and on x86_64: [doctest] test cases: 277 | 277 passed | 0 failed | 0 skipped A GCC build keeps its preamble and its JIT command, and passes the same 277 cases on aarch64.
rcfa
force-pushed
the
fix/cpu-jit-compiler
branch
from
October 13, 2026 15:39
6b5dd41 to
c4d4f5f
Compare
This was referenced Nov 3, 2026
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.
On Linux, in a Clang build, every CPU-JIT kernel fails to compile when the JIT uses its prebuilt
preamble, as
tests/testsin a build tree does. CMake preprocesses that preamble with the build'scompiler, but the JIT compiles each kernel with
g++. This PR makes both use one compiler,MLX_CPU_JIT_COMPILER(defaultg++).The base is
main(MLX 0.32.3). The two files this PR changes are the same there as onqwen4-exp,so the change is the same; the commit message now quotes
main's case counts, and says that theGCC build ran on aarch64.
What fails, and why
Off Darwin,
mlx/backend/cpu/CMakeLists.txtpreprocessescompiled_preamble.hwithCMAKE_CXX_COMPILER.jit_compiler.cppthen builds each kernel with a hard-codedg++ -std=c++17 -O3 -Wall -fPIC -shared. The JIT uses this prebuilt preamble when no headers areinstalled next to the binary, for example for
tests/testsin a build tree. In a Clang build thepreamble holds libstdc++ as clang++ preprocessed it: libstdc++ selects Clang builtins with
__has_builtin, and g++ rejects them. So all 16 cases oftests/compile_tests.cppthat compile akernel fail. GCC builds, which the fork's CI uses, are not affected: there both steps use g++.
The fix
MLX_CPU_JIT_COMPILER(defaultg++), names the compiler that the JIT runs onLinux. CMake preprocesses the preamble with the program that
find_programfinds for it.jit_compiler.cppas a definition, andavailable()andbuild_command()run it. The oldg++lines stay as the default, so a build without the definition(macOS, Windows, SwiftPM) compiles the same code as before.
the old behaviour: the JIT runs on the target, so a host compiler cannot make its preamble.
A GCC build whose compiler is the
g++onPATHdoes not change: the preamble is the same, byte forbyte, and the JIT runs the same command. A GCC build with another GCC (for example
CMAKE_CXX_COMPILER=g++-13andg++12 onPATH) now gets its preamble fromg++, the compilerits JIT runs.
MLX_CPU_JIT_COMPILERcan name another compiler for both.Evidence
Debian 12 containers with clang 21.0.0, g++ 12.2.0 and CMake 3.25.1, the x86_64 one under Rosetta on
an arm64 Mac; Release builds;
tests/testsfrom the build tree, with the JIT's kernel cache emptiedbefore each run.
Before, a Clang build on aarch64 at
main(5aba1ef), the 16 cases:After:
The preambles, by the first 16 hex digits of their SHA-256: on aarch64, a Clang build's is now
0ff8f59fdb1c66aa, the same as a GCC build's, and the same as g++ makes withmake_compiled_preamble.sh, which this PR does not change; before, it was1da16540461548b0, with__make_integer_seq. On x86_64, a Clang build's is now0700c0318c7756d2, the same as g++ makesfrom the same script; clang++ makes
d5bc8cd7a87aad68.The fallback:
-DMLX_CPU_JIT_COMPILER=/nonexistent/g++configures and builds, and printsThat build's preamble is the old Clang one (
1da16540461548b0), andjit_compiler.cppgets nodefinition. A configure with
-DCMAKE_SYSTEM_NAME=Linux, which makesCMAKE_CROSSCOMPILINGtrue, alsokeeps
/usr/bin/clang++for the preamble and adds no definition.On macOS, every source of vmlx-swift's
Cmlxtarget, preprocessed with an arm64 debug build'ssettings, is the same at vmlx-swift's
main(9db627f3, which records this fork'smain) and with thiscommit in place of its mlx: 195 Cmlx sources, and the same 135 Xcode project members. vmlx leaves
jit_compiler.cppout on Apple platforms, so it was also preprocessed on its own for macOS with thesame flags: the same text before and after this change. On Linux without the definition, as SwiftPM
compiles it, it is also the same text as before.
Not verified
MLX_CPU_JIT_COMPILER=clang++does not work with the JIT's flags: all 16cases fail, with
error: constexpr function never produces a constant expression [-Winvalid-constexpr], and this PR does not add-Wno-invalid-constexpr.runs only for native Linux builds.
compiled_preamble.hfrom an install tree), the Pythonbindings and CUDA builds.
fork-runtime-contracts(Ubuntu 22.04 with its default compiler, 15 named cases, none of them aCPU-JIT case). The GCC build above is GCC 12, Release.
g++onPATH.Upstream
ml-explore/mlx has the same code, and this change applies to it unchanged. The three files involved
are the same at v0.32.3 (64ea011), which
mainintegrated, and on ml-explore/mlx'smain(388f227, 2026-10-12).
There,
mlx/backend/cpu/CMakeLists.txt:1-7and
make_compiled_preamble.sh:27-31preprocess the preamble with the build's compiler, and
jit_compiler.cpp:273-277and
:209-214run
g++. The MSVC branch already prefers the same compiler family for the prebuilt preamble(
:106-113).🤖 Generated with Claude Code