Skip to content

Complete the C++20 compiler migration - #4

Draft
PhysicistJohn wants to merge 1 commit into
mainfrom
draft/cpu-jit-cxx20
Draft

Complete the C++20 compiler migration#4
PhysicistJohn wants to merge 1 commit into
mainfrom
draft/cpu-jit-cxx20

Conversation

@PhysicistJohn

@PhysicistJohn PhysicistJohn commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

MLX moved its project build and documented C++ requirement to C++20 in ml-explore#3050, but several compiler entry points still selected C++17:

  • CPU runtime compilation on Unix and Windows
  • build-time CPU preamble generation on non-Darwin Unix and Windows
  • the installed CMake target metadata
  • the three shipped external CMake examples

This change aligns those remaining paths. It also exports cxx_std_20 as a usage requirement so CMake consumers inherit the same standard as MLX itself.

Why

The mismatch is latent until a header shared with the CPU JIT uses a C++20 feature. The normal MLX build then succeeds under C++20, but mx.compile recompiles the generated source under C++17 and fails at runtime. Concepts are one concrete way to expose that split.

The added regression test sends a C++20 concept through MLX's actual CPU JIT compiler and generated preamble. The identical test, applied alone to base commit fb5133e1, invokes g++ -std=c++17 and fails with unknown type name 'concept'; on this branch it emits the shared library and passes all four assertions.

The change is intentionally limited to language-standard selection. It does not alter compiler discovery or the preamble architecture discussed in ml-explore#3430.

Validation

The MLX CPU, Metal, and Python builds used Apple Clang 21 with C++ switch warnings promoted to errors. xcrun --find metal resolved the active toolchain, and xcrun metal --version reported Apple Metal 32023.921.

  • CPU-only Release build: 245/245 native cases, 3,242/3,242 assertions
  • Static Metal Release build: 261/261 native cases, 3,494/3,494 assertions
  • New CPU JIT C++20 regression: 1/1 case and 4/4 assertions in both builds
  • CPU-disabled Metal-JIT configuration: tests target built successfully; the CPU-only regression source was absent from its build graph
  • Python suite: 791 tests run; 746 passed and 45 skipped
  • Python CPU compile suite: 62/62 passed
  • Native runtime-compile subset: 25/25 cases and 121/121 assertions passed
  • Spawned-compiler capture confirmed generated CPU kernels use -std=c++20
  • Linux-style preamble generation completed with -std=c++20; its 3,472,631-byte output compiled successfully
  • Installed-package probe began with CMAKE_CXX_STANDARD 17, linked mlx, and was correctly compiled as -std=gnu++20
  • cmake_project, export, and the C++/Metal extension examples configured and built successfully
  • pre-commit run --all-files and git diff --check passed

The MSVC and PowerShell paths were audited but cannot be executed locally on this macOS host; upstream Windows CI is the execution gate.

Size and cold-JIT impact

  • CPU libmlx.a: 29,204,112 bytes before and after; 83 archive members in both
  • Representative generated libraries retained identical executable text and identical file sizes:
    • simple kernel: 16,904 bytes
    • deeper fused kernel: 17,016 bytes
  • Cold Apple-Clang compilation, 15 alternating runs per standard:
    • simple kernel: 0.2360 s C++17 -> 0.3133 s C++20 (+77.3 ms, +32.8%)
    • deeper kernel: 0.2420 s C++17 -> 0.3140 s C++20 (+72.0 ms, +29.8%)

The measured cost is confined to first compilation of a CPU kernel cache key. Both samples produced identical machine-code text, so there is no measured cached-execution or binary-size regression.

Checklist

  • I have read the contributing guide
  • I have run pre-commit run --all-files
  • I have added a test that proves the fix is effective
  • Documentation is already current: Use C++20 ml-explore/mlx#3050 set the documented requirement to C++20

Use C++20 for CPU JIT compilation and preamble generation, and propagate the requirement through installed CMake targets and examples.

Add a runtime regression test that compiles a C++20 concept through MLX's actual CPU JIT path.
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.

1 participant