| 1 |
# Macros for use with xsimd <https://github.com/xtensor-stack/xsimd> |
| 2 |
# |
| 3 |
# The following macros are provided: |
| 4 |
# xsimd_determine_compiler |
| 5 |
# xsimd_set_preferred_compiler_flags |
| 6 |
# xsimd_compile_for_all_implementations |
| 7 |
# |
| 8 |
#============================================================================= |
| 9 |
# SPDX-FileCopyrightText: 2010-2015 Matthias Kretz <[email protected]> |
| 10 |
# SPDX-FileCopyrightText: 2022 L. E. Segovia <[email protected]> |
| 11 |
# SPDX-License-Identifier: BSD-3-Clause |
| 12 |
#============================================================================= |
| 13 |
|
| 14 |
if(CMAKE_VERSION VERSION_LESS 3.12.0) |
| 15 |
message(FATAL_ERROR "CMake 3.12.0 is required by xsimdMacros.cmake") |
| 16 |
endif() |
| 17 |
|
| 18 |
include ("${CMAKE_CURRENT_LIST_DIR}/xsimdAddCompilerFlag.cmake") |
| 19 |
|
| 20 |
set(xsimd_IS_CONFIGURATION_VALID TRUE) |
| 21 |
mark_as_advanced(xsimd_IS_CONFIGURATION_VALID) |
| 22 |
|
| 23 |
macro(xsimd_determine_compiler) |
| 24 |
set(xsimd_COMPILER_IS_CLANG false) |
| 25 |
set(xsimd_COMPILER_IS_MSVC false) |
| 26 |
set(xsimd_COMPILER_IS_GCC false) |
| 27 |
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 28 |
set(xsimd_COMPILER_IS_CLANG true) |
| 29 |
message(STATUS "Detected Compiler: Clang ${CMAKE_CXX_COMPILER_VERSION}") |
| 30 |
|
| 31 |
# break build with too old clang as early as possible. |
| 32 |
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.0) |
| 33 |
message(WARNING "xsimd requires at least clang 4.0") |
| 34 |
set(xsimd_IS_CONFIGURATION_VALID FALSE) |
| 35 |
endif() |
| 36 |
elseif(MSVC) |
| 37 |
set(xsimd_COMPILER_IS_MSVC true) |
| 38 |
message(STATUS "Detected Compiler: MSVC ${MSVC_VERSION}") |
| 39 |
# version detection of 2015 update 2 must be done against _MSC_FULL_VER == 190023918 |
| 40 |
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/msvc_version.c" "MSVC _MSC_FULL_VER") |
| 41 |
execute_process(COMMAND ${CMAKE_CXX_COMPILER} /nologo -EP "${CMAKE_CURRENT_BINARY_DIR}/msvc_version.c" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE xsimd_MSVC_VERSION) |
| 42 |
string(STRIP "${xsimd_MSVC_VERSION}" xsimd_MSVC_VERSION) |
| 43 |
string(REPLACE "MSVC " "" xsimd_MSVC_VERSION "${xsimd_MSVC_VERSION}") |
| 44 |
if (MSVC_VERSION LESS 1900 OR xsimd_MSVC_VERSION LESS 190023918) |
| 45 |
message(WARNING "xsimd requires at least MSVC 2015 Update 2") |
| 46 |
set(xsimd_IS_CONFIGURATION_VALID FALSE) |
| 47 |
endif() |
| 48 |
elseif(CMAKE_COMPILER_IS_GNUCXX) |
| 49 |
set(xsimd_COMPILER_IS_GCC true) |
| 50 |
message(STATUS "Detected Compiler: GCC ${CMAKE_CXX_COMPILER_VERSION}") |
| 51 |
|
| 52 |
# break build with too old GCC as early as possible. |
| 53 |
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) |
| 54 |
message(WARNING "xsimd requires at least GCC 4.9") |
| 55 |
set(xsimd_IS_CONFIGURATION_VALID FALSE) |
| 56 |
endif() |
| 57 |
else() |
| 58 |
message(WARNING "Untested/-supported Compiler (${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_VERSION}) for use with xsimd.\nPlease fill out the missing parts in the CMake scripts and submit a patch to https://invent.kde.org/graphics/krita") |
| 59 |
endif() |
| 60 |
endmacro() |
| 61 |
|
| 62 |
macro(xsimd_check_assembler) |
| 63 |
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-prog-name=as OUTPUT_VARIABLE _as RESULT_VARIABLE _as_result) |
| 64 |
mark_as_advanced(_as) |
| 65 |
mark_as_advanced(_as_result) |
| 66 |
if(_as_result) # error code is 0 == OK |
| 67 |
message(WARNING "Could not find 'as', the assembler used by GCC. Hoping everything will work out...") |
| 68 |
else() |
| 69 |
string(STRIP "${_as}" _as) |
| 70 |
if (WIN32 AND NOT "${_as}" MATCHES "\.exe$") |
| 71 |
set(_as "${_as}.exe") |
| 72 |
endif() |
| 73 |
execute_process(COMMAND ${_as} ARGS --version OUTPUT_VARIABLE _as_version RESULT_VARIABLE _as_result) |
| 74 |
if (_as_result) |
| 75 |
message(WARNING "'as' returned an error. Unable to detect version.") |
| 76 |
else() |
| 77 |
# LLVM tools can have multi-digit major versions. |
| 78 |
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" _as_version "${_as_version}") |
| 79 |
if(_as_version VERSION_LESS "2.21.0") |
| 80 |
message(WARNING "Your binutils is too old (${_as_version}) for reliably compiling xsimd.") |
| 81 |
set(xsimd_IS_CONFIGURATION_VALID FALSE) |
| 82 |
endif() |
| 83 |
endif() |
| 84 |
endif() |
| 85 |
endmacro() |
| 86 |
|
| 87 |
# Sets a list of the available architectures. |
| 88 |
macro(xsimd_set_available_architectures) |
| 89 |
set(_archs) |
| 90 |
set(_x86_aliases x86 i386 i686) |
| 91 |
set(_amd64_aliases x86_64 amd64 x86-64) |
| 92 |
set(_arm_aliases armv6l armv7l armv7-a) |
| 93 |
set(_arm64_aliases arm64 arm64e aarch64) |
| 94 |
|
| 95 |
if (NOT CMAKE_OSX_ARCHITECTURES) |
| 96 |
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" _processor) |
| 97 |
|
| 98 |
list(FIND _x86_aliases "${_processor}" _has_x86) |
| 99 |
list(FIND _amd64_aliases "${_processor}" _has_amd64) |
| 100 |
list(FIND _arm_aliases "${_processor}" _has_arm32) |
| 101 |
list(FIND _arm64_aliases "${_processor}" _has_aarch64) |
| 102 |
|
| 103 |
if(_has_x86 GREATER -1) |
| 104 |
list(APPEND _archs "x86") |
| 105 |
elseif(_has_amd64 GREATER -1) |
| 106 |
list(APPEND _archs "x86-64") |
| 107 |
elseif(_has_arm32 GREATER -1) |
| 108 |
list(APPEND _archs "arm") |
| 109 |
elseif(_has_aarch64 GREATER -1) |
| 110 |
list(APPEND _archs "aarch64") |
| 111 |
else() |
| 112 |
message(WARNING "Unknown processor: ${CMAKE_SYSTEM_PROCESSOR}") |
| 113 |
message(WARNING "Only the scalar architecture will be built.\nPlease fill out the missing parts in the CMake scripts and submit a patch to https://invent.kde.org/graphics/krita") |
| 114 |
endif() |
| 115 |
else() |
| 116 |
# Now handle CMAKE_OSX_ARCHITECTURES for lipoization. |
| 117 |
foreach(_arch IN LISTS CMAKE_OSX_ARCHITECTURES) |
| 118 |
message(STATUS "${_arch} ${CMAKE_OSX_ARCHITECTURES}") |
| 119 |
list(FIND _x86_aliases "${_arch}" _has_x86) |
| 120 |
list(FIND _amd64_aliases "${_arch}" _has_amd64) |
| 121 |
list(FIND _arm_aliases "${_arch}" _has_arm32) |
| 122 |
list(FIND _arm64_aliases "${_arch}" _has_aarch64) |
| 123 |
|
| 124 |
if(_has_x86 GREATER -1) |
| 125 |
list(APPEND _archs "x86") |
| 126 |
elseif(_has_amd64 GREATER -1) |
| 127 |
list(APPEND _archs "x86-64") |
| 128 |
elseif(_has_arm32 GREATER -1) |
| 129 |
list(APPEND _archs "arm") |
| 130 |
elseif(_has_aarch64 GREATER -1) |
| 131 |
list(APPEND _archs "aarch64") |
| 132 |
endif() |
| 133 |
endforeach() |
| 134 |
endif() |
| 135 |
|
| 136 |
set(XSIMD_ARCH "${_archs}" CACHE STRING "Define the available architectures for xsimd.") |
| 137 |
message(STATUS "Available architectures for xsimd: ${XSIMD_ARCH}") |
| 138 |
endmacro() |
| 139 |
|
| 140 |
macro(xsimd_set_preferred_compiler_flags) |
| 141 |
xsimd_determine_compiler() |
| 142 |
|
| 143 |
if (NOT xsimd_COMPILER_IS_MSVC AND NOT MSVC_VERSION) |
| 144 |
xsimd_check_assembler() |
| 145 |
endif() |
| 146 |
|
| 147 |
if(xsimd_COMPILER_IS_GCC) |
| 148 |
AddCompilerFlag("-Wabi" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 149 |
AddCompilerFlag("-fabi-version=0" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) # ABI version 4 is required to make __m128 and __m256 appear as different types. 0 should give us the latest version. |
| 150 |
AddCompilerFlag("-fabi-compat-version=0" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) # GCC 5 introduced this switch |
| 151 |
# and defaults it to 2 if -fabi-version is 0. But in that case the bug -fabi-version=0 is |
| 152 |
# supposed to fix resurfaces. For now just make sure that it compiles and links. |
| 153 |
elseif(xsimd_COMPILER_IS_MSVC) |
| 154 |
AddCompilerFlag("/bigobj" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) # required for building tests with AVX |
| 155 |
elseif(xsimd_COMPILER_IS_CLANG) |
| 156 |
# disable these warnings because clang shows them for function overloads that were discarded via SFINAE |
| 157 |
AddCompilerFlag("-Wno-local-type-template-args" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 158 |
AddCompilerFlag("-Wno-unnamed-type-template-args" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 159 |
endif() |
| 160 |
|
| 161 |
if(xsimd_COMPILER_IS_MSVC) |
| 162 |
AddCompilerFlag("/fp:fast" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 163 |
else() |
| 164 |
AddCompilerFlag("-ffp-contract=fast" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 165 |
|
| 166 |
if (NOT WIN32) |
| 167 |
AddCompilerFlag("-fPIC" CXX_FLAGS xsimd_ARCHITECTURE_FLAGS) |
| 168 |
endif() |
| 169 |
endif() |
| 170 |
|
| 171 |
xsimd_set_available_architectures() |
| 172 |
endmacro() |
| 173 |
|
| 174 |
# helper macro for xsimd_compile_for_all_implementations |
| 175 |
macro(_xsimd_compile_one_implementation _srcs _impl) |
| 176 |
list(FIND _disabled_targets "${_impl}" _disabled_index) |
| 177 |
list(FIND _only_targets "${_impl}" _only_index) |
| 178 |
if(${_disabled_index} GREATER -1) |
| 179 |
if(${_only_index} GREATER -1) |
| 180 |
# disabled and enabled -> error |
| 181 |
message(FATAL_ERROR "xsimd_compile_for_all_implementations lists ${_impl} in both the ONLY and EXCLUDE lists. Please remove one.") |
| 182 |
endif() |
| 183 |
list(REMOVE_AT _disabled_targets ${_disabled_index}) |
| 184 |
# skip the rest and return |
| 185 |
elseif((NOT _only_targets AND NOT _state EQUAL 3) OR ${_only_index} GREATER -1) |
| 186 |
if(${_only_index} GREATER -1) |
| 187 |
list(REMOVE_AT _only_targets ${_only_index}) |
| 188 |
endif() |
| 189 |
set(_extra_flags) |
| 190 |
set(_ok FALSE) |
| 191 |
foreach(_flags_it ${ARGN}) |
| 192 |
if(_flags_it STREQUAL "NO_FLAG") |
| 193 |
set(_ok TRUE) |
| 194 |
break() |
| 195 |
endif() |
| 196 |
string(REPLACE " " ";" _flag_list "${_flags_it}") |
| 197 |
foreach(_f ${_flag_list}) |
| 198 |
AddCompilerFlag(${_f} CXX_RESULT _ok) |
| 199 |
if(NOT _ok) |
| 200 |
break() |
| 201 |
endif() |
| 202 |
endforeach() |
| 203 |
if(_ok) |
| 204 |
set(_extra_flags ${_flags_it}) |
| 205 |
break() |
| 206 |
endif() |
| 207 |
endforeach() |
| 208 |
|
| 209 |
if(MSVC AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 210 |
# MSVC for 64bit does not recognize /arch:SSE2 anymore. Therefore we set override _ok if _impl |
| 211 |
# says SSE |
| 212 |
if("${_impl}" MATCHES "SSE") |
| 213 |
set(_ok TRUE) |
| 214 |
endif() |
| 215 |
endif() |
| 216 |
|
| 217 |
if(_ok) |
| 218 |
get_filename_component(_out "${__compile_src}" NAME_WE) |
| 219 |
get_filename_component(_ext "${__compile_src}" EXT) |
| 220 |
set(_out "${CMAKE_CURRENT_BINARY_DIR}/${_out}_${_impl}${_ext}") |
| 221 |
add_custom_command(OUTPUT "${_out}" |
| 222 |
COMMAND ${CMAKE_COMMAND} -E copy "${__compile_src}" "${_out}" |
| 223 |
DEPENDS "${__compile_src}" |
| 224 |
COMMENT "Copy to ${_out}" |
| 225 |
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 226 |
VERBATIM) |
| 227 |
set_source_files_properties( "${_out}" PROPERTIES |
| 228 |
COMPILE_FLAGS "${_flags} ${_extra_flags}" |
| 229 |
COMPILE_DEFINITIONS "XSIMD_IMPL=${_impl}" |
| 230 |
SKIP_PRECOMPILE_HEADERS TRUE |
| 231 |
) |
| 232 |
list(APPEND ${_srcs} "${_out}") |
| 233 |
endif() |
| 234 |
endif() |
| 235 |
endmacro() |
| 236 |
|
| 237 |
# Generate compile rules for the given C++ source file for all available implementations and return |
| 238 |
# the resulting list of object files in _obj |
| 239 |
# all remaining arguments are additional flags |
| 240 |
# Example: |
| 241 |
# xsimd_compile_for_all_implementations(_objs src/trigonometric.cpp FLAGS -DCOMPILE_BLAH EXCLUDE Scalar) |
| 242 |
# add_executable(executable main.cpp ${_objs}) |
| 243 |
macro(xsimd_compile_for_all_implementations _srcs _src) |
| 244 |
set(_flags) |
| 245 |
unset(_disabled_targets) |
| 246 |
unset(_only_targets) |
| 247 |
set(_state 0) |
| 248 |
foreach(_arg ${ARGN}) |
| 249 |
if(_arg STREQUAL "FLAGS") |
| 250 |
set(_state 1) |
| 251 |
elseif(_arg STREQUAL "EXCLUDE") |
| 252 |
set(_state 2) |
| 253 |
elseif(_arg STREQUAL "ONLY") |
| 254 |
set(_state 3) |
| 255 |
elseif(_state EQUAL 1) |
| 256 |
set(_flags "${_flags} ${_arg}") |
| 257 |
elseif(_state EQUAL 2) |
| 258 |
list(APPEND _disabled_targets "${_arg}") |
| 259 |
elseif(_state EQUAL 3) |
| 260 |
list(APPEND _only_targets "${_arg}") |
| 261 |
else() |
| 262 |
message(FATAL_ERROR "incorrect argument to xsimd_compile_for_all_implementations") |
| 263 |
endif() |
| 264 |
endforeach() |
| 265 |
|
| 266 |
set(__compile_src "${_src}") |
| 267 |
|
| 268 |
## Note the following settings of default_arch on GCC: |
| 269 |
## - fma3<sse> should be -msse -mfma but == fma3<avx> |
| 270 |
## - fma3<avx(2)> are -mavx(2) -mfma |
| 271 |
## - fma4 should be -mfma4 but == avx |
| 272 |
## |
| 273 |
## On MSVC: |
| 274 |
## - /arch:AVX512 enables all the 512 tandem |
| 275 |
## |
| 276 |
## To target the individual architectures, it must be |
| 277 |
## done manually or via a special definition header. |
| 278 |
|
| 279 |
## Note the following for Arm: |
| 280 |
## MSVC requires manual patching to detect NEON, |
| 281 |
## its intrinsics are available but they are not detectable. |
| 282 |
|
| 283 |
_xsimd_compile_one_implementation(${_srcs} Scalar |
| 284 |
NO_FLAG) |
| 285 |
|
| 286 |
if("aarch64" IN_LIST XSIMD_ARCH) |
| 287 |
_xsimd_compile_one_implementation(${_srcs} NEON64 NO_FLAG) |
| 288 |
endif() |
| 289 |
|
| 290 |
if ("arm" IN_LIST XSIMD_ARCH) |
| 291 |
_xsimd_compile_one_implementation(${_srcs} NEON |
| 292 |
"-mfpu=neon") |
| 293 |
endif() |
| 294 |
if ("x86" IN_LIST XSIMD_ARCH OR "x86-64" IN_LIST XSIMD_ARCH) |
| 295 |
_xsimd_compile_one_implementation(${_srcs} SSE2 |
| 296 |
"-msse2" "/arch:SSE2") |
| 297 |
_xsimd_compile_one_implementation(${_srcs} SSE3 |
| 298 |
"-msse3" "/arch:SSE2") |
| 299 |
_xsimd_compile_one_implementation(${_srcs} SSSE3 |
| 300 |
"-mssse3" "/arch:SSE2") |
| 301 |
_xsimd_compile_one_implementation(${_srcs} SSE4_1 |
| 302 |
"-msse4.1" "/arch:SSE2") |
| 303 |
_xsimd_compile_one_implementation(${_srcs} SSE4_2 |
| 304 |
"-msse4.2" "/arch:SSE2") |
| 305 |
_xsimd_compile_one_implementation(${_srcs} SSE4_2+FMA |
| 306 |
"-msse4.2 -mfma" "/arch:AVX") |
| 307 |
_xsimd_compile_one_implementation(${_srcs} FMA4 |
| 308 |
"-mfma4" "/arch:AVX") |
| 309 |
_xsimd_compile_one_implementation(${_srcs} AVX |
| 310 |
"-mavx" "/arch:AVX") |
| 311 |
_xsimd_compile_one_implementation(${_srcs} AVX+FMA |
| 312 |
"-mavx -mfma" "/arch:AVX") |
| 313 |
_xsimd_compile_one_implementation(${_srcs} AVX2 |
| 314 |
"-mavx2" "/arch:AVX2") |
| 315 |
_xsimd_compile_one_implementation(${_srcs} AVX2+FMA |
| 316 |
"-mavx2 -mfma" "/arch:AVX2") |
| 317 |
_xsimd_compile_one_implementation(${_srcs} AVX512F |
| 318 |
"-mavx512f" "/arch:AVX512") |
| 319 |
_xsimd_compile_one_implementation(${_srcs} AVX512BW |
| 320 |
"-mavx512bw" "/arch:AVX512") |
| 321 |
_xsimd_compile_one_implementation(${_srcs} AVX512CD |
| 322 |
"-mavx512cd" "/arch:AVX512") |
| 323 |
_xsimd_compile_one_implementation(${_srcs} AVX512DQ |
| 324 |
"-mavx512dq" "/arch:AVX512") |
| 325 |
endif() |
| 326 |
list(LENGTH _only_targets _len) |
| 327 |
if(_len GREATER 0) |
| 328 |
message(WARNING "The following unknown targets where listed in the ONLY list of xsimd_compile_for_all_implementations: '${_only_targets}'") |
| 329 |
endif() |
| 330 |
list(LENGTH _disabled_targets _len) |
| 331 |
if(_len GREATER 0) |
| 332 |
message(WARNING "The following unknown targets where listed in the EXCLUDE list of xsimd_compile_for_all_implementations: '${_disabled_targets}'") |
| 333 |
endif() |
| 334 |
endmacro() |