Skip to content

Commit 68d8b38

Browse files
authored
[builtins] Fix missing main() function in float16/bfloat16 support checks (#104478)
The CMake docs state that `check_c_source_compiles()` checks whether the supplied code "can be compiled as a C source file and linked as an executable (so it must contain at least a `main()` function)." https://cmake.org/cmake/help/v3.30/module/CheckCSourceCompiles.html In practice, this command is a wrapper around `try_compile()`: - https://gitlab.kitware.com/cmake/cmake/blob/2904ce00d2ed6ad5dac6d3459af62d8223e06ce0/Modules/CheckCSourceCompiles.cmake#L54 - https://gitlab.kitware.com/cmake/cmake/blob/2904ce00d2ed6ad5dac6d3459af62d8223e06ce0/Modules/Internal/CheckSourceCompiles.cmake#L101 When `CMAKE_SOURCE_DIR` is compiler-rt/lib/builtins/, `CMAKE_TRY_COMPILE_TARGET_TYPE` is set to `STATIC_LIBRARY`, so the checks for `float16` and `bfloat16` support work as intended in a Clang + compiler-rt runtime build for example, as it runs CMake recursively from that directory. However, when using llvm/ or compiler-rt/ as CMake source directory, as `CMAKE_TRY_COMPILE_TARGET_TYPE` defaults to `EXECUTABLE`, these checks will indeed fail if the code doesn't have a `main()` function. This results in LLVM using x86 SIMD registers when generating calls to builtins that, with Arch Linux's compiler-rt package for example, actually use a GPR for their argument or return value as they use `uint16_t` instead of `_Float16`. This had been caught in post-commit review: https://reviews.llvm.org/D145237#4521152. Use of the internal `CMAKE_C_COMPILER_WORKS` variable is not what hides the issue, however. PR #69842 tried to fix this by unconditionally setting `CMAKE_TRY_COMPILE_TARGET_TYPE` to `STATIC_LIBRARY`, but it apparently caused other issues, so it was reverted. This PR just adds a `main()` function in the checks, as per the CMake docs.
1 parent fab9256 commit 68d8b38

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,12 @@ else ()
868868
endif()
869869
endif()
870870
endif()
871-
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }"
871+
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
872+
int main(void) { return 0; }"
872873
COMPILER_RT_HAS_${arch}_FLOAT16)
873874
append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
874-
check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }"
875+
check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }
876+
int main(void) { return 0; }"
875877
COMPILER_RT_HAS_${arch}_BFLOAT16)
876878
# Build BF16 files only when "__bf16" is available.
877879
if(COMPILER_RT_HAS_${arch}_BFLOAT16)

0 commit comments

Comments
 (0)