152 lines · plain
1# ------------------------------------------------------------------------------2# Compiler features definition and flags3# ------------------------------------------------------------------------------4 5set(6 ALL_COMPILER_FEATURES7 "builtin_ceil_floor_rint_trunc"8 "builtin_fmax_fmin"9 "builtin_fmaxf16_fminf16"10 "builtin_round"11 "builtin_roundeven"12 "float16"13 "float16_conversion"14 "float128"15 "fixed_point"16 "cfloat16"17 "cfloat128"18 "ext_vector_type"19)20 21# Making sure ALL_COMPILER_FEATURES is sorted.22list(SORT ALL_COMPILER_FEATURES)23 24# Compiler features that are unavailable on GPU targets with the in-tree Clang.25set(26 CPU_ONLY_COMPILER_FEATURES27 "float128"28)29 30# Function to check whether the compiler supports the provided set of features.31# Usage:32# compiler_supports(33# <output variable>34# <list of cpu features>35# )36function(compiler_supports output_var features)37 _intersection(var "${LIBC_CPU_FEATURES}" "${features}")38 if("${var}" STREQUAL "${features}")39 set(${output_var} TRUE PARENT_SCOPE)40 else()41 unset(${output_var} PARENT_SCOPE)42 endif()43endfunction()44 45# ------------------------------------------------------------------------------46# Internal helpers and utilities.47# ------------------------------------------------------------------------------48 49# Computes the intersection between two lists.50function(_intersection output_var list1 list2)51 foreach(element IN LISTS list1)52 if("${list2}" MATCHES "(^|;)${element}(;|$)")53 list(APPEND tmp "${element}")54 endif()55 endforeach()56 set(${output_var} ${tmp} PARENT_SCOPE)57endfunction()58 59set(AVAILABLE_COMPILER_FEATURES "")60 61# Try compile a C file to check if flag is supported.62foreach(feature IN LISTS ALL_COMPILER_FEATURES)63 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)64 set(compile_options ${LIBC_COMPILE_OPTIONS_NATIVE})65 set(link_options "")66 if(${feature} STREQUAL "fixed_point")67 list(APPEND compile_options "-ffixed-point")68 elseif(${feature} MATCHES "^builtin_" OR69 ${feature} STREQUAL "float16_conversion")70 set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})71 set(link_options -nostdlib)72 # The compiler might handle calls to math builtins by generating calls to73 # the respective libc math functions, in which case we cannot use these74 # builtins in our implementations of these functions. We check that this is75 # not the case by trying to link an executable, since linking would fail due76 # to unresolved references with -nostdlib if calls to libc functions were77 # generated.78 #79 # We also had issues with soft-float float16 conversion functions using both80 # compiler-rt and libgcc, so we also check whether we can convert from and81 # to float16 without calls to compiler runtime functions by trying to link82 # an executable with -nostdlib.83 set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)84 endif()85 86 if(LIBC_TARGET_OS_IS_GPU)87 # CUDA shouldn't be required to build the libc, only to test it, so we can't88 # try to build CUDA binaries here. Since GPU builds are always compiled with89 # the in-tree Clang, we just hardcode which compiler features are available90 # when targeting GPUs.91 if(feature IN_LIST CPU_ONLY_COMPILER_FEATURES)92 set(has_feature FALSE)93 else()94 set(has_feature TRUE)95 endif()96 else()97 try_compile(98 has_feature99 ${CMAKE_CURRENT_BINARY_DIR}/compiler_features100 SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp101 COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${compile_options}102 LINK_OPTIONS ${link_options}103 )104 endif()105 106 if(has_feature)107 list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})108 if(${feature} STREQUAL "float16")109 set(LIBC_TYPES_HAS_FLOAT16 TRUE)110 elseif(${feature} STREQUAL "float16_conversion")111 add_compile_definitions(__LIBC_USE_FLOAT16_CONVERSION)112 elseif(${feature} STREQUAL "float128")113 set(LIBC_TYPES_HAS_FLOAT128 TRUE)114 elseif(${feature} STREQUAL "fixed_point")115 set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)116 elseif(${feature} STREQUAL "cfloat16")117 set(LIBC_TYPES_HAS_CFLOAT16 TRUE)118 elseif(${feature} STREQUAL "cfloat128")119 set(LIBC_TYPES_HAS_CFLOAT128 TRUE)120 elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")121 set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)122 elseif(${feature} STREQUAL "builtin_fmax_fmin")123 set(LIBC_COMPILER_HAS_BUILTIN_FMAX_FMIN TRUE)124 elseif(${feature} STREQUAL "builtin_fmaxf16_fminf16")125 set(LIBC_COMPILER_HAS_BUILTIN_FMAXF16_FMINF16 TRUE)126 elseif(${feature} STREQUAL "builtin_round")127 set(LIBC_COMPILER_HAS_BUILTIN_ROUND TRUE)128 elseif(${feature} STREQUAL "builtin_roundeven")129 set(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN TRUE)130 elseif(${feature} STREQUAL "ext_vector_type")131 set(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE TRUE)132 endif()133 endif()134endforeach()135 136set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)137set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})138set(link_options "")139 140message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")141 142### Compiler Feature Detection ###143 144# clang-8+, gcc-12+145check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)146 147# clang-6+, gcc-13+148check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)149 150# clang-3.0+151check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)152