Skip to content

Commit d1aca4b

Browse files
ndrewhpetrhosek
andauthored
Reland check-builtins target for LLVM_ENABLE_RUNTIMES (#171941)
Reland #166837 and #171741 (Reverts the revert in #171940) --------- Co-authored-by: Petr Hosek <phosek@google.com>
1 parent 4cba804 commit d1aca4b

6 files changed

Lines changed: 73 additions & 10 deletions

File tree

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
@@ -281,6 +281,14 @@ else()
281281
# Architectures supported by compiler-rt libraries.
282282
filter_available_targets(BUILTIN_SUPPORTED_ARCH
283283
${ALL_BUILTIN_SUPPORTED_ARCH})
284+
285+
# COMPILER_RT_HAS_${arch}_* defines that are shared between lib/builtins/ and test/builtins/
286+
foreach (arch ${BUILTIN_SUPPORTED_ARCH})
287+
# NOTE: The corresponding check for if(APPLE) is in CompilerRTDarwinUtils.cmake
288+
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
289+
int main(void) { return 0; }"
290+
COMPILER_RT_HAS_${arch}_FLOAT16)
291+
endforeach()
284292
endif()
285293

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

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,6 @@ else ()
994994
endif()
995995
endif()
996996
endif()
997-
check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
998-
int main(void) { return 0; }"
999-
COMPILER_RT_HAS_${arch}_FLOAT16)
1000997
append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
1001998
check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }
1002999
int main(void) { return 0; }"
@@ -1032,6 +1029,11 @@ else ()
10321029
C_STANDARD 11
10331030
CXX_STANDARD 17
10341031
PARENT_TARGET builtins)
1032+
1033+
# Write out the sources that were used to compile the builtins so that tests can be run in
1034+
# an independent compiler-rt build (see: compiler-rt/test/builtins/CMakeLists.txt)
1035+
set_output_name(BUILTIN_LIB_TARGET_NAME clang_rt.builtins ${arch})
1036+
file(WRITE "${CMAKE_BINARY_DIR}/${BUILTIN_LIB_TARGET_NAME}.sources.txt" "${${arch}_SOURCES}")
10351037
cmake_pop_check_state()
10361038
endif ()
10371039
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: 24 additions & 5 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.
@@ -82,12 +92,21 @@ foreach(arch ${BUILTIN_TEST_ARCH})
8292
# TODO: Support other Apple platforms.
8393
set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins_${arch}_osx")
8494
else()
85-
set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins-${arch}")
95+
set_output_name(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
@@ -262,7 +262,10 @@ function(runtime_default_target)
262262

263263
if(LLVM_INCLUDE_TESTS)
264264
set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_TESTSUITES "@${LLVM_BINARY_DIR}/runtimes/runtimes-bins/lit.tests")
265-
list(APPEND test_targets runtimes-test-depends check-runtimes)
265+
list(APPEND test_targets runtimes-test-depends check-runtimes check-builtins)
266+
267+
# The default runtimes target can run tests the default builtins target
268+
list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-bins/")
266269
endif()
267270

268271
set_enable_per_target_runtime_dir()
@@ -369,6 +372,15 @@ function(runtime_register_target name)
369372
list(APPEND ${name}_test_targets ${target}-${name})
370373
list(APPEND test_targets ${target}-${name})
371374
endforeach()
375+
376+
# If a builtins-${name} target exists, we'll test those builtins
377+
# with this runtimes build
378+
if(TARGET builtins-${name})
379+
list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-${name}-bins/")
380+
set(check-builtins-${name} check-builtins)
381+
list(APPEND ${name}_test_targets check-builtins-${name})
382+
list(APPEND test_targets check-builtins-${name})
383+
endif()
372384
set(test_targets "${test_targets}" PARENT_SCOPE)
373385
endif()
374386

@@ -434,6 +446,9 @@ function(runtime_register_target name)
434446
if(LLVM_INCLUDE_TESTS)
435447
add_dependencies(check-runtimes check-runtimes-${name})
436448
add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
449+
if(TARGET builtins-${name})
450+
add_dependencies(check-builtins check-builtins-${name})
451+
endif()
437452
endif()
438453
foreach(runtime_name ${runtime_names})
439454
if(NOT TARGET ${runtime_name})
@@ -609,6 +624,17 @@ if(build_runtimes)
609624
PROPERTIES FOLDER "Runtimes"
610625
)
611626
set(test_targets "")
627+
628+
# NOTE: Currently, the builtins tests are run with the runtimes build,
629+
# and the default runtimes target installs a check-builtins target
630+
# which forwards to the default builtins build. If the default runtimes
631+
# target is not used, we create a custom target which will depend on
632+
# each check-builtins-${name}.
633+
add_custom_target(check-builtins)
634+
set_target_properties(
635+
check-builtins
636+
PROPERTIES FOLDER "Compiler-RT"
637+
)
612638
endif()
613639
if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
614640
foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})

0 commit comments

Comments
 (0)