Skip to content

Commit 2e8a45a

Browse files
authored
Move KleidiAI support detection to CMake logic. (microsoft#26178)
### Description <!-- Describe your changes. --> Use of the KleidiAI library is not supported on all platforms. Previously, this support detection was done in both `tools/ci_build/build.py` and `cmake/CMakeLists.txt`. This change consolidates and simplifies the checks. They are now done in one place in the CMake logic. Move setting of onnxruntime_target_platform into a separate file. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Clean up and simplify.
1 parent 8ebd0bf commit 2e8a45a

File tree

4 files changed

+154
-118
lines changed

4 files changed

+154
-118
lines changed

cmake/CMakeLists.txt

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ option(onnxruntime_USE_SVE "Build with SVE support in MLAS" OFF)
9191
option(onnxruntime_USE_ARM_NEON_NCHWC "Build with ARM Neon NCHWc kernels in MLAS" OFF)
9292

9393
option(onnxruntime_USE_KLEIDIAI "Build with KleidiAI integration in MLAS" OFF)
94-
# iOS simulator build explicitly builds targets with USE_KLEIDIAI=ON so attempting to force override if so
95-
if(APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")
96-
message(WARNING "Disabling KleidiAI: not supported on Apple x86_64 platforms")
97-
set(onnxruntime_USE_KLEIDIAI OFF CACHE BOOL "" FORCE)
98-
endif()
9994
option(onnxruntime_BUILD_UNIT_TESTS "Build ONNXRuntime unit tests" ON)
10095
option(onnxruntime_BUILD_CSHARP "Build C# library" OFF)
10196
option(onnxruntime_BUILD_OBJC "Build Objective-C library" OFF)
@@ -259,6 +254,8 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_
259254
message(FATAL_ERROR "GCC version must be greater than or equal to 11.1")
260255
endif()
261256

257+
include(detect_onnxruntime_target_platform.cmake)
258+
262259
# ENABLE_TRAINING includes all training functionality
263260
# The following 2 entry points
264261
# 1. ORTModule
@@ -435,14 +432,6 @@ set(ORTTRAINING_SOURCE_DIR ${ORTTRAINING_ROOT}/orttraining)
435432

436433
include(adjust_global_compile_flags.cmake)
437434

438-
if (APPLE)
439-
if (NOT CMAKE_OSX_ARCHITECTURES)
440-
message("Building ONNX Runtime for ${CMAKE_HOST_SYSTEM_PROCESSOR} CPU ARCH")
441-
endif()
442-
elseif (NOT WIN32 AND NOT APPLE)
443-
message("Building ONNX Runtime for ${onnxruntime_target_platform} CPU ARCH")
444-
endif()
445-
446435
# We need to link with libatomic on systems that do not have built-in atomics, or
447436
# don't have built-in support for 8 byte atomics
448437
# Derived from https://github.com/protocolbuffers/protobuf/blob/master/cmake/CMakeLists.txt
@@ -514,6 +503,66 @@ if (onnxruntime_BUILD_SHARED_LIB OR onnxruntime_ENABLE_PYTHON)
514503
endif()
515504
endif()
516505

506+
if (onnxruntime_USE_ARM_NEON_NCHWC)
507+
message(STATUS "Building MLAS with ARM Neon NCHWc kernels")
508+
endif()
509+
510+
if(onnxruntime_USE_SVE)
511+
if(LINUX AND onnxruntime_target_platform STREQUAL "aarch64")
512+
check_cxx_compiler_flag("-march=armv8.2-a+sve" HAS_ARM64_SVE)
513+
if(HAS_ARM64_SVE)
514+
message(STATUS "Compiler supports SVE!")
515+
else()
516+
message(WARNING "onnxruntime_USE_SVE was set but compiler does not support SVE. It will be disabled.")
517+
set(onnxruntime_USE_SVE OFF)
518+
endif()
519+
else()
520+
message(WARNING "onnxruntime_USE_SVE was set but it is not supported on this platform. It will be disabled.")
521+
set(onnxruntime_USE_SVE OFF)
522+
endif()
523+
endif()
524+
525+
if(onnxruntime_USE_KLEIDIAI)
526+
function(is_kleidiai_supported is_supported_var)
527+
# check for supported target platforms
528+
if(NOT (onnxruntime_target_platform STREQUAL "aarch64" OR
529+
onnxruntime_target_platform STREQUAL "ARM64" OR
530+
onnxruntime_target_platform STREQUAL "arm64"))
531+
message(WARNING "KleidiAI is not supported on this platform.")
532+
533+
set(${is_supported_var} FALSE PARENT_SCOPE)
534+
return()
535+
endif()
536+
537+
# check for compiler support
538+
if(MSVC)
539+
# TODO detect on MSVC
540+
else()
541+
check_cxx_compiler_flag(-march=armv8.2-a+dotprod HAS_ARM64_DOTPROD)
542+
check_cxx_compiler_flag(-march=armv8.2-a+i8mm HAS_ARM64_I8MM)
543+
if(NOT HAS_ARM64_DOTPROD)
544+
message(WARNING "The compiler doesn't support dotprod instructions.")
545+
endif()
546+
if(NOT HAS_ARM64_I8MM)
547+
message(WARNING "The compiler doesn't support i8mm instructions.")
548+
endif()
549+
if(NOT HAS_ARM64_DOTPROD OR NOT HAS_ARM64_I8MM)
550+
set(${is_supported_var} FALSE PARENT_SCOPE)
551+
return()
552+
endif()
553+
endif()
554+
555+
set(${is_supported_var} TRUE PARENT_SCOPE)
556+
endfunction()
557+
558+
is_kleidiai_supported(is_kleidiai_supported_result)
559+
560+
if(NOT is_kleidiai_supported_result)
561+
message(WARNING "onnxruntime_USE_KLEIDIAI was set but it is not supported. It will be disabled.")
562+
set(onnxruntime_USE_KLEIDIAI OFF)
563+
endif()
564+
endif()
565+
517566
#Dependencies begin
518567
get_filename_component(ONNXRUNTIME_ROOT "${ONNXRUNTIME_ROOT}" ABSOLUTE)
519568
get_filename_component(ORTTRAINING_ROOT "${ORTTRAINING_ROOT}" ABSOLUTE)
@@ -664,47 +713,6 @@ else()
664713
endif()
665714
endif()
666715

667-
if (onnxruntime_USE_ARM_NEON_NCHWC)
668-
message(STATUS "Building MLAS with ARM Neon NCHWc kernels")
669-
endif()
670-
671-
if(onnxruntime_USE_SVE)
672-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
673-
check_cxx_compiler_flag("-march=armv8.2-a+sve" HAS_ARM64_SVE)
674-
if(HAS_ARM64_SVE)
675-
message(STATUS "Compiler supports SVE!")
676-
else()
677-
message(WARNING "onnxruntime_USE_SVE was set but compiler does not support SVE. It will be disabled.")
678-
set(onnxruntime_USE_SVE OFF)
679-
endif()
680-
else()
681-
message(WARNING "onnxruntime_USE_SVE was set but it is not supported on this platform. It will be disabled.")
682-
set(onnxruntime_USE_SVE OFF)
683-
endif()
684-
endif()
685-
686-
if (onnxruntime_USE_KLEIDIAI AND (
687-
(onnxruntime_target_platform STREQUAL "aarch64") OR
688-
(onnxruntime_target_platform STREQUAL "ARM64") OR
689-
(APPLE AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")))
690-
691-
# TODO Add checks for MSVC Compilation
692-
if(NOT MSVC)
693-
check_cxx_compiler_flag(-march=armv8.2-a+dotprod HAS_ARM64_DOTPROD)
694-
check_cxx_compiler_flag(-march=armv8.2-a+i8mm HAS_ARM64_I8MM)
695-
if (NOT HAS_ARM64_DOTPROD)
696-
message(FATAL_ERROR "The compiler doesn't support dotprod")
697-
endif()
698-
if (NOT HAS_ARM64_I8MM)
699-
message(FATAL_ERROR "The compiler doesn't support i8mm")
700-
endif()
701-
else()
702-
message(STATUS "Skipping -march= checks on MSVC (not supported), assuming dotprod/i8mm support manually.")
703-
set(HAS_ARM64_DOTPROD TRUE)
704-
set(HAS_ARM64_I8MM TRUE)
705-
endif()
706-
endif()
707-
708716
#names in this var must match the directory names under onnxruntime/core/providers
709717
#ONNXRUNTIME_PROVIDER_NAMES is the list of providers that needs to export additional symbols in the global namespace.
710718
#For example CUDA EP exports "OrtSessionOptionsAppendExecutionProvider_CUDA", which is a global function.

cmake/adjust_global_compile_flags.cmake

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,20 @@ endmacro()
217217
#Set global compile flags for all the source code(including third_party code like protobuf)
218218
#This section must be before any add_subdirectory, otherwise build may fail because /MD,/MT mismatch
219219
if (MSVC)
220-
if (CMAKE_VS_PLATFORM_NAME)
221-
# Multi-platform generator
222-
set(onnxruntime_target_platform ${CMAKE_VS_PLATFORM_NAME})
223-
else()
224-
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
225-
endif()
226-
if (onnxruntime_target_platform STREQUAL "ARM64")
227-
set(onnxruntime_target_platform "ARM64")
228-
enable_language(ASM_MARMASM)
229-
elseif (onnxruntime_target_platform STREQUAL "ARM64EC")
220+
if (onnxruntime_target_platform STREQUAL "ARM64" OR
221+
onnxruntime_target_platform STREQUAL "ARM64EC" OR
222+
onnxruntime_target_platform STREQUAL "ARM")
230223
enable_language(ASM_MARMASM)
231-
elseif (onnxruntime_target_platform STREQUAL "ARM" OR CMAKE_GENERATOR MATCHES "ARM")
232-
set(onnxruntime_target_platform "ARM")
233-
enable_language(ASM_MARMASM)
234-
elseif (onnxruntime_target_platform STREQUAL "x64" OR onnxruntime_target_platform STREQUAL "x86_64" OR onnxruntime_target_platform STREQUAL "AMD64" OR CMAKE_GENERATOR MATCHES "Win64")
235-
set(onnxruntime_target_platform "x64")
236-
enable_language(ASM_MASM)
237-
elseif (onnxruntime_target_platform STREQUAL "Win32" OR onnxruntime_target_platform STREQUAL "x86" OR onnxruntime_target_platform STREQUAL "i386" OR onnxruntime_target_platform STREQUAL "i686")
238-
set(onnxruntime_target_platform "x86")
224+
elseif (onnxruntime_target_platform STREQUAL "x64" OR
225+
onnxruntime_target_platform STREQUAL "x86")
239226
enable_language(ASM_MASM)
240-
message("Enabling SAFESEH for x86 build")
241-
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
227+
228+
if (onnxruntime_target_platform STREQUAL "x86")
229+
message("Enabling SAFESEH for x86 build")
230+
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
231+
endif()
242232
else()
243-
message(FATAL_ERROR "Unknown CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
233+
message(FATAL_ERROR "Unsupported onnxruntime_target_platform value: ${onnxruntime_target_platform}")
244234
endif()
245235

246236
#Always enable exception handling, even for Windows ARM
@@ -269,34 +259,6 @@ if (MSVC)
269259
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Gw /GL")
270260
endif()
271261
else()
272-
if (NOT APPLE)
273-
#XXX: Sometimes the value of CMAKE_SYSTEM_PROCESSOR is set but it's wrong. For example, if you run an armv7 docker
274-
#image on an aarch64 machine with an aarch64 Ubuntu host OS, in the docker instance cmake may still report
275-
# CMAKE_SYSTEM_PROCESSOR as aarch64 by default. Given compiling this code may need more than 2GB memory, we do not
276-
# support compiling for ARM32 natively(only support cross-compiling), we will ignore this issue for now.
277-
if(NOT CMAKE_SYSTEM_PROCESSOR)
278-
message(WARNING "CMAKE_SYSTEM_PROCESSOR is not set. Please set it in your toolchain cmake file.")
279-
# Try to detect it
280-
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
281-
execute_process(
282-
COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
283-
OUTPUT_VARIABLE GCC_DUMP_MACHINE_OUT OUTPUT_STRIP_TRAILING_WHITESPACE
284-
ERROR_VARIABLE _err
285-
RESULT_VARIABLE _res
286-
)
287-
if(NOT _res EQUAL 0)
288-
message(SEND_ERROR "Failed to run 'gcc -dumpmachine':\n ${_res}")
289-
endif()
290-
string(REPLACE "-" ";" GCC_DUMP_MACHINE_OUT_LIST "${GCC_DUMP_MACHINE_OUT}")
291-
list(LENGTH GCC_DUMP_MACHINE_OUT_LIST GCC_TRIPLET_LEN)
292-
if(GCC_TRIPLET_LEN EQUAL 4)
293-
list(GET GCC_DUMP_MACHINE_OUT_LIST 0 CMAKE_SYSTEM_PROCESSOR)
294-
message("Setting CMAKE_SYSTEM_PROCESSOR to ${CMAKE_SYSTEM_PROCESSOR}")
295-
endif()
296-
endif()
297-
endif()
298-
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
299-
endif()
300262
if (onnxruntime_BUILD_FOR_NATIVE_MACHINE)
301263
string(APPEND CMAKE_CXX_FLAGS " -march=native -mtune=native")
302264
string(APPEND CMAKE_C_FLAGS " -march=native -mtune=native")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# This file will set the onnxruntime_target_platform variable, if applicable.
2+
# onnxruntime_target_platform identifies the platform to compile for.
3+
block(PROPAGATE onnxruntime_target_platform)
4+
5+
unset(onnxruntime_target_platform)
6+
7+
if (MSVC)
8+
if (CMAKE_VS_PLATFORM_NAME)
9+
# Multi-platform generator
10+
set(onnxruntime_target_platform ${CMAKE_VS_PLATFORM_NAME})
11+
else()
12+
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
13+
endif()
14+
15+
if (onnxruntime_target_platform STREQUAL "ARM64" OR
16+
onnxruntime_target_platform STREQUAL "ARM64EC")
17+
# Do nothing. We'll just use the current value of onnxruntime_target_platform.
18+
elseif (onnxruntime_target_platform STREQUAL "ARM" OR
19+
CMAKE_GENERATOR MATCHES "ARM")
20+
set(onnxruntime_target_platform "ARM")
21+
elseif (onnxruntime_target_platform STREQUAL "x64" OR
22+
onnxruntime_target_platform STREQUAL "x86_64" OR
23+
onnxruntime_target_platform STREQUAL "AMD64" OR
24+
CMAKE_GENERATOR MATCHES "Win64")
25+
set(onnxruntime_target_platform "x64")
26+
elseif (onnxruntime_target_platform STREQUAL "Win32" OR
27+
onnxruntime_target_platform STREQUAL "x86" OR
28+
onnxruntime_target_platform STREQUAL "i386" OR
29+
onnxruntime_target_platform STREQUAL "i686")
30+
set(onnxruntime_target_platform "x86")
31+
else()
32+
message(FATAL_ERROR "Unknown target platform: ${onnxruntime_target_platform}")
33+
endif()
34+
elseif(APPLE)
35+
if(DEFINED CMAKE_OSX_ARCHITECTURES)
36+
# We'll only set onnxruntime_target_platform when CMAKE_OSX_ARCHITECTURES specifies a single architecture.
37+
list(LENGTH CMAKE_OSX_ARCHITECTURES CMAKE_OSX_ARCHITECTURES_LEN)
38+
if(CMAKE_OSX_ARCHITECTURES_LEN EQUAL 1)
39+
set(onnxruntime_target_platform ${CMAKE_OSX_ARCHITECTURES})
40+
endif()
41+
else()
42+
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
43+
endif()
44+
else()
45+
#XXX: Sometimes the value of CMAKE_SYSTEM_PROCESSOR is set but it's wrong. For example, if you run an armv7 docker
46+
#image on an aarch64 machine with an aarch64 Ubuntu host OS, in the docker instance cmake may still report
47+
# CMAKE_SYSTEM_PROCESSOR as aarch64 by default. Given compiling this code may need more than 2GB memory, we do not
48+
# support compiling for ARM32 natively(only support cross-compiling), we will ignore this issue for now.
49+
if(NOT CMAKE_SYSTEM_PROCESSOR)
50+
message(WARNING "CMAKE_SYSTEM_PROCESSOR is not set. Please set it in your toolchain cmake file.")
51+
# Try to detect it
52+
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
53+
execute_process(
54+
COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
55+
OUTPUT_VARIABLE GCC_DUMP_MACHINE_OUT
56+
OUTPUT_STRIP_TRAILING_WHITESPACE
57+
ERROR_VARIABLE _err
58+
RESULT_VARIABLE _res
59+
)
60+
if(NOT _res EQUAL 0)
61+
message(SEND_ERROR "Failed to run 'gcc -dumpmachine':\n ${_res}")
62+
endif()
63+
string(REPLACE "-" ";" GCC_DUMP_MACHINE_OUT_LIST "${GCC_DUMP_MACHINE_OUT}")
64+
list(LENGTH GCC_DUMP_MACHINE_OUT_LIST GCC_TRIPLET_LEN)
65+
if(GCC_TRIPLET_LEN EQUAL 4)
66+
list(GET GCC_DUMP_MACHINE_OUT_LIST 0 CMAKE_SYSTEM_PROCESSOR)
67+
message("Setting CMAKE_SYSTEM_PROCESSOR to ${CMAKE_SYSTEM_PROCESSOR}")
68+
endif()
69+
endif()
70+
endif()
71+
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
72+
endif()
73+
74+
if(DEFINED onnxruntime_target_platform)
75+
message(STATUS "onnxruntime_target_platform = ${onnxruntime_target_platform}")
76+
else()
77+
message(WARNING "onnxruntime_target_platform is not set")
78+
endif()
79+
80+
endblock()

tools/ci_build/build.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -879,22 +879,8 @@ def generate_build_tree(
879879
if args.use_snpe:
880880
cmake_args += ["-Donnxruntime_USE_SNPE=ON"]
881881

882-
# Set onnxruntime_USE_KLEIDIAI based on:
883-
# * Default value above is NO.
884-
# * Leave disabled if "no_kleidiai" argument was specified.
885-
# * Enable if the target is Android and args.android_abi contains arm64*
886-
# * Enable for a Windows cross compile build if compile target is an Arm one.
887-
# * Finally enable if platform.machine contains "arm64" and not a WebAssembly build. This should cover the following cases:
888-
# * Linux on Arm
889-
# * MacOs (case must be ignored)
890-
# * TODO Delegate responsibility for Onnxruntime_USE_KLEIDIAI = ON to CMake logic
891882
if not args.no_kleidiai:
892-
if (
893-
(args.android and "arm64" in args.android_abi.lower())
894-
or (is_windows() and (args.arm64 or args.arm64ec or args.arm) and platform.architecture()[0] != "AMD64")
895-
or ("arm64" in platform.machine().lower() and not args.build_wasm)
896-
):
897-
cmake_args += ["-Donnxruntime_USE_KLEIDIAI=ON"]
883+
cmake_args += ["-Donnxruntime_USE_KLEIDIAI=ON"]
898884

899885
if args.enable_arm_neon_nchwc:
900886
cmake_args += ["-Donnxruntime_USE_ARM_NEON_NCHWC=ON"]

0 commit comments

Comments
 (0)