Skip to content

Commit cb44fc8

Browse files
authored
cmake : fix ARM feature verification (#17170)
* cmake : fix ARM feature verification Use check_cxx_source_compiles to prevent conflicts with the existing GGML_NATIVE detection code. Signed-off-by: Adrien Gallouët <[email protected]> * cmake : unset __ARM_FEATURE when feature is disabled Signed-off-by: Adrien Gallouët <[email protected]> * cmake : fix scope, this is really a macro Signed-off-by: Adrien Gallouët <[email protected]> * arm_neon.h is useless Signed-off-by: Adrien Gallouët <[email protected]> --------- Signed-off-by: Adrien Gallouët <[email protected]>
1 parent cb623de commit cb44fc8

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

ggml/src/ggml-cpu/CMakeLists.txt

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,27 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
145145

146146
include(CheckCXXSourceRuns)
147147

148-
function(check_arm_feature tag code)
148+
macro(check_arm_feature tag feature code)
149149
set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
150150
set(CMAKE_REQUIRED_FLAGS "${ARM_NATIVE_FLAG}+${tag}")
151151
check_cxx_source_runs("${code}" GGML_MACHINE_SUPPORTS_${tag})
152152
if (GGML_MACHINE_SUPPORTS_${tag})
153-
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+${tag}" PARENT_SCOPE)
153+
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+${tag}")
154154
else()
155155
set(CMAKE_REQUIRED_FLAGS "${ARM_NATIVE_FLAG}+no${tag}")
156156
check_cxx_source_compiles("int main() { return 0; }" GGML_MACHINE_SUPPORTS_no${tag})
157157
if (GGML_MACHINE_SUPPORTS_no${tag})
158-
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+no${tag}" PARENT_SCOPE)
158+
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+no${tag}")
159+
list(APPEND ARCH_FLAGS -U__ARM_FEATURE_${feature})
159160
endif()
160161
endif()
161162
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
162-
endfunction()
163+
endmacro()
163164

164-
check_arm_feature(dotprod "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vdotq_s32(_s, _a, _b); return 0; }")
165-
check_arm_feature(i8mm "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vmmlaq_s32(_s, _a, _b); return 0; }")
166-
check_arm_feature(sve "#include <arm_sve.h>\nint main() { svfloat32_t _a, _b; volatile svfloat32_t _c = svadd_f32_z(svptrue_b8(), _a, _b); return 0; }")
167-
check_arm_feature(sme "#include <arm_sme.h>\n__arm_locally_streaming int main() { __asm__ volatile(\"smstart; smstop;\"); return 0; }")
165+
check_arm_feature(dotprod DOTPROD "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vdotq_s32(_s, _a, _b); return 0; }")
166+
check_arm_feature(i8mm MATMUL_INT8 "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vmmlaq_s32(_s, _a, _b); return 0; }")
167+
check_arm_feature(sve SVE "#include <arm_sve.h>\nint main() { svfloat32_t _a, _b; volatile svfloat32_t _c = svadd_f32_z(svptrue_b8(), _a, _b); return 0; }")
168+
check_arm_feature(sme SME "#include <arm_sme.h>\n__arm_locally_streaming int main() { __asm__ volatile(\"smstart; smstop;\"); return 0; }")
168169

169170
list(APPEND ARCH_FLAGS "${ARM_NATIVE_FLAG}${ARM_NATIVE_FLAG_FIX}")
170171
else()
@@ -216,35 +217,27 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
216217
endif()
217218
endif()
218219

219-
# show enabled features
220-
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
221-
set(FEAT_INPUT_FILE "NUL")
222-
else()
223-
set(FEAT_INPUT_FILE "/dev/null")
224-
endif()
220+
message(STATUS "Checking for ARM features using flags:")
221+
foreach(flag IN LISTS ARCH_FLAGS)
222+
message(STATUS " ${flag}")
223+
endforeach()
225224

226-
execute_process(
227-
COMMAND ${CMAKE_C_COMPILER} ${ARCH_FLAGS} -dM -E -
228-
INPUT_FILE ${FEAT_INPUT_FILE}
229-
OUTPUT_VARIABLE ARM_FEATURE
230-
RESULT_VARIABLE ARM_FEATURE_RESULT
231-
)
232-
if (ARM_FEATURE_RESULT)
233-
message(WARNING "Failed to get ARM features")
234-
else()
235-
foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC SME)
236-
string(FIND "${ARM_FEATURE}" "__ARM_FEATURE_${feature} 1" feature_pos)
237-
if (NOT ${feature_pos} EQUAL -1)
238-
# Special handling for MATMUL_INT8 when machine doesn't support i8mm
239-
if ("${feature}" STREQUAL "MATMUL_INT8" AND GGML_MACHINE_SUPPORTS_noi8mm)
240-
message(STATUS "ARM feature ${feature} detected but unsetting due to machine not supporting i8mm")
241-
list(APPEND ARCH_FLAGS -U__ARM_FEATURE_MATMUL_INT8)
242-
else()
243-
message(STATUS "ARM feature ${feature} enabled")
244-
endif()
245-
endif()
246-
endforeach()
247-
endif()
225+
include(CheckCXXSourceCompiles)
226+
set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
227+
set(CMAKE_REQUIRED_FLAGS "${ARCH_FLAGS}")
228+
foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC SME)
229+
set(ARM_FEATURE "HAVE_${feature}")
230+
check_cxx_source_compiles(
231+
"
232+
#if !defined(__ARM_FEATURE_${feature})
233+
# error \"Feature ${feature} is not defined\"
234+
#endif
235+
int main() { return 0; }
236+
"
237+
${ARM_FEATURE}
238+
)
239+
endforeach()
240+
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
248241
endif()
249242
elseif (GGML_SYSTEM_ARCH STREQUAL "x86")
250243
message(STATUS "x86 detected")

0 commit comments

Comments
 (0)