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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
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})
26 string(REPLACE ":" ";" l ${l})
29 string(STRIP ${name} name)
30 string(STRIP ${value} value)
31 if(${name} STREQUAL "CPU implementer")
32 set(CPU_IMPLEMENTER ${value})
34 if(${name} STREQUAL "CPU part")
35 set(CPU_PART ${value})
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)
44 set(VPP_LOG2_CACHE_LINE_SIZE 7)
47 set(VPP_LOG2_CACHE_LINE_SIZE 6)
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}")
52 set(VPP_LOG2_CACHE_LINE_SIZE 6)
55 set(VPP_LOG2_CACHE_LINE_SIZE ${VPP_LOG2_CACHE_LINE_SIZE}
56 CACHE STRING "Target CPU cache line size (power of 2)")
58 ##############################################################################
59 # Gnu Assembler AVX-512 bug detection
60 # - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465
61 ##############################################################################
62 if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
63 if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
64 set(pfx ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/gas_avx512_bug_test)
65 file(WRITE ${pfx}.s "vmovaps 0x40(,%rax), %zmm0\n")
66 execute_process(COMMAND ${CMAKE_C_COMPILER} -c ${pfx}.s -o ${pfx}.o)
67 execute_process(COMMAND objdump -s ${pfx}.o OUTPUT_VARIABLE _output)
68 if (NOT _output MATCHES "62f17c48 28040540 000000")
69 set(GNU_ASSEMBLER_AVX512_BUG 1)
74 ##############################################################################
75 # CPU optimizations and multiarch support
76 ##############################################################################
77 macro(add_vpp_march_variant v)
78 cmake_parse_arguments(ARG
88 foreach(f ${ARG_FLAGS})
89 string(APPEND fs " ${f}")
90 string(REGEX REPLACE "[-=+]" "_" sfx ${f})
91 if(NOT DEFINED compiler_flag${sfx})
92 check_c_compiler_flag(${f} compiler_flag${sfx})
94 if(NOT compiler_flag${sfx})
99 string(APPEND fs " -DCLIB_N_PREFETCHES=${ARG_N_PREFETCHES}")
102 string(TOUPPER ${v} uv)
104 option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." OFF)
106 option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." ON)
108 if (VPP_MARCH_VARIANT_${uv})
109 list(APPEND MARCH_VARIANTS "${v}\;${fs}")
115 if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
116 set(CMAKE_C_FLAGS "-march=corei7 -mtune=corei7-avx ${CMAKE_C_FLAGS}")
118 add_vpp_march_variant(hsw
119 FLAGS -march=haswell -mtune=haswell
122 add_vpp_march_variant(trm
123 FLAGS -march=tremont -mtune=tremont
127 if (GNU_ASSEMBLER_AVX512_BUG)
128 message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
130 add_vpp_march_variant(skx
131 FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256
134 add_vpp_march_variant(icl
135 FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512
138 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
139 set(CMAKE_C_FLAGS "-march=armv8-a+crc ${CMAKE_C_FLAGS}")
141 add_vpp_march_variant(qdf24xx
142 FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx
147 add_vpp_march_variant(octeontx2
148 FLAGS -march=armv8.2-a+crc+crypto+lse
152 add_vpp_march_variant(thunderx2t99
153 FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99
157 add_vpp_march_variant(cortexa72
158 FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72
162 add_vpp_march_variant(neoversen1
163 FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1
168 macro(vpp_library_set_multiarch_sources lib)
169 cmake_parse_arguments(ARG
176 foreach(V ${MARCH_VARIANTS})
177 list(GET V 0 VARIANT)
178 list(GET V 1 VARIANT_FLAGS)
179 set(l ${lib}_${VARIANT})
180 add_library(${l} OBJECT ${ARG_SOURCES})
182 add_dependencies(${l} ${ARG_DEPENDS})
184 set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
185 target_compile_options(${l} PUBLIC "-DCLIB_MARCH_VARIANT=${VARIANT}")
186 separate_arguments(VARIANT_FLAGS)
187 target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
188 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)