2 #//===----------------------------------------------------------------------===//
4 #// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 #// See https://llvm.org/LICENSE.txt for license information.
6 #// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 #//===----------------------------------------------------------------------===//
11 include(CheckCCompilerFlag)
12 include(CheckCSourceCompiles)
13 include(CheckCXXSourceCompiles)
14 include(CheckCXXCompilerFlag)
15 include(CheckIncludeFile)
16 include(CheckLibraryExists)
17 include(CheckIncludeFiles)
18 include(CheckSymbolExists)
19 include(LibompCheckFortranFlag)
20 include(LLVMCheckCompilerLinkerFlag)
22 # Check for versioned symbols
23 function(libomp_check_version_symbols retval)
26 void func1() { printf(\"Hello\"); }
27 void func2() { printf(\"World\"); }
28 __asm__(\".symver func1, func@VER1\");
29 __asm__(\".symver func2, func@VER2\");
35 set(version_script_source "VER1 { }; VER2 { } VER1;")
36 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/__version_script.txt "${version_script_source}")
37 set(CMAKE_REQUIRED_FLAGS -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/__version_script.txt)
38 check_c_source_compiles("${source_code}" ${retval})
39 set(${retval} ${${retval}} PARENT_SCOPE)
40 file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/__version_script.txt)
43 # Includes the architecture flag in both compile and link phase
44 function(libomp_check_architecture_flag flag retval)
45 set(CMAKE_REQUIRED_FLAGS "${flag}")
46 check_c_compiler_flag("${flag}" ${retval})
47 set(${retval} ${${retval}} PARENT_SCOPE)
50 # Checking CXX, Linker Flags
52 # GCC silently accepts any -Wno-<foo> option, but warns about those options
53 # being unrecognized only if the compilation triggers other warnings to be
54 # printed. Therefore, check for whether the compiler supports options in the
55 # form -W<foo>, and if supported, add the corresponding -Wno-<foo> option.
57 check_cxx_compiler_flag(-fno-exceptions LIBOMP_HAVE_FNO_EXCEPTIONS_FLAG)
58 check_cxx_compiler_flag(-fno-rtti LIBOMP_HAVE_FNO_RTTI_FLAG)
59 check_cxx_compiler_flag(-Wclass-memaccess LIBOMP_HAVE_WCLASS_MEMACCESS_FLAG)
60 check_cxx_compiler_flag(-Wcovered-switch-default LIBOMP_HAVE_WCOVERED_SWITCH_DEFAULT_FLAG)
61 check_cxx_compiler_flag(-Wframe-address LIBOMP_HAVE_WFRAME_ADDRESS_FLAG)
62 check_cxx_compiler_flag(-Wstrict-aliasing LIBOMP_HAVE_WSTRICT_ALIASING_FLAG)
63 check_cxx_compiler_flag(-Wstringop-overflow=0 LIBOMP_HAVE_WSTRINGOP_OVERFLOW_FLAG)
64 check_cxx_compiler_flag(-Wstringop-truncation LIBOMP_HAVE_WSTRINGOP_TRUNCATION_FLAG)
65 check_cxx_compiler_flag(-Wswitch LIBOMP_HAVE_WSWITCH_FLAG)
66 check_cxx_compiler_flag(-Wuninitialized LIBOMP_HAVE_WUNINITIALIZED_FLAG)
67 check_cxx_compiler_flag(-Wreturn-type-c-linkage LIBOMP_HAVE_WRETURN_TYPE_C_LINKAGE_FLAG)
68 check_cxx_compiler_flag(-Wcast-qual LIBOMP_HAVE_WCAST_QUAL_FLAG)
69 check_cxx_compiler_flag(-Wint-to-void-pointer-cast LIBOMP_HAVE_WINT_TO_VOID_POINTER_CAST_FLAG)
70 # check_cxx_compiler_flag(-Wconversion LIBOMP_HAVE_WCONVERSION_FLAG)
71 check_cxx_compiler_flag(-msse2 LIBOMP_HAVE_MSSE2_FLAG)
72 check_cxx_compiler_flag(-ftls-model=initial-exec LIBOMP_HAVE_FTLS_MODEL_FLAG)
73 libomp_check_architecture_flag(-mmic LIBOMP_HAVE_MMIC_FLAG)
74 libomp_check_architecture_flag(-m32 LIBOMP_HAVE_M32_FLAG)
77 # Check Windows MSVC style flags.
78 check_cxx_compiler_flag(/EHsc LIBOMP_HAVE_EHSC_FLAG)
79 check_cxx_compiler_flag(/GS LIBOMP_HAVE_GS_FLAG)
80 check_cxx_compiler_flag(/Oy- LIBOMP_HAVE_Oy__FLAG)
81 check_cxx_compiler_flag(/arch:SSE2 LIBOMP_HAVE_ARCH_SSE2_FLAG)
82 check_cxx_compiler_flag(/Qsafeseh LIBOMP_HAVE_QSAFESEH_FLAG)
84 check_cxx_compiler_flag(-mrtm LIBOMP_HAVE_MRTM_FLAG)
85 # It is difficult to create a dummy masm assembly file
86 # and then check the MASM assembler to see if these flags exist and work,
87 # so we assume they do for Windows.
88 set(LIBOMP_HAVE_SAFESEH_MASM_FLAG TRUE)
89 set(LIBOMP_HAVE_COFF_MASM_FLAG TRUE)
90 # Change Windows flags /MDx to /MTx
91 foreach(libomp_lang IN ITEMS C CXX)
92 foreach(libomp_btype IN ITEMS DEBUG RELWITHDEBINFO RELEASE MINSIZEREL)
93 string(REPLACE "/MD" "/MT"
94 CMAKE_${libomp_lang}_FLAGS_${libomp_btype}
95 "${CMAKE_${libomp_lang}_FLAGS_${libomp_btype}}"
100 if(${LIBOMP_FORTRAN_MODULES})
101 libomp_check_fortran_flag(-m32 LIBOMP_HAVE_M32_FORTRAN_FLAG)
104 # Check non-posix pthread API here before CMAKE_REQUIRED_DEFINITIONS gets messed up
105 check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_PTHREAD_SETNAME_NP)
106 check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
108 # Check for Unix shared memory
109 check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_NO_LRT)
110 if (NOT LIBOMP_HAVE_SHM_OPEN_NO_LRT)
111 set(CMAKE_REQUIRED_LIBRARIES -lrt)
112 check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_WITH_LRT)
113 set(CMAKE_REQUIRED_LIBRARIES)
116 # Check for aligned memory allocator function
117 check_include_file(xmmintrin.h LIBOMP_HAVE_XMMINTRIN_H)
118 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
119 if (LIBOMP_HAVE_XMMINTRIN_H)
120 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -DLIBOMP_HAVE_XMMINTRIN_H")
122 set(source_code "// check for _mm_malloc
123 #ifdef LIBOMP_HAVE_XMMINTRIN_H
124 #include <xmmintrin.h>
126 int main() { void *ptr = _mm_malloc(sizeof(int) * 1000, 64); _mm_free(ptr); return 0; }")
127 check_cxx_source_compiles("${source_code}" LIBOMP_HAVE__MM_MALLOC)
128 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
129 check_symbol_exists(aligned_alloc "stdlib.h" LIBOMP_HAVE_ALIGNED_ALLOC)
130 check_symbol_exists(posix_memalign "stdlib.h" LIBOMP_HAVE_POSIX_MEMALIGN)
131 check_symbol_exists(_aligned_malloc "malloc.h" LIBOMP_HAVE__ALIGNED_MALLOC)
135 llvm_check_compiler_linker_flag(C /SAFESEH LIBOMP_HAVE_SAFESEH_FLAG)
137 llvm_check_compiler_linker_flag(C -Wl,-x LIBOMP_HAVE_X_FLAG)
138 llvm_check_compiler_linker_flag(C -Wl,--as-needed LIBOMP_HAVE_AS_NEEDED_FLAG)
139 llvm_check_compiler_linker_flag(C "-Wl,--version-script=${LIBOMP_SRC_DIR}/exports_test_so.txt" LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
140 llvm_check_compiler_linker_flag(C -static-libgcc LIBOMP_HAVE_STATIC_LIBGCC_FLAG)
141 llvm_check_compiler_linker_flag(C -Wl,-z,noexecstack LIBOMP_HAVE_Z_NOEXECSTACK_FLAG)
144 # Check Intel(R) C Compiler specific flags
145 if(CMAKE_C_COMPILER_ID STREQUAL "Intel" OR CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM")
146 check_cxx_compiler_flag(/Qlong_double LIBOMP_HAVE_LONG_DOUBLE_FLAG)
147 check_cxx_compiler_flag(/Qdiag-disable:177 LIBOMP_HAVE_DIAG_DISABLE_177_FLAG)
148 check_cxx_compiler_flag(/Qinline-min-size=1 LIBOMP_HAVE_INLINE_MIN_SIZE_FLAG)
149 check_cxx_compiler_flag(-Qoption,cpp,--extended_float_types LIBOMP_HAVE_EXTENDED_FLOAT_TYPES_FLAG)
150 check_cxx_compiler_flag(-falign-stack=maintain-16-byte LIBOMP_HAVE_FALIGN_STACK_FLAG)
151 check_cxx_compiler_flag("-opt-streaming-stores never" LIBOMP_HAVE_OPT_STREAMING_STORES_FLAG)
152 llvm_check_compiler_linker_flag(C -static-intel LIBOMP_HAVE_STATIC_INTEL_FLAG)
153 llvm_check_compiler_linker_flag(C -no-intel-extensions LIBOMP_HAVE_NO_INTEL_EXTENSIONS_FLAG)
154 check_library_exists(irc_pic _intel_fast_memcpy "" LIBOMP_HAVE_IRC_PIC_LIBRARY)
157 # Checking threading requirements. Note that compiling to WebAssembly threads
158 # with either the Emscripten or wasi-threads flavor ends up using the pthreads
159 # interface in a WebAssembly-compiled libc; CMake does not yet know how to
162 find_package(Threads REQUIRED)
164 if(NOT CMAKE_USE_WIN32_THREADS_INIT)
165 libomp_error_say("Need Win32 thread interface on Windows.")
168 if(NOT CMAKE_USE_PTHREADS_INIT)
169 libomp_error_say("Need pthread interface on Unix-like systems.")
174 # Checking for x86-specific waitpkg and rtm attribute and intrinsics
176 check_include_file(immintrin.h LIBOMP_HAVE_IMMINTRIN_H)
177 if (NOT LIBOMP_HAVE_IMMINTRIN_H)
178 check_include_file(intrin.h LIBOMP_HAVE_INTRIN_H)
180 check_cxx_source_compiles("__attribute__((target(\"rtm\")))
181 int main() {return 0;}" LIBOMP_HAVE_ATTRIBUTE_RTM)
182 check_cxx_source_compiles("__attribute__((target(\"waitpkg\")))
183 int main() {return 0;}" LIBOMP_HAVE_ATTRIBUTE_WAITPKG)
184 libomp_append(CMAKE_REQUIRED_DEFINITIONS -DIMMINTRIN_H LIBOMP_HAVE_IMMINTRIN_H)
185 libomp_append(CMAKE_REQUIRED_DEFINITIONS -DINTRIN_H LIBOMP_HAVE_INTRIN_H)
186 libomp_append(CMAKE_REQUIRED_DEFINITIONS -DATTRIBUTE_WAITPKG LIBOMP_HAVE_ATTRIBUTE_WAITPKG)
187 libomp_append(CMAKE_REQUIRED_DEFINITIONS -DATTRIBUTE_RTM LIBOMP_HAVE_ATTRIBUTE_RTM)
188 set(source_code "// check for attribute and wait pkg intrinsics
190 #include <immintrin.h>
195 #ifdef ATTRIBUTE_WAITPKG
196 __attribute__((target(\"waitpkg\")))
198 static inline int __kmp_umwait(unsigned hint, unsigned long long counter) {
199 return _umwait(hint, counter);
201 int main() { int a = __kmp_umwait(0, 1000); return a; }")
202 check_cxx_source_compiles("${source_code}" LIBOMP_HAVE_WAITPKG_INTRINSICS)
203 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
204 if (LIBOMP_HAVE_MRTM_FLAG)
205 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mrtm")
207 set(source_code "// check for attribute rtm and rtm intrinsics
209 #include <immintrin.h>
215 __attribute__((target(\"rtm\")))
217 static inline int __kmp_xbegin() {
220 int main() { int a = __kmp_xbegin(); return a; }")
221 check_cxx_source_compiles("${source_code}" LIBOMP_HAVE_RTM_INTRINSICS)
222 set(CMAKE_REQUIRED_DEFINITIONS)
223 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
226 # Find python3 executable
227 # Python3 is used to create kmp_i18n_id.inc and
228 # kmp_i18n_default.inc and for Windows the *.def files.
229 find_package(Python3 REQUIRED COMPONENTS Interpreter)
232 # Check if version symbol assembler directives are supported
233 if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
234 libomp_check_version_symbols(LIBOMP_HAVE_VERSION_SYMBOLS)
236 set(LIBOMP_HAVE_VERSION_SYMBOLS FALSE)
239 # Check if quad precision types are available
240 if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
241 set(LIBOMP_HAVE_QUAD_PRECISION TRUE)
242 elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel" OR CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM")
243 if(LIBOMP_HAVE_EXTENDED_FLOAT_TYPES_FLAG)
244 set(LIBOMP_HAVE_QUAD_PRECISION TRUE)
246 set(LIBOMP_HAVE_QUAD_PRECISION TRUE)
249 set(LIBOMP_HAVE_QUAD_PRECISION FALSE)
252 # Check if adaptive locks are available
253 if((${IA32} OR ${INTEL64}) AND NOT MSVC)
254 set(LIBOMP_HAVE_ADAPTIVE_LOCKS TRUE)
256 set(LIBOMP_HAVE_ADAPTIVE_LOCKS FALSE)
259 # Check if stats-gathering is available
261 check_c_source_compiles(
263 int main(int argc, char** argv)
264 { x = argc; return x; }"
265 LIBOMP_HAVE___THREAD)
266 check_c_source_compiles(
267 "int main(int argc, char** argv)
268 { unsigned long long t = __builtin_readcyclecounter(); return 0; }"
269 LIBOMP_HAVE___BUILTIN_READCYCLECOUNTER)
270 if(NOT LIBOMP_HAVE___BUILTIN_READCYCLECOUNTER)
271 if(${IA32} OR ${INTEL64} OR ${MIC})
272 check_include_file(x86intrin.h LIBOMP_HAVE_X86INTRIN_H)
273 libomp_append(CMAKE_REQUIRED_DEFINITIONS -DLIBOMP_HAVE_X86INTRIN_H LIBOMP_HAVE_X86INTRIN_H)
274 check_c_source_compiles(
275 "#ifdef LIBOMP_HAVE_X86INTRIN_H
276 # include <x86intrin.h>
278 int main(int argc, char** argv) { unsigned long long t = __rdtsc(); return 0; }" LIBOMP_HAVE___RDTSC)
279 set(CMAKE_REQUIRED_DEFINITIONS)
282 if(LIBOMP_HAVE___THREAD AND (LIBOMP_HAVE___RDTSC OR LIBOMP_HAVE___BUILTIN_READCYCLECOUNTER))
283 set(LIBOMP_HAVE_STATS TRUE)
285 set(LIBOMP_HAVE_STATS FALSE)
289 # Check if OMPT support is available
290 # Currently, __builtin_frame_address() is required for OMPT
291 # Weak attribute is required for Unices (except Darwin), LIBPSAPI is used for Windows
292 check_c_source_compiles("int main(int argc, char** argv) {
293 void* p = __builtin_frame_address(0);
294 return 0;}" LIBOMP_HAVE___BUILTIN_FRAME_ADDRESS)
295 check_c_source_compiles("__attribute__ ((weak)) int foo(int a) { return a*a; }
296 int main(int argc, char** argv) {
297 return foo(argc);}" LIBOMP_HAVE_WEAK_ATTRIBUTE)
298 set(CMAKE_REQUIRED_LIBRARIES psapi)
299 check_c_source_compiles("#include <windows.h>
301 int main(int artc, char** argv) {
302 return EnumProcessModules(NULL, NULL, 0, NULL);
303 }" LIBOMP_HAVE_PSAPI)
304 set(CMAKE_REQUIRED_LIBRARIES)
305 if(NOT LIBOMP_HAVE___BUILTIN_FRAME_ADDRESS)
306 set(LIBOMP_HAVE_OMPT_SUPPORT FALSE)
308 if( # hardware architecture supported?
309 ((LIBOMP_ARCH STREQUAL x86_64) OR
310 (LIBOMP_ARCH STREQUAL i386) OR
311 # (LIBOMP_ARCH STREQUAL arm) OR
312 (LIBOMP_ARCH STREQUAL aarch64) OR
313 (LIBOMP_ARCH STREQUAL aarch64_32) OR
314 (LIBOMP_ARCH STREQUAL aarch64_a64fx) OR
315 (LIBOMP_ARCH STREQUAL ppc64le) OR
316 (LIBOMP_ARCH STREQUAL ppc64) OR
317 (LIBOMP_ARCH STREQUAL riscv64) OR
318 (LIBOMP_ARCH STREQUAL loongarch64) OR
319 (LIBOMP_ARCH STREQUAL s390x))
321 ((WIN32 AND LIBOMP_HAVE_PSAPI) OR APPLE OR
322 (NOT (WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX") AND LIBOMP_HAVE_WEAK_ATTRIBUTE)))
323 set(LIBOMP_HAVE_OMPT_SUPPORT TRUE)
325 set(LIBOMP_HAVE_OMPT_SUPPORT FALSE)
329 set(LIBOMP_HAVE_OMPT_SUPPORT ${LIBOMP_HAVE_OMPT_SUPPORT} PARENT_SCOPE)
331 # Check if HWLOC support is available
332 if(${LIBOMP_USE_HWLOC})
333 find_path(LIBOMP_HWLOC_INCLUDE_DIR NAMES hwloc.h HINTS ${LIBOMP_HWLOC_INSTALL_DIR} PATH_SUFFIXES include)
334 set(CMAKE_REQUIRED_INCLUDES ${LIBOMP_HWLOC_INCLUDE_DIR})
335 check_include_file(hwloc.h LIBOMP_HAVE_HWLOC_H)
336 set(CMAKE_REQUIRED_INCLUDES)
337 find_library(LIBOMP_HWLOC_LIBRARY
339 HINTS ${LIBOMP_HWLOC_INSTALL_DIR}/lib)
340 if(LIBOMP_HWLOC_LIBRARY)
341 check_library_exists(${LIBOMP_HWLOC_LIBRARY} hwloc_topology_init
342 ${LIBOMP_HWLOC_INSTALL_DIR}/lib LIBOMP_HAVE_LIBHWLOC)
343 get_filename_component(LIBOMP_HWLOC_LIBRARY_DIR ${LIBOMP_HWLOC_LIBRARY} PATH)
345 if(LIBOMP_HAVE_HWLOC_H AND LIBOMP_HAVE_LIBHWLOC AND LIBOMP_HWLOC_LIBRARY)
346 set(LIBOMP_HAVE_HWLOC TRUE)
348 set(LIBOMP_HAVE_HWLOC FALSE)
349 libomp_say("Could not find hwloc")
353 # Check if ThreadSanitizer support is available
354 if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux" AND ${INTEL64})
355 set(LIBOMP_HAVE_TSAN_SUPPORT TRUE)
357 set(LIBOMP_HAVE_TSAN_SUPPORT FALSE)