Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 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 | |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 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 | |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 24 | ############################################################################## |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 25 | # Cache line size |
Damjan Marion | edc4387 | 2018-09-02 11:16:00 +0200 | [diff] [blame] | 26 | ############################################################################## |
Damjan Marion | edc4387 | 2018-09-02 11:16:00 +0200 | [diff] [blame] | 27 | |
Damjan Marion | 01fe7ab | 2023-10-23 18:36:18 +0200 | [diff] [blame] | 28 | if(DEFINED VPP_PLATFORM_CACHE_LINE_SIZE) |
| 29 | set(VPP_CACHE_LINE_SIZE ${VPP_PLATFORM_CACHE_LINE_SIZE}) |
| 30 | else() |
| 31 | if(DEFINED VPP_CACHE_LINE_SIZE) |
| 32 | # Cache line size assigned via cmake args |
| 33 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)") |
| 34 | set(VPP_CACHE_LINE_SIZE 128) |
| 35 | else() |
| 36 | set(VPP_CACHE_LINE_SIZE 64) |
| 37 | endif() |
| 38 | |
| 39 | set(VPP_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE} |
| 40 | CACHE STRING "Target CPU cache line size") |
| 41 | endif() |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 42 | |
| 43 | set_log2_cacheline_size(VPP_LOG2_CACHE_LINE_SIZE ${VPP_CACHE_LINE_SIZE}) |
Damjan Marion | edc4387 | 2018-09-02 11:16:00 +0200 | [diff] [blame] | 44 | |
| 45 | ############################################################################## |
Damjan Marion | 6e39ff0 | 2020-04-29 15:15:45 +0200 | [diff] [blame] | 46 | # Gnu Assembler AVX-512 bug detection |
| 47 | # - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465 |
| 48 | ############################################################################## |
| 49 | if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*") |
| 50 | if (CMAKE_C_COMPILER_ID STREQUAL "GNU") |
| 51 | set(pfx ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/gas_avx512_bug_test) |
| 52 | file(WRITE ${pfx}.s "vmovaps 0x40(,%rax), %zmm0\n") |
| 53 | execute_process(COMMAND ${CMAKE_C_COMPILER} -c ${pfx}.s -o ${pfx}.o) |
| 54 | execute_process(COMMAND objdump -s ${pfx}.o OUTPUT_VARIABLE _output) |
| 55 | if (NOT _output MATCHES "62f17c48 28040540 000000") |
| 56 | set(GNU_ASSEMBLER_AVX512_BUG 1) |
| 57 | endif() |
| 58 | endif() |
| 59 | endif() |
| 60 | |
| 61 | ############################################################################## |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 62 | # CPU optimizations and multiarch support |
| 63 | ############################################################################## |
Damjan Marion | 45e0539 | 2022-04-25 12:38:40 +0200 | [diff] [blame] | 64 | |
Damjan Marion | 01fe7ab | 2023-10-23 18:36:18 +0200 | [diff] [blame] | 65 | if(NOT DEFINED VPP_PLATFORM) |
| 66 | option(VPP_BUILD_NATIVE_ONLY "Build only for native CPU." OFF) |
| 67 | endif() |
Damjan Marion | 45e0539 | 2022-04-25 12:38:40 +0200 | [diff] [blame] | 68 | |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 69 | macro(add_vpp_march_variant v) |
| 70 | cmake_parse_arguments(ARG |
| 71 | "OFF" |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 72 | "N_PREFETCHES;CACHE_PREFETCH_BYTES" |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 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() |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 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() |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 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}") |
Damjan Marion | 45e0539 | 2022-04-25 12:38:40 +0200 | [diff] [blame] | 106 | list(APPEND MARCH_VARIANTS_NAMES "${v}") |
Damjan Marion | 312fb4d | 2021-05-06 20:48:34 +0200 | [diff] [blame] | 107 | else() |
| 108 | list(APPEND MARCH_VARIANTS_DISABLED "${v}\;${fs}") |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 109 | endif() |
| 110 | endif() |
| 111 | endif() |
| 112 | endmacro() |
| 113 | |
Damjan Marion | 01fe7ab | 2023-10-23 18:36:18 +0200 | [diff] [blame] | 114 | if(DEFINED VPP_PLATFORM) |
| 115 | if(DEFINED VPP_PLATFORM_MARCH_FLAGS) |
| 116 | set(VPP_DEFAULT_MARCH_FLAGS ${VPP_PLATFORM_MARCH_FLAGS}) |
| 117 | check_c_compiler_flag(${VPP_DEFAULT_MARCH_FLAGS} compiler_flag_march) |
| 118 | if(NOT compiler_flag_march) |
| 119 | message(FATAL_ERROR "platform build with ${VPP_DEFAULT_MARCH_FLAGS} is not supported by compiler") |
| 120 | endif() |
| 121 | else() |
| 122 | set(VPP_DEFAULT_MARCH_FLAGS "") |
| 123 | endif() |
| 124 | set(MARCH_VARIANTS_NAMES "platform-only") |
| 125 | elseif(VPP_BUILD_NATIVE_ONLY) |
Damjan Marion | f0fc65a | 2023-08-31 17:00:16 +0200 | [diff] [blame] | 126 | set(VPP_BUILD_NATIVE_ARCH "native" CACHE STRING "native CPU -march= value.") |
| 127 | set(VPP_DEFAULT_MARCH_FLAGS -march=${VPP_BUILD_NATIVE_ARCH}) |
Damjan Marion | 01fe7ab | 2023-10-23 18:36:18 +0200 | [diff] [blame] | 128 | check_c_compiler_flag(${VPP_DEFAULT_MARCH_FLAGS} compiler_flag_march) |
| 129 | if(NOT compiler_flag_march) |
| 130 | message(FATAL_ERROR "Native-only build with ${VPP_DEFAULT_MARCH_FLAGS} is not supported by compiler") |
Damjan Marion | f0fc65a | 2023-08-31 17:00:16 +0200 | [diff] [blame] | 131 | endif() |
Damjan Marion | 45e0539 | 2022-04-25 12:38:40 +0200 | [diff] [blame] | 132 | set(MARCH_VARIANTS_NAMES "native-only") |
| 133 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*") |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 134 | set(VPP_DEFAULT_MARCH_FLAGS -march=corei7 -mtune=corei7-avx) |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 135 | |
| 136 | add_vpp_march_variant(hsw |
| 137 | FLAGS -march=haswell -mtune=haswell |
| 138 | ) |
| 139 | |
| 140 | add_vpp_march_variant(trm |
| 141 | FLAGS -march=tremont -mtune=tremont |
| 142 | OFF |
| 143 | ) |
| 144 | |
Damjan Marion | 1552228 | 2023-03-14 13:34:59 +0100 | [diff] [blame] | 145 | add_vpp_march_variant(adl |
| 146 | FLAGS -march=alderlake -mtune=alderlake -mprefer-vector-width=256 |
| 147 | OFF |
| 148 | ) |
| 149 | |
Damjan Marion | 98f7f0a | 2023-04-17 09:38:11 +0000 | [diff] [blame] | 150 | add_vpp_march_variant(scalar |
| 151 | FLAGS -march=core2 -mno-mmx -mno-sse |
| 152 | OFF |
| 153 | ) |
| 154 | |
Sivaprasad Tummala | 206592b | 2023-04-17 05:05:15 -0700 | [diff] [blame] | 155 | add_vpp_march_variant(znver3 |
| 156 | FLAGS -march=znver3 -mtune=znver3 -mprefer-vector-width=256 |
| 157 | OFF |
| 158 | ) |
| 159 | |
Damjan Marion | 6e39ff0 | 2020-04-29 15:15:45 +0200 | [diff] [blame] | 160 | if (GNU_ASSEMBLER_AVX512_BUG) |
| 161 | message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug") |
| 162 | else() |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 163 | add_vpp_march_variant(skx |
| 164 | FLAGS -march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256 |
| 165 | ) |
| 166 | |
| 167 | add_vpp_march_variant(icl |
| 168 | FLAGS -march=icelake-client -mtune=icelake-client -mprefer-vector-width=512 |
| 169 | ) |
Damjan Marion | 1552228 | 2023-03-14 13:34:59 +0100 | [diff] [blame] | 170 | |
| 171 | add_vpp_march_variant(spr |
| 172 | FLAGS -march=sapphirerapids -mtune=sapphirerapids -mprefer-vector-width=512 |
| 173 | OFF |
| 174 | ) |
Sivaprasad Tummala | 206592b | 2023-04-17 05:05:15 -0700 | [diff] [blame] | 175 | |
| 176 | add_vpp_march_variant(znver4 |
| 177 | FLAGS -march=znver4 -mtune=znver4 -mprefer-vector-width=512 |
| 178 | OFF |
| 179 | ) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 180 | endif() |
| 181 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)") |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 182 | set(VPP_DEFAULT_MARCH_FLAGS -march=armv8-a+crc) |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 183 | |
| 184 | add_vpp_march_variant(qdf24xx |
| 185 | FLAGS -march=armv8-a+crc+crypto -mtune=qdf24xx |
| 186 | N_PREFETCHES 8 |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 187 | CACHE_PREFETCH_BYTES 64 |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 188 | OFF |
| 189 | ) |
| 190 | |
| 191 | add_vpp_march_variant(octeontx2 |
| 192 | FLAGS -march=armv8.2-a+crc+crypto+lse |
| 193 | N_PREFETCHES 8 |
| 194 | ) |
| 195 | |
| 196 | add_vpp_march_variant(thunderx2t99 |
| 197 | FLAGS -march=armv8.1-a+crc+crypto -mtune=thunderx2t99 |
| 198 | N_PREFETCHES 8 |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 199 | CACHE_PREFETCH_BYTES 64 |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 200 | ) |
| 201 | |
| 202 | add_vpp_march_variant(cortexa72 |
| 203 | FLAGS -march=armv8-a+crc+crypto -mtune=cortex-a72 |
| 204 | N_PREFETCHES 6 |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 205 | CACHE_PREFETCH_BYTES 64 |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 206 | ) |
| 207 | |
| 208 | add_vpp_march_variant(neoversen1 |
| 209 | FLAGS -march=armv8.2-a+crc+crypto -mtune=neoverse-n1 |
| 210 | N_PREFETCHES 6 |
Damjan Marion | 24d65a1 | 2021-07-14 18:18:08 +0200 | [diff] [blame] | 211 | CACHE_PREFETCH_BYTES 64 |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 212 | ) |
Damjan Marion | 7bf8f5e | 2023-09-12 15:08:58 +0200 | [diff] [blame] | 213 | add_vpp_march_variant(neoversen2 |
Jieqiang Wang | bef2d6d | 2023-07-04 12:05:33 +0800 | [diff] [blame] | 214 | FLAGS -march=armv9-a+crypto -mtune=neoverse-n2 |
| 215 | N_PREFETCHES 6 |
Damjan Marion | 7bf8f5e | 2023-09-12 15:08:58 +0200 | [diff] [blame] | 216 | CACHE_PREFETCH_BYTES 64 |
| 217 | OFF |
| 218 | ) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 219 | endif() |
| 220 | |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 221 | macro(vpp_library_set_multiarch_sources lib) |
| 222 | cmake_parse_arguments(ARG |
| 223 | "" |
| 224 | "" |
Damjan Marion | 312fb4d | 2021-05-06 20:48:34 +0200 | [diff] [blame] | 225 | "SOURCES;DEPENDS;FORCE_ON" |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 226 | ${ARGN} |
| 227 | ) |
| 228 | |
Damjan Marion | 312fb4d | 2021-05-06 20:48:34 +0200 | [diff] [blame] | 229 | set(VARIANTS "${MARCH_VARIANTS}") |
| 230 | |
| 231 | if(ARG_FORCE_ON) |
| 232 | foreach(F ${ARG_FORCE_ON}) |
| 233 | foreach(V ${MARCH_VARIANTS_DISABLED}) |
| 234 | list(GET V 0 VARIANT) |
| 235 | if (VARIANT STREQUAL F) |
| 236 | list(GET V 1 VARIANT_FLAGS) |
| 237 | list(APPEND VARIANTS "${VARIANT}\;${VARIANT_FLAGS}") |
| 238 | endif() |
| 239 | endforeach() |
| 240 | endforeach() |
| 241 | endif() |
| 242 | |
| 243 | foreach(V ${VARIANTS}) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 244 | list(GET V 0 VARIANT) |
| 245 | list(GET V 1 VARIANT_FLAGS) |
| 246 | set(l ${lib}_${VARIANT}) |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 247 | add_library(${l} OBJECT ${ARG_SOURCES}) |
| 248 | if(ARG_DEPENDS) |
| 249 | add_dependencies(${l} ${ARG_DEPENDS}) |
Aloys Augustin | 2a65804 | 2020-10-13 15:43:00 +0200 | [diff] [blame] | 250 | endif() |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 251 | set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON) |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 252 | target_compile_definitions(${l} PUBLIC CLIB_MARCH_VARIANT=${VARIANT}) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 253 | separate_arguments(VARIANT_FLAGS) |
| 254 | target_compile_options(${l} PUBLIC ${VARIANT_FLAGS}) |
| 255 | target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>) |
| 256 | endforeach() |
| 257 | endmacro() |
| 258 | |