OpenCVCompilerOptimizations.cmake 38 KB

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