Skip to content

Preprocess the CPU JIT's preamble with the compiler the JIT runs - #13

Merged
jjang-ai merged 1 commit into
osaurus-ai:mainfrom
rcfa:fix/cpu-jit-compiler
Nov 22, 2026
Merged

jjang-ai merged 1 commit into
osaurus-ai:mainfrom
rcfa:fix/cpu-jit-compiler

Conversation

@rcfa

@rcfa rcfa commented Sep 28, 2026 •

Copy link
Copy Markdown

On Linux, in a Clang build, every CPU-JIT kernel fails to compile when the JIT uses its prebuilt
preamble, as tests/tests in a build tree does. CMake preprocesses that preamble with the build's
compiler, but the JIT compiles each kernel with g++. This PR makes both use one compiler,
MLX_CPU_JIT_COMPILER (default g++).

The base is main (MLX 0.32.3). The two files this PR changes are the same there as on qwen4-exp,
so the change is the same; the commit message now quotes main's case counts, and says that the
GCC build ran on aarch64.

What fails, and why

Off Darwin, mlx/backend/cpu/CMakeLists.txt preprocesses compiled_preamble.h with
CMAKE_CXX_COMPILER. jit_compiler.cpp then builds each kernel with a hard-coded
g++ -std=c++17 -O3 -Wall -fPIC -shared. The JIT uses this prebuilt preamble when no headers are
installed next to the binary, for example for tests/tests in a build tree. In a Clang build the
preamble holds libstdc++ as clang++ preprocessed it: libstdc++ selects Clang builtins with
__has_builtin, and g++ rejects them. So all 16 cases of tests/compile_tests.cpp that compile a
kernel fail. GCC builds, which the fork's CI uses, are not affected: there both steps use g++.

The fix

  • A cache variable, MLX_CPU_JIT_COMPILER (default g++), names the compiler that the JIT runs on
    Linux. CMake preprocesses the preamble with the program that find_program finds for it.
  • CMake passes the value to jit_compiler.cpp as a definition, and available() and
    build_command() run it. The old g++ lines stay as the default, so a build without the definition
    (macOS, Windows, SwiftPM) compiles the same code as before.
  • If CMake does not find the compiler, it warns and keeps the old behaviour. A cross build also keeps
    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++ on PATH does not change: the preamble is the same, byte for
byte, and the JIT runs the same command. A GCC build with another GCC (for example
CMAKE_CXX_COMPILER=g++-13 and g++ 12 on PATH) now gets its preamble from g++, the compiler
its JIT runs. MLX_CPU_JIT_COMPILER can 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/tests from the build tree, with the JIT's kernel cache emptied
before each run.

Before, a Clang build on aarch64 at main (5aba1ef), the 16 cases:

error: '__make_integer_seq' does not name a type
error: '__builtin_operator_new' was not declared in this scope; did you mean '__builtin_return'?
[doctest] test cases: 16 |  0 passed | 16 failed | 261 skipped

After:

Clang, aarch64, the 16 cases:  [doctest] test cases: 16 | 16 passed | 0 failed | 261 skipped
Clang, aarch64, all cases:     [doctest] test cases:  277 |  277 passed | 0 failed | 0 skipped
Clang, x86_64, the 16 cases:   [doctest] test cases: 16 | 16 passed | 0 failed | 261 skipped
Clang, x86_64, all cases:      [doctest] test cases:  277 |  277 passed | 0 failed | 0 skipped
GCC, aarch64, all cases:       [doctest] test cases:  277 |  277 passed | 0 failed | 0 skipped

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 with
make_compiled_preamble.sh, which this PR does not change; before, it was 1da16540461548b0, with
__make_integer_seq. On x86_64, a Clang build's is now 0700c0318c7756d2, the same as g++ makes
from the same script; clang++ makes d5bc8cd7a87aad68.

The fallback: -DMLX_CPU_JIT_COMPILER=/nonexistent/g++ configures and builds, and prints

CMake Warning at mlx/backend/cpu/CMakeLists.txt:41 (message):
  MLX_CPU_JIT_COMPILER (/nonexistent/g++) not found.  The CPU JIT runs g++ on
  a preamble from /usr/bin/clang++.

That build's preamble is the old Clang one (1da16540461548b0), and jit_compiler.cpp gets no
definition. A configure with -DCMAKE_SYSTEM_NAME=Linux, which makes CMAKE_CROSSCOMPILING true, also
keeps /usr/bin/clang++ for the preamble and adds no definition.

On macOS, every source of vmlx-swift's Cmlx target, preprocessed with an arm64 debug build's
settings, is the same at vmlx-swift's main (9db627f3, which records this fork's main) and with this
commit in place of its mlx: 195 Cmlx sources, and the same 135 Xcode project members. vmlx leaves
jit_compiler.cpp out on Apple platforms, so it was also preprocessed on its own for macOS with the
same 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

  • A Clang JIT compiler. MLX_CPU_JIT_COMPILER=clang++ does not work with the JIT's flags: all 16
    cases fail, with error: constexpr function never produces a constant expression [-Winvalid-constexpr], and this PR does not add -Wno-invalid-constexpr.
  • A macOS or Windows CMake build, a real cross build, and Unix systems other than Linux. The new code
    runs only for native Linux builds.
  • The installed-headers path (the JIT includes compiled_preamble.h from an install tree), the Python
    bindings and CUDA builds.
  • The CI's configurations: GCC 15 on Fedora, GCC 11 with the sanitizers and warnings as errors, and
    fork-runtime-contracts (Ubuntu 22.04 with its default compiler, 15 named cases, none of them a
    CPU-JIT case). The GCC build above is GCC 12, Release.
  • A GCC build whose compiler is not the g++ on PATH.

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 main integrated, and on ml-explore/mlx's main
(388f227, 2026-10-12).
There, mlx/backend/cpu/CMakeLists.txt:1-7
and make_compiled_preamble.sh:27-31
preprocess the preamble with the build's compiler, and
jit_compiler.cpp:273-277
and :209-214
run g++. The MSVC branch already prefers the same compiler family for the prebuilt preamble
(:106-113).

🤖 Generated with Claude Code

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants