From 88ef80672bf28ab654d11b8ec1c8ec8965ef23fa Mon Sep 17 00:00:00 2001 From: Simon Shillaker <554768+Shillaker@users.noreply.github.com> Date: Sat, 19 Feb 2022 14:37:06 +0100 Subject: [PATCH] Detect CPU information (#231) * Add CPU detection * Tweak script * Remove unused vars --- CMakeLists.txt | 6 ++---- cmake/OptimiseCPU.cmake | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 cmake/OptimiseCPU.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d22398d9..a13f7cd25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,8 @@ elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_compile_options(-fcolor-diagnostics) endif() -# Assume skylake for some easy performance wins -if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") - add_compile_options(-march=skylake -mtune=skylake) -endif () +# Optimise for CPU +include(cmake/OptimiseCPU.cmake) # Top-level CMake config set(CMAKE_CXX_FLAGS "-Wall") diff --git a/cmake/OptimiseCPU.cmake b/cmake/OptimiseCPU.cmake new file mode 100644 index 000000000..14bcd96e1 --- /dev/null +++ b/cmake/OptimiseCPU.cmake @@ -0,0 +1,41 @@ +set(CPU_VENDOR) +set(CPU_FAMILY) +set(CPU_MODEL) +set(CPU_PROPS) + +file(READ "/proc/cpuinfo" _cpuinfo) +string(REGEX REPLACE ".*vendor_id[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1" + CPU_VENDOR "${_cpuinfo}") +string(REGEX REPLACE ".*cpu family[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1" + CPU_FAMILY "${_cpuinfo}") +string(REGEX REPLACE ".*model[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1" + CPU_MODEL "${_cpuinfo}") +string(REGEX REPLACE ".*flags[ \t]*:[ \t]+([^\n]+).*" "\\1" + CPU_PROPS "${_cpuinfo}") + +message("Found CPU: Vendor = ${CPU_VENDOR} Family = ${CPU_FAMILY} Model = ${CPU_MODEL} Props = ${CPU_PROPS}") + +# See this file for an example of extending this list: +# https://github.com/VcDevel/Vc/blob/1.4/cmake/OptimizeForArchitecture.cmake +# See the LLVM source for list of supported arch, e.g. this test: +# https://github.com/llvm/llvm-project/blob/main/clang/test/Driver/x86-march.c + +set(CPU_COMPILE_FLAGS) +if(CPU_VENDOR STREQUAL "GenuineIntel") + if(CPU_FAMILY EQUAL 6) + if(CPU_MODEL EQUAL 78 OR CPU_MODEL EQUAL 94) + # Skylake + set(CPU_COMPILE_FLAGS -march=skylake -mtune=skylake) + elseif(CPU_MODEL EQUAL 58 OR CPU_MODEL EQUAL 62) + # Ivy bridge + set(CPU_COMPILE_FLAGS -march=ivybridge -mtune=ivybridge) + endif() + endif() +endif() + +if(CPU_COMPILE_FLAGS) + message("Setting following CPU-specific compile flags: ${CPU_COMPILE_FLAGS}") + add_compile_options(${CPU_COMPILE_FLAGS}) +else() + message("Could not provide any CPU-specific compile flags") +endif()