4 # set_target_vectorizer_report_flags
5 # print_target_compiler_options
8 # set processor speicif compilation options, based either on
9 # externally defined ENABLE_SSE42/ENABLE_AVX2 options or
10 # based on detected host processor featured (HAVE_SSE/HAVE_AVX2)
11 # Note, when ENABLE_AVX2 option is on by any means then ENABLE_SSE42 option
12 # will be turned on if not set explicitely
15 function(set_target_cpu_flags TARGET_NAME)
16 # if have cpuid info and not requested specific cpu features externally
17 # turn on cpu specific compile options based on detected features
19 # if don't have cpuid info or cpu specific features explicitly requested
20 # set compile options based on requested features
21 if(${HAVE_CPUID_INFO})
22 # ENABLE_SSE42, ENABLE_AVX2, ENABLE_AVX512 weren't set explicitly,
23 # so derive it from host cpu features
24 if( (NOT DEFINED ENABLE_SSE42) AND (NOT DEFINED ENABLE_AVX2) AND (NOT DEFINED ENABLE_AVX512F) )
25 set(ENABLE_SSE42 ${HAVE_SSE42})
26 set(ENABLE_AVX2 ${HAVE_AVX2})
27 set(ENABLE_AVX512F ${HAVE_AVX512F})
29 # ENABLE_SSE42 was set explicitly, ENABLE_AVX2 and ENABLE_AVX512F were not defined.
30 # Consider as request to build for Atom, turn off AVX2 and AVX512
31 if( (${ENABLE_SSE42}) AND (NOT DEFINED ENABLE_AVX2) AND (NOT DEFINED ENABLE_AVX512F) )
33 set(ENABLE_AVX512F OFF)
35 # ENABLE_AVX2 was set explicitly, ENABLE_SSE42 and ENABLE_AVX512F were not defined
36 # Consider as request to build for Core, turn on SSE42 as supported feature
37 if( (NOT DEFINED ENABLE_SSE42) AND (${ENABLE_AVX2}) AND (NOT DEFINED ENABLE_AVX512F) )
39 set(ENABLE_AVX512F OFF)
41 # ENABLE_AVX512 was set explicitly, ENABLE_SSE42 and ENABLE_AVX2 were not defined
42 # Consider as request to build for Xeon (Skylake server and later), turn on SSE42 and AVX2 as supported feature
43 if( (NOT DEFINED ENABLE_SSE42) AND (NOT DEFINED ENABLE_AVX2) AND (${ENABLE_AVX512F}) )
47 # Compiler doesn't support AVX512 instructuion set
48 if( (${ENABLE_AVX512F}) AND (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) AND (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)) )
49 set(ENABLE_AVX512F OFF)
50 ext_message(WARNING "Compiler doesn't support AVX512 instructuion set")
56 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
57 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX2")
58 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX512F")
59 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
60 target_compile_options(${TARGET_NAME} PUBLIC "/QxCOMMON-AVX512")
61 target_compile_options(${TARGET_NAME} PUBLIC "/Qvc14")
63 if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
64 ext_message(WARNING "MSVC Compiler doesn't support AVX512 instructuion set")
66 elseif(${ENABLE_AVX2})
67 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
68 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX2")
69 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
70 target_compile_options(${TARGET_NAME} PUBLIC "/QxCORE-AVX2")
71 target_compile_options(${TARGET_NAME} PUBLIC "/Qvc14")
73 if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
74 target_compile_options(${TARGET_NAME} PUBLIC "/arch:AVX2")
76 elseif(${ENABLE_SSE42})
77 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
78 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
79 target_compile_options(${TARGET_NAME} PUBLIC "/arch:SSE4.2")
80 target_compile_options(${TARGET_NAME} PUBLIC "/QxSSE4.2")
81 target_compile_options(${TARGET_NAME} PUBLIC "/Qvc14")
83 if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
84 target_compile_options(${TARGET_NAME} PUBLIC "/arch:SSE4.2")
90 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
91 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX2")
92 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX512F")
93 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
94 target_compile_options(${TARGET_NAME} PUBLIC "-xCOMMON-AVX512")
96 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
97 target_compile_options(${TARGET_NAME} PUBLIC "-mavx512f")
98 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
100 elseif(${ENABLE_AVX2})
101 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
102 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_AVX2")
103 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
104 target_compile_options(${TARGET_NAME} PUBLIC "-march=core-avx2")
105 target_compile_options(${TARGET_NAME} PUBLIC "-xCORE-AVX2")
106 target_compile_options(${TARGET_NAME} PUBLIC "-mtune=core-avx2")
108 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
109 target_compile_options(${TARGET_NAME} PUBLIC "-mavx2")
110 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
112 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
113 target_compile_options(${TARGET_NAME} PUBLIC "-mavx2")
114 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
116 elseif(${ENABLE_SSE42})
117 target_compile_definitions(${TARGET_NAME} PUBLIC "-DHAVE_SSE")
118 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
119 target_compile_options(${TARGET_NAME} PUBLIC "-msse4.2")
120 target_compile_options(${TARGET_NAME} PUBLIC "-xSSE4.2")
122 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
123 target_compile_options(${TARGET_NAME} PUBLIC "-msse4.2")
130 # function set vectorization report flags in case of
131 # Intel compiler (might be useful for analisys of which loops were not
132 # vectorized and why)
133 function(set_target_vectorizer_report_flags TARGET_NAME)
135 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
136 target_compile_options(${TARGET_NAME} PUBLIC "/Qopt-report=3")
137 target_compile_options(${TARGET_NAME} PUBLIC "/Qopt-report-format=vs")
138 target_compile_options(${TARGET_NAME} PUBLIC "/Qopt-report-per-object")
142 if(${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
143 target_compile_options(${TARGET_NAME} PUBLIC "-qopt-report=3")
144 target_compile_options(${TARGET_NAME} PUBLIC "-qopt-report-format=text")
145 target_compile_options(${TARGET_NAME} PUBLIC "-qopt-report-per-object")
151 # function print target compiler options to console
152 function(print_target_compiler_options TARGET_NAME)
154 if(NOT TARGET ${TARGET_NAME})
155 ext_message(WARNING "There is no target named '${TARGET_NAME}'")
159 ext_message(STATUS "Target ${TARGET_NAME}")
160 ext_message(STATUS " compiler definitions:")
161 get_target_property(TARGET_COMPILE_DEFINITIONS ${TARGET_NAME} COMPILE_DEFINITIONS)
162 if(TARGET_COMPILE_DEFINITIONS)
163 ext_message(STATUS " ${TARGET_COMPILE_DEFINITIONS}")
165 ext_message(STATUS " not set")
167 ext_message(STATUS " compiler options:")
168 get_target_property(TARGET_COMPILE_OPTIONS ${TARGET_NAME} COMPILE_OPTIONS)
169 if(TARGET_COMPILE_OPTIONS)
170 ext_message(STATUS " ${TARGET_COMPILE_OPTIONS}")
172 ext_message(STATUS " not set")
174 ext_message(STATUS " compiler flags:")
175 get_target_property(TARGET_COMPILE_FLAGS ${TARGET_NAME} COMPILE_FLAGS)
176 if(TARGET_COMPILE_FLAGS)
177 ext_message(STATUS " ${TARGET_COMPILE_FLAGS}")
179 ext_message(STATUS " not set")
182 ext_message(STATUS " CXX_FLAGS:")
183 ext_message(STATUS " ${CMAKE_CXX_FLAGS}")