Publishing R5 content (#72)
[platform/upstream/dldt.git] / inference-engine / src / extension / cmake / OptimizationFlags.cmake
1 #
2 # service functions:
3 #   set_target_cpu_flags
4 #   set_target_vectorizer_report_flags
5 #   print_target_compiler_options
6
7
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
13
14
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
18     # of host processor
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})
28         endif()
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) )
32             set(ENABLE_AVX2    OFF)
33             set(ENABLE_AVX512F OFF)
34         endif()
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) )
38             set(ENABLE_SSE42   ON)
39             set(ENABLE_AVX512F OFF)
40         endif()
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}) )
44             set(ENABLE_SSE42 ON)
45             set(ENABLE_AVX2  ON)
46         endif()
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")
51         endif()
52     endif()
53
54     if(WIN32)
55         if(${ENABLE_AVX512F})
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")
62             endif()
63             if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
64                 ext_message(WARNING "MSVC Compiler doesn't support AVX512 instructuion set")
65             endif()
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")
72             endif()
73             if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
74                 target_compile_options(${TARGET_NAME} PUBLIC "/arch:AVX2")
75             endif()
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")
82             endif()
83             if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
84                 target_compile_options(${TARGET_NAME} PUBLIC "/arch:SSE4.2")
85             endif()
86         endif()
87     endif()
88     if(UNIX)
89         if(${ENABLE_AVX512F})
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")
95             endif()
96             if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
97                 target_compile_options(${TARGET_NAME} PUBLIC "-mavx512f")
98                 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
99             endif()
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")
107             endif()
108             if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
109                 target_compile_options(${TARGET_NAME} PUBLIC "-mavx2")
110                 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
111             endif()
112             if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
113                 target_compile_options(${TARGET_NAME} PUBLIC "-mavx2")
114                 target_compile_options(${TARGET_NAME} PUBLIC "-mfma")
115             endif()
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")
121             endif()
122             if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
123                 target_compile_options(${TARGET_NAME} PUBLIC "-msse4.2")
124             endif()
125         endif()
126     endif()
127 endfunction()
128
129
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)
134     if(WIN32)
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")
139         endif()
140     endif()
141     if(UNIX)
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")
146         endif()
147     endif()
148 endfunction()
149
150
151 # function print target compiler options to console
152 function(print_target_compiler_options TARGET_NAME)
153
154     if(NOT TARGET ${TARGET_NAME})
155         ext_message(WARNING "There is no target named '${TARGET_NAME}'")
156         return()
157     endif()
158
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}")
164     else()
165         ext_message(STATUS "        not set")
166     endif()
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}")
171     else()
172         ext_message(STATUS "        not set")
173     endif()
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}")
178     else()
179         ext_message(STATUS "        not set")
180     endif()
181
182     ext_message(STATUS "    CXX_FLAGS:")
183     ext_message(STATUS "        ${CMAKE_CXX_FLAGS}")
184
185 endfunction()