blob: b54155d4bb377b515cd7fb2c354e03b45f1d0d59 [file] [log] [blame]
Damjan Mariond16004d2018-08-26 10:14:52 +02001# 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
Damjan Marion24d65a12021-07-14 18:18:08 +020014macro(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()
22endmacro()
23
Damjan Mariond16004d2018-08-26 10:14:52 +020024##############################################################################
Damjan Marion24d65a12021-07-14 18:18:08 +020025# Cache line size
Damjan Marionedc43872018-09-02 11:16:00 +020026##############################################################################
Damjan Marion24d65a12021-07-14 18:18:08 +020027if(DEFINED VPP_CACHE_LINE_SIZE)
Lijian.Zhang49c1bc82019-02-27 18:17:34 +080028 # Cache line size assigned via cmake args
Damjan Mariondd395292019-01-15 00:36:03 +010029elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
Damjan Marion24d65a12021-07-14 18:18:08 +020030 set(VPP_CACHE_LINE_SIZE 128)
Damjan Marionedc43872018-09-02 11:16:00 +020031else()
Damjan Marion24d65a12021-07-14 18:18:08 +020032 set(VPP_CACHE_LINE_SIZE 64)
Damjan Marionedc43872018-09-02 11:16:00 +020033endif()
34
Damjan Marion24d65a12021-07-14 18:18:08 +020035set(VPP_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE}
36 CACHE STRING "Target CPU cache line size")
37
38set_log2_cacheline_size(VPP_LOG2_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE})
Damjan Marionedc43872018-09-02 11:16:00 +020039
40##############################################################################
Damjan Marion6e39ff02020-04-29 15:15:45 +020041# Gnu Assembler AVX-512 bug detection
42# - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465
43##############################################################################
44if(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()
54endif()
55
56##############################################################################
Damjan Mariond16004d2018-08-26 10:14:52 +020057# CPU optimizations and multiarch support
58##############################################################################
Damjan Marion45e05392022-04-25 12:38:40 +020059
60option(VPP_BUILD_NATIVE_ONLY "Build only for native CPU." OFF)
61
62if(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()
67endif()
68
Damjan Marionc8983242021-04-30 19:31:09 +020069macro(add_vpp_march_variant v)
70 cmake_parse_arguments(ARG
71 "OFF"
Damjan Marion24d65a12021-07-14 18:18:08 +020072 "N_PREFETCHES;CACHE_PREFETCH_BYTES"
Damjan Marionc8983242021-04-30 19:31:09 +020073 "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()
Damjan Marion24d65a12021-07-14 18:18:08 +020093 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()
Damjan Marionc8983242021-04-30 19:31:09 +020097 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}")
Damjan Marion45e05392022-04-25 12:38:40 +0200106 list(APPEND MARCH_VARIANTS_NAMES "${v}")
Damjan Marion312fb4d2021-05-06 20:48:34 +0200107 else()
108 list(APPEND MARCH_VARIANTS_DISABLED "${v}\;${fs}")
Damjan Marionc8983242021-04-30 19:31:09 +0200109 endif()
110 endif()
111 endif()
112endmacro()
113
Damjan Marion45e05392022-04-25 12:38:40 +0200114if(VPP_BUILD_NATIVE_ONLY)
115 set(VPP_DEFAULT_MARCH_FLAGS -march=native)
116 set(MARCH_VARIANTS_NAMES "native-only")
117elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
Damjan Marion0d39cba2021-05-10 14:51:44 +0200118 set(VPP_DEFAULT_MARCH_FLAGS -march=corei7 -mtune=corei7-avx)
Damjan Marionc8983242021-04-30 19:31:09 +0200119
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
Damjan Marion15522282023-03-14 13:34:59 +0100129 add_vpp_march_variant(adl
130 FLAGS -march=alderlake -mtune=alderlake -mprefer-vector-width=256
131 OFF
132 )
133
Damjan Marion98f7f0a2023-04-17 09:38:11 +0000134 add_vpp_march_variant(scalar
135 FLAGS -march=core2 -mno-mmx -mno-sse
136 OFF
137 )
138
Sivaprasad Tummala206592b2023-04-17 05:05:15 -0700139 add_vpp_march_variant(znver3
140 FLAGS -march=znver3 -mtune=znver3 -mprefer-vector-width=256
141 OFF
142 )
143
Damjan Marion6e39ff02020-04-29 15:15:45 +0200144 if (GNU_ASSEMBLER_AVX512_BUG)
145 message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
146 else()
Damjan Marionc8983242021-04-30 19:31:09 +0200147 add_vpp_march_variant(skx
148 FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256
149 )
150
151 add_vpp_march_variant(icl
152 FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512
153 )
Damjan Marion15522282023-03-14 13:34:59 +0100154
155 add_vpp_march_variant(spr
156 FLAGS -march=sapphirerapids -mtune=sapphirerapids -mprefer-vector-width=512
157 OFF
158 )
Sivaprasad Tummala206592b2023-04-17 05:05:15 -0700159
160 add_vpp_march_variant(znver4
161 FLAGS -march=znver4 -mtune=znver4 -mprefer-vector-width=512
162 OFF
163 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200164 endif()
165elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
Damjan Marion0d39cba2021-05-10 14:51:44 +0200166 set(VPP_DEFAULT_MARCH_FLAGS -march=armv8-a+crc)
Damjan Marionc8983242021-04-30 19:31:09 +0200167
168 add_vpp_march_variant(qdf24xx
169 FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx
170 N_PREFETCHES 8
Damjan Marion24d65a12021-07-14 18:18:08 +0200171 CACHE_PREFETCH_BYTES 64
Damjan Marionc8983242021-04-30 19:31:09 +0200172 OFF
173 )
174
175 add_vpp_march_variant(octeontx2
176 FLAGS -march=armv8.2-a+crc+crypto+lse
177 N_PREFETCHES 8
178 )
179
180 add_vpp_march_variant(thunderx2t99
181 FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99
182 N_PREFETCHES 8
Damjan Marion24d65a12021-07-14 18:18:08 +0200183 CACHE_PREFETCH_BYTES 64
Damjan Marionc8983242021-04-30 19:31:09 +0200184 )
185
186 add_vpp_march_variant(cortexa72
187 FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72
188 N_PREFETCHES 6
Damjan Marion24d65a12021-07-14 18:18:08 +0200189 CACHE_PREFETCH_BYTES 64
Damjan Marionc8983242021-04-30 19:31:09 +0200190 )
191
192 add_vpp_march_variant(neoversen1
193 FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1
194 N_PREFETCHES 6
Damjan Marion24d65a12021-07-14 18:18:08 +0200195 CACHE_PREFETCH_BYTES 64
Damjan Marionc8983242021-04-30 19:31:09 +0200196 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200197endif()
198
Ole Troan7d527a22020-12-02 14:19:49 +0100199macro(vpp_library_set_multiarch_sources lib)
200 cmake_parse_arguments(ARG
201 ""
202 ""
Damjan Marion312fb4d2021-05-06 20:48:34 +0200203 "SOURCES;DEPENDS;FORCE_ON"
Ole Troan7d527a22020-12-02 14:19:49 +0100204 ${ARGN}
205 )
206
Damjan Marion312fb4d2021-05-06 20:48:34 +0200207 set(VARIANTS "${MARCH_VARIANTS}")
208
209 if(ARG_FORCE_ON)
210 foreach(F ${ARG_FORCE_ON})
211 foreach(V ${MARCH_VARIANTS_DISABLED})
212 list(GET V 0 VARIANT)
213 if (VARIANT STREQUAL F)
214 list(GET V 1 VARIANT_FLAGS)
215 list(APPEND VARIANTS "${VARIANT}\;${VARIANT_FLAGS}")
216 endif()
217 endforeach()
218 endforeach()
219 endif()
220
221 foreach(V ${VARIANTS})
Damjan Mariond16004d2018-08-26 10:14:52 +0200222 list(GET V 0 VARIANT)
223 list(GET V 1 VARIANT_FLAGS)
224 set(l ${lib}_${VARIANT})
Ole Troan7d527a22020-12-02 14:19:49 +0100225 add_library(${l} OBJECT ${ARG_SOURCES})
226 if(ARG_DEPENDS)
227 add_dependencies(${l} ${ARG_DEPENDS})
Aloys Augustin2a658042020-10-13 15:43:00 +0200228 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +0200229 set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200230 target_compile_definitions(${l} PUBLIC CLIB_MARCH_VARIANT=${VARIANT})
Damjan Mariond16004d2018-08-26 10:14:52 +0200231 separate_arguments(VARIANT_FLAGS)
232 target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
233 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)
234 endforeach()
235endmacro()
236