Skip to content

Commit 5924aed

Browse files
committed
ggml-cpu : inspect -march and -mcpu to found the CPU
Signed-off-by: Adrien Gallouët <[email protected]>
1 parent bacddc0 commit 5924aed

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

ggml/src/ggml-cpu/CMakeLists.txt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,36 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
126126
)
127127
if (NOT ARM_MCPU_RESULT)
128128
string(REGEX MATCH "-mcpu=[^ ']+" ARM_MCPU_FLAG "${ARM_MCPU}")
129+
string(REGEX MATCH "-march=[^ ']+" ARM_MARCH_FLAG "${ARM_MCPU}")
130+
131+
# on some old GCC we need to read -march=
132+
if (ARM_MARCH_FLAG AND NOT "${ARM_MARCH_FLAG}" STREQUAL "-march=native")
133+
set(ARM_NATIVE_FLAG "${ARM_MARCH_FLAG}")
134+
elseif(ARM_MCPU_FLAG AND NOT "${ARM_MCPU_FLAG}" STREQUAL "-mcpu=native")
135+
set(ARM_NATIVE_FLAG "${ARM_MCPU_FLAG}")
136+
endif()
129137
endif()
130-
if ("${ARM_MCPU_FLAG}" STREQUAL "")
131-
set(ARM_MCPU_FLAG -mcpu=native)
132-
message(STATUS "ARM -mcpu not found, -mcpu=native will be used")
138+
139+
if ("${ARM_NATIVE_FLAG}" STREQUAL "")
140+
set(ARM_NATIVE_FLAG -mcpu=native)
141+
message(WARNING "ARM -march/-mcpu not found, -mcpu=native will be used")
142+
else()
143+
message(STATUS "ARM detected flags: ${ARM_NATIVE_FLAG}")
133144
endif()
134145

135146
include(CheckCXXSourceRuns)
136147

137148
function(check_arm_feature tag code)
138149
set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
139-
set(CMAKE_REQUIRED_FLAGS "${ARM_MCPU_FLAG}+${tag}")
150+
set(CMAKE_REQUIRED_FLAGS "${ARM_NATIVE_FLAG}+${tag}")
140151
check_cxx_source_runs("${code}" GGML_MACHINE_SUPPORTS_${tag})
141152
if (GGML_MACHINE_SUPPORTS_${tag})
142-
set(ARM_MCPU_FLAG_FIX "${ARM_MCPU_FLAG_FIX}+${tag}" PARENT_SCOPE)
153+
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+${tag}" PARENT_SCOPE)
143154
else()
144-
set(CMAKE_REQUIRED_FLAGS "${ARM_MCPU_FLAG}+no${tag}")
155+
set(CMAKE_REQUIRED_FLAGS "${ARM_NATIVE_FLAG}+no${tag}")
145156
check_cxx_source_compiles("int main() { return 0; }" GGML_MACHINE_SUPPORTS_no${tag})
146157
if (GGML_MACHINE_SUPPORTS_no${tag})
147-
set(ARM_MCPU_FLAG_FIX "${ARM_MCPU_FLAG_FIX}+no${tag}" PARENT_SCOPE)
158+
set(ARM_NATIVE_FLAG_FIX "${ARM_NATIVE_FLAG_FIX}+no${tag}" PARENT_SCOPE)
148159
endif()
149160
endif()
150161
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
@@ -155,7 +166,7 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
155166
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; }")
156167
check_arm_feature(sme "#include <arm_sme.h>\n__arm_locally_streaming int main() { __asm__ volatile(\"smstart; smstop;\"); return 0; }")
157168

158-
list(APPEND ARCH_FLAGS "${ARM_MCPU_FLAG}${ARM_MCPU_FLAG_FIX}")
169+
list(APPEND ARCH_FLAGS "${ARM_NATIVE_FLAG}${ARM_NATIVE_FLAG_FIX}")
159170
else()
160171
if (GGML_CPU_ARM_ARCH)
161172
list(APPEND ARCH_FLAGS -march=${GGML_CPU_ARM_ARCH})

0 commit comments

Comments
 (0)