Skip to content

Commit fd64482

Browse files
gchateletSiva Chandra Reddy
authored andcommitted
[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. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D142108
1 parent 92f0e4c commit fd64482

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

libc/src/__support/FPUtil/aarch64/FMA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_AARCH64_FMA_H
1111

1212
#include "src/__support/architectures.h"
13+
#include "src/__support/cpu_features.h"
1314

1415
#if !defined(LLVM_LIBC_ARCH_AARCH64)
1516
#error "Invalid include"

libc/src/__support/FPUtil/multiply_add.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
namespace __llvm_libc {
1617
namespace fputil {

libc/src/__support/FPUtil/x86_64/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(LLVM_LIBC_ARCH_X86_64)
1617
#error "Invalid include"

libc/src/__support/architectures.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,4 @@
4949
#define LLVM_LIBC_ARCH_ANY_ARM
5050
#endif
5151

52-
#if defined(LLVM_LIBC_ARCH_AARCH64)
53-
#define LIBC_TARGET_HAS_FMA
54-
#elif defined(LLVM_LIBC_ARCH_X86_64)
55-
#if (defined(__AVX2__) || defined(__FMA__))
56-
#define LIBC_TARGET_HAS_FMA
57-
#endif
58-
#endif
59-
6052
#endif // LLVM_LIBC_SUPPORT_ARCHITECTURES_H

libc/src/__support/cpu_features.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- Compile time cpu feature detection ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// This file lists target cpu features by introspecting compiler enabled
9+
// preprocessor definitions.
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef LLVM_LIBC_SRC_SUPPORT_CPU_FEATURES_H
13+
#define LLVM_LIBC_SRC_SUPPORT_CPU_FEATURES_H
14+
15+
#if defined(__SSE2__)
16+
#define LIBC_TARGET_HAS_SSE2
17+
#endif
18+
19+
#if defined(__SSE4_2__)
20+
#define LIBC_TARGET_HAS_SSE4_2
21+
#endif
22+
23+
#if defined(__AVX__)
24+
#define LIBC_TARGET_HAS_AVX
25+
#endif
26+
27+
#if defined(__AVX2__)
28+
#define LIBC_TARGET_HAS_AVX2
29+
#endif
30+
31+
#if defined(__AVX512F__)
32+
#define LIBC_TARGET_HAS_AVX512F
33+
#endif
34+
35+
#if defined(__AVX512BW__)
36+
#define LIBC_TARGET_HAS_AVX512BW
37+
#endif
38+
39+
#if defined(__ARM_FEATURE_FMA) || defined(__AVX2__)
40+
#define LIBC_TARGET_HAS_FMA
41+
#endif
42+
43+
#endif // LLVM_LIBC_SRC_SUPPORT_CPU_FEATURES_H

libc/src/math/generic/asinf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "src/__support/FPUtil/except_value_utils.h"
1414
#include "src/__support/FPUtil/multiply_add.h"
1515
#include "src/__support/FPUtil/sqrt.h"
16+
#include "src/__support/cpu_features.h"
1617

1718
#include <errno.h>
1819

libc/src/math/generic/cosf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "src/__support/FPUtil/except_value_utils.h"
1515
#include "src/__support/FPUtil/multiply_add.h"
1616
#include "src/__support/common.h"
17+
#include "src/__support/cpu_features.h"
1718

1819
#include <errno.h>
1920

libc/src/math/generic/expm1f.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "src/__support/FPUtil/multiply_add.h"
1717
#include "src/__support/FPUtil/nearest_integer.h"
1818
#include "src/__support/common.h"
19+
#include "src/__support/cpu_features.h"
1920

2021
#include <errno.h>
2122

libc/src/math/generic/sincosf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/FPUtil/FPBits.h"
1313
#include "src/__support/FPUtil/multiply_add.h"
1414
#include "src/__support/common.h"
15+
#include "src/__support/cpu_features.h"
1516

1617
#include <errno.h>
1718

libc/src/math/generic/sincosf_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/FPUtil/FPBits.h"
1313
#include "src/__support/FPUtil/PolyEval.h"
1414
#include "src/__support/common.h"
15+
#include "src/__support/cpu_features.h"
1516

1617
#if defined(LIBC_TARGET_HAS_FMA)
1718
#include "range_reduction_fma.h"

libc/src/math/generic/sinf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "src/__support/FPUtil/PolyEval.h"
1515
#include "src/__support/FPUtil/multiply_add.h"
1616
#include "src/__support/common.h"
17+
#include "src/__support/cpu_features.h"
1718

1819
#include <errno.h>
1920

libc/src/math/generic/tanf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/__support/FPUtil/multiply_add.h"
1616
#include "src/__support/FPUtil/nearest_integer.h"
1717
#include "src/__support/common.h"
18+
#include "src/__support/cpu_features.h"
1819

1920
#include <errno.h>
2021

libc/src/math/generic/tanhf.cpp

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

99
#include "src/math/tanhf.h"
1010
#include "src/__support/FPUtil/FPBits.h"
11+
#include "src/__support/cpu_features.h"
1112
#include "src/math/generic/explogxf.h"
1213

1314
namespace __llvm_libc {

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ libc_support_library(
7979
hdrs = [
8080
"src/__support/architectures.h",
8181
"src/__support/common.h",
82+
"src/__support/cpu_features.h",
8283
"src/__support/endian.h",
8384
],
8485
)

0 commit comments

Comments
 (0)