OpenCVCompilerOptimizations.cmake 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. # x86/x86-64 arch:
  2. # SSE / SSE2 (always available on 64-bit CPUs)
  3. # SSE3 / SSSE3
  4. # SSE4_1 / SSE4_2 / POPCNT
  5. # AVX / AVX2 / AVX_512F
  6. # FMA3
  7. #
  8. # AVX512 details: https://en.wikipedia.org/wiki/AVX-512#CPUs_with_AVX-512
  9. #
  10. # CPU features groups:
  11. # AVX512_COMMON (Common instructions AVX-512F/CD for all CPUs that support AVX-512)
  12. # AVX512_KNL (Knights Landing with AVX-512F/CD/ER/PF)
  13. # AVX512_KNM (Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ)
  14. # AVX512_SKX (Skylake-X with AVX-512F/CD/BW/DQ/VL)
  15. # AVX512_CNL (Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI)
  16. # AVX512_CLX (Cascade Lake with AVX-512F/CD/BW/DQ/VL/VNNI)
  17. # AVX512_ICL (Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ/VPCLMULQDQ*/GFNI*/VAES*)
  18. # ppc64le arch:
  19. # VSX (always available on Power8)
  20. # VSX3 (always available on Power9)
  21. # RISC-V arch:
  22. # RVV
  23. # CPU_{opt}_SUPPORTED=ON/OFF - compiler support (possibly with additional flag)
  24. # CPU_{opt}_IMPLIES=<list>
  25. # CPU_{opt}_FORCE=<list> - subset of "implies" list
  26. # CPU_{opt}_GROUP=<list> - similar to "implies" list, but additionally merges compiler flags
  27. # CPU_{opt}_FLAGS_ON=""
  28. # CPU_{opt}_FEATURE_ALIAS - mapping to CV_CPU_* HWFeature enum
  29. # Input variables:
  30. # CPU_BASELINE=<list> - preferred list of baseline optimizations
  31. # CPU_DISPATCH=<list> - preferred list of dispatched optimizations
  32. # Advanced input variables:
  33. # CPU_BASELINE_REQUIRE=<list> - list of required baseline optimizations
  34. # CPU_DISPATCH_REQUIRE=<list> - list of required dispatched optimizations
  35. # CPU_BASELINE_DISABLE=<list> - list of disabled baseline optimizations
  36. # Output variables:
  37. # CPU_BASELINE_FINAL=<list> - final list of enabled compiler optimizations
  38. # CPU_DISPATCH_FINAL=<list> - final list of dispatched optimizations
  39. #
  40. # CPU_DISPATCH_FLAGS_${opt} - flags for source files compiled separately (<name>.avx2.cpp)
  41. #
  42. # CPU_{opt}_ENABLED_DEFAULT=ON/OFF - has compiler support without additional flag (CPU_BASELINE_DETECT=ON only)
  43. set(CPU_ALL_OPTIMIZATIONS "SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2;POPCNT;AVX;FP16;AVX2;FMA3;AVX_512F")
  44. list(APPEND CPU_ALL_OPTIMIZATIONS "AVX512_COMMON;AVX512_KNL;AVX512_KNM;AVX512_SKX;AVX512_CNL;AVX512_CLX;AVX512_ICL")
  45. list(APPEND CPU_ALL_OPTIMIZATIONS NEON VFPV3 FP16 NEON_DOTPROD)
  46. list(APPEND CPU_ALL_OPTIMIZATIONS MSA)
  47. list(APPEND CPU_ALL_OPTIMIZATIONS VSX VSX3)
  48. list(APPEND CPU_ALL_OPTIMIZATIONS RVV)
  49. list(APPEND CPU_ALL_OPTIMIZATIONS LASX)
  50. list(REMOVE_DUPLICATES CPU_ALL_OPTIMIZATIONS)
  51. ocv_update(CPU_VFPV3_FEATURE_ALIAS "")
  52. set(HELP_CPU_BASELINE "Specify list of enabled baseline CPU optimizations")
  53. set(HELP_CPU_BASELINE_REQUIRE "Specify list of required baseline CPU optimizations")
  54. set(HELP_CPU_BASELINE_DISABLE "Specify list of forbidden baseline CPU optimizations")
  55. set(HELP_CPU_DISPATCH "Specify list of dispatched CPU optimizations")
  56. set(HELP_CPU_DISPATCH_REQUIRE "Specify list of required dispatched CPU optimizations")
  57. foreach(var CPU_BASELINE CPU_BASELINE_REQUIRE CPU_BASELINE_DISABLE CPU_DISPATCH CPU_DISPATCH_REQUIRE)
  58. if(DEFINED ${var})
  59. string(REPLACE "," ";" _list "${${var}}")
  60. set(${var} "${_list}" CACHE STRING "${HELP_${var}}" FORCE)
  61. endif()
  62. endforeach()
  63. # process legacy flags
  64. macro(ocv_optimization_process_obsolete_option legacy_flag OPT legacy_warn)
  65. if(DEFINED "${legacy_flag}")
  66. if("${legacy_warn}")
  67. message(STATUS "WARNING: Option ${legacy_flag}='${${legacy_flag}}' is deprecated and should not be used anymore")
  68. message(STATUS " Behaviour of this option is not backward compatible")
  69. message(STATUS " Refer to 'CPU_BASELINE'/'CPU_DISPATCH' CMake options documentation")
  70. endif()
  71. if("${${legacy_flag}}")
  72. if(NOT ";${CPU_BASELINE_REQUIRE};" MATCHES ";${OPT};")
  73. set(CPU_BASELINE_REQUIRE "${CPU_BASELINE_REQUIRE};${OPT}" CACHE STRING "${HELP_CPU_BASELINE_REQUIRE}" FORCE)
  74. endif()
  75. else()
  76. if(NOT ";${CPU_BASELINE_DISABLE};" MATCHES ";${OPT};")
  77. set(CPU_BASELINE_DISABLE "${CPU_BASELINE_DISABLE};${OPT}" CACHE STRING "${HELP_CPU_BASELINE_DISABLE}" FORCE)
  78. endif()
  79. endif()
  80. endif()
  81. endmacro()
  82. ocv_optimization_process_obsolete_option(ENABLE_SSE SSE ON)
  83. ocv_optimization_process_obsolete_option(ENABLE_SSE2 SSE2 ON)
  84. ocv_optimization_process_obsolete_option(ENABLE_SSE3 SSE3 ON)
  85. ocv_optimization_process_obsolete_option(ENABLE_SSSE3 SSSE3 ON)
  86. ocv_optimization_process_obsolete_option(ENABLE_SSE41 SSE4_1 ON)
  87. ocv_optimization_process_obsolete_option(ENABLE_SSE42 SSE4_2 ON)
  88. ocv_optimization_process_obsolete_option(ENABLE_POPCNT POPCNT ON)
  89. ocv_optimization_process_obsolete_option(ENABLE_AVX AVX ON)
  90. ocv_optimization_process_obsolete_option(ENABLE_AVX2 AVX2 ON)
  91. ocv_optimization_process_obsolete_option(ENABLE_FMA3 FMA3 ON)
  92. ocv_optimization_process_obsolete_option(ENABLE_VFPV3 VFPV3 OFF)
  93. ocv_optimization_process_obsolete_option(ENABLE_NEON NEON OFF)
  94. ocv_optimization_process_obsolete_option(ENABLE_VSX VSX ON)
  95. macro(ocv_is_optimization_in_list resultvar check_opt)
  96. set(__checked "")
  97. set(__queue ${ARGN})
  98. set(${resultvar} 0)
  99. while(__queue AND NOT ${resultvar})
  100. list(REMOVE_DUPLICATES __queue)
  101. set(__queue_current ${__queue})
  102. set(__queue "")
  103. foreach(OPT ${__queue_current})
  104. if("x${OPT}" STREQUAL "x${check_opt}")
  105. set(${resultvar} 1)
  106. break()
  107. elseif(NOT ";${__checked};" MATCHES ";${OPT};")
  108. list(APPEND __queue ${CPU_${OPT}_IMPLIES})
  109. endif()
  110. list(APPEND __checked ${OPT})
  111. endforeach()
  112. endwhile()
  113. endmacro()
  114. macro(ocv_is_optimization_in_force_list resultvar check_opt)
  115. set(__checked "")
  116. set(__queue ${ARGN})
  117. set(${resultvar} 0)
  118. while(__queue AND NOT ${resultvar})
  119. list(REMOVE_DUPLICATES __queue)
  120. set(__queue_current ${__queue})
  121. set(__queue "")
  122. foreach(OPT ${__queue_current})
  123. if(OPT STREQUAL "${check_opt}")
  124. set(${resultvar} 1)
  125. break()
  126. elseif(NOT ";${__checked};" MATCHES ";${OPT};")
  127. list(APPEND __queue ${CPU_${OPT}_FORCE})
  128. endif()
  129. list(APPEND __checked ${OPT})
  130. endforeach()
  131. endwhile()
  132. endmacro()
  133. macro(ocv_append_optimization_flag var OPT)
  134. if(CPU_${OPT}_FLAGS_CONFLICT)
  135. string(REGEX REPLACE " ${CPU_${OPT}_FLAGS_CONFLICT}" "" ${var} " ${${var}} ")
  136. string(REGEX REPLACE "^ +" "" ${var} "${${var}}")
  137. endif()
  138. set(${var} "${${var}} ${CPU_${OPT}_FLAGS_ON}")
  139. endmacro()
  140. # Support GCC -march=native or Intel Compiler -xHost flags
  141. if(";${CPU_BASELINE};" MATCHES ";NATIVE;" OR ";${CPU_BASELINE};" MATCHES ";HOST;")
  142. set(CPU_BASELINE_DETECT ON)
  143. set(_add_native_flag ON)
  144. elseif(";${CPU_BASELINE};" MATCHES ";DETECT;")
  145. set(CPU_BASELINE_DETECT ON)
  146. elseif(" ${CMAKE_CXX_FLAGS} " MATCHES " -march=native | -xHost | /QxHost ")
  147. if(DEFINED CPU_BASELINE)
  148. message(STATUS "CPU: Detected '-march=native' or '-xHost' compiler flag. Force CPU_BASELINE=DETECT.")
  149. endif()
  150. set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
  151. set(CPU_BASELINE_DETECT ON)
  152. endif()
  153. if(X86 OR X86_64)
  154. ocv_update(CPU_KNOWN_OPTIMIZATIONS "SSE;SSE2;SSE3;SSSE3;SSE4_1;POPCNT;SSE4_2;FP16;FMA3;AVX;AVX2;AVX_512F;AVX512_COMMON;AVX512_KNL;AVX512_KNM;AVX512_SKX;AVX512_CNL;AVX512_CLX;AVX512_ICL")
  155. ocv_update(CPU_AVX512_COMMON_GROUP "AVX_512F;AVX_512CD")
  156. ocv_update(CPU_AVX512_KNL_GROUP "AVX512_COMMON;AVX512_KNL_EXTRA")
  157. ocv_update(CPU_AVX512_KNM_GROUP "AVX512_KNL;AVX512_KNM_EXTRA;AVX_512VPOPCNTDQ")
  158. ocv_update(CPU_AVX512_SKX_GROUP "AVX512_COMMON;AVX_512VL;AVX_512BW;AVX_512DQ")
  159. ocv_update(CPU_AVX512_CNL_GROUP "AVX512_SKX;AVX_512IFMA;AVX_512VBMI")
  160. ocv_update(CPU_AVX512_CLX_GROUP "AVX512_SKX;AVX_512VNNI")
  161. ocv_update(CPU_AVX512_ICL_GROUP "AVX512_SKX;AVX_512IFMA;AVX_512VBMI;AVX_512VNNI;AVX_512VBMI2;AVX_512BITALG;AVX_512VPOPCNTDQ") # ? VPCLMULQDQ, GFNI, VAES
  162. ocv_update(CPU_SSE_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_sse.cpp")
  163. ocv_update(CPU_SSE2_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_sse2.cpp")
  164. ocv_update(CPU_SSE3_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_sse3.cpp")
  165. ocv_update(CPU_SSSE3_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_ssse3.cpp")
  166. ocv_update(CPU_SSE4_1_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_sse41.cpp")
  167. ocv_update(CPU_SSE4_2_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_sse42.cpp")
  168. ocv_update(CPU_POPCNT_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_popcnt.cpp")
  169. ocv_update(CPU_AVX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx.cpp")
  170. ocv_update(CPU_AVX2_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx2.cpp")
  171. ocv_update(CPU_FP16_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_fp16.cpp")
  172. ocv_update(CPU_AVX_512F_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512.cpp")
  173. ocv_update(CPU_AVX512_COMMON_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512common.cpp")
  174. ocv_update(CPU_AVX512_KNL_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512knl.cpp")
  175. ocv_update(CPU_AVX512_KNM_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512knm.cpp")
  176. ocv_update(CPU_AVX512_SKX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512skx.cpp")
  177. ocv_update(CPU_AVX512_CNL_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512cnl.cpp")
  178. ocv_update(CPU_AVX512_CLX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512clx.cpp")
  179. ocv_update(CPU_AVX512_ICL_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_avx512icl.cpp")
  180. if(NOT OPENCV_CPU_OPT_IMPLIES_IGNORE)
  181. ocv_update(CPU_AVX512_ICL_IMPLIES "AVX512_SKX")
  182. ocv_update(CPU_AVX512_CLX_IMPLIES "AVX512_SKX")
  183. ocv_update(CPU_AVX512_CNL_IMPLIES "AVX512_SKX")
  184. ocv_update(CPU_AVX512_SKX_IMPLIES "AVX512_COMMON")
  185. ocv_update(CPU_AVX512_KNM_IMPLIES "AVX512_KNL")
  186. ocv_update(CPU_AVX512_KNL_IMPLIES "AVX512_COMMON")
  187. ocv_update(CPU_AVX512_COMMON_IMPLIES "AVX_512F")
  188. ocv_update(CPU_AVX_512F_IMPLIES "AVX2")
  189. ocv_update(CPU_AVX_512F_FORCE "") # Don't force other optimizations
  190. ocv_update(CPU_AVX2_IMPLIES "AVX;FMA3;FP16")
  191. ocv_update(CPU_FMA3_IMPLIES "AVX2")
  192. ocv_update(CPU_FMA3_FORCE "") # Don't force other optimizations
  193. ocv_update(CPU_FP16_IMPLIES "AVX")
  194. ocv_update(CPU_FP16_FORCE "") # Don't force other optimizations
  195. ocv_update(CPU_AVX_IMPLIES "SSE4_2")
  196. ocv_update(CPU_SSE4_2_IMPLIES "SSE4_1;POPCNT")
  197. ocv_update(CPU_POPCNT_IMPLIES "SSE4_1")
  198. ocv_update(CPU_POPCNT_FORCE "") # Don't force other optimizations
  199. ocv_update(CPU_SSE4_1_IMPLIES "SSE3;SSSE3")
  200. ocv_update(CPU_SSSE3_IMPLIES "SSE3")
  201. ocv_update(CPU_SSE3_IMPLIES "SSE2")
  202. ocv_update(CPU_SSE2_IMPLIES "SSE")
  203. endif()
  204. if(CV_ICC)
  205. macro(ocv_intel_compiler_optimization_option name unix_flags msvc_flags)
  206. ocv_update(CPU_${name}_FLAGS_NAME "${name}")
  207. if(MSVC)
  208. set(enable_flags "${msvc_flags}")
  209. set(flags_conflict "/arch:[^ ]*|/Qx:[^ ]+")
  210. else()
  211. set(enable_flags "${unix_flags}")
  212. set(flags_conflict "-msse[^ ]*|-mssse3|-mavx[^ ]*|-march[^ ]*|-x[^ ]+")
  213. endif()
  214. ocv_update(CPU_${name}_FLAGS_ON "${enable_flags}")
  215. if(flags_conflict)
  216. ocv_update(CPU_${name}_FLAGS_CONFLICT "${flags_conflict}")
  217. endif()
  218. endmacro()
  219. ocv_intel_compiler_optimization_option(AVX2 "-march=core-avx2" "/arch:CORE-AVX2")
  220. ocv_intel_compiler_optimization_option(FP16 "-mavx" "/arch:AVX")
  221. ocv_intel_compiler_optimization_option(AVX "-mavx" "/arch:AVX")
  222. ocv_intel_compiler_optimization_option(FMA3 "" "")
  223. ocv_intel_compiler_optimization_option(POPCNT "-mpopcnt" "") # -mpopcnt is available since ICC 19.0.0
  224. ocv_intel_compiler_optimization_option(SSE4_2 "-msse4.2" "/arch:SSE4.2")
  225. ocv_intel_compiler_optimization_option(SSE4_1 "-msse4.1" "/arch:SSE4.1")
  226. ocv_intel_compiler_optimization_option(SSE3 "-msse3" "/arch:SSE3")
  227. ocv_intel_compiler_optimization_option(SSSE3 "-mssse3" "/arch:SSSE3")
  228. ocv_intel_compiler_optimization_option(SSE2 "-msse2" "/arch:SSE2")
  229. if(NOT X86_64) # x64 compiler doesn't support /arch:sse
  230. ocv_intel_compiler_optimization_option(SSE "-msse" "/arch:SSE")
  231. endif()
  232. ocv_intel_compiler_optimization_option(AVX_512F "-xCOMMON-AVX512" "/Qx:COMMON-AVX512")
  233. ocv_intel_compiler_optimization_option(AVX512_COMMON "-xCOMMON-AVX512" "/Qx:COMMON-AVX512")
  234. ocv_intel_compiler_optimization_option(AVX512_KNL "-xKNL" "/Qx:KNL")
  235. ocv_intel_compiler_optimization_option(AVX512_KNM "-xKNM" "/Qx:KNM")
  236. ocv_intel_compiler_optimization_option(AVX512_SKX "-xSKYLAKE-AVX512" "/Qx:SKYLAKE-AVX512")
  237. ocv_intel_compiler_optimization_option(AVX512_CNL "-xCANNONLAKE" "/Qx:CANNONLAKE")
  238. ocv_intel_compiler_optimization_option(AVX512_CLX "-xCASCADELAKE" "/Qx:CASCADELAKE")
  239. ocv_intel_compiler_optimization_option(AVX512_ICL "-xICELAKE-CLIENT" "/Qx:ICELAKE-CLIENT")
  240. elseif(CV_GCC OR CV_CLANG)
  241. ocv_update(CPU_AVX2_FLAGS_ON "-mavx2")
  242. ocv_update(CPU_FP16_FLAGS_ON "-mf16c")
  243. ocv_update(CPU_AVX_FLAGS_ON "-mavx")
  244. ocv_update(CPU_FMA3_FLAGS_ON "-mfma")
  245. ocv_update(CPU_POPCNT_FLAGS_ON "-mpopcnt")
  246. ocv_update(CPU_SSE4_2_FLAGS_ON "-msse4.2")
  247. ocv_update(CPU_SSE4_1_FLAGS_ON "-msse4.1")
  248. ocv_update(CPU_SSE3_FLAGS_ON "-msse3")
  249. ocv_update(CPU_SSSE3_FLAGS_ON "-mssse3")
  250. ocv_update(CPU_SSE2_FLAGS_ON "-msse2")
  251. ocv_update(CPU_SSE_FLAGS_ON "-msse")
  252. if(NOT (CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")) # GCC >= 5.0
  253. ocv_update(CPU_AVX_512F_FLAGS_ON "-mavx512f")
  254. ocv_update(CPU_AVX_512CD_FLAGS_ON "-mavx512cd")
  255. ocv_update(CPU_AVX512_KNL_EXTRA_FLAGS_ON "-mavx512er -mavx512pf")
  256. ocv_update(CPU_AVX512_KNM_EXTRA_FLAGS_ON "-mavx5124fmaps -mavx5124vnniw")
  257. ocv_update(CPU_AVX_512BW_FLAGS_ON "-mavx512bw")
  258. ocv_update(CPU_AVX_512DQ_FLAGS_ON "-mavx512dq")
  259. ocv_update(CPU_AVX_512VL_FLAGS_ON "-mavx512vl")
  260. ocv_update(CPU_AVX_512IFMA_FLAGS_ON "-mavx512ifma")
  261. ocv_update(CPU_AVX_512VBMI_FLAGS_ON "-mavx512vbmi")
  262. ocv_update(CPU_AVX_512VNNI_FLAGS_ON "-mavx512vnni")
  263. ocv_update(CPU_AVX_512VBMI2_FLAGS_ON "-mavx512vbmi2")
  264. ocv_update(CPU_AVX_512BITALG_FLAGS_ON "-mavx512bitalg")
  265. ocv_update(CPU_AVX_512VPOPCNTDQ_FLAGS_ON "-mavx512vpopcntdq")
  266. else()
  267. ocv_update(CPU_AVX_512F_SUPPORTED OFF)
  268. endif()
  269. elseif(MSVC)
  270. ocv_update(CPU_AVX2_FLAGS_ON "/arch:AVX2")
  271. ocv_update(CPU_AVX_FLAGS_ON "/arch:AVX")
  272. ocv_update(CPU_FP16_FLAGS_ON "/arch:AVX")
  273. if(NOT X86_64)
  274. # 64-bit MSVC compiler uses SSE/SSE2 by default
  275. ocv_update(CPU_SSE_FLAGS_ON "/arch:SSE")
  276. ocv_update(CPU_SSE_SUPPORTED ON)
  277. ocv_update(CPU_SSE2_FLAGS_ON "/arch:SSE2")
  278. ocv_update(CPU_SSE2_SUPPORTED ON)
  279. else()
  280. ocv_update(CPU_SSE_SUPPORTED ON)
  281. ocv_update(CPU_SSE2_SUPPORTED ON)
  282. ocv_update(CPU_AVX_512F_FLAGS_ON "/arch:AVX512")
  283. endif()
  284. # Other instruction sets are supported by default since MSVC 2008 at least
  285. else()
  286. message(WARNING "TODO: Unsupported compiler")
  287. endif()
  288. if(NOT DEFINED CPU_DISPATCH)
  289. if(X86_64)
  290. set(CPU_DISPATCH "SSE4_1;SSE4_2;AVX;FP16;AVX2;AVX512_SKX" CACHE STRING "${HELP_CPU_DISPATCH}")
  291. else()
  292. set(CPU_DISPATCH "SSE4_1;SSE4_2;AVX;FP16" CACHE STRING "${HELP_CPU_DISPATCH}")
  293. endif()
  294. endif()
  295. if(NOT DEFINED CPU_BASELINE)
  296. if(APPLE)
  297. # MacOS X has limited set of possible supported H/W, so compiler is configured well
  298. set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
  299. elseif(X86_64)
  300. set(CPU_BASELINE "SSE3" CACHE STRING "${HELP_CPU_BASELINE}")
  301. else()
  302. set(CPU_BASELINE "SSE2" CACHE STRING "${HELP_CPU_BASELINE}")
  303. endif()
  304. endif()
  305. elseif(ARM OR AARCH64)
  306. ocv_update(CPU_NEON_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_neon.cpp")
  307. ocv_update(CPU_FP16_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_fp16.cpp")
  308. ocv_update(CPU_NEON_DOTPROD_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_dotprod.cpp")
  309. if(NOT AARCH64)
  310. ocv_update(CPU_KNOWN_OPTIMIZATIONS "VFPV3;NEON;FP16")
  311. if(NOT MSVC)
  312. ocv_update(CPU_VFPV3_FLAGS_ON "-mfpu=vfpv3")
  313. ocv_update(CPU_NEON_FLAGS_ON "-mfpu=neon")
  314. ocv_update(CPU_NEON_FLAGS_CONFLICT "-mfpu=[^ ]*")
  315. ocv_update(CPU_FP16_FLAGS_ON "-mfpu=neon-fp16 -mfp16-format=ieee")
  316. ocv_update(CPU_FP16_FLAGS_CONFLICT "-mfpu=[^ ]*")
  317. endif()
  318. ocv_update(CPU_FP16_IMPLIES "NEON")
  319. else()
  320. ocv_update(CPU_KNOWN_OPTIMIZATIONS "NEON;FP16;NEON_DOTPROD")
  321. ocv_update(CPU_NEON_FLAGS_ON "")
  322. ocv_update(CPU_FP16_IMPLIES "NEON")
  323. ocv_update(CPU_NEON_DOTPROD_FLAGS_ON "-march=armv8.2-a+dotprod")
  324. ocv_update(CPU_NEON_DOTPROD_IMPLIES "NEON")
  325. set(CPU_BASELINE "NEON;FP16" CACHE STRING "${HELP_CPU_BASELINE}")
  326. endif()
  327. elseif(MIPS)
  328. ocv_update(CPU_MSA_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_msa.cpp")
  329. ocv_update(CPU_KNOWN_OPTIMIZATIONS "MSA")
  330. ocv_update(CPU_MSA_FLAGS_ON "-mmsa")
  331. set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
  332. elseif(PPC64LE)
  333. ocv_update(CPU_KNOWN_OPTIMIZATIONS "VSX;VSX3")
  334. ocv_update(CPU_VSX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx.cpp")
  335. ocv_update(CPU_VSX3_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx3.cpp")
  336. if(NOT OPENCV_CPU_OPT_IMPLIES_IGNORE)
  337. ocv_update(CPU_VSX3_IMPLIES "VSX")
  338. endif()
  339. if(CV_CLANG AND (NOT ${CMAKE_CXX_COMPILER} MATCHES "xlc"))
  340. ocv_update(CPU_VSX_FLAGS_ON "-mvsx -maltivec")
  341. ocv_update(CPU_VSX3_FLAGS_ON "-mpower9-vector")
  342. else()
  343. ocv_update(CPU_VSX_FLAGS_ON "-mcpu=power8")
  344. ocv_update(CPU_VSX3_FLAGS_ON "-mcpu=power9 -mtune=power9")
  345. endif()
  346. set(CPU_DISPATCH "VSX3" CACHE STRING "${HELP_CPU_DISPATCH}")
  347. set(CPU_BASELINE "VSX" CACHE STRING "${HELP_CPU_BASELINE}")
  348. elseif(RISCV)
  349. option(RISCV_RVV_SCALABLE "Use scalable RVV API on RISC-V" ON)
  350. ocv_update(CPU_RVV_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_rvv.cpp")
  351. ocv_update(CPU_KNOWN_OPTIMIZATIONS "RVV")
  352. ocv_update(CPU_RVV_FLAGS_ON "-march=rv64gcv")
  353. if(RISCV_RVV_SCALABLE)
  354. set(CPU_RVV_FLAGS_ON "${CPU_RVV_FLAGS_ON} -DCV_RVV_SCALABLE")
  355. endif()
  356. ocv_update(CPU_RVV_FLAGS_CONFLICT "-march=[^ ]*")
  357. set(CPU_DISPATCH "" CACHE STRING "${HELP_CPU_DISPATCH}")
  358. set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
  359. elseif(LOONGARCH64)
  360. ocv_update(CPU_LASX_TEST_FILE "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_lasx.cpp")
  361. ocv_update(CPU_KNOWN_OPTIMIZATIONS "LASX")
  362. ocv_update(CPU_LASX_FLAGS_ON "-mlasx")
  363. set(CPU_BASELINE "LASX" CACHE STRING "${HELP_CPU_BASELINE}")
  364. endif()
  365. # Helper values for cmake-gui
  366. set(CPU_BASELINE "DETECT" CACHE STRING "${HELP_CPU_BASELINE}")
  367. set(CPU_DISPATCH "" CACHE STRING "${HELP_CPU_DISPATCH}")
  368. set_property(CACHE CPU_BASELINE PROPERTY STRINGS "" ${CPU_KNOWN_OPTIMIZATIONS})
  369. set_property(CACHE CPU_DISPATCH PROPERTY STRINGS "" ${CPU_KNOWN_OPTIMIZATIONS})
  370. # Update CPU_BASELINE_DETECT flag
  371. if(";${CPU_BASELINE};" MATCHES ";DETECT;")
  372. set(CPU_BASELINE_DETECT ON)
  373. endif()
  374. set(CPU_BASELINE_FLAGS "")
  375. set(CPU_BASELINE_FINAL "")
  376. set(CPU_DISPATCH_FINAL "")
  377. if(CV_DISABLE_OPTIMIZATION)
  378. set(CPU_DISPATCH "")
  379. set(CPU_DISPATCH_REQUIRE "")
  380. endif()
  381. if("x${CPU_DISPATCH}" STREQUAL "xALL")
  382. set(CPU_DISPATCH "${CPU_KNOWN_OPTIMIZATIONS}")
  383. endif()
  384. macro(ocv_check_compiler_optimization OPT)
  385. if(NOT DEFINED CPU_${OPT}_SUPPORTED)
  386. if((DEFINED CPU_${OPT}_FLAGS_ON AND NOT "x${CPU_${OPT}_FLAGS_ON}" STREQUAL "x") OR CPU_${OPT}_TEST_FILE)
  387. set(_varname "")
  388. if(CPU_${OPT}_TEST_FILE)
  389. set(__available 0)
  390. if(CPU_BASELINE_DETECT)
  391. set(_varname "HAVE_CPU_${OPT}_SUPPORT")
  392. ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" "${_varname}" "${CPU_${OPT}_TEST_FILE}")
  393. if(${_varname})
  394. list(APPEND CPU_BASELINE_FINAL ${OPT})
  395. set(CPU_${OPT}_ENABLED_DEFAULT ON)
  396. set(__available 1)
  397. endif()
  398. endif()
  399. if(NOT __available)
  400. if(NOT "x${CPU_${OPT}_FLAGS_NAME}" STREQUAL "x")
  401. set(_varname "HAVE_CPU_${CPU_${OPT}_FLAGS_NAME}")
  402. set(_compile_flags "${CPU_BASELINE_FLAGS}")
  403. ocv_append_optimization_flag(_compile_flags ${OPT})
  404. ocv_check_compiler_flag(CXX "${_compile_flags}" "${_varname}" "${CPU_${OPT}_TEST_FILE}")
  405. elseif(NOT "x${CPU_${OPT}_FLAGS_ON}" STREQUAL "x")
  406. ocv_check_flag_support(CXX "${CPU_${OPT}_FLAGS_ON}" _varname "" "${CPU_${OPT}_TEST_FILE}")
  407. else()
  408. set(_varname "HAVE_CPU_${OPT}_SUPPORT")
  409. set(_compile_flags "${CPU_BASELINE_FLAGS}")
  410. ocv_append_optimization_flag(_compile_flags ${OPT})
  411. ocv_check_compiler_flag(CXX "${_compile_flags}" "${_varname}" "${CPU_${OPT}_TEST_FILE}")
  412. endif()
  413. endif()
  414. else()
  415. ocv_check_flag_support(CXX "${CPU_${OPT}_FLAGS_ON}" _varname "")
  416. endif()
  417. if(_varname AND ${_varname})
  418. set(CPU_${OPT}_SUPPORTED ON)
  419. elseif(NOT CPU_${OPT}_SUPPORTED)
  420. message(STATUS "${OPT} is not supported by C++ compiler")
  421. endif()
  422. else()
  423. set(CPU_${OPT}_SUPPORTED ON)
  424. endif()
  425. endif()
  426. endmacro()
  427. foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS})
  428. set(CPU_${OPT}_USAGE_COUNT 0 CACHE INTERNAL "")
  429. if("${CPU_${OPT}_FLAGS_ON}" STREQUAL "disabled")
  430. set(CPU_${OPT}_SUPPORTED OFF)
  431. elseif(DEFINED CPU_${OPT}_GROUP)
  432. if(NOT DEFINED CPU_${OPT}_IMPLIES)
  433. set(CPU_${OPT}_IMPLIES "${CPU_${OPT}_GROUP}")
  434. endif()
  435. set(__disabled 0)
  436. set(__flags "")
  437. foreach(OPT2 ${CPU_${OPT}_GROUP})
  438. if("${CPU_${OPT2}_FLAGS_ON}" STREQUAL "disabled" OR (DEFINED CPU_${OPT2}_SUPPORTED AND NOT CPU_${OPT}_SUPPORTED))
  439. set(__disabled 1)
  440. endif()
  441. set(__flags "${__flags} ${CPU_${OPT2}_FLAGS_ON}")
  442. string(STRIP "${__flags}" __flags)
  443. endforeach()
  444. if(__disabled)
  445. set(CPU_${OPT}_SUPPORTED OFF)
  446. else()
  447. if(NOT DEFINED CPU_${OPT}_FLAGS_ON)
  448. set(CPU_${OPT}_FLAGS_ON "${__flags}")
  449. endif()
  450. endif()
  451. endif()
  452. if(NOT DEFINED CPU_${OPT}_FORCE)
  453. set(CPU_${OPT}_FORCE "${CPU_${OPT}_IMPLIES}")
  454. endif()
  455. #message("${OPT}: CPU_${OPT}_FLAGS_ON=${CPU_${OPT}_FLAGS_ON}")
  456. endforeach()
  457. if(_add_native_flag)
  458. set(_varname "HAVE_CPU_NATIVE_SUPPORT")
  459. ocv_check_compiler_flag(CXX "-march=native" "${_varname}" "")
  460. if(${_varname})
  461. set(CPU_BASELINE_FLAGS "${CPU_BASELINE_FLAGS} -march=native")
  462. else()
  463. set(_varname "HAVE_CPU_HOST_SUPPORT")
  464. if(MSVC)
  465. set(_flag "/QxHost")
  466. else()
  467. set(_flag "-xHost")
  468. endif()
  469. ocv_check_compiler_flag(CXX "${_flag}" "${_varname}" "")
  470. if(${_varname})
  471. set(CPU_BASELINE_FLAGS "${CPU_BASELINE_FLAGS} ${_flag}")
  472. endif()
  473. endif()
  474. endif()
  475. foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS})
  476. set(__is_disabled 0)
  477. foreach(OPT2 ${CPU_BASELINE_DISABLE})
  478. ocv_is_optimization_in_list(__is_disabled ${OPT2} ${OPT})
  479. if(__is_disabled)
  480. break()
  481. endif()
  482. endforeach()
  483. if(__is_disabled)
  484. set(__is_from_baseline 0)
  485. else()
  486. if(CPU_${OPT}_SUPPORTED AND CPU_BASELINE_DETECT)
  487. list(APPEND CPU_BASELINE_FINAL ${OPT})
  488. endif()
  489. ocv_is_optimization_in_list(__is_from_baseline ${OPT} ${CPU_BASELINE_REQUIRE})
  490. if(NOT __is_from_baseline)
  491. ocv_is_optimization_in_list(__is_from_baseline ${OPT} ${CPU_BASELINE})
  492. endif()
  493. endif()
  494. ocv_is_optimization_in_list(__is_from_dispatch ${OPT} ${CPU_DISPATCH_REQUIRE})
  495. if(NOT __is_from_dispatch)
  496. ocv_is_optimization_in_list(__is_from_dispatch ${OPT} ${CPU_DISPATCH})
  497. endif()
  498. if(__is_from_dispatch OR __is_from_baseline OR CPU_BASELINE_DETECT)
  499. ocv_check_compiler_optimization(${OPT})
  500. endif()
  501. if(CPU_BASELINE_DETECT AND NOT __is_from_baseline AND NOT __is_disabled)
  502. ocv_is_optimization_in_list(__is_from_baseline ${OPT} ${CPU_BASELINE_FINAL})
  503. endif()
  504. if(CPU_${OPT}_SUPPORTED)
  505. if(";${CPU_DISPATCH};" MATCHES ";${OPT};" AND NOT __is_from_baseline)
  506. list(APPEND CPU_DISPATCH_FINAL ${OPT})
  507. elseif(__is_from_baseline)
  508. if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
  509. list(APPEND CPU_BASELINE_FINAL ${OPT})
  510. endif()
  511. if(NOT CPU_${OPT}_ENABLED_DEFAULT) # Don't change compiler flags in 'detection' mode
  512. ocv_append_optimization_flag(CPU_BASELINE_FLAGS ${OPT})
  513. endif()
  514. endif()
  515. endif()
  516. endforeach()
  517. foreach(OPT ${CPU_BASELINE_REQUIRE})
  518. if(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
  519. message(SEND_ERROR "Required baseline optimization is not supported: ${OPT} (CPU_BASELINE_REQUIRE=${CPU_BASELINE_REQUIRE})")
  520. endif()
  521. endforeach()
  522. foreach(OPT ${CPU_BASELINE})
  523. if(OPT STREQUAL "DETECT" OR OPT STREQUAL "HOST" OR OPT STREQUAL "NATIVE")
  524. # nothing
  525. elseif(NOT ";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
  526. message(STATUS "Optimization ${OPT} is not available, skipped")
  527. endif()
  528. endforeach()
  529. foreach(OPT ${CPU_DISPATCH_REQUIRE})
  530. if(";${CPU_DISPATCH_FINAL};" MATCHES ";${OPT};")
  531. # OK
  532. elseif(";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
  533. message(SEND_ERROR "Dispatched optimization ${OPT} is in baseline list (CPU_DISPATCH_REQUIRE=${CPU_DISPATCH_REQUIRE})")
  534. else()
  535. message(SEND_ERROR "Required dispatch optimization is not supported: ${OPT} (CPU_DISPATCH_REQUIRE=${CPU_DISPATCH_REQUIRE})")
  536. endif()
  537. endforeach()
  538. foreach(OPT ${CPU_DISPATCH})
  539. if(";${CPU_DISPATCH_FINAL};" MATCHES ";${OPT};")
  540. # OK
  541. elseif(";${CPU_BASELINE_FINAL};" MATCHES ";${OPT};")
  542. # OK
  543. else()
  544. message(STATUS "Dispatch optimization ${OPT} is not available, skipped")
  545. endif()
  546. endforeach()
  547. #message(STATUS "CPU_BASELINE_FINAL=${CPU_BASELINE_FINAL}")
  548. #message(STATUS "CPU_DISPATCH_FINAL=${CPU_DISPATCH_FINAL}")
  549. #if(CPU_DISPATCH_FINAL AND NOT PYTHON_DEFAULT_EXECUTABLE)
  550. # message(FATAL_ERROR "Python is required for CPU dispatched optimization support")
  551. #endif()
  552. macro(ocv_compiler_optimization_options)
  553. set(__flags "${OPENCV_EXTRA_CXX_FLAGS} ${CPU_BASELINE_FLAGS}")
  554. if(NOT __flags STREQUAL CACHED_CPU_BASELINE_FLAGS)
  555. set(CACHED_CPU_BASELINE_FLAGS "${__flags}" CACHE INTERNAL "" FORCE)
  556. ocv_clear_vars(HAVE_CPU_BASELINE_FLAGS)
  557. endif()
  558. ocv_check_compiler_flag(CXX "${__flags}" HAVE_CPU_BASELINE_FLAGS)
  559. if(NOT HAVE_CPU_BASELINE_FLAGS)
  560. message(FATAL_ERROR "Compiler doesn't support baseline optimization flags: ${CPU_BASELINE_FLAGS}")
  561. endif()
  562. add_extra_compiler_option_force("${CPU_BASELINE_FLAGS}")
  563. foreach(OPT ${CPU_DISPATCH_FINAL})
  564. set(__dispatch_flags "")
  565. set(__dispatch_definitions "")
  566. set(__dispatch_opts "")
  567. set(__dispatch_opts_force "")
  568. foreach(OPT2 ${CPU_KNOWN_OPTIMIZATIONS})
  569. if(NOT CPU_${OPT2}_SUPPORTED)
  570. #continue()
  571. else()
  572. ocv_is_optimization_in_list(__is_from_baseline ${OPT2} ${CPU_BASELINE_FINAL})
  573. if(NOT __is_from_baseline)
  574. ocv_is_optimization_in_list(__is_active ${OPT2} ${OPT})
  575. if(__is_active)
  576. ocv_append_optimization_flag(__dispatch_flags ${OPT2})
  577. list(APPEND __dispatch_definitions "CV_CPU_COMPILE_${OPT2}=1")
  578. list(APPEND __dispatch_opts "${OPT2}")
  579. endif()
  580. ocv_is_optimization_in_force_list(__is_force ${OPT2} ${OPT})
  581. if(__is_force)
  582. list(APPEND __dispatch_opts_force "${OPT2}")
  583. endif()
  584. endif()
  585. endif()
  586. endforeach()
  587. set(__flags "${OPENCV_EXTRA_CXX_FLAGS} ${__dispatch_flags}")
  588. if(NOT __flags STREQUAL CACHED_CPU_DISPATCH_${OPT}_FLAGS)
  589. set(CACHED_CPU_DISPATCH_${OPT}_FLAGS "${__flags}" CACHE INTERNAL "" FORCE)
  590. ocv_clear_vars(HAVE_CPU_DISPATCH_FLAGS_${OPT})
  591. endif()
  592. ocv_check_compiler_flag(CXX "${__flags}" HAVE_CPU_DISPATCH_FLAGS_${OPT})
  593. if(NOT HAVE_CPU_DISPATCH_FLAGS_${OPT})
  594. message(FATAL_ERROR "Compiler doesn't support optimization flags for ${OPT} dispatch mode: ${__dispatch_flags}")
  595. endif()
  596. set(CPU_DISPATCH_FLAGS_${OPT} "${__dispatch_flags}")
  597. set(CPU_DISPATCH_DEFINITIONS_${OPT} "${__dispatch_definitions}")
  598. set(CPU_DISPATCH_${OPT}_INCLUDED "${__dispatch_opts}")
  599. set(CPU_DISPATCH_${OPT}_FORCED "${__dispatch_opts_force}")
  600. endforeach()
  601. if(ENABLE_POWERPC)
  602. add_extra_compiler_option("-mcpu=G3 -mtune=G5")
  603. endif()
  604. endmacro()
  605. macro(ocv_compiler_optimization_options_finalize)
  606. if((CV_GCC OR CV_CLANG) AND (X86 OR X86_64))
  607. if(NOT APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 4)
  608. if(OPENCV_EXTRA_CXX_FLAGS MATCHES "-m(sse2|avx)")
  609. add_extra_compiler_option(-mfpmath=sse) # !! important - be on the same wave with x64 compilers
  610. else()
  611. add_extra_compiler_option(-mfpmath=387)
  612. endif()
  613. endif()
  614. endif()
  615. if(MSVC)
  616. # Generate Intrinsic Functions
  617. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Oi")
  618. endif(MSVC)
  619. endmacro()
  620. macro(ocv_compiler_optimization_process_sources SOURCES_VAR_NAME LIBS_VAR_NAME TARGET_BASE_NAME)
  621. set(__result "")
  622. set(__result_libs "")
  623. foreach(OPT ${CPU_DISPATCH_FINAL})
  624. set(__result_${OPT} "")
  625. endforeach()
  626. foreach(fname ${${SOURCES_VAR_NAME}})
  627. string(TOLOWER "${fname}" fname_LOWER)
  628. get_filename_component(fname_LOWER "${fname_LOWER}" NAME)
  629. if(fname_LOWER MATCHES ".+\\.([^\\.]*)\\.cpp$")
  630. string(TOUPPER "${CMAKE_MATCH_1}" OPT_)
  631. if(OPT_ MATCHES "(CUDA.*|DISPATCH.*|OCL)") # don't touch files like filename.cuda.cpp
  632. list(APPEND __result "${fname}")
  633. #continue()
  634. elseif(CV_DISABLE_OPTIMIZATION OR NOT CV_ENABLE_INTRINSICS)
  635. ocv_get_smart_file_name(fname_ "${fname}")
  636. message(STATUS "Excluding from source files list (optimization is disabled): ${fname_}")
  637. #continue()
  638. else()
  639. get_source_file_property(__definitions "${fname}" COMPILE_DEFINITIONS)
  640. if(__definitions)
  641. list(APPEND __definitions "CV_CPU_DISPATCH_MODE=${OPT_}")
  642. else()
  643. set(__definitions "CV_CPU_DISPATCH_MODE=${OPT_}")
  644. endif()
  645. set_source_files_properties("${fname}" PROPERTIES COMPILE_DEFINITIONS "${__definitions}")
  646. set(__opt_found 0)
  647. foreach(OPT ${CPU_BASELINE_FINAL})
  648. string(TOLOWER "${OPT}" OPT_LOWER)
  649. if(fname_LOWER MATCHES "\\.${OPT_LOWER}\\.cpp$")
  650. #message("${fname} BASELINE-${OPT}")
  651. set(__opt_found 1)
  652. list(APPEND __result_${OPT} "${fname}")
  653. break()
  654. endif()
  655. endforeach()
  656. foreach(OPT ${CPU_DISPATCH_FINAL})
  657. foreach(OPT2 ${CPU_DISPATCH_${OPT}_FORCED})
  658. string(TOLOWER "${OPT2}" OPT2_LOWER)
  659. if(fname_LOWER MATCHES "\\.${OPT2_LOWER}\\.cpp$")
  660. list(APPEND __result_${OPT} "${fname}")
  661. math(EXPR CPU_${OPT}_USAGE_COUNT "${CPU_${OPT}_USAGE_COUNT}+1")
  662. set(CPU_${OPT}_USAGE_COUNT "${CPU_${OPT}_USAGE_COUNT}" CACHE INTERNAL "" FORCE)
  663. #message("(${CPU_${OPT}_USAGE_COUNT})${fname} ${OPT}")
  664. #message(" ${CPU_DISPATCH_${OPT}_INCLUDED}")
  665. #message(" ${CPU_DISPATCH_DEFINITIONS_${OPT}}")
  666. #message(" ${CPU_DISPATCH_FLAGS_${OPT}}")
  667. set(__opt_found 1)
  668. break()
  669. endif()
  670. endforeach()
  671. if(__opt_found)
  672. set(__opt_found 1)
  673. break()
  674. endif()
  675. endforeach()
  676. if(NOT __opt_found)
  677. ocv_get_smart_file_name(fname_ "${fname}")
  678. message(STATUS "Excluding from source files list: ${fname_}")
  679. endif()
  680. endif()
  681. else()
  682. list(APPEND __result "${fname}")
  683. endif()
  684. endforeach()
  685. foreach(OPT ${CPU_BASELINE_FINAL} ${CPU_DISPATCH_FINAL})
  686. if(__result_${OPT})
  687. #message("${OPT}: ${__result_${OPT}}")
  688. if(CMAKE_GENERATOR MATCHES "^Visual"
  689. OR OPENCV_CMAKE_CPU_OPTIMIZATIONS_FORCE_TARGETS
  690. )
  691. # MSVS generator is not able to properly order compilation flags:
  692. # extra flags are added before common flags, so switching between optimizations doesn't work correctly
  693. # Also CMAKE_CXX_FLAGS doesn't work (it is directory-based, so add_subdirectory is required)
  694. add_library(${TARGET_BASE_NAME}_${OPT} OBJECT ${__result_${OPT}})
  695. ocv_append_dependant_targets(${TARGET_BASE_NAME} ${TARGET_BASE_NAME}_${OPT})
  696. set_target_properties(${TARGET_BASE_NAME}_${OPT} PROPERTIES COMPILE_DEFINITIONS "${CPU_DISPATCH_DEFINITIONS_${OPT}}")
  697. set_target_properties(${TARGET_BASE_NAME}_${OPT} PROPERTIES COMPILE_FLAGS "${CPU_DISPATCH_FLAGS_${OPT}}")
  698. target_include_directories(${TARGET_BASE_NAME}_${OPT} PRIVATE $<TARGET_PROPERTY:${TARGET_BASE_NAME},INCLUDE_DIRECTORIES>)
  699. #list(APPEND __result_libs ${TARGET_BASE_NAME}_${OPT})
  700. list(APPEND __result "$<TARGET_OBJECTS:${TARGET_BASE_NAME}_${OPT}>")
  701. if(ENABLE_SOLUTION_FOLDERS)
  702. set_target_properties(${TARGET_BASE_NAME}_${OPT} PROPERTIES FOLDER "dispatched")
  703. endif()
  704. else()
  705. foreach(fname ${__result_${OPT}})
  706. get_source_file_property(__definitions "${fname}" COMPILE_DEFINITIONS)
  707. if(__definitions)
  708. list(APPEND __definitions "${CPU_DISPATCH_DEFINITIONS_${OPT}}")
  709. else()
  710. set(__definitions "${CPU_DISPATCH_DEFINITIONS_${OPT}}")
  711. endif()
  712. set_source_files_properties("${fname}" PROPERTIES COMPILE_DEFINITIONS "${__definitions}")
  713. set_source_files_properties("${fname}" PROPERTIES COMPILE_FLAGS "${CPU_DISPATCH_FLAGS_${OPT}}")
  714. endforeach()
  715. list(APPEND __result ${__result_${OPT}})
  716. endif()
  717. endif()
  718. endforeach()
  719. set(${SOURCES_VAR_NAME} "${__result}")
  720. list(APPEND ${LIBS_VAR_NAME} ${__result_libs})
  721. endmacro()
  722. macro(ocv_compiler_optimization_fill_cpu_config)
  723. set(OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE "")
  724. foreach(OPT ${CPU_BASELINE_FINAL})
  725. set(OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE}
  726. #define CV_CPU_COMPILE_${OPT} 1
  727. #define CV_CPU_BASELINE_COMPILE_${OPT} 1
  728. ")
  729. endforeach()
  730. set(OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE}
  731. #define CV_CPU_BASELINE_FEATURES 0 \\")
  732. foreach(OPT ${CPU_BASELINE_FINAL})
  733. if(NOT DEFINED CPU_${OPT}_FEATURE_ALIAS OR NOT "x${CPU_${OPT}_FEATURE_ALIAS}" STREQUAL "x")
  734. set(OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE}
  735. , CV_CPU_${OPT} \\")
  736. endif()
  737. endforeach()
  738. set(OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_BASELINE_DEFINITIONS_CONFIGMAKE}\n")
  739. set(__dispatch_modes "")
  740. foreach(OPT ${CPU_DISPATCH_FINAL})
  741. list(APPEND __dispatch_modes ${CPU_DISPATCH_${OPT}_FORCE} ${OPT})
  742. endforeach()
  743. list(REMOVE_DUPLICATES __dispatch_modes)
  744. foreach(OPT ${__dispatch_modes})
  745. set(OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE}
  746. #define CV_CPU_DISPATCH_COMPILE_${OPT} 1")
  747. endforeach()
  748. set(OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE}
  749. \n\n#define CV_CPU_DISPATCH_FEATURES 0 \\")
  750. foreach(OPT ${__dispatch_modes})
  751. if(NOT DEFINED CPU_${OPT}_FEATURE_ALIAS OR NOT "x${CPU_${OPT}_FEATURE_ALIAS}" STREQUAL "x")
  752. set(OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE}
  753. , CV_CPU_${OPT} \\")
  754. endif()
  755. endforeach()
  756. set(OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_DISPATCH_DEFINITIONS_CONFIGMAKE}\n")
  757. set(OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE "// AUTOGENERATED, DO NOT EDIT\n")
  758. foreach(OPT ${CPU_ALL_OPTIMIZATIONS})
  759. if(NOT DEFINED CPU_${OPT}_FEATURE_ALIAS OR NOT "x${CPU_${OPT}_FEATURE_ALIAS}" STREQUAL "x")
  760. set(OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE}
  761. #if !defined CV_DISABLE_OPTIMIZATION && defined CV_ENABLE_INTRINSICS && defined CV_CPU_COMPILE_${OPT}
  762. # define CV_TRY_${OPT} 1
  763. # define CV_CPU_FORCE_${OPT} 1
  764. # define CV_CPU_HAS_SUPPORT_${OPT} 1
  765. # define CV_CPU_CALL_${OPT}(fn, args) return (cpu_baseline::fn args)
  766. # define CV_CPU_CALL_${OPT}_(fn, args) return (opt_${OPT}::fn args)
  767. #elif !defined CV_DISABLE_OPTIMIZATION && defined CV_ENABLE_INTRINSICS && defined CV_CPU_DISPATCH_COMPILE_${OPT}
  768. # define CV_TRY_${OPT} 1
  769. # define CV_CPU_FORCE_${OPT} 0
  770. # define CV_CPU_HAS_SUPPORT_${OPT} (cv::checkHardwareSupport(CV_CPU_${OPT}))
  771. # define CV_CPU_CALL_${OPT}(fn, args) if (CV_CPU_HAS_SUPPORT_${OPT}) return (opt_${OPT}::fn args)
  772. # define CV_CPU_CALL_${OPT}_(fn, args) if (CV_CPU_HAS_SUPPORT_${OPT}) return (opt_${OPT}::fn args)
  773. #else
  774. # define CV_TRY_${OPT} 0
  775. # define CV_CPU_FORCE_${OPT} 0
  776. # define CV_CPU_HAS_SUPPORT_${OPT} 0
  777. # define CV_CPU_CALL_${OPT}(fn, args)
  778. # define CV_CPU_CALL_${OPT}_(fn, args)
  779. #endif
  780. #define __CV_CPU_DISPATCH_CHAIN_${OPT}(fn, args, mode, ...) CV_CPU_CALL_${OPT}(fn, args); __CV_EXPAND(__CV_CPU_DISPATCH_CHAIN_ ## mode(fn, args, __VA_ARGS__))
  781. ")
  782. endif()
  783. endforeach()
  784. set(OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE "${OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE}
  785. #define CV_CPU_CALL_BASELINE(fn, args) return (cpu_baseline::fn args)
  786. #define __CV_CPU_DISPATCH_CHAIN_BASELINE(fn, args, mode, ...) CV_CPU_CALL_BASELINE(fn, args) /* last in sequence */
  787. ")
  788. set(__file "${OpenCV_SOURCE_DIR}/modules/core/include/opencv2/core/cv_cpu_helper.h")
  789. if(EXISTS "${__file}")
  790. file(READ "${__file}" __content)
  791. endif()
  792. if(__content STREQUAL OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE)
  793. #message(STATUS "${__file} contains same content")
  794. else()
  795. file(WRITE "${__file}" "${OPENCV_CPU_CONTROL_DEFINITIONS_CONFIGMAKE}")
  796. message(WARNING "${__file} is updated")
  797. endif()
  798. endmacro()
  799. macro(__ocv_add_dispatched_file filename target_src_var src_directory dst_directory precomp_hpp optimizations_var)
  800. if(NOT OPENCV_INITIAL_PASS)
  801. set(__codestr "
  802. #include \"${src_directory}/${precomp_hpp}\"
  803. #include \"${src_directory}/${filename}.simd.hpp\"
  804. ")
  805. set(__declarations_str "#define CV_CPU_SIMD_FILENAME \"${src_directory}/${filename}.simd.hpp\"")
  806. set(__dispatch_modes "BASELINE")
  807. set(__optimizations "${${optimizations_var}}")
  808. if(CV_DISABLE_OPTIMIZATION OR NOT CV_ENABLE_INTRINSICS)
  809. set(__optimizations "")
  810. endif()
  811. foreach(OPT ${__optimizations})
  812. string(TOLOWER "${OPT}" OPT_LOWER)
  813. set(__file "${CMAKE_CURRENT_BINARY_DIR}/${dst_directory}${filename}.${OPT_LOWER}.cpp")
  814. if(EXISTS "${__file}")
  815. file(READ "${__file}" __content)
  816. else()
  817. set(__content "")
  818. endif()
  819. if(__content STREQUAL __codestr)
  820. #message(STATUS "${__file} contains up-to-date content")
  821. else()
  822. file(WRITE "${__file}" "${__codestr}")
  823. endif()
  824. if(";${CPU_DISPATCH_FINAL};" MATCHES "${OPT}" OR __CPU_DISPATCH_INCLUDE_ALL)
  825. if(EXISTS "${src_directory}/${filename}.${OPT_LOWER}.cpp")
  826. message(STATUS "Using overridden ${OPT} source: ${src_directory}/${filename}.${OPT_LOWER}.cpp")
  827. else()
  828. list(APPEND ${target_src_var} "${__file}")
  829. endif()
  830. set(__declarations_str "${__declarations_str}
  831. #define CV_CPU_DISPATCH_MODE ${OPT}
  832. #include \"opencv2/core/private/cv_cpu_include_simd_declarations.hpp\"
  833. ")
  834. set(__dispatch_modes "${OPT}, ${__dispatch_modes}")
  835. endif()
  836. endforeach()
  837. set(__declarations_str "${__declarations_str}
  838. #define CV_CPU_DISPATCH_MODES_ALL ${__dispatch_modes}
  839. #undef CV_CPU_SIMD_FILENAME
  840. ")
  841. set(__file "${CMAKE_CURRENT_BINARY_DIR}/${dst_directory}${filename}.simd_declarations.hpp")
  842. if(EXISTS "${__file}")
  843. file(READ "${__file}" __content)
  844. endif()
  845. if(__content STREQUAL __declarations_str)
  846. #message(STATUS "${__file} contains up-to-date content")
  847. else()
  848. file(WRITE "${__file}" "${__declarations_str}")
  849. endif()
  850. endif()
  851. endmacro()
  852. macro(ocv_add_dispatched_file filename)
  853. set(__optimizations "${ARGN}")
  854. if(" ${ARGV1}" STREQUAL " TEST")
  855. list(REMOVE_AT __optimizations 0)
  856. __ocv_add_dispatched_file("${filename}" "OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED" "${CMAKE_CURRENT_LIST_DIR}/test" "test/" "test_precomp.hpp" __optimizations)
  857. else()
  858. __ocv_add_dispatched_file("${filename}" "OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED" "${CMAKE_CURRENT_LIST_DIR}/src" "" "precomp.hpp" __optimizations)
  859. endif()
  860. endmacro()
  861. # Workaround to support code which always require all code paths
  862. macro(ocv_add_dispatched_file_force_all)
  863. set(__CPU_DISPATCH_INCLUDE_ALL 1)
  864. ocv_add_dispatched_file(${ARGN})
  865. unset(__CPU_DISPATCH_INCLUDE_ALL)
  866. endmacro()
  867. if(CV_DISABLE_OPTIMIZATION OR CV_ICC)
  868. ocv_update(CV_ENABLE_UNROLLED 0)
  869. else()
  870. ocv_update(CV_ENABLE_UNROLLED 1)
  871. endif()