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 macro(set_log2_cacheline_size var n)
20 message(FATAL_ERROR "Cacheline size ${n} not supported")
24 ##############################################################################
26 ##############################################################################
27 if(DEFINED VPP_CACHE_LINE_SIZE)
28 # Cache line size assigned via cmake args
29 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
30 set(VPP_CACHE_LINE_SIZE 128)
32 set(VPP_CACHE_LINE_SIZE 64)
35 set(VPP_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE}
36 CACHE STRING "Target CPU cache line size")
38 set_log2_cacheline_size(VPP_LOG2_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE})
40 ##############################################################################
41 # Gnu Assembler AVX-512 bug detection
42 # - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465
43 ##############################################################################
44 if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
45 if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
46 set(pfx ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/gas_avx512_bug_test)
47 file(WRITE ${pfx}.s "vmovaps 0x40(,%rax), %zmm0\n")
48 execute_process(COMMAND ${CMAKE_C_COMPILER} -c ${pfx}.s -o ${pfx}.o)
49 execute_process(COMMAND objdump -s ${pfx}.o OUTPUT_VARIABLE _output)
50 if (NOT _output MATCHES "62f17c48 28040540 000000")
51 set(GNU_ASSEMBLER_AVX512_BUG 1)
56 ##############################################################################
57 # CPU optimizations and multiarch support
58 ##############################################################################
60 option(VPP_BUILD_NATIVE_ONLY "Build only for native CPU." OFF)
62 macro(add_vpp_march_variant v)
63 cmake_parse_arguments(ARG
65 "N_PREFETCHES;CACHE_PREFETCH_BYTES"
73 foreach(f ${ARG_FLAGS})
74 string(APPEND fs " ${f}")
75 string(REGEX REPLACE "[-=+]" "_" sfx ${f})
76 if(NOT DEFINED compiler_flag${sfx})
77 check_c_compiler_flag(${f} compiler_flag${sfx})
79 if(NOT compiler_flag${sfx})
84 string(APPEND fs " -DCLIB_N_PREFETCHES=${ARG_N_PREFETCHES}")
86 if(ARG_CACHE_PREFETCH_BYTES)
87 set_log2_cacheline_size(log2 ${ARG_CACHE_PREFETCH_BYTES})
88 string(APPEND fs " -DCLIB_LOG2_CACHE_PREFETCH_BYTES=${log2}")
91 string(TOUPPER ${v} uv)
93 option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." OFF)
95 option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." ON)
97 if (VPP_MARCH_VARIANT_${uv})
98 list(APPEND MARCH_VARIANTS "${v}\;${fs}")
99 list(APPEND MARCH_VARIANTS_NAMES "${v}")
101 list(APPEND MARCH_VARIANTS_DISABLED "${v}\;${fs}")
107 if(VPP_BUILD_NATIVE_ONLY)
108 set(VPP_BUILD_NATIVE_ARCH "native" CACHE STRING "native CPU -march= value.")
109 set(VPP_DEFAULT_MARCH_FLAGS -march=${VPP_BUILD_NATIVE_ARCH})
110 if(VPP_BUILD_NATIVE_ONLY)
111 check_c_compiler_flag(${VPP_DEFAULT_MARCH_FLAGS} compiler_flag_march)
112 if(NOT compiler_flag_march)
113 message(FATAL_ERROR "Native-only build with ${VPP_DEFAULT_MARCH_FLAGS} is not supported by compiler")
116 set(MARCH_VARIANTS_NAMES "native-only")
117 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
118 set(VPP_DEFAULT_MARCH_FLAGS -march=corei7 -mtune=corei7-avx)
120 add_vpp_march_variant(hsw
121 FLAGS -march=haswell -mtune=haswell
124 add_vpp_march_variant(trm
125 FLAGS -march=tremont -mtune=tremont
129 add_vpp_march_variant(adl
130 FLAGS -march=alderlake -mtune=alderlake -mprefer-vector-width=256
134 add_vpp_march_variant(scalar
135 FLAGS -march=core2 -mno-mmx -mno-sse
139 add_vpp_march_variant(znver3
140 FLAGS -march=znver3 -mtune=znver3 -mprefer-vector-width=256
144 if (GNU_ASSEMBLER_AVX512_BUG)
145 message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
147 add_vpp_march_variant(skx
148 FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256
151 add_vpp_march_variant(icl
152 FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512
155 add_vpp_march_variant(spr
156 FLAGS -march=sapphirerapids -mtune=sapphirerapids -mprefer-vector-width=512
160 add_vpp_march_variant(znver4
161 FLAGS -march=znver4 -mtune=znver4 -mprefer-vector-width=512
165 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
166 set(VPP_DEFAULT_MARCH_FLAGS -march=armv8-a+crc)
168 add_vpp_march_variant(qdf24xx
169 FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx
171 CACHE_PREFETCH_BYTES 64
175 add_vpp_march_variant(octeontx2
176 FLAGS -march=armv8.2-a+crc+crypto+lse
180 add_vpp_march_variant(thunderx2t99
181 FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99
183 CACHE_PREFETCH_BYTES 64
186 add_vpp_march_variant(cortexa72
187 FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72
189 CACHE_PREFETCH_BYTES 64
192 add_vpp_march_variant(neoversen1
193 FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1
195 CACHE_PREFETCH_BYTES 64
197 add_vpp_march_variant(neoversen2
198 FLAGS -march=armv9-a+crypto -mtune=neoverse-n2
200 CACHE_PREFETCH_BYTES 64
205 macro(vpp_library_set_multiarch_sources lib)
206 cmake_parse_arguments(ARG
209 "SOURCES;DEPENDS;FORCE_ON"
213 set(VARIANTS "${MARCH_VARIANTS}")
216 foreach(F ${ARG_FORCE_ON})
217 foreach(V ${MARCH_VARIANTS_DISABLED})
218 list(GET V 0 VARIANT)
219 if (VARIANT STREQUAL F)
220 list(GET V 1 VARIANT_FLAGS)
221 list(APPEND VARIANTS "${VARIANT}\;${VARIANT_FLAGS}")
227 foreach(V ${VARIANTS})
228 list(GET V 0 VARIANT)
229 list(GET V 1 VARIANT_FLAGS)
230 set(l ${lib}_${VARIANT})
231 add_library(${l} OBJECT ${ARG_SOURCES})
233 add_dependencies(${l} ${ARG_DEPENDS})
235 set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
236 target_compile_definitions(${l} PUBLIC CLIB_MARCH_VARIANT=${VARIANT})
237 separate_arguments(VARIANT_FLAGS)
238 target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
239 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)