c10158b4ab996f0fbfb1a57be2e3c68703bc8391
[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 # Cache line size
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)
31 else()
32   set(VPP_CACHE_LINE_SIZE 64)
33 endif()
34
35 set(VPP_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE}
36     CACHE STRING "Target CPU cache line size")
37
38 set_log2_cacheline_size(VPP_LOG2_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE})
39
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)
52     endif()
53   endif()
54 endif()
55
56 ##############################################################################
57 # CPU optimizations and multiarch support
58 ##############################################################################
59
60 option(VPP_BUILD_NATIVE_ONLY "Build only for native CPU." OFF)
61
62 if(VPP_BUILD_NATIVE_ONLY)
63   check_c_compiler_flag("-march=native" compiler_flag_march_native)
64   if(NOT compiler_flag_march_native)
65     message(FATAL_ERROR "Native-only build not supported by compiler")
66   endif()
67 endif()
68
69 macro(add_vpp_march_variant v)
70   cmake_parse_arguments(ARG
71     "OFF"
72     "N_PREFETCHES;CACHE_PREFETCH_BYTES"
73     "FLAGS"
74     ${ARGN}
75   )
76
77   if(ARG_FLAGS)
78     set(flags_ok 1)
79     set(fs "")
80     foreach(f ${ARG_FLAGS})
81       string(APPEND fs " ${f}")
82       string(REGEX REPLACE "[-=+]" "_" sfx ${f})
83       if(NOT DEFINED compiler_flag${sfx})
84         check_c_compiler_flag(${f} compiler_flag${sfx})
85       endif()
86       if(NOT compiler_flag${sfx})
87         unset(flags_ok)
88       endif()
89     endforeach()
90     if(ARG_N_PREFETCHES)
91       string(APPEND fs " -DCLIB_N_PREFETCHES=${ARG_N_PREFETCHES}")
92     endif()
93     if(ARG_CACHE_PREFETCH_BYTES)
94       set_log2_cacheline_size(log2 ${ARG_CACHE_PREFETCH_BYTES})
95       string(APPEND fs " -DCLIB_LOG2_CACHE_PREFETCH_BYTES=${log2}")
96     endif()
97     if(flags_ok)
98       string(TOUPPER ${v} uv)
99       if(ARG_OFF)
100         option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." OFF)
101       else()
102         option(VPP_MARCH_VARIANT_${uv} "Build ${v} multiarch variant." ON)
103       endif()
104       if (VPP_MARCH_VARIANT_${uv})
105         list(APPEND MARCH_VARIANTS "${v}\;${fs}")
106         list(APPEND MARCH_VARIANTS_NAMES "${v}")
107       else()
108         list(APPEND MARCH_VARIANTS_DISABLED "${v}\;${fs}")
109       endif()
110     endif()
111   endif()
112 endmacro()
113
114 if(VPP_BUILD_NATIVE_ONLY)
115   set(VPP_DEFAULT_MARCH_FLAGS -march=native)
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)
119
120   add_vpp_march_variant(hsw
121     FLAGS -march=haswell -mtune=haswell
122   )
123
124   add_vpp_march_variant(trm
125     FLAGS -march=tremont -mtune=tremont
126     OFF
127   )
128
129   add_vpp_march_variant(adl
130     FLAGS -march=alderlake -mtune=alderlake -mprefer-vector-width=256
131     OFF
132   )
133
134   if (GNU_ASSEMBLER_AVX512_BUG)
135      message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
136   else()
137     add_vpp_march_variant(skx
138       FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256
139     )
140
141     add_vpp_march_variant(icl
142       FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512
143     )
144
145     add_vpp_march_variant(spr
146       FLAGS -march=sapphirerapids -mtune=sapphirerapids -mprefer-vector-width=512
147       OFF
148     )
149   endif()
150 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
151   set(VPP_DEFAULT_MARCH_FLAGS -march=armv8-a+crc)
152
153   add_vpp_march_variant(qdf24xx
154     FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx
155     N_PREFETCHES 8
156     CACHE_PREFETCH_BYTES 64
157     OFF
158   )
159
160   add_vpp_march_variant(octeontx2
161     FLAGS -march=armv8.2-a+crc+crypto+lse
162     N_PREFETCHES 8
163   )
164
165   add_vpp_march_variant(thunderx2t99
166     FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99
167     N_PREFETCHES 8
168     CACHE_PREFETCH_BYTES 64
169   )
170
171   add_vpp_march_variant(cortexa72
172     FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72
173     N_PREFETCHES 6
174     CACHE_PREFETCH_BYTES 64
175   )
176
177   add_vpp_march_variant(neoversen1
178     FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1
179     N_PREFETCHES 6
180     CACHE_PREFETCH_BYTES 64
181   )
182 endif()
183
184 macro(vpp_library_set_multiarch_sources lib)
185   cmake_parse_arguments(ARG
186     ""
187     ""
188     "SOURCES;DEPENDS;FORCE_ON"
189     ${ARGN}
190   )
191
192   set(VARIANTS "${MARCH_VARIANTS}")
193
194   if(ARG_FORCE_ON)
195     foreach(F ${ARG_FORCE_ON})
196       foreach(V ${MARCH_VARIANTS_DISABLED})
197         list(GET V 0 VARIANT)
198         if (VARIANT STREQUAL F)
199           list(GET V 1 VARIANT_FLAGS)
200           list(APPEND VARIANTS "${VARIANT}\;${VARIANT_FLAGS}")
201         endif()
202       endforeach()
203     endforeach()
204   endif()
205
206   foreach(V ${VARIANTS})
207     list(GET V 0 VARIANT)
208     list(GET V 1 VARIANT_FLAGS)
209     set(l ${lib}_${VARIANT})
210     add_library(${l} OBJECT ${ARG_SOURCES})
211     if(ARG_DEPENDS)
212       add_dependencies(${l} ${ARG_DEPENDS})
213     endif()
214     set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
215     target_compile_definitions(${l} PUBLIC CLIB_MARCH_VARIANT=${VARIANT})
216     separate_arguments(VARIANT_FLAGS)
217     target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
218     target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)
219   endforeach()
220 endmacro()
221