Skip to content

Commit c5d17bd

Browse files
authored
[compiler-rt] Add check-builtins target for LLVM_ENABLE_RUNTIMES builds (#166837)
When doing a LLVM_ENABLE_RUNTIMES-based build of clang+compiler_rt, the `check-builtins` target is missing and builtins tests are not run (#112105, #144090). This provides one possible path forward for enabling these tests. The approach taken here is to test the builtins with the `compiler-rt` runtime build (i.e. only if you pass `LLVM_ENABLE_RUNTIMES='compiler-rt'`). The builtins tests currently rely on shared test infrastructure in `compiler-rt/`, so this is probably the easiest solution without relocating the builtins and their tests outside of `compiler-rt/`. The main challenge is that the built-ins test configuration expects to be able to inspect the builtin target and see the sources used to build it: ``` get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES) ``` Since the builtins build and runtimes build are separate under LLVM_ENABLE_RUNTIMES, this target inspection is not possible. To get around this, we write a temporary file alongside each builtins library containing the list of sources (e.g. `"${CMAKE_BINARY_DIR}/clang_rt.builtins-${arch}.sources.txt"`). Then, we introduce an undocumented compiler-rt option `COMPILER_RT_FORCE_TEST_BUILTINS_DIR` (which is only intended to be used by the LLVM_ENABLE_RUNTIMES build, and could be removed after builtins re relocated) that passes the path to the directory containing this file, and configures builtins tests (even though the runtimes build has `COMPILER_RT_BUILD_BUILTINS=OFF`) rdar://163518748
1 parent fa511cd commit c5d17bd

File tree

6 files changed

+70
-9
lines changed

6 files changed

+70
-9
lines changed

compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ macro(darwin_add_builtin_library name suffix)
284284
${ARGN})
285285
set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
286286
add_library(${libname} STATIC ${LIB_SOURCES})
287+
288+
# Write out the sources that were used to compile the builtins so that tests can be run in
289+
# an independent compiler-rt build (see: compiler-rt/test/builtins/CMakeLists.txt)
290+
file(WRITE "${CMAKE_BINARY_DIR}/${libname}.sources.txt" "${LIB_SOURCES}")
291+
287292
if(DARWIN_${LIB_OS}_SYSROOT)
288293
set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
289294
endif()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ else()
280280
# Architectures supported by compiler-rt libraries.
281281
filter_available_targets(BUILTIN_SUPPORTED_ARCH
282282
${ALL_BUILTIN_SUPPORTED_ARCH})
283+
284+
# COMPILER_RT_HAS_${arch}_* defines that are shared between lib/builtins/ and test/builtins/
285+
foreach (arch ${BUILTIN_SUPPORTED_ARCH})
286+
# NOTE: The corresponding check for if(APPLE) is in CompilerRTDarwinUtils.cmake
287+
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
288+
int main(void) { return 0; }"
289+
COMPILER_RT_HAS_${arch}_FLOAT16)
290+
endforeach()
283291
endif()
284292

285293
if(OS_NAME MATCHES "Linux|SerenityOS" AND NOT LLVM_USE_SANITIZER AND NOT

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,6 @@ else ()
990990
endif()
991991
endif()
992992
endif()
993-
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
994-
int main(void) { return 0; }"
995-
COMPILER_RT_HAS_${arch}_FLOAT16)
996993
append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
997994
check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }
998995
int main(void) { return 0; }"
@@ -1028,6 +1025,9 @@ else ()
10281025
C_STANDARD 11
10291026
CXX_STANDARD 17
10301027
PARENT_TARGET builtins)
1028+
# Write out the sources that were used to compile the builtins so that tests can be run in
1029+
# an independent compiler-rt build (see: compiler-rt/test/builtins/CMakeLists.txt)
1030+
file(WRITE "${CMAKE_BINARY_DIR}/clang_rt.builtins-${arch}.sources.txt" "${${arch}_SOURCES}")
10311031
cmake_pop_check_state()
10321032
endif ()
10331033
endforeach ()

compiler-rt/test/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ endfunction()
7373
# Run sanitizer tests only if we're sure that clang would produce
7474
# working binaries.
7575
if(COMPILER_RT_CAN_EXECUTE_TESTS)
76-
if(COMPILER_RT_BUILD_BUILTINS)
76+
# COMPILER_RT_TEST_BUILTINS_DIR allows running tests against builtins built
77+
# in an independent build. This option is only indended to be used by
78+
# LLVM_ENABLE_RUNTIMES-based builds.
79+
if(COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_TEST_BUILTINS_DIR)
7780
add_subdirectory(builtins)
7881
endif()
7982
if(COMPILER_RT_BUILD_SANITIZERS)

compiler-rt/test/builtins/CMakeLists.txt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
set(BUILTINS_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
22

3-
set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
3+
# If COMPILER_RT_TEST_BUILTINS_DIR is set, the builtins
4+
# were already built and we are just going to test them.
5+
# NOTE: This is currently an LLVM-internal option which should
6+
# only be used by the LLVM_ENABLE_RUNTIMES build configured
7+
# in llvm/runtimes/CMakeLists.txt
8+
if(COMPILER_RT_TEST_BUILTINS_DIR)
9+
set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
10+
else()
11+
set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
12+
endif()
13+
414
set(BUILTINS_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/TestCases)
515

616
# Test cases.
@@ -84,10 +94,19 @@ foreach(arch ${BUILTIN_TEST_ARCH})
8494
else()
8595
set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins-${arch}")
8696
endif()
87-
if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
88-
message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
97+
# Normally, we can just inspect the target directly to get the sources, but if
98+
# we are testing an externally-built builtins library, we expect
99+
# COMPILER_RT_TEST_BUILTINS_DIR to be set and contain a file named
100+
# ${BUILTIN_LIB_TARGET_NAME}.sources.txt from the builtins build. This file
101+
# is created by compiler-rt/lib/builtins/CMakeLists.txt
102+
if(NOT COMPILER_RT_TEST_BUILTINS_DIR)
103+
if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
104+
message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
105+
endif()
106+
get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
107+
else()
108+
file(READ "${COMPILER_RT_TEST_BUILTINS_DIR}/${BUILTIN_LIB_TARGET_NAME}.sources.txt" BUILTIN_LIB_SOURCES)
89109
endif()
90-
get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
91110
list(LENGTH BUILTIN_LIB_SOURCES BUILTIN_LIB_SOURCES_LENGTH)
92111
if (BUILTIN_LIB_SOURCES_LENGTH EQUAL 0)
93112
message(FATAL_ERROR "Failed to find source files for ${arch} builtin library")

llvm/runtimes/CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ function(runtime_default_target)
255255

256256
if(LLVM_INCLUDE_TESTS)
257257
set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_TESTSUITES "@${LLVM_BINARY_DIR}/runtimes/runtimes-bins/lit.tests")
258-
list(APPEND test_targets runtimes-test-depends check-runtimes)
258+
list(APPEND test_targets runtimes-test-depends check-runtimes check-builtins)
259+
260+
# The default runtimes target can run tests the default builtins target
261+
list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_FORCE_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-bins/")
259262
endif()
260263

261264
set_enable_per_target_runtime_dir()
@@ -362,6 +365,15 @@ function(runtime_register_target name)
362365
list(APPEND ${name}_test_targets ${target}-${name})
363366
list(APPEND test_targets ${target}-${name})
364367
endforeach()
368+
369+
# If a builtins-${name} target exists, we'll test those builtins
370+
# with this runtimes build
371+
if(TARGET builtins-${name})
372+
list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_FORCE_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-${name}-bins/")
373+
set(check-builtins-${name} check-builtins)
374+
list(APPEND ${name}_test_targets check-builtins-${name})
375+
list(APPEND test_targets check-builtins-${name})
376+
endif()
365377
set(test_targets "${test_targets}" PARENT_SCOPE)
366378
endif()
367379

@@ -427,6 +439,9 @@ function(runtime_register_target name)
427439
if(LLVM_INCLUDE_TESTS)
428440
add_dependencies(check-runtimes check-runtimes-${name})
429441
add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
442+
if(TARGET builtins-${name})
443+
add_dependencies(check-builtins check-builtins-${name})
444+
endif()
430445
endif()
431446
foreach(runtime_name ${runtime_names})
432447
if(NOT TARGET ${runtime_name})
@@ -602,6 +617,17 @@ if(build_runtimes)
602617
PROPERTIES FOLDER "Runtimes"
603618
)
604619
set(test_targets "")
620+
621+
# NOTE: Currently, the builtins tests are run with the runtimes build,
622+
# and the default runtimes target installs a check-builtins target
623+
# which forwards to the default builtins build. If the default runtimes
624+
# target is not used, we create a custom target which will depend on
625+
# each check-builtins-${name}.
626+
add_custom_target(check-builtins)
627+
set_target_properties(
628+
check-builtins
629+
PROPERTIES FOLDER "Compiler-RT"
630+
)
605631
endif()
606632
if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
607633
foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})

0 commit comments

Comments
 (0)