From 4c81e174bfb28e67251d33ac812ba0cc57a2f3c4 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Thu, 12 Sep 2024 18:07:24 +0300 Subject: [PATCH] Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline RISC-V/AArch64: disable CPU features detection #25901 This PR is the first step in fixing current issues with NEON/RVV, FP16, BF16 and other CPU features on AArch64 and RISC-V platforms. On AArch64 and RISC-V platforms we usually have the platform set by default in the toolchain when we compile it or in the cmake toolchain file or in CMAKE_CXX_FLAGS by user. Then, there are two ways to set platform options: a) "-mcpu=" ; b) "-march=" (e.g. "rv64gcv"). Furthermore, there are no similar "levels" of optimizations as for x86_64, instead we have features (RVV, FP16,...) which can be enabled or disabled. So, for example, if a user has "rv64gc" set by the toolchain and we want to enable RVV. Then we need to somehow parse their current feature set and append "v" (vector optimizations) to this string. This task is quite hard and the whole procedure is prone to errors. I propose to use "CPU_BASELINE=DETECT" by default on AArch64 and RISC-V platforms. And somehow remove other features or make them read-only/detect-only, so that OpenCV wouldn't add any extra "-march" flags to the default configuration. We would rely only on the flags provided by the compiler and cmake toolchain file. We can have some predefined configurations in our cmake toolchain files. Changes made by this PR: - `CMakeLists.txt`: - use `CMAKE_CROSSCOMPILING` instead of `CMAKE_TOOLCHAIN_FILE` to detect cross-compilation. This might be useful in cases of native compilation with a toolchain file - removed obsolete variables `ENABLE_NEON` and `ENABLE_VFPV3`, the first one have been turned ON by default on AArch64 platform which caused setting `CPU_BASELINE=NEON` - raise minimum cmake version allowed to 3.7 to allow using `CMAKE_CXX_FLAGS_INIT` in toolchain files - added separate files with arch flags for native compilation on AArch64 and RISC-V, these files will be used in our toolchain files and in regular cmake - use `DETECT` as default value for `CPU_BASELINE` also allow `NATIVE`, warn user if other values were used (only for AArch64 and RISC-V) - for each feature listed in `CPU_DISPATCH` check if corresponding `CPU_${opt}_FLAGS_ON` has been provided, warn user if it is empty (only for AArch64 and RISC-V) - use `CPU_BASELINE_DISABLE` variable to actually turn off macros responsible for corresponding features even if they are enabled by compiler - removed Aarch64 feature merge procedure (it didn't support `-mcpu` and built-in `-march`) - reworked AArch64 and two RISC-V cmake toolchain files (does not affect Android/OSX/iOS/Win): - use `CMAKE_CXX_FLAGS_INIT` to set compiler flags - use variables `ENABLE_BF16`, `ENABLE_DOTPROD`, `ENABLE_RVV`, `ENABLE_FP16` to control `-march` - AArch64: removed other compiler and linker flags - `-fdata-sections`, `-fsigned-char`, `-Wl,--no-undefined`, `-Wl,--gc-sections` - already set by OpenCV - `-Wa,--noexecstack`, `-Wl,-z,noexecstack`, `-Wl,-z,relro`, `-Wl,-z,now` - can be enabled by OpenCV via `ENABLE_HARDENING` - `-Wno-psabi` - this option used to disable some warnings on older ARM platforms, shouldn't harm - ARM: removed same common flags as for AArch64, but left `-mthumb` and `--fix-cortex-a8`, `-z nocopyreloc` --- CMakeLists.txt | 6 +- cmake/OpenCVCompilerOptimizations.cmake | 72 +++++++++---------- cmake/OpenCVCompilerOptions.cmake | 11 +++ cmake/OpenCVMinDepVersions.cmake | 2 +- cmake/platforms/OpenCV-Linux.cmake | 10 ++- platforms/linux/arm.toolchain.cmake | 37 ++++------ platforms/linux/flags-aarch64.cmake | 19 +++++ platforms/linux/flags-riscv64.cmake | 9 +++ .../linux/riscv64-andes-gcc.toolchain.cmake | 12 ++-- platforms/linux/riscv64-clang.toolchain.cmake | 9 ++- platforms/linux/riscv64-gcc.toolchain.cmake | 9 +-- 11 files changed, 115 insertions(+), 81 deletions(-) create mode 100644 platforms/linux/flags-aarch64.cmake create mode 100644 platforms/linux/flags-riscv64.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 623c2b9540..9d103ca195 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,7 +145,7 @@ if(NOT OPENCV_SKIP_CMAKE_SYSTEM_FILE) endif() if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) # https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT.html - if(NOT CMAKE_TOOLCHAIN_FILE) + if(NOT CMAKE_CROSSCOMPILING) if(WIN32) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory" FORCE) else() @@ -513,10 +513,6 @@ OCV_OPTION(OPENCV_ENABLE_MEMORY_SANITIZER "Better support for memory/address san OCV_OPTION(ENABLE_OMIT_FRAME_POINTER "Enable -fomit-frame-pointer for GCC" ON IF CV_GCC ) OCV_OPTION(ENABLE_POWERPC "Enable PowerPC for GCC" ON IF (CV_GCC AND CMAKE_SYSTEM_PROCESSOR MATCHES powerpc.*) ) OCV_OPTION(ENABLE_FAST_MATH "Enable compiler options for fast math optimizations on FP computations (not recommended)" OFF) -if(NOT IOS AND (NOT ANDROID OR OPENCV_ANDROID_USE_LEGACY_FLAGS) AND CMAKE_CROSSCOMPILING) # Use CPU_BASELINE instead -OCV_OPTION(ENABLE_NEON "Enable NEON instructions" (NEON OR ANDROID_ARM_NEON OR AARCH64) IF (CV_GCC OR CV_CLANG) AND (ARM OR AARCH64 OR IOS OR XROS) ) -OCV_OPTION(ENABLE_VFPV3 "Enable VFPv3-D32 instructions" OFF IF (CV_GCC OR CV_CLANG) AND (ARM OR AARCH64 OR IOS OR XROS) ) -endif() OCV_OPTION(ENABLE_NOISY_WARNINGS "Show all warnings even if they are too noisy" OFF ) OCV_OPTION(OPENCV_WARNINGS_ARE_ERRORS "Treat warnings as errors" OFF ) OCV_OPTION(ANDROID_EXAMPLES_WITH_LIBS "Build binaries of Android examples with native libraries" OFF IF ANDROID ) diff --git a/cmake/OpenCVCompilerOptimizations.cmake b/cmake/OpenCVCompilerOptimizations.cmake index c415a75493..a0c503ba57 100644 --- a/cmake/OpenCVCompilerOptimizations.cmake +++ b/cmake/OpenCVCompilerOptimizations.cmake @@ -104,7 +104,7 @@ ocv_optimization_process_obsolete_option(ENABLE_AVX2 AVX2 ON) ocv_optimization_process_obsolete_option(ENABLE_FMA3 FMA3 ON) ocv_optimization_process_obsolete_option(ENABLE_VFPV3 VFPV3 OFF) -ocv_optimization_process_obsolete_option(ENABLE_NEON NEON OFF) +ocv_optimization_process_obsolete_option(ENABLE_NEON NEON ON) ocv_optimization_process_obsolete_option(ENABLE_VSX VSX ON) @@ -170,7 +170,29 @@ elseif(" ${CMAKE_CXX_FLAGS} " MATCHES " -march=native | -xHost | /QxHost ") set(CPU_BASELINE_DETECT ON) endif() +# For platforms which don't allow enabling of extra instruction sets with separate compiler options. +# E.g. GCC/Clang for RISC-V/AArch64 use suffixes for -march option. So we should avoid using existing +# CPU features mechanisms and rely on cmake-toolchain files or flags provided via command-line. +macro(ocv_default_baseline_detect_and_check_dispatch) + set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}") + if(NOT CPU_BASELINE MATCHES "^(DETECT|NATIVE|)$") + message(WARNING "CPU_BASELINE is set to '${CPU_BASELINE}', but '${CMAKE_SYSTEM_PROCESSOR}' " + "platform is designed to work with DETECT|NATIVE|, " + "otherwise target CPU architecture may be changed unexpectedly. " + "Please check your resulting compiler flags in the CMake output.") + endif() + foreach(opt ${CPU_DISPATCH}) + if(NOT DEFINED CPU_${opt}_FLAGS_ON) + message(WARNING "${opt} is in the CPU_DISPATCH list, but 'CPU_${opt}_FLAGS_ON' is not set. " + "Please provide feature-specific compiler options explicitly.") + endif() + endforeach() +endmacro() + +#=================================================================================================== + if(X86 OR X86_64) + ocv_update(CPU_KNOWN_OPTIMIZATIONS "SSE;SSE2;SSE3;SSSE3;SSE4_1;POPCNT;SSE4_2;AVX;FP16;AVX2;FMA3;AVX_512F;AVX512_COMMON;AVX512_KNL;AVX512_KNM;AVX512_SKX;AVX512_CNL;AVX512_CLX;AVX512_ICL") ocv_update(CPU_AVX512_COMMON_GROUP "AVX_512F;AVX_512CD") @@ -330,6 +352,7 @@ if(X86 OR X86_64) endif() elseif(ARM OR AARCH64) + ocv_update(CPU_NEON_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_neon.cpp") ocv_update(CPU_FP16_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_fp16.cpp") ocv_update(CPU_NEON_FP16_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_neon_fp16.cpp") @@ -347,7 +370,6 @@ elseif(ARM OR AARCH64) ocv_update(CPU_FP16_IMPLIES "NEON") else() ocv_update(CPU_KNOWN_OPTIMIZATIONS "NEON;FP16;NEON_DOTPROD;NEON_FP16;NEON_BF16") - ocv_update(CPU_NEON_FLAGS_ON "") ocv_update(CPU_FP16_IMPLIES "NEON") ocv_update(CPU_NEON_DOTPROD_IMPLIES "NEON") ocv_update(CPU_NEON_FP16_IMPLIES "NEON") @@ -361,15 +383,19 @@ elseif(ARM OR AARCH64) ocv_update(CPU_NEON_FP16_FLAGS_ON "-march=armv8.2-a+fp16") ocv_update(CPU_NEON_BF16_FLAGS_ON "-march=armv8.2-a+bf16") endif() - set(CPU_BASELINE "NEON;FP16" CACHE STRING "${HELP_CPU_BASELINE}") set(CPU_DISPATCH "NEON_FP16;NEON_BF16;NEON_DOTPROD" CACHE STRING "${HELP_CPU_DISPATCH}") + ocv_default_baseline_detect_and_check_dispatch() endif() + elseif(MIPS) + ocv_update(CPU_MSA_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_msa.cpp") ocv_update(CPU_KNOWN_OPTIMIZATIONS "MSA") ocv_update(CPU_MSA_FLAGS_ON "-mmsa") set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}") + elseif(PPC64LE) + ocv_update(CPU_KNOWN_OPTIMIZATIONS "VSX;VSX3") ocv_update(CPU_VSX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx.cpp") ocv_update(CPU_VSX3_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx3.cpp") @@ -390,15 +416,13 @@ elseif(PPC64LE) set(CPU_BASELINE "VSX" CACHE STRING "${HELP_CPU_BASELINE}") elseif(RISCV) - ocv_update(CPU_RVV_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_rvv.cpp") - ocv_update(CPU_KNOWN_OPTIMIZATIONS "RVV") - ocv_update(CPU_RVV_FLAGS_ON "-march=rv64gcv") - ocv_update(CPU_RVV_FLAGS_CONFLICT "-march=[^ ]*") - set(CPU_DISPATCH "" CACHE STRING "${HELP_CPU_DISPATCH}") - set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}") + ocv_update(CPU_KNOWN_OPTIMIZATIONS "RVV") + ocv_update(CPU_RVV_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_rvv.cpp") + ocv_default_baseline_detect_and_check_dispatch() elseif(LOONGARCH64) + ocv_update(CPU_LSX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_lsx.cpp") ocv_update(CPU_LASX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_lasx.cpp") ocv_update(CPU_KNOWN_OPTIMIZATIONS "LSX;LASX") @@ -440,7 +464,7 @@ macro(ocv_check_compiler_optimization OPT) set(_varname "") if(CPU_${OPT}_TEST_FILE) set(__available 0) - if(__is_from_baseline OR CPU_BASELINE_DETECT) + if(NOT __is_disabled AND (__is_from_baseline OR CPU_BASELINE_DETECT)) set(_varname "HAVE_CPU_${OPT}_SUPPORT") ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" "${_varname}" "${CPU_${OPT}_TEST_FILE}") if(${_varname}) @@ -478,23 +502,6 @@ macro(ocv_check_compiler_optimization OPT) endif() endmacro() -macro(ocv_cpu_aarch64_baseline_merge_feature_options FEATURE_NAME_LIST FLAG_STRING COMMON_OPTION) - unset(_POSTFIX) - # Check each feature option - foreach(OPT IN LISTS ${FEATURE_NAME_LIST}) - string(FIND "${${FLAG_STRING}}" "${CPU_${OPT}_FLAGS_ON}" OPT_FOUND) - if(NOT ${OPT_FOUND} EQUAL -1) - string(REPLACE "${COMMON_OPTION}" "" TRAILING_PART "${CPU_${OPT}_FLAGS_ON}") - string(APPEND _POSTFIX "${TRAILING_PART}") - string(REPLACE " ${CPU_${OPT}_FLAGS_ON}" "" ${FLAG_STRING} ${${FLAG_STRING}}) - endif() - endforeach() - # If more than one option found, merge them - if(NOT "x${_POSTFIX}" STREQUAL "x") - set(${FLAG_STRING} "${${FLAG_STRING}} ${COMMON_OPTION}${_POSTFIX}") - endif() -endmacro() - foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS}) set(CPU_${OPT}_USAGE_COUNT 0 CACHE INTERNAL "") if("${CPU_${OPT}_FLAGS_ON}" STREQUAL "disabled") @@ -577,7 +584,7 @@ foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS}) if(CPU_${OPT}_SUPPORTED) if(";${CPU_DISPATCH};" MATCHES ";${OPT};" AND NOT __is_from_baseline) list(APPEND CPU_DISPATCH_FINAL ${OPT}) - elseif(__is_from_baseline) + elseif(__is_from_baseline AND NOT __is_disabled) if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};") list(APPEND CPU_BASELINE_FINAL ${OPT}) endif() @@ -588,15 +595,6 @@ foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS}) endif() endforeach() -if(AARCH64) - if(NOT MSVC) - # Define the list of NEON options to check - set(NEON_OPTIONS_LIST NEON_DOTPROD NEON_FP16 NEON_BF16) - set(BASE_ARCHITECTURE "-march=armv8.2-a") - ocv_cpu_aarch64_baseline_merge_feature_options(NEON_OPTIONS_LIST CPU_BASELINE_FLAGS ${BASE_ARCHITECTURE}) - endif() -endif() - foreach(OPT ${CPU_BASELINE_REQUIRE}) if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};") message(SEND_ERROR "Required baseline optimization is not supported: ${OPT} (CPU_BASELINE_REQUIRE=${CPU_BASELINE_REQUIRE})") diff --git a/cmake/OpenCVCompilerOptions.cmake b/cmake/OpenCVCompilerOptions.cmake index f23bb13dc5..f0d6378bd7 100644 --- a/cmake/OpenCVCompilerOptions.cmake +++ b/cmake/OpenCVCompilerOptions.cmake @@ -77,6 +77,17 @@ macro(add_env_definitions option) add_definitions("-D${option}=\"${value}\"") endmacro() +# Use same flags for native AArch64 and RISC-V compilation as for cross-compile (Linux) +if(NOT CMAKE_CROSSCOMPILING AND NOT CMAKE_TOOLCHAIN_FILE AND COMMAND ocv_set_platform_flags) + unset(platform_flags) + ocv_set_platform_flags(platform_flags) + # externally-provided flags should have higher priority - prepend our flags + if(platform_flags) + set(CMAKE_CXX_FLAGS "${platform_flags} ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "${platform_flags} ${CMAKE_C_FLAGS}") + endif() +endif() + if(NOT MSVC) # OpenCV fails some tests when 'char' is 'unsigned' by default add_extra_compiler_option(-fsigned-char) diff --git a/cmake/OpenCVMinDepVersions.cmake b/cmake/OpenCVMinDepVersions.cmake index db225e2ab5..563761dfd2 100644 --- a/cmake/OpenCVMinDepVersions.cmake +++ b/cmake/OpenCVMinDepVersions.cmake @@ -1,5 +1,5 @@ if(NOT DEFINED MIN_VER_CMAKE) - set(MIN_VER_CMAKE 3.5.1) + set(MIN_VER_CMAKE 3.7) endif() set(MIN_VER_CUDA 6.5) set(MIN_VER_CUDNN 7.5) diff --git a/cmake/platforms/OpenCV-Linux.cmake b/cmake/platforms/OpenCV-Linux.cmake index 1bb8bf6d7f..5f015dfb79 100644 --- a/cmake/platforms/OpenCV-Linux.cmake +++ b/cmake/platforms/OpenCV-Linux.cmake @@ -1 +1,9 @@ -# empty +if((CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + AND NOT CMAKE_CROSSCOMPILING + AND NOT CMAKE_TOOLCHAIN_FILE) + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") # Maybe use AARCH64 variable? + include(${CMAKE_CURRENT_LIST_DIR}/../../platforms/linux/flags-aarch64.cmake) + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") + include(${CMAKE_CURRENT_LIST_DIR}/../../platforms/linux/flags-riscv64.cmake) + endif() +endif() diff --git a/platforms/linux/arm.toolchain.cmake b/platforms/linux/arm.toolchain.cmake index 184997fba5..ddbad83e51 100644 --- a/platforms/linux/arm.toolchain.cmake +++ b/platforms/linux/arm.toolchain.cmake @@ -48,32 +48,23 @@ if(NOT DEFINED ARM_LINUX_SYSROOT AND DEFINED GNU_MACHINE) set(ARM_LINUX_SYSROOT /usr/${GNU_MACHINE}${FLOAT_ABI_SUFFIX}) endif() -if(NOT DEFINED CMAKE_CXX_FLAGS) - set(CMAKE_CXX_FLAGS "" CACHE INTERNAL "") - set(CMAKE_C_FLAGS "" CACHE INTERNAL "") - set(CMAKE_SHARED_LINKER_FLAGS "" CACHE INTERNAL "") - set(CMAKE_MODULE_LINKER_FLAGS "" CACHE INTERNAL "") - set(CMAKE_EXE_LINKER_FLAGS "" CACHE INTERNAL "") - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi") - if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm) - set(CMAKE_CXX_FLAGS "-mthumb ${CMAKE_CXX_FLAGS}") - set(CMAKE_C_FLAGS "-mthumb ${CMAKE_C_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,nocopyreloc") - endif() - if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm) - set(ARM_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") - elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) - set(ARM_LINKER_FLAGS "-Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") +# == Compiler flags +if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm) + set(CMAKE_CXX_FLAGS_INIT "-mthumb") + set(CMAKE_C_FLAGS_INIT "-mthumb") + set(common_ld_opt "-Wl,--fix-cortex-a8") + set(CMAKE_SHARED_LINKER_FLAGS_INIT "${common_ld_opt}") + set(CMAKE_MODULE_LINKER_FLAGS_INIT "${common_ld_opt}") + set(CMAKE_EXE_LINKER_FLAGS_INIT "${common_ld_opt} -Wl,-z,nocopyreloc") +elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") + include("${CMAKE_CURRENT_LIST_DIR}/flags-aarch64.cmake") + if(COMMAND ocv_set_platform_flags) + ocv_set_platform_flags(CMAKE_CXX_FLAGS_INIT) + ocv_set_platform_flags(CMAKE_C_FLAGS_INIT) endif() - set(CMAKE_SHARED_LINKER_FLAGS "${ARM_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}") - set(CMAKE_MODULE_LINKER_FLAGS "${ARM_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${ARM_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}") -else() - #message(WARNING "CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}' is defined") endif() + if(USE_NEON) message(WARNING "You use obsolete variable USE_NEON to enable NEON instruction set. Use -DENABLE_NEON=ON instead." ) set(ENABLE_NEON TRUE) diff --git a/platforms/linux/flags-aarch64.cmake b/platforms/linux/flags-aarch64.cmake new file mode 100644 index 0000000000..5aeb7a2b6a --- /dev/null +++ b/platforms/linux/flags-aarch64.cmake @@ -0,0 +1,19 @@ +# see https://gcc.gnu.org/onlinedocs/gcc/AArch64-Options.html#index-march +function(ocv_set_platform_flags VAR) + unset(flags) + if(ENABLE_BF16) + set(flags "${flags}+bf16") + endif() + if(ENABLE_DOTPROD) + set(flags "${flags}+dotprod") + endif() + if(ENABLE_FP16) + set(flags "${flags}+fp16") + endif() + if(DEFINED ENABLE_NEON AND NOT ENABLE_NEON) + set(flags "${flags}+nosimd") + endif() + if(flags) + set(${VAR} "-march=armv8.2-a${flags}" PARENT_SCOPE) + endif() +endfunction() diff --git a/platforms/linux/flags-riscv64.cmake b/platforms/linux/flags-riscv64.cmake new file mode 100644 index 0000000000..4488cf5887 --- /dev/null +++ b/platforms/linux/flags-riscv64.cmake @@ -0,0 +1,9 @@ +# see https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html#index-march-14 +function(ocv_set_platform_flags VAR) + if(ENABLE_RVV OR RISCV_RVV_SCALABLE) + set(flags "-march=rv64gcv") + else() + set(flags "-march=rv64gc") + endif() + set(${VAR} "${flags}" PARENT_SCOPE) +endfunction() diff --git a/platforms/linux/riscv64-andes-gcc.toolchain.cmake b/platforms/linux/riscv64-andes-gcc.toolchain.cmake index 9b9c0b5246..a18c3df9e1 100755 --- a/platforms/linux/riscv64-andes-gcc.toolchain.cmake +++ b/platforms/linux/riscv64-andes-gcc.toolchain.cmake @@ -10,16 +10,12 @@ set(CMAKE_C_COMPILER ${RISCV_GCC_INSTALL_ROOT}/bin/riscv64-linux-gcc) set(CMAKE_CXX_COMPILER ${RISCV_GCC_INSTALL_ROOT}/bin/riscv64-linux-g++) # fix toolchain macro - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__ANDES=1") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__ANDES=1") - # enable rvp -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64gc -mext-dsp") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gc -mext-dsp") +set(CMAKE_C_FLAGS_INIT "-march=rv64gc -mext-dsp -D__ANDES=1") +set(CMAKE_CXX_FLAGS_INIT "-march=rv64gc -mext-dsp -D__ANDES=1") # fix segment address -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment=0x50000") -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Ttext-segment=0x50000") +set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-Ttext-segment=0x50000") +set(CMAKE_SHARED_LINKER_FLAGS_INIT "-Wl,-Ttext-segment=0x50000") diff --git a/platforms/linux/riscv64-clang.toolchain.cmake b/platforms/linux/riscv64-clang.toolchain.cmake index 612be05eab..939350fcbd 100644 --- a/platforms/linux/riscv64-clang.toolchain.cmake +++ b/platforms/linux/riscv64-clang.toolchain.cmake @@ -17,8 +17,13 @@ set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE}) # Don't run the linker on compiler check set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) -set(CMAKE_C_FLAGS "-march=rv64gc --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w ${CMAKE_C_FLAGS}") -set(CMAKE_CXX_FLAGS "-march=rv64gc --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w ${CMAKE_CXX_FLAGS}") +include("${CMAKE_CURRENT_LIST_DIR}/flags-riscv64.cmake") +if(COMMAND ocv_set_platform_flags) + ocv_set_platform_flags(CMAKE_CXX_FLAGS_INIT) + ocv_set_platform_flags(CMAKE_C_FLAGS_INIT) +endif() +set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w") +set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w") set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) diff --git a/platforms/linux/riscv64-gcc.toolchain.cmake b/platforms/linux/riscv64-gcc.toolchain.cmake index c3a0e161e3..7a067d3f1a 100644 --- a/platforms/linux/riscv64-gcc.toolchain.cmake +++ b/platforms/linux/riscv64-gcc.toolchain.cmake @@ -1,10 +1,11 @@ set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR riscv64) set(GNU_MACHINE riscv64-unknown-linux-gnu CACHE STRING "GNU compiler triple") -if(NOT DEFINED CMAKE_CXX_FLAGS) # guards toolchain multiple calls - set(CMAKE_C_FLAGS "-march=rv64gc") - set(CMAKE_CXX_FLAGS "-march=rv64gc") +include("${CMAKE_CURRENT_LIST_DIR}/flags-riscv64.cmake") +if(COMMAND ocv_set_platform_flags) + ocv_set_platform_flags(CMAKE_CXX_FLAGS_INIT) + ocv_set_platform_flags(CMAKE_C_FLAGS_INIT) endif() include("${CMAKE_CURRENT_LIST_DIR}/riscv-gnu.toolchain.cmake")