[libc-commits] [compiler-rt] [libc] [llvm] [Compiler-rt][builtins] introduce libc math routines (PR #197950)

via libc-commits libc-commits at lists.llvm.org
Sat May 16 16:18:50 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-github-workflow

Author: hulxv (hulxv)

<details>
<summary>Changes</summary>

Introduce using libc math routines in compiler-rt builtins. this pr adds `COMPILER_RT_USE_LIBC_MATH` flag and uses math routines in basic arithmetic operations for Quad-Precision values

RFC: https://discourse.llvm.org/t/rfc-explore-using-llvm-libc-math-routines-for-compiler-rt-floating-point-builtins/89670

Part of #<!-- -->197824

---
Full diff: https://github.com/llvm/llvm-project/pull/197950.diff


11 Files Affected:

- (modified) .github/workflows/libc-overlay-tests.yml (+10-2) 
- (modified) compiler-rt/lib/builtins/CMakeLists.txt (+16-1) 
- (renamed) compiler-rt/lib/builtins/addtf3.cpp (+16-4) 
- (added) compiler-rt/lib/builtins/fp_libc_config.h (+12) 
- (modified) compiler-rt/lib/builtins/int_lib.h (+11-3) 
- (added) libc/shared/builtins.h (+16) 
- (added) libc/shared/builtins/addtf3.h (+29) 
- (modified) libc/src/__support/CMakeLists.txt (+1) 
- (modified) libc/src/__support/FPUtil/dyadic_float.h (+3) 
- (added) libc/src/__support/builtins/CMakeLists.txt (+9) 
- (added) libc/src/__support/builtins/addtf3.h (+33) 


``````````diff
diff --git a/.github/workflows/libc-overlay-tests.yml b/.github/workflows/libc-overlay-tests.yml
index f63150983aa03..17b31c6467779 100644
--- a/.github/workflows/libc-overlay-tests.yml
+++ b/.github/workflows/libc-overlay-tests.yml
@@ -97,8 +97,9 @@ jobs:
         -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
         -DCMAKE_POLICY_DEFAULT_CMP0141=NEW
         -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded
+        -DCOMPILER_RT_USE_LIBC_MATH=ON
         -DLIBC_COMPILE_OPTIONS_NATIVE=""
-        -DLLVM_ENABLE_RUNTIMES=libc
+        -DLLVM_ENABLE_RUNTIMES="libc;compiler-rt"
         -G Ninja
         -S ${{ github.workspace }}/runtimes
 
@@ -109,9 +110,16 @@ jobs:
         --parallel 
         --target libc
 
-    - name: Test
+    - name: Test libc
       run: >
         cmake 
         --build ${{ steps.strings.outputs.build-output-dir }} 
         --parallel 
         --target check-libc
+    
+    - name: Test compiler-rt builtins
+      run: >
+        cmake
+        --build ${{ steps.strings.outputs.build-output-dir }}
+        --parallel
+        --target check-builtins
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 3fa21578c86ad..2843d0870f39c 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -215,7 +215,7 @@ set(BF16_SOURCES
 )
 
 set(GENERIC_TF_SOURCES
-  addtf3.c
+  addtf3.cpp
   comparetf2.c
   divtc3.c
   divtf3.c
@@ -243,6 +243,21 @@ set(GENERIC_TF_SOURCES
   trunctfsf2.c
 )
 
+option(COMPILER_RT_USE_LIBC_MATH
+      "Use LLVM libc math routines for floating-point builtins" OFF)
+
+if(COMPILER_RT_USE_LIBC_MATH)
+  set(LLVM_LIBC_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../libc"
+      CACHE PATH "Path to LLVM libc source directory")
+
+  if(NOT EXISTS "${LLVM_LIBC_SRC_DIR}/src/__support/FPUtil/FPBits.h")
+    message(FATAL_ERROR "LLVM libc not found at ${LLVM_LIBC_SRC_DIR}")
+  endif()
+
+  include_directories(SYSTEM ${LLVM_LIBC_SRC_DIR})
+  add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
+endif()
+
 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
   "Skip the atomic builtin (these should normally be provided by a shared library)"
   On)
diff --git a/compiler-rt/lib/builtins/addtf3.c b/compiler-rt/lib/builtins/addtf3.cpp
similarity index 67%
rename from compiler-rt/lib/builtins/addtf3.c
rename to compiler-rt/lib/builtins/addtf3.cpp
index 2cb3a4d591917..f8d0f72a4357d 100644
--- a/compiler-rt/lib/builtins/addtf3.c
+++ b/compiler-rt/lib/builtins/addtf3.cpp
@@ -13,11 +13,23 @@
 #define QUAD_PRECISION
 #include "fp_lib.h"
 
-#if defined(CRT_HAS_TF_MODE)
-#include "fp_add_impl.inc"
+#ifdef COMPILER_RT_USE_LIBC_MATH
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/addtf3.h"
 
 COMPILER_RT_ABI fp_t __addtf3(fp_t a, fp_t b) {
-  return __addXf3__(a, b);
+  return LIBC_NAMESPACE::shared::addtf3(a, b);
 }
 
-#endif
+#else
+
+#include "fp_add_impl.inc"
+#if defined(CRT_HAS_TF_MODE)
+
+COMPILER_RT_ABI fp_t __addtf3(fp_t a, fp_t b) { return __addXf3__(a, b); }
+
+#endif // defined(CRT_HAS_TF_MODE)
+
+#endif // COMPILER_RT_USE_LIBC_MATH
\ No newline at end of file
diff --git a/compiler-rt/lib/builtins/fp_libc_config.h b/compiler-rt/lib/builtins/fp_libc_config.h
new file mode 100644
index 0000000000000..1dc69a9f95167
--- /dev/null
+++ b/compiler-rt/lib/builtins/fp_libc_config.h
@@ -0,0 +1,12 @@
+#ifndef FP_LIBC_CONFIG_H
+#define FP_LIBC_CONFIG_H
+
+#ifndef LIBC_COPT_PUBLIC_PACKAGING
+#define LIBC_COPT_PUBLIC_PACKAGING
+#endif
+
+#ifndef LIBC_NAMESPACE
+#define LIBC_NAMESPACE __llvm_libc_rt
+#endif
+
+#endif
\ No newline at end of file
diff --git a/compiler-rt/lib/builtins/int_lib.h b/compiler-rt/lib/builtins/int_lib.h
index 629c3065da6a0..8e76985134d94 100644
--- a/compiler-rt/lib/builtins/int_lib.h
+++ b/compiler-rt/lib/builtins/int_lib.h
@@ -20,14 +20,22 @@
 
 // ABI macro definitions
 
+// In C++ translation units we must give compiler-rt entry points C linkage so
+// that their symbol names are not mangled.  C TUs are unaffected.
+#ifdef __cplusplus
+#define COMPILER_RT_C_LINKAGE extern "C"
+#else
+#define COMPILER_RT_C_LINKAGE
+#endif
+
 #if __ARM_EABI__
 #ifdef COMPILER_RT_ARMHF_TARGET
-#define COMPILER_RT_ABI
+#define COMPILER_RT_ABI COMPILER_RT_C_LINKAGE
 #else
-#define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
+#define COMPILER_RT_ABI COMPILER_RT_C_LINKAGE __attribute__((__pcs__("aapcs")))
 #endif
 #else
-#define COMPILER_RT_ABI
+#define COMPILER_RT_ABI COMPILER_RT_C_LINKAGE
 #endif
 
 #define AEABI_RTABI __attribute__((__pcs__("aapcs")))
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
new file mode 100644
index 0000000000000..157b6622cbcff
--- /dev/null
+++ b/libc/shared/builtins.h
@@ -0,0 +1,16 @@
+//===-- Compiler-runtime builtin primitives ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_H
+#define LLVM_LIBC_SHARED_BUILTINS_H
+
+#include "libc_common.h"
+
+#include "builtins/addtf3.h"
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_H
diff --git a/libc/shared/builtins/addtf3.h b/libc/shared/builtins/addtf3.h
new file mode 100644
index 0000000000000..5dfda22d929b7
--- /dev/null
+++ b/libc/shared/builtins/addtf3.h
@@ -0,0 +1,29 @@
+//===-- Shared __addtf3 function -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
+#define LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/addtf3.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::addtf3;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 098fb6ef86936..389855cf95afc 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -462,6 +462,7 @@ add_subdirectory(wchar)
 add_subdirectory(wctype)
 
 add_subdirectory(math)
+add_subdirectory(builtins)
 if(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE)
   add_subdirectory(mathvec)
 endif()
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 8ce041247716b..04e7c0c34859b 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -415,6 +415,9 @@ template <size_t Bits> struct DyadicFloat {
     if constexpr (cpp::is_same_v<T, bfloat16>
 #if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(__LIBC_USE_FLOAT16_CONVERSION)
                   || cpp::is_same_v<T, float16>
+#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128)
+                  || cpp::is_same_v<T, float128>
 #endif
     )
       return generic_as<T, ShouldSignalExceptions>();
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
new file mode 100644
index 0000000000000..c4982887d0a7c
--- /dev/null
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -0,0 +1,9 @@
+add_header_library(
+  addtf3
+  HDRS
+    addtf3.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/builtins/addtf3.h b/libc/src/__support/builtins/addtf3.h
new file mode 100644
index 0000000000000..f2c5b5332e877
--- /dev/null
+++ b/libc/src/__support/builtins/addtf3.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for __addtf3 ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Same-precision quad-precision addition.
+// Mirrors the compiler-rt __addtf3 ABI: a + b at float128 precision.
+LIBC_INLINE float128 addtf3(float128 x, float128 y) {
+  return fputil::generic::add<float128>(x, y);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H

``````````

</details>


https://github.com/llvm/llvm-project/pull/197950


More information about the libc-commits mailing list