Skip to content

Commit 9acc2f3

Browse files
committed
[libc][NFC] Detect host CPU features using try_compile instead of try_run.
This implements the same behavior as D141997 but makes sure that the same detection mechanism is used between CMake and source code. Differential Revision: https://reviews.llvm.org/D142108
1 parent 262dad4 commit 9acc2f3

24 files changed

+110
-75
lines changed

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,32 @@ function(_intersection output_var list1 list2)
4444
set(${output_var} ${tmp} PARENT_SCOPE)
4545
endfunction()
4646

47-
# Generates a cpp file to introspect the compiler defined flags.
48-
function(_generate_check_code)
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)
4955
foreach(feature IN LISTS ALL_CPU_FEATURES)
50-
set(DEFINITIONS
51-
"${DEFINITIONS}
52-
#ifdef __${feature}__
53-
\"${feature}\",
54-
#endif")
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()
5565
endforeach()
56-
configure_file(
57-
"${LIBC_SOURCE_DIR}/cmake/modules/cpu_features/check_cpu_features.cpp.in"
58-
"cpu_features/check_cpu_features.cpp" @ONLY)
59-
endfunction()
60-
_generate_check_code()
66+
endif()
6167

62-
set(LIBC_CPU_FEATURES "" CACHE PATH "Host supported CPU features")
68+
set(LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features")
6369

64-
if(LIBC_CROSSBUILD)
65-
_intersection(cpu_features "${ALL_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
66-
if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
67-
message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
68-
endif()
69-
message(STATUS "Set CPU features: ${cpu_features}")
70-
set(LIBC_CPU_FEATURES "${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}")
7173
else()
72-
# Populates the LIBC_CPU_FEATURES list from host.
73-
try_run(
74-
run_result compile_result "${CMAKE_CURRENT_BINARY_DIR}/check_${feature}"
75-
"${CMAKE_CURRENT_BINARY_DIR}/cpu_features/check_cpu_features.cpp"
76-
COMPILE_DEFINITIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
77-
COMPILE_OUTPUT_VARIABLE compile_output
78-
RUN_OUTPUT_VARIABLE run_output)
79-
if("${run_result}" EQUAL 0)
80-
message(STATUS "Set CPU features: ${run_output}")
81-
set(LIBC_CPU_FEATURES "${run_output}")
82-
elseif(NOT ${compile_result})
83-
message(FATAL_ERROR "Failed to compile: ${compile_output}")
84-
else()
85-
message(FATAL_ERROR "Failed to run: ${run_output}")
86-
endif()
74+
message(STATUS "Set CPU features: ${cpu_features}")
8775
endif()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_AVX2
4+
#error unsupported
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_AVX512BW
4+
#error unsupported
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_AVX512F
4+
#error unsupported
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_FMA
4+
#error unsupported
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_SSE2
4+
#error unsupported
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/cpu_features.h"
2+
3+
#ifndef LIBC_TARGET_HAS_SSE4_2
4+
#error unsupported
5+
#endif

libc/cmake/modules/cpu_features/check_cpu_features.cpp.in

Lines changed: 0 additions & 32 deletions
This file was deleted.

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_header_library(
3333
HDRS
3434
architectures.h
3535
common.h
36+
cpu_features.h
3637
endian.h
3738
)
3839

libc/src/__support/FPUtil/FMA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "src/__support/architectures.h"
1313
#include "src/__support/common.h"
14+
#include "src/__support/cpu_features.h"
1415

1516
#if defined(LIBC_TARGET_HAS_FMA)
1617

0 commit comments

Comments
 (0)