From ef799a9b423babc4e3ece31b8d8f3d84a94be0fa Mon Sep 17 00:00:00 2001 From: Xavier Arteaga Date: Wed, 18 Nov 2020 11:02:14 +0100 Subject: [PATCH] Avoid GCC native architecture for AVX512 CPUs when AVX512 is disabled --- CMakeLists.txt | 4 ++-- cmake/modules/FindSSE.cmake | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf0284dd6..dd15dff7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -406,8 +406,8 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") endif (HAVE_FMA) if (HAVE_AVX512) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${GCC_ARCH} -mavx512f -mavx512cd -mavx512bw -mavx512dq -DLV_HAVE_AVX512") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${GCC_ARCH} -mavx512f -mavx512cd -mavx512bw -mavx512dq -DLV_HAVE_AVX512") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -mavx512cd -mavx512bw -mavx512dq -DLV_HAVE_AVX512") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx512f -mavx512cd -mavx512bw -mavx512dq -DLV_HAVE_AVX512") endif(HAVE_AVX512) if(NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") diff --git a/cmake/modules/FindSSE.cmake b/cmake/modules/FindSSE.cmake index 24fc23662..846cd8aa7 100644 --- a/cmake/modules/FindSSE.cmake +++ b/cmake/modules/FindSSE.cmake @@ -168,6 +168,20 @@ if (ENABLE_SSE) if (HAVE_AVX512) message(STATUS "AVX512 is enabled - target CPU must support it") endif() + elseif (${GCC_ARCH} MATCHES "native" AND CMAKE_CXX_COMPILER_ID MATCHES "GNU") + # When GCC flag -march=native and the CPU supports AVX512 (skylake-avx512 architecture), GCC uses AVX512 instructions + # automatically, independently of the rest of flags. + + # Get the CPU architecture + execute_process(COMMAND ${CMAKE_C_COMPILER} -march=native -Q --help=target + OUTPUT_VARIABLE DETECT_SKYLAKE_AVX512) + + # Check if the native architecture matches with skylake-avx512 + if (${DETECT_SKYLAKE_AVX512} MATCHES "march=.*skylake-avx512") + # Force skylake architecture without AVX512 + set(GCC_ARCH "skylake") + message(STATUS "This is a skylake-avx512 CPU, as AVX512 was disabled the architecture will be set to skylake") + endif (${IS_SKYLAKE_AVX512}) endif()