[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / cmake / modules / LLVMLibCCheckCpuFeatures.cmake
blob0b522318aaa129ee6d25eba2b8171b710ea06cb7
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   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
26     unset(${output_var} PARENT_SCOPE)
27     return()
28   endif()
29   _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
30   if("${var}" STREQUAL "${features}")
31     set(${output_var} TRUE PARENT_SCOPE)
32   else()
33     unset(${output_var} PARENT_SCOPE)
34   endif()
35 endfunction()
37 # ------------------------------------------------------------------------------
38 # Internal helpers and utilities.
39 # ------------------------------------------------------------------------------
41 # Computes the intersection between two lists.
42 function(_intersection output_var list1 list2)
43   foreach(element IN LISTS list1)
44     if("${list2}" MATCHES "(^|;)${element}(;|$)")
45       list(APPEND tmp "${element}")
46     endif()
47   endforeach()
48   set(${output_var} ${tmp} PARENT_SCOPE)
49 endfunction()
51 set(AVAILABLE_CPU_FEATURES "")
52 if(LIBC_CROSSBUILD)
53   # If we are doing a cross build, we will just assume that all CPU features
54   # are available.
55   set(AVAILABLE_CPU_FEATURES ${ALL_CPU_FEATURES})
56 else()
57   # Try compile a C file to check if flag is supported.
58   set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
59   foreach(feature IN LISTS ALL_CPU_FEATURES)
60     try_compile(
61       has_feature
62       ${CMAKE_CURRENT_BINARY_DIR}/cpu_features
63       SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/cpu_features/check_${feature}.cpp
64       COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
65     )
66     if(has_feature)
67       list(APPEND AVAILABLE_CPU_FEATURES ${feature})
68     endif()
69   endforeach()
70 endif()
72 set(LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features")
74 _intersection(cpu_features "${AVAILABLE_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
75 if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
76   message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
77 else()
78   message(STATUS "Set CPU features: ${cpu_features}")
79 endif()