@@ -35,20 +35,43 @@ include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
3535find_package_torch ()
3636
3737set (_aoti_metal_sources
38- runtime/metal_backend.cpp
3938 runtime/stats.cpp
39+ # v1-only sources (no v2 replacement yet — built in both branches):
40+ runtime/shims/utils.cpp
41+ )
42+
43+ # AOTI shim layer: choose between v1 (vendored MLX qmv_fast for int4,
44+ # legacy stream/encoding/buffer-mgmt shims) and v2 (routes through
45+ # metal_v2::MetalStream + MetalOpRegistry → AffineQuantizedLinearOp,
46+ # SDPAOp with NAX / qmm_t / qmm_t_splitk dispatch + per-CB residency).
47+ #
48+ # v1 and v2 shim files share extern "C" symbol names (intentionally —
49+ # PTE files lower against the same AOTI shim ABI). They CANNOT coexist
50+ # in the same build; this if/else picks one.
51+ if (EXECUTORCH_USE_METAL_V2)
52+ list (APPEND _aoti_metal_sources
53+ runtime/metal_backend_v2.cpp
54+ runtime/shims/v2/aoti_tensor.cpp
55+ runtime/shims/v2/aoti_dtype_stubs.cpp
56+ runtime/shims/v2/runtime.mm
57+ runtime/shims/v2/aoti_kernel.mm
58+ runtime/shims/v2/aoti_fallback_op.mm
59+ )
60+ else ()
61+ list (APPEND _aoti_metal_sources
62+ runtime/metal_backend.cpp
4063 runtime/shims/memory.cpp
4164 runtime/shims/et_metal.mm
4265 runtime/shims/shim_mps.mm
4366 runtime/shims/tensor_attribute.cpp
44- runtime/shims/utils.cpp
4567 runtime/ops/common.mm
4668 runtime/ops/op_bmm.mm
4769 runtime/ops/op_convolution.mm
4870 runtime/ops/op_linear_4bit.mm
4971 runtime/ops/op_mm.mm
5072 runtime/ops/op_sdpa.mm
51- )
73+ )
74+ endif ()
5275
5376add_library (metal_backend STATIC ${_aoti_metal_sources} )
5477target_include_directories (
@@ -87,6 +110,14 @@ endif()
87110
88111target_link_options (metal_backend PUBLIC -Wl,-export_dynamic )
89112
113+ # Under the v2 shim path, metal_backend depends on the metal_v2 lib
114+ # (defined in backends/metal/) for MetalStream, MetalOpRegistry,
115+ # AffineQuantizedLinearOp, etc. PRIVATE link so it doesn't propagate
116+ # through install exports of metal_backend's downstream consumers.
117+ if (EXECUTORCH_USE_METAL_V2)
118+ target_link_libraries (metal_backend PRIVATE metal_v2 )
119+ endif ()
120+
90121# Find PyTorch's OpenMP library specifically for libtorch-less AOTI
91122get_torch_base_path (TORCH_BASE_PATH )
92123find_library (
0 commit comments