From e7edabec51377e1ced5f84b999b1e1174fea439a Mon Sep 17 00:00:00 2001 From: Joachim Schurig Date: Thu, 5 Oct 2023 16:25:56 +0200 Subject: [PATCH 1/2] test availability of -march=native clang does not support -march=native on all platforms. Therefore do a test compile if -march=native is requested and the compiler is set to clang. If the test fails, replace -march=native with -mtune=native -mcpu=native (which should be supported everywhere) --- CMakeLists.txt | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a8a25fe6..d41d776a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,7 +358,29 @@ else(MSVC) --post-js ${CMAKE_SOURCE_DIR}/wasm/post-module.js \ --shell-file ${CMAKE_SOURCE_DIR}/wasm/custom_shell.html") else(COMPILE_WASM) - set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_GCC_FLAGS} -fPIC ${DISABLE_GLOBALLY} -march=${BUILD_ARCH} ${INTRINSICS} ${BUILD_WIDTH}") + if (BUILD_ARCH STREQUAL "native" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # clang does not support -march=native on all platforms + set(TEST_FILE_NAME "test_march.c") + set(TEST_FILES_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_march_dir") + file(MAKE_DIRECTORY "${TEST_FILES_DIR}") + FILE(WRITE "${TEST_FILES_DIR}/${TEST_FILE_NAME}" "int main() { return 0; }") + EXECUTE_PROCESS( + WORKING_DIRECTORY "${TEST_FILES_DIR}" + COMMAND clang -march=native "${TEST_FILE_NAME}" -o test + RESULT_VARIABLE MARCH_RESULT + ERROR_QUIET + ) + FILE(REMOVE_RECURSE "${TEST_FILES_DIR}") + if (MARCH_RESULT EQUAL 0) + set(BUILD_FOR "-march=native") + else() + set(BUILD_FOR "-mtune=native -mcpu=native") + endif() + else() + set(BUILD_FOR "-march=${BUILD_ARCH}") + endif() + + set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_GCC_FLAGS} -fPIC ${DISABLE_GLOBALLY} ${BUILD_FOR} ${INTRINSICS} ${BUILD_WIDTH}") set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${BUILD_WIDTH} -funroll-loops") set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g ${CMAKE_RDYNAMIC_FLAG}") set(CMAKE_CXX_FLAGS_SLIM "-O3 ${BUILD_WIDTH} -funroll-loops -DNDEBUG") From 6328c80066acd6e44145087dd66e0fbf09b2dc4d Mon Sep 17 00:00:00 2001 From: Joachim Schurig Date: Thu, 5 Oct 2023 16:35:11 +0200 Subject: [PATCH 2/2] test availability of -march=native for C as well --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d41d776a5..fdd759c8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -390,7 +390,7 @@ else(MSVC) set(CMAKE_CXX_FLAGS_PROFUSE "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fprofile-use -fprofile-correction") # these need to be set separately - set(CMAKE_C_FLAGS "-pthread ${CMAKE_GCC_FLAGS} -fPIC ${DISABLE_GLOBALLY} -march=${BUILD_ARCH} ${INTRINSICS}") + set(CMAKE_C_FLAGS "-pthread ${CMAKE_GCC_FLAGS} -fPIC ${DISABLE_GLOBALLY} ${BUILD_FOR} ${INTRINSICS}") set(CMAKE_C_FLAGS_RELEASE "-O3 ${BUILD_WIDTH} -funroll-loops") set(CMAKE_C_FLAGS_DEBUG "-O0 -g ${CMAKE_RDYNAMIC_FLAG}") set(CMAKE_C_FLAGS_SLIM "-O3 ${BUILD_WIDTH} -funroll-loops -DNDEBUG")