Skip to content

Commit 88ef806

Browse files
authored
Detect CPU information (#231)
* Add CPU detection * Tweak script * Remove unused vars
1 parent 258c63a commit 88ef806

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
1212
add_compile_options(-fcolor-diagnostics)
1313
endif()
1414

15-
# Assume skylake for some easy performance wins
16-
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
17-
add_compile_options(-march=skylake -mtune=skylake)
18-
endif ()
15+
# Optimise for CPU
16+
include(cmake/OptimiseCPU.cmake)
1917

2018
# Top-level CMake config
2119
set(CMAKE_CXX_FLAGS "-Wall")

cmake/OptimiseCPU.cmake

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
set(CPU_VENDOR)
2+
set(CPU_FAMILY)
3+
set(CPU_MODEL)
4+
set(CPU_PROPS)
5+
6+
file(READ "/proc/cpuinfo" _cpuinfo)
7+
string(REGEX REPLACE ".*vendor_id[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1"
8+
CPU_VENDOR "${_cpuinfo}")
9+
string(REGEX REPLACE ".*cpu family[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1"
10+
CPU_FAMILY "${_cpuinfo}")
11+
string(REGEX REPLACE ".*model[ \t]*:[ \t]+([a-zA-Z0-9_-]+).*" "\\1"
12+
CPU_MODEL "${_cpuinfo}")
13+
string(REGEX REPLACE ".*flags[ \t]*:[ \t]+([^\n]+).*" "\\1"
14+
CPU_PROPS "${_cpuinfo}")
15+
16+
message("Found CPU: Vendor = ${CPU_VENDOR} Family = ${CPU_FAMILY} Model = ${CPU_MODEL} Props = ${CPU_PROPS}")
17+
18+
# See this file for an example of extending this list:
19+
# https://github.com/VcDevel/Vc/blob/1.4/cmake/OptimizeForArchitecture.cmake
20+
# See the LLVM source for list of supported arch, e.g. this test:
21+
# https://github.com/llvm/llvm-project/blob/main/clang/test/Driver/x86-march.c
22+
23+
set(CPU_COMPILE_FLAGS)
24+
if(CPU_VENDOR STREQUAL "GenuineIntel")
25+
if(CPU_FAMILY EQUAL 6)
26+
if(CPU_MODEL EQUAL 78 OR CPU_MODEL EQUAL 94)
27+
# Skylake
28+
set(CPU_COMPILE_FLAGS -march=skylake -mtune=skylake)
29+
elseif(CPU_MODEL EQUAL 58 OR CPU_MODEL EQUAL 62)
30+
# Ivy bridge
31+
set(CPU_COMPILE_FLAGS -march=ivybridge -mtune=ivybridge)
32+
endif()
33+
endif()
34+
endif()
35+
36+
if(CPU_COMPILE_FLAGS)
37+
message("Setting following CPU-specific compile flags: ${CPU_COMPILE_FLAGS}")
38+
add_compile_options(${CPU_COMPILE_FLAGS})
39+
else()
40+
message("Could not provide any CPU-specific compile flags")
41+
endif()

0 commit comments

Comments
 (0)