Skip to content

Commit

Permalink
Detect CPU information (#231)
Browse files Browse the repository at this point in the history
* Add CPU detection

* Tweak script

* Remove unused vars
  • Loading branch information
Shillaker committed Feb 19, 2022
1 parent 258c63a commit 88ef806
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
41 changes: 41 additions & 0 deletions cmake/OptimiseCPU.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 88ef806

Please sign in to comment.