[MLIR][LLVM] Remove typed pointer remnants from target tests (#71210)
[llvm-project.git] / libc / cmake / modules / LLVMLibCCheckCpuFeatures.cmake
blob73b249374a0667c34da91dbb9069d624b86db6df
1 # ------------------------------------------------------------------------------
2 # Cpu features definition and flags
3 # ------------------------------------------------------------------------------
5 # Initialize ALL_CPU_FEATURES as empty list.
6 set(ALL_CPU_FEATURES "")
8 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
9   set(ALL_CPU_FEATURES SSE2 SSE4_2 AVX AVX2 AVX512F AVX512BW FMA)
10   set(LIBC_COMPILE_OPTIONS_NATIVE -march=native)
11 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
12   set(LIBC_COMPILE_OPTIONS_NATIVE -mcpu=native)
13 endif()
15 # Making sure ALL_CPU_FEATURES is sorted.
16 list(SORT ALL_CPU_FEATURES)
18 # Function to check whether the target CPU supports the provided set of features.
19 # Usage:
20 # cpu_supports(
21 #   <output variable>
22 #   <list of cpu features>
23 # )
24 function(cpu_supports output_var features)
25   _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
26   if("${var}" STREQUAL "${features}")
27     set(${output_var} TRUE PARENT_SCOPE)
28   else()
29     unset(${output_var} PARENT_SCOPE)
30   endif()
31 endfunction()
33 # ------------------------------------------------------------------------------
34 # Internal helpers and utilities.
35 # ------------------------------------------------------------------------------
37 # Computes the intersection between two lists.
38 function(_intersection output_var list1 list2)
39   foreach(element IN LISTS list1)
40     if("${list2}" MATCHES "(^|;)${element}(;|$)")
41       list(APPEND tmp "${element}")
42     endif()
43   endforeach()
44   set(${output_var} ${tmp} PARENT_SCOPE)
45 endfunction()
47 set(AVAILABLE_CPU_FEATURES "")
48 if(LIBC_CROSSBUILD)
49   # If we are doing a cross build, we will just assume that all CPU features
50   # are available.
51   set(AVAILABLE_CPU_FEATURES ${ALL_CPU_FEATURES})
52 else()
53   # Try compile a C file to check if flag is supported.
54   set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
55   foreach(feature IN LISTS ALL_CPU_FEATURES)
56     try_compile(
57       has_feature
58       ${CMAKE_CURRENT_BINARY_DIR}/cpu_features
59       SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/cpu_features/check_${feature}.cpp
60       COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
61     )
62     if(has_feature)
63       list(APPEND AVAILABLE_CPU_FEATURES ${feature})
64     endif()
65   endforeach()
66 endif()
68 set(LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features")
70 _intersection(cpu_features "${AVAILABLE_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
71 if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
72   message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
73 else()
74   message(STATUS "Set CPU features: ${cpu_features}")
75 endif()