[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 17:01:24 PDT 2026


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

>From 01e90165690afd710b2d96a56d5419b52b97f6c1 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sun, 17 May 2026 02:12:46 +0300
Subject: [PATCH 1/2] [Compiler-rt][builtins] introduce libc math routines

---
 .github/workflows/libc-overlay-tests.yml      | 12 +++++--
 compiler-rt/lib/builtins/CMakeLists.txt       | 17 +++++++++-
 .../lib/builtins/{addtf3.c => addtf3.cpp}     | 20 ++++++++---
 compiler-rt/lib/builtins/fp_libc_config.h     | 12 +++++++
 compiler-rt/lib/builtins/int_lib.h            | 14 ++++++--
 libc/shared/builtins.h                        | 16 +++++++++
 libc/shared/builtins/addtf3.h                 | 29 ++++++++++++++++
 libc/src/__support/CMakeLists.txt             |  1 +
 libc/src/__support/FPUtil/dyadic_float.h      |  3 ++
 libc/src/__support/builtins/CMakeLists.txt    |  9 +++++
 libc/src/__support/builtins/addtf3.h          | 33 +++++++++++++++++++
 11 files changed, 156 insertions(+), 10 deletions(-)
 rename compiler-rt/lib/builtins/{addtf3.c => addtf3.cpp} (67%)
 create mode 100644 compiler-rt/lib/builtins/fp_libc_config.h
 create mode 100644 libc/shared/builtins.h
 create mode 100644 libc/shared/builtins/addtf3.h
 create mode 100644 libc/src/__support/builtins/CMakeLists.txt
 create mode 100644 libc/src/__support/builtins/addtf3.h

diff --git a/.github/workflows/libc-overlay-tests.yml b/.github/workflows/libc-overlay-tests.yml
index 6bb01d502050e..4a7a8e2ef8887 100644
--- a/.github/workflows/libc-overlay-tests.yml
+++ b/.github/workflows/libc-overlay-tests.yml
@@ -95,7 +95,8 @@ jobs:
         -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
         -DCMAKE_POLICY_DEFAULT_CMP0141=NEW
         -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded
-        -DLLVM_ENABLE_RUNTIMES=libc
+        -DCOMPILER_RT_USE_LIBC_MATH=ON
+        -DLLVM_ENABLE_RUNTIMES="libc;compiler-rt"
         -G Ninja
         -S ${{ github.workspace }}/runtimes
 
@@ -106,9 +107,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 7ce929657eb82..d6ff8df88abe4 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -202,7 +202,7 @@ set(BF16_SOURCES
 )
 
 set(GENERIC_TF_SOURCES
-  addtf3.c
+  addtf3.cpp
   comparetf2.c
   divtc3.c
   divtf3.c
@@ -230,6 +230,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 943430de259d8..53e6640f61e29 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 253bc27bded20..c43f31a5b0afd 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -442,6 +442,7 @@ if(NOT (LIBC_TARGET_OS_IS_DARWIN))
 endif()
 
 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 cc0710fbf7b02..be7bbd866c13f 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -412,6 +412,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

>From 06ff18543183744bcc3ac00f863177724e0df4c7 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sun, 17 May 2026 03:01:09 +0300
Subject: [PATCH 2/2] fix the ci

---
 .github/workflows/libc-fullbuild-tests.yml | 19 +++++++++++++++++++
 .github/workflows/libc-overlay-tests.yml   |  9 +--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/.github/workflows/libc-fullbuild-tests.yml b/.github/workflows/libc-fullbuild-tests.yml
index 4a7cfa119b74c..0dbd913a3c4e2 100644
--- a/.github/workflows/libc-fullbuild-tests.yml
+++ b/.github/workflows/libc-fullbuild-tests.yml
@@ -150,6 +150,10 @@ jobs:
 
         if [[ "${{ matrix.include_scudo }}" == "ON" || "${{ matrix.build_fuzzing_tests }}" == "ON" ]]; then
           export RUNTIMES="$RUNTIMES;compiler-rt"
+          export CMAKE_FLAGS="$CMAKE_FLAGS
+            -DCOMPILER_RT_USE_LIBC_MATH=ON
+            -DCOMPILER_RT_INCLUDE_TESTS=ON
+            -DLLVM_INCLUDE_TESTS=ON"
         fi
 
         if [[ "${{ matrix.include_scudo }}" == "ON" ]]; then
@@ -198,3 +202,18 @@ jobs:
         --build ${{ steps.strings.outputs.build-output-dir }} 
         --parallel
         --target check-libc
+
+    - name: Test compiler-rt builtins
+      # Only run when compiler-rt is part of RUNTIMES (gated on scudo or
+      # fuzzing entries above) and the target supports executable tests.
+      if: ${{
+          (matrix.include_scudo == 'ON' || matrix.build_fuzzing_tests == 'ON') &&
+          !endsWith(matrix.target, '-uefi-llvm') &&
+          !endsWith(matrix.target, '-none-eabi') &&
+          matrix.target != 'riscv32-unknown-elf'
+        }}
+      run: >
+        cmake
+        --build ${{ steps.strings.outputs.build-output-dir }}
+        --parallel
+        --target check-builtins
diff --git a/.github/workflows/libc-overlay-tests.yml b/.github/workflows/libc-overlay-tests.yml
index 17b31c6467779..d3bf657274f43 100644
--- a/.github/workflows/libc-overlay-tests.yml
+++ b/.github/workflows/libc-overlay-tests.yml
@@ -97,9 +97,8 @@ 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;compiler-rt"
+        -DLLVM_ENABLE_RUNTIMES=libc
         -G Ninja
         -S ${{ github.workspace }}/runtimes
 
@@ -117,9 +116,3 @@ jobs:
         --parallel 
         --target check-libc
     
-    - name: Test compiler-rt builtins
-      run: >
-        cmake
-        --build ${{ steps.strings.outputs.build-output-dir }}
-        --parallel
-        --target check-builtins



More information about the libc-commits mailing list