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=<some_cpu>" ; b) "-march=<arch description>" (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`
pull/26146/head
Maksim Shabunin 3 months ago committed by GitHub
parent 85923c8f30
commit 4c81e174bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      CMakeLists.txt
  2. 72
      cmake/OpenCVCompilerOptimizations.cmake
  3. 11
      cmake/OpenCVCompilerOptions.cmake
  4. 2
      cmake/OpenCVMinDepVersions.cmake
  5. 10
      cmake/platforms/OpenCV-Linux.cmake
  6. 37
      platforms/linux/arm.toolchain.cmake
  7. 19
      platforms/linux/flags-aarch64.cmake
  8. 9
      platforms/linux/flags-riscv64.cmake
  9. 12
      platforms/linux/riscv64-andes-gcc.toolchain.cmake
  10. 9
      platforms/linux/riscv64-clang.toolchain.cmake
  11. 9
      platforms/linux/riscv64-gcc.toolchain.cmake

@ -145,7 +145,7 @@ if(NOT OPENCV_SKIP_CMAKE_SYSTEM_FILE)
endif() endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) # https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT.html 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) if(WIN32)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory" FORCE) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory" FORCE)
else() 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_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_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) 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(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(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 ) OCV_OPTION(ANDROID_EXAMPLES_WITH_LIBS "Build binaries of Android examples with native libraries" OFF IF ANDROID )

@ -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_FMA3 FMA3 ON)
ocv_optimization_process_obsolete_option(ENABLE_VFPV3 VFPV3 OFF) 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) 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) set(CPU_BASELINE_DETECT ON)
endif() 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|<empty>, "
"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) 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_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") ocv_update(CPU_AVX512_COMMON_GROUP "AVX_512F;AVX_512CD")
@ -330,6 +352,7 @@ if(X86 OR X86_64)
endif() endif()
elseif(ARM OR AARCH64) elseif(ARM OR AARCH64)
ocv_update(CPU_NEON_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_neon.cpp") 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_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") 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") ocv_update(CPU_FP16_IMPLIES "NEON")
else() else()
ocv_update(CPU_KNOWN_OPTIMIZATIONS "NEON;FP16;NEON_DOTPROD;NEON_FP16;NEON_BF16") 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_FP16_IMPLIES "NEON")
ocv_update(CPU_NEON_DOTPROD_IMPLIES "NEON") ocv_update(CPU_NEON_DOTPROD_IMPLIES "NEON")
ocv_update(CPU_NEON_FP16_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_FP16_FLAGS_ON "-march=armv8.2-a+fp16")
ocv_update(CPU_NEON_BF16_FLAGS_ON "-march=armv8.2-a+bf16") ocv_update(CPU_NEON_BF16_FLAGS_ON "-march=armv8.2-a+bf16")
endif() 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}") set(CPU_DISPATCH "NEON_FP16;NEON_BF16;NEON_DOTPROD" CACHE STRING "${HELP_CPU_DISPATCH}")
ocv_default_baseline_detect_and_check_dispatch()
endif() endif()
elseif(MIPS) elseif(MIPS)
ocv_update(CPU_MSA_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_msa.cpp") ocv_update(CPU_MSA_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_msa.cpp")
ocv_update(CPU_KNOWN_OPTIMIZATIONS "MSA") ocv_update(CPU_KNOWN_OPTIMIZATIONS "MSA")
ocv_update(CPU_MSA_FLAGS_ON "-mmsa") ocv_update(CPU_MSA_FLAGS_ON "-mmsa")
set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}") set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
elseif(PPC64LE) elseif(PPC64LE)
ocv_update(CPU_KNOWN_OPTIMIZATIONS "VSX;VSX3") ocv_update(CPU_KNOWN_OPTIMIZATIONS "VSX;VSX3")
ocv_update(CPU_VSX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx.cpp") 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") 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}") set(CPU_BASELINE "VSX" CACHE STRING "${HELP_CPU_BASELINE}")
elseif(RISCV) 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}") ocv_update(CPU_KNOWN_OPTIMIZATIONS "RVV")
set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}") ocv_update(CPU_RVV_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_rvv.cpp")
ocv_default_baseline_detect_and_check_dispatch()
elseif(LOONGARCH64) elseif(LOONGARCH64)
ocv_update(CPU_LSX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_lsx.cpp") 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_LASX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_lasx.cpp")
ocv_update(CPU_KNOWN_OPTIMIZATIONS "LSX;LASX") ocv_update(CPU_KNOWN_OPTIMIZATIONS "LSX;LASX")
@ -440,7 +464,7 @@ macro(ocv_check_compiler_optimization OPT)
set(_varname "") set(_varname "")
if(CPU_${OPT}_TEST_FILE) if(CPU_${OPT}_TEST_FILE)
set(__available 0) 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") set(_varname "HAVE_CPU_${OPT}_SUPPORT")
ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" "${_varname}" "${CPU_${OPT}_TEST_FILE}") ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" "${_varname}" "${CPU_${OPT}_TEST_FILE}")
if(${_varname}) if(${_varname})
@ -478,23 +502,6 @@ macro(ocv_check_compiler_optimization OPT)
endif() endif()
endmacro() 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}) foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS})
set(CPU_${OPT}_USAGE_COUNT 0 CACHE INTERNAL "") set(CPU_${OPT}_USAGE_COUNT 0 CACHE INTERNAL "")
if("${CPU_${OPT}_FLAGS_ON}" STREQUAL "disabled") if("${CPU_${OPT}_FLAGS_ON}" STREQUAL "disabled")
@ -577,7 +584,7 @@ foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS})
if(CPU_${OPT}_SUPPORTED) if(CPU_${OPT}_SUPPORTED)
if(";${CPU_DISPATCH};" MATCHES ";${OPT};" AND NOT __is_from_baseline) if(";${CPU_DISPATCH};" MATCHES ";${OPT};" AND NOT __is_from_baseline)
list(APPEND CPU_DISPATCH_FINAL ${OPT}) 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};") if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
list(APPEND CPU_BASELINE_FINAL ${OPT}) list(APPEND CPU_BASELINE_FINAL ${OPT})
endif() endif()
@ -588,15 +595,6 @@ foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS})
endif() endif()
endforeach() 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}) foreach(OPT ${CPU_BASELINE_REQUIRE})
if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};") if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
message(SEND_ERROR "Required baseline optimization is not supported: ${OPT} (CPU_BASELINE_REQUIRE=${CPU_BASELINE_REQUIRE})") message(SEND_ERROR "Required baseline optimization is not supported: ${OPT} (CPU_BASELINE_REQUIRE=${CPU_BASELINE_REQUIRE})")

@ -77,6 +77,17 @@ macro(add_env_definitions option)
add_definitions("-D${option}=\"${value}\"") add_definitions("-D${option}=\"${value}\"")
endmacro() 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) if(NOT MSVC)
# OpenCV fails some tests when 'char' is 'unsigned' by default # OpenCV fails some tests when 'char' is 'unsigned' by default
add_extra_compiler_option(-fsigned-char) add_extra_compiler_option(-fsigned-char)

@ -1,5 +1,5 @@
if(NOT DEFINED MIN_VER_CMAKE) if(NOT DEFINED MIN_VER_CMAKE)
set(MIN_VER_CMAKE 3.5.1) set(MIN_VER_CMAKE 3.7)
endif() endif()
set(MIN_VER_CUDA 6.5) set(MIN_VER_CUDA 6.5)
set(MIN_VER_CUDNN 7.5) set(MIN_VER_CUDNN 7.5)

@ -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()

@ -48,32 +48,23 @@ if(NOT DEFINED ARM_LINUX_SYSROOT AND DEFINED GNU_MACHINE)
set(ARM_LINUX_SYSROOT /usr/${GNU_MACHINE}${FLOAT_ABI_SUFFIX}) set(ARM_LINUX_SYSROOT /usr/${GNU_MACHINE}${FLOAT_ABI_SUFFIX})
endif() endif()
if(NOT DEFINED CMAKE_CXX_FLAGS) # == Compiler flags
set(CMAKE_CXX_FLAGS "" CACHE INTERNAL "") if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
set(CMAKE_C_FLAGS "" CACHE INTERNAL "") set(CMAKE_CXX_FLAGS_INIT "-mthumb")
set(CMAKE_SHARED_LINKER_FLAGS "" CACHE INTERNAL "") set(CMAKE_C_FLAGS_INIT "-mthumb")
set(CMAKE_MODULE_LINKER_FLAGS "" CACHE INTERNAL "") set(common_ld_opt "-Wl,--fix-cortex-a8")
set(CMAKE_EXE_LINKER_FLAGS "" CACHE INTERNAL "") set(CMAKE_SHARED_LINKER_FLAGS_INIT "${common_ld_opt}")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "${common_ld_opt}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi") set(CMAKE_EXE_LINKER_FLAGS_INIT "${common_ld_opt} -Wl,-z,nocopyreloc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi") elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm) include("${CMAKE_CURRENT_LIST_DIR}/flags-aarch64.cmake")
set(CMAKE_CXX_FLAGS "-mthumb ${CMAKE_CXX_FLAGS}") if(COMMAND ocv_set_platform_flags)
set(CMAKE_C_FLAGS "-mthumb ${CMAKE_C_FLAGS}") ocv_set_platform_flags(CMAKE_CXX_FLAGS_INIT)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,nocopyreloc") ocv_set_platform_flags(CMAKE_C_FLAGS_INIT)
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")
endif() 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() endif()
if(USE_NEON) if(USE_NEON)
message(WARNING "You use obsolete variable USE_NEON to enable NEON instruction set. Use -DENABLE_NEON=ON instead." ) message(WARNING "You use obsolete variable USE_NEON to enable NEON instruction set. Use -DENABLE_NEON=ON instead." )
set(ENABLE_NEON TRUE) set(ENABLE_NEON TRUE)

@ -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()

@ -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()

@ -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++) set(CMAKE_CXX_COMPILER ${RISCV_GCC_INSTALL_ROOT}/bin/riscv64-linux-g++)
# fix toolchain macro # 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 # enable rvp
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64gc -mext-dsp") set(CMAKE_C_FLAGS_INIT "-march=rv64gc -mext-dsp -D__ANDES=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gc -mext-dsp") set(CMAKE_CXX_FLAGS_INIT "-march=rv64gc -mext-dsp -D__ANDES=1")
# fix segment address # fix segment address
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment=0x50000") set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-Ttext-segment=0x50000")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Ttext-segment=0x50000") set(CMAKE_SHARED_LINKER_FLAGS_INIT "-Wl,-Ttext-segment=0x50000")

@ -17,8 +17,13 @@ set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
# Don't run the linker on compiler check # Don't run the linker on compiler check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_FLAGS "-march=rv64gc --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w ${CMAKE_C_FLAGS}") include("${CMAKE_CURRENT_LIST_DIR}/flags-riscv64.cmake")
set(CMAKE_CXX_FLAGS "-march=rv64gc --gcc-toolchain=${RISCV_GCC_INSTALL_ROOT} -w ${CMAKE_CXX_FLAGS}") 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 ${CMAKE_SYSROOT})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

@ -1,10 +1,11 @@
set(CMAKE_SYSTEM_NAME Linux) 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") set(GNU_MACHINE riscv64-unknown-linux-gnu CACHE STRING "GNU compiler triple")
if(NOT DEFINED CMAKE_CXX_FLAGS) # guards toolchain multiple calls include("${CMAKE_CURRENT_LIST_DIR}/flags-riscv64.cmake")
set(CMAKE_C_FLAGS "-march=rv64gc") if(COMMAND ocv_set_platform_flags)
set(CMAKE_CXX_FLAGS "-march=rv64gc") ocv_set_platform_flags(CMAKE_CXX_FLAGS_INIT)
ocv_set_platform_flags(CMAKE_C_FLAGS_INIT)
endif() endif()
include("${CMAKE_CURRENT_LIST_DIR}/riscv-gnu.toolchain.cmake") include("${CMAKE_CURRENT_LIST_DIR}/riscv-gnu.toolchain.cmake")

Loading…
Cancel
Save