Skip to content

Commit e0ffaab

Browse files
arichardsontstellar
authored andcommitted
[builtins] Only build float16/bfloat16 code if actually supported
When building compiler-rt builtins for x86_64 they library will by default also be built for i386. We unconditionally add the Float16 compile flags since the check for Float16 support will be done using x86_64 compiler flags, but i386 does not actually support it. Fix this by moving the COMPILER_RT_HAS_FLOAT16 and COMPILER_RT_HAS_FLOAT16 checks to a per-target-architecture check inside the loop (using `check_c_source_compiles` and `cmake_{push,pop}_check_state`). Many of the checks in the builtin-config-ix file should probably also be changed to per-target-arch checks, but so far only the Float16 one has caused issues. This is an alternative to D136044 which added a special case for i386 FreeBSD. Fixes: #57224 Differential Revision: https://reviews.llvm.org/D145237 (cherry picked from commit 489bda6)
1 parent c1949c6 commit e0ffaab

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

compiler-rt/cmake/builtin-config-ix.cmake

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ int foo(int x, int y) {
2222
}
2323
")
2424

25-
builtin_check_c_compiler_source(COMPILER_RT_HAS_FLOAT16
26-
"
27-
_Float16 foo(_Float16 x) {
28-
return x;
29-
}
30-
"
31-
)
32-
33-
builtin_check_c_compiler_source(COMPILER_RT_HAS_BFLOAT16
34-
"
35-
__bf16 foo(__bf16 x) {
36-
return x;
37-
}
38-
"
39-
)
40-
4125
builtin_check_c_compiler_source(COMPILER_RT_HAS_ASM_LSE
4226
"
4327
asm(\".arch armv8-a+lse\");

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ if (COMPILER_RT_STANDALONE_BUILD)
5858
endif()
5959

6060
include(builtin-config-ix)
61+
include(CMakePushCheckState)
6162

6263
if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
6364
include(CompilerRTAIXUtils)
@@ -190,14 +191,11 @@ set(GENERIC_SOURCES
190191
umodti3.c
191192
)
192193

193-
# Build BF16 files only when "__bf16" is available.
194-
if(COMPILER_RT_HAS_BFLOAT16 AND NOT APPLE)
195-
set(GENERIC_SOURCES
196-
${GENERIC_SOURCES}
194+
# We only build BF16 files when "__bf16" is available.
195+
set(BF16_SOURCES
197196
truncdfbf2.c
198197
truncsfbf2.c
199-
)
200-
endif()
198+
)
201199

202200
# TODO: Several "tf" files (and divtc3.c, but not multc3.c) are in
203201
# GENERIC_SOURCES instead of here.
@@ -747,8 +745,6 @@ else ()
747745
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
748746
endif()
749747

750-
append_list_if(COMPILER_RT_HAS_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS)
751-
752748
append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
753749

754750
# These flags would normally be added to CMAKE_C_FLAGS by the llvm
@@ -780,7 +776,11 @@ else ()
780776

781777
foreach (arch ${BUILTIN_SUPPORTED_ARCH})
782778
if (CAN_TARGET_${arch})
779+
cmake_push_check_state()
780+
# TODO: we should probably make most of the checks in builtin-config depend on the target flags.
781+
message(STATUS "Performing additional configure checks with target flags: ${TARGET_${arch}_CFLAGS}")
783782
set(BUILTIN_CFLAGS_${arch} ${BUILTIN_CFLAGS})
783+
list(APPEND CMAKE_REQUIRED_FLAGS ${TARGET_${arch}_CFLAGS} ${BUILTIN_CFLAGS_${arch}})
784784
# For ARM archs, exclude any VFP builtins if VFP is not supported
785785
if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em|armv8m.main|armv8.1m.main)$")
786786
string(REPLACE ";" " " _TARGET_${arch}_CFLAGS "${TARGET_${arch}_CFLAGS}")
@@ -799,6 +799,15 @@ else ()
799799
endif()
800800
endif()
801801
endif()
802+
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }"
803+
COMPILER_RT_HAS_${arch}_FLOAT16)
804+
append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
805+
check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }"
806+
COMPILER_RT_HAS_${arch}_BFLOAT16)
807+
# Build BF16 files only when "__bf16" is available.
808+
if(COMPILER_RT_HAS_${arch}_BFLOAT16)
809+
list(APPEND ${arch}_SOURCES ${BF16_SOURCES})
810+
endif()
802811

803812
# Remove a generic C builtin when an arch-specific builtin is specified.
804813
filter_builtin_sources(${arch}_SOURCES ${arch})
@@ -833,6 +842,7 @@ else ()
833842
DEFS ${BUILTIN_DEFS}
834843
CFLAGS ${BUILTIN_CFLAGS_${arch}}
835844
PARENT_TARGET builtins)
845+
cmake_pop_check_state()
836846
endif ()
837847
endforeach ()
838848
endif ()

0 commit comments

Comments
 (0)