[compiler-rt] [llvm] [compiler-rt] Add check-builtins target for LLVM_ENABLE_RUNTIMES builds (PR #166837)

Andrew Haberlandt via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 8 09:35:48 PST 2025


https://github.com/ndrewh updated https://github.com/llvm/llvm-project/pull/166837

>From ab30eb2a4ec6b9bfeb0913e3002b3d74aa25fe42 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Wed, 5 Nov 2025 21:16:42 -0800
Subject: [PATCH 1/3] [compiler-rt] Add check-builtins target for
 LLVM_ENABLE_RUNTIMES-based build

---
 .../cmake/Modules/CompilerRTDarwinUtils.cmake |  1 +
 compiler-rt/lib/builtins/CMakeLists.txt       |  1 +
 compiler-rt/test/CMakeLists.txt               |  2 +-
 compiler-rt/test/builtins/CMakeLists.txt      | 26 ++++++++++++++---
 llvm/runtimes/CMakeLists.txt                  | 28 ++++++++++++++++++-
 5 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake b/compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake
index c6777bda9f0d1..7b679b2d27ae5 100644
--- a/compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake
+++ b/compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake
@@ -284,6 +284,7 @@ macro(darwin_add_builtin_library name suffix)
     ${ARGN})
   set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
   add_library(${libname} STATIC ${LIB_SOURCES})
+  file(WRITE "${CMAKE_BINARY_DIR}/${libname}.sources.txt" "${LIB_SOURCES}")
   if(DARWIN_${LIB_OS}_SYSROOT)
     set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
   endif()
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 7e8621855eb84..ecda9512d9de6 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -1028,6 +1028,7 @@ else ()
                               C_STANDARD 11
                               CXX_STANDARD 17
                               PARENT_TARGET builtins)
+      file(WRITE "${CMAKE_BINARY_DIR}/clang_rt.builtins-${arch}.sources.txt" "${${arch}_SOURCES}")
       cmake_pop_check_state()
     endif ()
   endforeach ()
diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt
index 9cfb7ea559475..c94e7c04b021f 100644
--- a/compiler-rt/test/CMakeLists.txt
+++ b/compiler-rt/test/CMakeLists.txt
@@ -73,7 +73,7 @@ endfunction()
 # Run sanitizer tests only if we're sure that clang would produce
 # working binaries.
 if(COMPILER_RT_CAN_EXECUTE_TESTS)
-  if(COMPILER_RT_BUILD_BUILTINS)
+  if(COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
     add_subdirectory(builtins)
   endif()
   if(COMPILER_RT_BUILD_SANITIZERS)
diff --git a/compiler-rt/test/builtins/CMakeLists.txt b/compiler-rt/test/builtins/CMakeLists.txt
index 36135c7905900..39cbc06bef233 100644
--- a/compiler-rt/test/builtins/CMakeLists.txt
+++ b/compiler-rt/test/builtins/CMakeLists.txt
@@ -1,6 +1,16 @@
 set(BUILTINS_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
-set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
+# If COMPILER_RT_FORCE_TEST_BUILTINS_DIR is set, the builtins
+# were already built and we are just going to test them.
+# NOTE: This is currently an LLVM-internal option which should
+# only be used by the LLVM_ENABLE_RUNTIMES build configured
+# in llvm/runtimes/CMakeLists.txt
+if(COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
+  set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
+else()
+  set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
+endif()
+
 set(BUILTINS_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/TestCases)
 
 # Test cases.
@@ -84,10 +94,18 @@ foreach(arch ${BUILTIN_TEST_ARCH})
   else()
     set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins-${arch}")
   endif()
-  if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
-    message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
+  # Normally, we can just inspect the target directly to get the sources, but if
+  # we are testing an externally-built builtins library, we expect
+  # COMPILER_RT_FORCE_TEST_BUILTINS_DIR to be set and contain a file named
+  # ${BUILTIN_LIB_TARGET_NAME}.sources.txt from the builtins build
+  if(NOT COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
+    if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
+      message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
+    endif()
+    get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
+  else()
+    file(READ "${COMPILER_RT_FORCE_TEST_BUILTINS_DIR}/${BUILTIN_LIB_TARGET_NAME}.sources.txt" BUILTIN_LIB_SOURCES)
   endif()
-  get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
   list(LENGTH BUILTIN_LIB_SOURCES BUILTIN_LIB_SOURCES_LENGTH)
   if (BUILTIN_LIB_SOURCES_LENGTH EQUAL 0)
     message(FATAL_ERROR "Failed to find source files for ${arch} builtin library")
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index d877f0b883cc4..88fb323e5e449 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -255,7 +255,10 @@ function(runtime_default_target)
 
   if(LLVM_INCLUDE_TESTS)
     set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_TESTSUITES "@${LLVM_BINARY_DIR}/runtimes/runtimes-bins/lit.tests")
-    list(APPEND test_targets runtimes-test-depends check-runtimes)
+    list(APPEND test_targets runtimes-test-depends check-runtimes check-builtins)
+
+    # The default runtimes target can run tests the default builtins target
+    list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_FORCE_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-bins/")
   endif()
 
   set_enable_per_target_runtime_dir()
@@ -362,6 +365,15 @@ function(runtime_register_target name)
       list(APPEND ${name}_test_targets ${target}-${name})
       list(APPEND test_targets ${target}-${name})
     endforeach()
+
+    # If a builtins-${name} target exists, we'll test those builtins
+    # with this runtimes build
+    if(TARGET builtins-${name})
+      list(APPEND ARG_CMAKE_ARGS "-DCOMPILER_RT_FORCE_TEST_BUILTINS_DIR=${LLVM_BINARY_DIR}/runtimes/builtins-${name}-bins/")
+      set(check-builtins-${name} check-builtins)
+      list(APPEND ${name}_test_targets check-builtins-${name})
+      list(APPEND test_targets check-builtins-${name})
+    endif()
     set(test_targets "${test_targets}" PARENT_SCOPE)
   endif()
 
@@ -427,6 +439,9 @@ function(runtime_register_target name)
   if(LLVM_INCLUDE_TESTS)
     add_dependencies(check-runtimes check-runtimes-${name})
     add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
+    if(TARGET builtins-${name})
+      add_dependencies(check-builtins check-builtins-${name})
+    endif()
   endif()
   foreach(runtime_name ${runtime_names})
     if(NOT TARGET ${runtime_name})
@@ -602,6 +617,17 @@ if(build_runtimes)
           PROPERTIES FOLDER "Runtimes"
         )
         set(test_targets "")
+
+        # NOTE: Currently, the builtins tests are run with the runtimes build,
+        # and the default runtimes target installs a check-builtins target
+        # which forwards to the default builtins build. If the default runtimes
+        # target is not used, we create a custom target which will depend on
+        # each check-builtins-${name}.
+        add_custom_target(check-builtins)
+        set_target_properties(
+          check-builtins
+          PROPERTIES FOLDER "Compiler-RT"
+        )
       endif()
       if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
         foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})

>From 11b426061d8572568387e48eedfff54fc77bd755 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Sun, 7 Dec 2025 21:38:30 -0800
Subject: [PATCH 2/3] Move COMPILER_RT_HAS__FLOAT16 to builtin-config-ix.cmake

---
 compiler-rt/cmake/builtin-config-ix.cmake | 8 ++++++++
 compiler-rt/lib/builtins/CMakeLists.txt   | 3 ---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/cmake/builtin-config-ix.cmake b/compiler-rt/cmake/builtin-config-ix.cmake
index 238749681a6ca..2642e095dcfc4 100644
--- a/compiler-rt/cmake/builtin-config-ix.cmake
+++ b/compiler-rt/cmake/builtin-config-ix.cmake
@@ -280,6 +280,14 @@ else()
   # Architectures supported by compiler-rt libraries.
   filter_available_targets(BUILTIN_SUPPORTED_ARCH
     ${ALL_BUILTIN_SUPPORTED_ARCH})
+
+  # COMPILER_RT_HAS_${arch}_* defines that are shared between lib/builtins/ and test/builtins/
+  foreach (arch ${BUILTIN_SUPPORTED_ARCH})
+    # NOTE: The corresponding check for if(APPLE) is in CompilerRTDarwinUtils.cmake
+    check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
+                              int main(void) { return 0; }"
+                            COMPILER_RT_HAS_${arch}_FLOAT16)
+  endforeach()
 endif()
 
 if(OS_NAME MATCHES "Linux|SerenityOS" AND NOT LLVM_USE_SANITIZER AND NOT
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index ecda9512d9de6..8071668588c93 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -990,9 +990,6 @@ else ()
           endif()
         endif()
       endif()
-      check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }
-                               int main(void) { return 0; }"
-                              COMPILER_RT_HAS_${arch}_FLOAT16)
       append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
       check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }
                                int main(void) { return 0; }"

>From fa813311c26ce76b7f2de1cd5317a44d1b48d198 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Mon, 8 Dec 2025 09:35:33 -0800
Subject: [PATCH 3/3] Improve comments; COMPILER_RT_FORCE_TEST_BUILTINS_DIR =>
 COMPILER_RT_TEST_BUILTINS_DIR}

---
 compiler-rt/lib/builtins/CMakeLists.txt  |  2 ++
 compiler-rt/test/CMakeLists.txt          |  5 ++++-
 compiler-rt/test/builtins/CMakeLists.txt | 13 +++++++------
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 8071668588c93..19483d2547997 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -1025,6 +1025,8 @@ else ()
                               C_STANDARD 11
                               CXX_STANDARD 17
                               PARENT_TARGET builtins)
+      # Write out the sources that were used to compile the builtins so that tests can be run in
+      # an independent compiler-rt build (see: compiler-rt/test/builtins/CMakeLists.txt)
       file(WRITE "${CMAKE_BINARY_DIR}/clang_rt.builtins-${arch}.sources.txt" "${${arch}_SOURCES}")
       cmake_pop_check_state()
     endif ()
diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt
index c94e7c04b021f..749f244913dc9 100644
--- a/compiler-rt/test/CMakeLists.txt
+++ b/compiler-rt/test/CMakeLists.txt
@@ -73,7 +73,10 @@ endfunction()
 # Run sanitizer tests only if we're sure that clang would produce
 # working binaries.
 if(COMPILER_RT_CAN_EXECUTE_TESTS)
-  if(COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
+  # COMPILER_RT_TEST_BUILTINS_DIR allows running tests against builtins built
+  # in an independent build. This option is only indended to be used by
+  # LLVM_ENABLE_RUNTIMES-based builds.
+  if(COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_TEST_BUILTINS_DIR)
     add_subdirectory(builtins)
   endif()
   if(COMPILER_RT_BUILD_SANITIZERS)
diff --git a/compiler-rt/test/builtins/CMakeLists.txt b/compiler-rt/test/builtins/CMakeLists.txt
index 39cbc06bef233..64545a711b0bc 100644
--- a/compiler-rt/test/builtins/CMakeLists.txt
+++ b/compiler-rt/test/builtins/CMakeLists.txt
@@ -1,11 +1,11 @@
 set(BUILTINS_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
-# If COMPILER_RT_FORCE_TEST_BUILTINS_DIR is set, the builtins
+# If COMPILER_RT_TEST_BUILTINS_DIR is set, the builtins
 # were already built and we are just going to test them.
 # NOTE: This is currently an LLVM-internal option which should
 # only be used by the LLVM_ENABLE_RUNTIMES build configured
 # in llvm/runtimes/CMakeLists.txt
-if(COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
+if(COMPILER_RT_TEST_BUILTINS_DIR)
   set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
 else()
   set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
@@ -96,15 +96,16 @@ foreach(arch ${BUILTIN_TEST_ARCH})
   endif()
   # Normally, we can just inspect the target directly to get the sources, but if
   # we are testing an externally-built builtins library, we expect
-  # COMPILER_RT_FORCE_TEST_BUILTINS_DIR to be set and contain a file named
-  # ${BUILTIN_LIB_TARGET_NAME}.sources.txt from the builtins build
-  if(NOT COMPILER_RT_FORCE_TEST_BUILTINS_DIR)
+  # COMPILER_RT_TEST_BUILTINS_DIR to be set and contain a file named
+  # ${BUILTIN_LIB_TARGET_NAME}.sources.txt from the builtins build. This file
+  # is created by compiler-rt/lib/builtins/CMakeLists.txt
+  if(NOT COMPILER_RT_TEST_BUILTINS_DIR)
     if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
       message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
     endif()
     get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
   else()
-    file(READ "${COMPILER_RT_FORCE_TEST_BUILTINS_DIR}/${BUILTIN_LIB_TARGET_NAME}.sources.txt" BUILTIN_LIB_SOURCES)
+    file(READ "${COMPILER_RT_TEST_BUILTINS_DIR}/${BUILTIN_LIB_TARGET_NAME}.sources.txt" BUILTIN_LIB_SOURCES)
   endif()
   list(LENGTH BUILTIN_LIB_SOURCES BUILTIN_LIB_SOURCES_LENGTH)
   if (BUILTIN_LIB_SOURCES_LENGTH EQUAL 0)



More information about the llvm-commits mailing list