build: expose VPP_PLATFORM to out-of-tree plugins
[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 macro(set_log2_cacheline_size var n)
15   if(${n} EQUAL 128)
16     set(${var} 7)
17   elseif(${n} EQUAL 64)
18     set(${var} 6)
19   else()
20      message(FATAL_ERROR "Cacheline size ${n} not supported")
21   endif()
22 endmacro()
23
24 ##############################################################################
25 # Platform selection
26 ##############################################################################
27
28 if(DEFINED VPP_PLATFORM AND VPP_PLATFORM STREQUAL "default")
29   unset(VPP_PLATFORM)
30   unset(VPP_PLATFORM CACHE)
31   set(VPP_PLATFORM_NAME "default")
32 elseif(DEFINED VPP_PLATFORM)
33         set(platform_file ${CMAKE_CURRENT_LIST_DIR}/platform/${VPP_PLATFORM}.cmake)
34   if(NOT EXISTS ${platform_file})
35      message(FATAL_ERROR "unknown platform ${VPP_PLATFORM}")
36   endif()
37   include(${platform_file})
38   set(VPP_PLATFORM_NAME ${VPP_PLATFORM})
39 else()
40   set(VPP_PLATFORM_NAME "default")
41 endif()
42
43 if (DEFINED VPP_PLATFORM_C_COMPILER_NAMES)
44   set(CMAKE_C_COMPILER_NAMES ${VPP_PLATFORM_C_COMPILER_NAME})
45 else()
46   set(CMAKE_C_COMPILER_NAMES clang gcc cc)
47 endif()
48
49 ##############################################################################
50 # Cache line size
51 ##############################################################################
52
53 if(DEFINED VPP_PLATFORM_CACHE_LINE_SIZE)
54   set(VPP_CACHE_LINE_SIZE ${VPP_PLATFORM_CACHE_LINE_SIZE})
55 else()
56   if(DEFINED VPP_CACHE_LINE_SIZE)
57     # Cache line size assigned via cmake args
58   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
59     set(VPP_CACHE_LINE_SIZE 128)
60   else()
61     set(VPP_CACHE_LINE_SIZE 64)
62   endif()
63
64   set(VPP_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE}
65       CACHE STRING "Target CPU cache line size")
66 endif()
67
68 set_log2_cacheline_size(VPP_LOG2_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE})
69
70 ##############################################################################
71 # Gnu Assembler AVX-512 bug detection
72 # - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465
73 ##############################################################################
74 if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
75   if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
76     set(pfx ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/gas_avx512_bug_test)
77     file(WRITE ${pfx}.s "vmovaps 0x40(,%rax), %zmm0\n")
78     execute_process(COMMAND ${CMAKE_C_COMPILER} -c ${pfx}.s -o ${pfx}.o)
79     execute_process(COMMAND objdump -s ${pfx}.o OUTPUT_VARIABLE _output)
80     if (NOT _output MATCHES "62f17c48 28040540 000000")
81       set(GNU_ASSEMBLER_AVX512_BUG 1)
82     endif()
83   endif()
84 endif()
85
86 ##############################################################################
87 # CPU optimizations and multiarch support
88 ##############################################################################
89
90 if(NOT DEFINED VPP_PLATFORM)
91   option(VPP_BUILD_NATIVE_ONLY "Build only for native CPU." OFF)
92 endif()
93
94 macro(add_vpp_march_variant v)
95   cmake_parse_arguments(ARG
96     "OFF"
97     "N_PREFETCHES;CACHE_PREFETCH_BYTES"
98     "FLAGS"
99     ${ARGN}
100   )
101
102   if(ARG_FLAGS)
103     set(flags_ok 1)
104     set(fs "")
105     foreach(f ${ARG_FLAGS})
106       string(APPEND fs " ${f}")
107       string(REGEX REPLACE "[-=+]" "_" sfx ${f})
108       if(NOT DEFINED compiler_flag${sfx})
109         check_c_compiler_flag(${f} compiler_flag${sfx})
110       endif()
111       if(NOT compiler_flag${sfx})
112         unset(flags_ok)
113       endif()
114     endforeach()
115     if(ARG_N_PREFETCHES)
116       string(APPEND fs " -DCLIB_N_PREFETCHES=${ARG_N_PREFETCHES}")
117     endif()
118     if(ARG_CACHE_PREFETCH_BYTES)
119       set_log2_cacheline_size(log2 ${ARG_CACHE_PREFETCH_BYTES})
120       string(APPEND fs " -DCLIB_LOG2_CACHE_PREFETCH_BYTES=${log2}")
121     endif()
122     if(flags_ok)
123       string(TOUPPER ${v} uv)
124       if(ARG_OFF)
125         option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." OFF)
126       else()
127         option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." ON)
128       endif()
129       if (VPP_MARCH_VARIANT_${uv})
130         list(APPEND MARCH_VARIANTS "${v}\;${fs}")
131         list(APPEND MARCH_VARIANTS_NAMES "${v}")
132       else()
133         list(APPEND MARCH_VARIANTS_DISABLED "${v}\;${fs}")
134       endif()
135     endif()
136   endif()
137 endmacro()
138
139 if(DEFINED VPP_PLATFORM)
140   if(DEFINED VPP_PLATFORM_MARCH_FLAGS)
141      set(VPP_DEFAULT_MARCH_FLAGS ${VPP_PLATFORM_MARCH_FLAGS})
142      check_c_compiler_flag(${VPP_DEFAULT_MARCH_FLAGS} compiler_flag_march)
143      if(NOT compiler_flag_march)
144        message(FATAL_ERROR "platform build with ${VPP_DEFAULT_MARCH_FLAGS} is not supported by compiler")
145      endif()
146   else()
147      set(VPP_DEFAULT_MARCH_FLAGS "")
148   endif()
149   set(MARCH_VARIANTS_NAMES "platform-only")
150 elseif(VPP_BUILD_NATIVE_ONLY)
151   set(VPP_BUILD_NATIVE_ARCH "native" CACHE STRING "native CPU -march= value.")
152   set(VPP_DEFAULT_MARCH_FLAGS -march=${VPP_BUILD_NATIVE_ARCH})
153   check_c_compiler_flag(${VPP_DEFAULT_MARCH_FLAGS} compiler_flag_march)
154   if(NOT compiler_flag_march)
155     message(FATAL_ERROR "Native-only build with ${VPP_DEFAULT_MARCH_FLAGS} is not supported by compiler")
156   endif()
157   set(MARCH_VARIANTS_NAMES "native-only")
158 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
159   set(VPP_DEFAULT_MARCH_FLAGS -march=corei7 -mtune=corei7-avx)
160
161   add_vpp_march_variant(hsw
162     FLAGS -march=haswell -mtune=haswell
163   )
164
165   add_vpp_march_variant(trm
166     FLAGS -march=tremont -mtune=tremont
167     OFF
168   )
169
170   add_vpp_march_variant(adl
171     FLAGS -march=alderlake -mtune=alderlake -mprefer-vector-width=256
172     OFF
173   )
174
175   add_vpp_march_variant(scalar
176     FLAGS -march=core2 -mno-mmx -mno-sse
177     OFF
178   )
179
180   add_vpp_march_variant(znver3
181     FLAGS -march=znver3 -mtune=znver3 -mprefer-vector-width=256
182     OFF
183   )
184
185   if (GNU_ASSEMBLER_AVX512_BUG)
186      message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
187   else()
188     add_vpp_march_variant(skx
189       FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256
190     )
191
192     add_vpp_march_variant(icl
193       FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512
194     )
195
196     add_vpp_march_variant(spr
197       FLAGS -march=sapphirerapids -mtune=sapphirerapids -mprefer-vector-width=512
198       OFF
199     )
200
201     add_vpp_march_variant(znver4
202       FLAGS -march=znver4 -mtune=znver4 -mprefer-vector-width=512
203       OFF
204     )
205   endif()
206 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
207   set(VPP_DEFAULT_MARCH_FLAGS -march=armv8-a+crc)
208
209   add_vpp_march_variant(qdf24xx
210     FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx
211     N_PREFETCHES 8
212     CACHE_PREFETCH_BYTES 64
213     OFF
214   )
215
216   add_vpp_march_variant(octeontx2
217     FLAGS -march=armv8.2-a+crc+crypto+lse
218     N_PREFETCHES 8
219   )
220
221   add_vpp_march_variant(thunderx2t99
222     FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99
223     N_PREFETCHES 8
224     CACHE_PREFETCH_BYTES 64
225   )
226
227   add_vpp_march_variant(cortexa72
228     FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72
229     N_PREFETCHES 6
230     CACHE_PREFETCH_BYTES 64
231   )
232
233   add_vpp_march_variant(neoversen1
234     FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1
235     N_PREFETCHES 6
236     CACHE_PREFETCH_BYTES 64
237   )
238   add_vpp_march_variant(neoversen2
239     FLAGS -march=armv9-a+crypto -mtune=neoverse-n2
240     N_PREFETCHES 6
241     CACHE_PREFETCH_BYTES 64
242     OFF
243   )
244 endif()
245
246 macro(vpp_library_set_multiarch_sources lib)
247   cmake_parse_arguments(ARG
248     ""
249     ""
250     "SOURCES;DEPENDS;FORCE_ON"
251     ${ARGN}
252   )
253
254   set(VARIANTS "${MARCH_VARIANTS}")
255
256   if(ARG_FORCE_ON)
257     foreach(F ${ARG_FORCE_ON})
258       foreach(V ${MARCH_VARIANTS_DISABLED})
259         list(GET V 0 VARIANT)
260         if (VARIANT STREQUAL F)
261           list(GET V 1 VARIANT_FLAGS)
262           list(APPEND VARIANTS "${VARIANT}\;${VARIANT_FLAGS}")
263         endif()
264       endforeach()
265     endforeach()
266   endif()
267
268   foreach(V ${VARIANTS})
269     list(GET V 0 VARIANT)
270     list(GET V 1 VARIANT_FLAGS)
271     set(l ${lib}_${VARIANT})
272     add_library(${l} OBJECT ${ARG_SOURCES})
273     if(ARG_DEPENDS)
274       add_dependencies(${l} ${ARG_DEPENDS})
275     endif()
276     set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
277     target_compile_definitions(${l} PUBLIC CLIB_MARCH_VARIANT=${VARIANT})
278     separate_arguments(VARIANT_FLAGS)
279     target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
280     target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)
281   endforeach()
282 endmacro()
283