blob: c7ad1a4a0bc385ea47a6c59bba6d03c43f207fc2 [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
14##############################################################################
Damjan Marionedc43872018-09-02 11:16:00 +020015# Cache line size detection
16##############################################################################
Damjan Mariondd395292019-01-15 00:36:03 +010017if(CMAKE_CROSSCOMPILING)
18 message(STATUS "Cross-compiling - cache line size detection disabled")
19 set(VPP_LOG2_CACHE_LINE_SIZE 6)
Lijian.Zhang49c1bc82019-02-27 18:17:34 +080020elseif(DEFINED VPP_LOG2_CACHE_LINE_SIZE)
21 # Cache line size assigned via cmake args
Damjan Mariondd395292019-01-15 00:36:03 +010022elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
Damjan Marionedc43872018-09-02 11:16:00 +020023 file(READ "/proc/cpuinfo" cpuinfo)
24 string(REPLACE "\n" ";" cpuinfo ${cpuinfo})
25 foreach(l ${cpuinfo})
26 string(REPLACE ":" ";" l ${l})
27 list(GET l 0 name)
28 list(GET l 1 value)
29 string(STRIP ${name} name)
30 string(STRIP ${value} value)
31 if(${name} STREQUAL "CPU implementer")
32 set(CPU_IMPLEMENTER ${value})
Marco Varlese07450362018-09-07 11:02:26 +020033 endif()
Damjan Marionedc43872018-09-02 11:16:00 +020034 if(${name} STREQUAL "CPU part")
35 set(CPU_PART ${value})
Marco Varlese07450362018-09-07 11:02:26 +020036 endif()
Damjan Marionedc43872018-09-02 11:16:00 +020037 endforeach()
Nitin Saxena66bff592018-09-06 13:45:41 +000038 # Implementer 0x43 - Cavium
39 # Part 0x0af - ThunderX2 is 64B, rest all are 128B
Marco Varlese07450362018-09-07 11:02:26 +020040 if (${CPU_IMPLEMENTER} STREQUAL "0x43")
41 if (${CPU_PART} STREQUAL "0x0af")
42 set(VPP_LOG2_CACHE_LINE_SIZE 6)
43 else()
44 set(VPP_LOG2_CACHE_LINE_SIZE 7)
45 endif()
Nitin Saxena66bff592018-09-06 13:45:41 +000046 else()
Marco Varlese07450362018-09-07 11:02:26 +020047 set(VPP_LOG2_CACHE_LINE_SIZE 6)
Damjan Marionedc43872018-09-02 11:16:00 +020048 endif()
49 math(EXPR VPP_CACHE_LINE_SIZE "1 << ${VPP_LOG2_CACHE_LINE_SIZE}")
50 message(STATUS "ARM AArch64 CPU implementer ${CPU_IMPLEMENTER} part ${CPU_PART} cacheline size ${VPP_CACHE_LINE_SIZE}")
51else()
52 set(VPP_LOG2_CACHE_LINE_SIZE 6)
53endif()
54
55set(VPP_LOG2_CACHE_LINE_SIZE ${VPP_LOG2_CACHE_LINE_SIZE}
56 CACHE STRING "Target CPU cache line size (power of 2)")
57
58##############################################################################
Damjan Marion6e39ff02020-04-29 15:15:45 +020059# Gnu Assembler AVX-512 bug detection
60# - see: https://sourceware.org/bugzilla/show_bug.cgi?id=23465
61##############################################################################
62if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
63 if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
64 set(pfx ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/gas_avx512_bug_test)
65 file(WRITE ${pfx}.s "vmovaps 0x40(,%rax), %zmm0\n")
66 execute_process(COMMAND ${CMAKE_C_COMPILER} -c ${pfx}.s -o ${pfx}.o)
67 execute_process(COMMAND objdump -s ${pfx}.o OUTPUT_VARIABLE _output)
68 if (NOT _output MATCHES "62f17c48 28040540 000000")
69 set(GNU_ASSEMBLER_AVX512_BUG 1)
70 endif()
71 endif()
72endif()
73
74##############################################################################
Damjan Mariond16004d2018-08-26 10:14:52 +020075# CPU optimizations and multiarch support
76##############################################################################
77if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
78 set(CMAKE_C_FLAGS "-march=corei7 -mtune=corei7-avx ${CMAKE_C_FLAGS}")
Damjan Marion162330f2020-04-29 21:28:15 +020079 check_c_compiler_flag("-march=haswell" compiler_flag_march_haswell)
80 if(compiler_flag_march_haswell)
81 list(APPEND MARCH_VARIANTS "hsw\;-march=haswell -mtune=haswell")
Damjan Mariond16004d2018-08-26 10:14:52 +020082 endif()
Damjan Marion6e39ff02020-04-29 15:15:45 +020083 if (GNU_ASSEMBLER_AVX512_BUG)
84 message(WARNING "AVX-512 multiarch variant(s) disabled due to GNU Assembler bug")
85 else()
Damjan Marion162330f2020-04-29 21:28:15 +020086 check_c_compiler_flag("-mprefer-vector-width=256" compiler_flag_mprefer_vector_width)
Damjan Marion6e39ff02020-04-29 15:15:45 +020087 check_c_compiler_flag("-march=skylake-avx512" compiler_flag_march_skylake_avx512)
88 check_c_compiler_flag("-march=icelake-client" compiler_flag_march_icelake_client)
Damjan Marion162330f2020-04-29 21:28:15 +020089 if(compiler_flag_march_skylake_avx512 AND compiler_flag_mprefer_vector_width)
90 list(APPEND MARCH_VARIANTS "skx\;-march=skylake-avx512 -mtune=skylake-avx512 -mprefer-vector-width=256")
91 endif()
92 if(compiler_flag_march_icelake_client AND compiler_flag_mprefer_vector_width)
93 list(APPEND MARCH_VARIANTS "icl\;-march=icelake-client -mtune=icelake-client -mprefer-vector-width=512")
Damjan Marion6e39ff02020-04-29 15:15:45 +020094 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +020095 endif()
96elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
97 set(CMAKE_C_FLAGS "-march=armv8-a+crc ${CMAKE_C_FLAGS}")
Lijian Zhang2e237212018-09-10 17:13:56 +080098 check_c_compiler_flag("-march=armv8-a+crc+crypto -mtune=qdf24xx" compiler_flag_march_core_qdf24xx)
99 if(compiler_flag_march_core_qdf24xx)
100 list(APPEND MARCH_VARIANTS "qdf24xx\;-march=armv8-a+crc+crypto -DCLIB_N_PREFETCHES=8")
101 endif()
Nitin Saxenae2f52362020-08-25 19:58:37 +0530102 check_c_compiler_flag("-march=armv8.2-a+crc+crypto+lse" compiler_flag_march_core_octeontx2)
103 if(compiler_flag_march_core_octeontx2)
104 list(APPEND MARCH_VARIANTS "octeontx2\;-march=armv8.2-a+crc+crypto+lse -DCLIB_N_PREFETCHES=8")
105 endif()
Lijian Zhang2e237212018-09-10 17:13:56 +0800106 check_c_compiler_flag("-march=armv8.1-a+crc+crypto -mtune=thunderx2t99" compiler_flag_march_thunderx2t99)
107 if(compiler_flag_march_thunderx2t99)
jialv01ea397f52019-09-04 11:31:10 +0800108 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
Lijian Zhang2e237212018-09-10 17:13:56 +0800109 list(APPEND MARCH_VARIANTS "thunderx2t99\;-march=armv8.1-a+crc+crypto -mtune=thunderx2t99 -DCLIB_N_PREFETCHES=8")
110 else()
111 list(APPEND MARCH_VARIANTS "thunderx2t99\;-march=armv8.1-a+crc+crypto -DCLIB_N_PREFETCHES=8")
112 endif()
113 endif()
114 check_c_compiler_flag("-march=armv8-a+crc+crypto -mtune=cortex-a72" compiler_flag_march_cortexa72)
115 if(compiler_flag_march_cortexa72)
116 list(APPEND MARCH_VARIANTS "cortexa72\;-march=armv8-a+crc+crypto -mtune=cortex-a72 -DCLIB_N_PREFETCHES=6")
117 endif()
Lijian.Zhang690ce862020-02-18 19:58:19 +0800118 check_c_compiler_flag("-march=armv8.2-a+crc+crypto -mtune=neoverse-n1" compiler_flag_march_neoversen1)
119 if(compiler_flag_march_neoversen1)
120 list(APPEND MARCH_VARIANTS "neoversen1\;-march=armv8.2-a+crc+crypto -mtune=neoverse-n1 -DCLIB_N_PREFETCHES=6")
121 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +0200122endif()
123
124macro(vpp_library_set_multiarch_sources lib)
125 foreach(V ${MARCH_VARIANTS})
126 list(GET V 0 VARIANT)
127 list(GET V 1 VARIANT_FLAGS)
128 set(l ${lib}_${VARIANT})
129 add_library(${l} OBJECT ${ARGN})
130 set_target_properties(${l} PROPERTIES POSITION_INDEPENDENT_CODE ON)
BenoƮt Ganne49ee6842019-04-30 11:50:46 +0200131 target_compile_options(${l} PUBLIC "-DCLIB_MARCH_VARIANT=${VARIANT}")
Damjan Mariond16004d2018-08-26 10:14:52 +0200132 separate_arguments(VARIANT_FLAGS)
133 target_compile_options(${l} PUBLIC ${VARIANT_FLAGS})
134 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${l}>)
135 endforeach()
136endmacro()
137