e586ecc523c70f00d810ca92caa3e7d0cd912afa
[vpp.git] / src / cmake / cpu.cmake
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 ##############################################################################
15 # Cache line size detection
16 ##############################################################################
17 if(CMAKE_CROSSCOMPILING)
18   message(STATUS "Cross-compiling - cache line size detection disabled")
19   set(VPP_LOG2_CACHE_LINE_SIZE 6)
20 elseif(DEFINED VPP_LOG2_CACHE_LINE_SIZE)
21   # Cache line size assigned via cmake args
22 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
23   file(READ "/proc/cpuinfo" cpuinfo)
24   string(REPLACE "\n" ";" cpuinfo ${cpuinfo})
25   foreach(l ${cpuinfo})
26     string(REPLACE ":" ";" l ${l})
27     list(GET l 0 name)
28     list(GET l 1 value)
29     string(STRIP ${name} name)
30     string(STRIP ${value} value)
31     if(${name} STREQUAL "CPU implementer")
32       set(CPU_IMPLEMENTER ${value})
33     endif()
34     if(${name} STREQUAL "CPU part")
35       set(CPU_PART ${value})
36     endif()
37   endforeach()
38   # Implementer 0x43 - Cavium
39   #  Part 0x0af - ThunderX2 is 64B, rest all are 128B
40   if (${CPU_IMPLEMENTER} STREQUAL "0x43")
41     if (${CPU_PART} STREQUAL "0x0af")
42       set(VPP_LOG2_CACHE_LINE_SIZE 6)
43     else()
44       set(VPP_LOG2_CACHE_LINE_SIZE 7)
45     endif()
46   else()
47       set(VPP_LOG2_CACHE_LINE_SIZE 6)
48   endif()
49   math(EXPR VPP_CACHE_LINE_SIZE "1 << ${VPP_LOG2_CACHE_LINE_SIZE}")
50   message(STATUS "ARM AArch64 CPU implementer ${CPU_IMPLEMENTER} part ${CPU_PART} cacheline size ${VPP_CACHE_LINE_SIZE}")
51 else()
52   set(VPP_LOG2_CACHE_LINE_SIZE 6)
53 endif()
54
55 set(VPP_LOG2_CACHE_LINE_SIZE ${VPP_LOG2_CACHE_LINE_SIZE}
56     CACHE STRING "Target CPU cache line size (power of 2)")
57
58 ##############################################################################
59 # CPU optimizations and multiarch support
60 ##############################################################################
61 if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
62   set(CMAKE_C_FLAGS "-march=corei7 -mtune=corei7-avx ${CMAKE_C_FLAGS}")
63   check_c_compiler_flag("-march=core-avx2" compiler_flag_march_core_avx2)
64   if(compiler_flag_march_core_avx2)
65     list(APPEND MARCH_VARIANTS "avx2\;-march=core-avx2 -mtune=core-avx2")
66   endif()
67   check_c_compiler_flag("-march=skylake-avx512" compiler_flag_march_skylake_avx512)
68   if(compiler_flag_march_skylake_avx512)
69     list(APPEND MARCH_VARIANTS "avx512\;-march=skylake-avx512 -mtune=skylake-avx512")
70   endif()
71 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
72   set(CMAKE_C_FLAGS "-march=armv8-a+crc ${CMAKE_C_FLAGS}")
73   check_c_compiler_flag("-march=armv8-a+crc+crypto -mtune=qdf24xx" compiler_flag_march_core_qdf24xx)
74   if(compiler_flag_march_core_qdf24xx)
75     list(APPEND MARCH_VARIANTS "qdf24xx\;-march=armv8-a+crc+crypto -DCLIB_N_PREFETCHES=8")
76   endif()
77   check_c_compiler_flag("-march=armv8.1-a+crc+crypto -mtune=thunderx2t99" compiler_flag_march_thunderx2t99)
78   if(compiler_flag_march_thunderx2t99)
79     if (CMAKE_C_COMPILER_VERSION VERSION_GREATER 7.3)
80       list(APPEND MARCH_VARIANTS "thunderx2t99\;-march=armv8.1-a+crc+crypto -mtune=thunderx2t99 -DCLIB_N_PREFETCHES=8")
81     else()
82       list(APPEND MARCH_VARIANTS "thunderx2t99\;-march=armv8.1-a+crc+crypto -DCLIB_N_PREFETCHES=8")
83     endif()
84   endif()
85   check_c_compiler_flag("-march=armv8-a+crc+crypto -mtune=cortex-a72" compiler_flag_march_cortexa72)
86   if(compiler_flag_march_cortexa72)
87     list(APPEND MARCH_VARIANTS "cortexa72\;-march=armv8-a+crc+crypto -mtune=cortex-a72 -DCLIB_N_PREFETCHES=6")
88   endif()
89 endif()
90
91 macro(vpp_library_set_multiarch_sources lib)
92   foreach(V ${MARCH_VARIANTS})
93     list(GET V 0 VARIANT)
94     list(GET V 1 VARIANT_FLAGS)
95     set(l ${lib}_${VARIANT})
96     add_library(${l} OBJECT ${ARGN})
97     set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
98     target_compile_options(${l} PUBLIC "-DCLIB_MARCH_VARIANT=${VARIANT}")
99     separate_arguments(VARIANT_FLAGS)
100     target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
101     target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)
102   endforeach()
103 endmacro()
104