Skip to content

[compiler-rt][builtins] introduce libc math routines - #197950

Merged
hulxv merged 8 commits into
llvm:mainfrom
hulxv:compiler_rt/builtins/init-libc-integration-project
Jul 2, 2026
Merged

[compiler-rt][builtins] introduce libc math routines#197950
hulxv merged 8 commits into
llvm:mainfrom
hulxv:compiler_rt/builtins/init-libc-integration-project

Conversation

@hulxv

@hulxv hulxv commented May 15, 2026

Copy link
Copy Markdown
Member

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

@github-actions

github-actions Bot commented May 15, 2026

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 6586 tests passed
  • 1315 tests skipped

✅ The build succeeded and all tests passed.

@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch 3 times, most recently from b0980c2 to 01e9016 Compare May 16, 2026 23:13
Comment thread .github/workflows/libc-overlay-tests.yml Fixed
@llvmorg-github-actions

llvmorg-github-actions Bot commented May 16, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-libc

@llvm/pr-subscribers-github-workflow

Author: hulxv (hulxv)

Changes

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 --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

@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch from 2c25c38 to d89166f Compare May 16, 2026 23:39
Comment thread .github/workflows/libc-overlay-tests.yml Fixed
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch from d89166f to 06ff185 Compare May 17, 2026 00:01
Comment thread .github/workflows/libc-fullbuild-tests.yml Fixed
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch 4 times, most recently from a6ea507 to a5f6493 Compare May 17, 2026 22:10
Comment thread .github/workflows/libc-fullbuild-tests.yml Fixed
Comment thread .github/workflows/libc-fullbuild-tests.yml Fixed
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch 2 times, most recently from 4ff9013 to 03c61ee Compare May 17, 2026 22:33
Comment thread .github/workflows/libc-fullbuild-tests.yml Fixed
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch from 03c61ee to 5c618aa Compare May 17, 2026 22:43
Comment thread compiler-rt/lib/builtins/fp_libc_config.h
@lntue lntue changed the title [Compiler-rt][builtins] introduce libc math routines [compiler-rt][builtins] introduce libc math routines May 25, 2026
@hulxv
hulxv requested a review from lntue May 25, 2026 23:22
Comment thread .github/workflows/compiler-rt-libc-builtins-tests.yml Outdated
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch 2 times, most recently from d4701c4 to 456f013 Compare May 27, 2026 23:14
@hulxv
hulxv requested a review from statham-arm May 27, 2026 23:14
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch from 456f013 to 1277b1c Compare May 27, 2026 23:17
Comment thread compiler-rt/lib/builtins/addtf3.cpp
@hulxv
hulxv force-pushed the compiler_rt/builtins/init-libc-integration-project branch from 76d5f5a to 31501d8 Compare May 31, 2026 22:06
hulxv added a commit that referenced this pull request Jun 26, 2026
Introduce shared compiler-rt builtins to libc and addtf3 builtin

Split from #197950

Part of #197824
@hulxv
hulxv requested a review from compnerd June 26, 2026 18:38
LouisLu060211 pushed a commit to LouisLu060211/llvm-project that referenced this pull request Jun 30, 2026
Introduce shared compiler-rt builtins to libc and addtf3 builtin

Split from llvm#197950

Part of llvm#197824
maarcosrmz pushed a commit to maarcosrmz/llvm-project that referenced this pull request Jul 1, 2026
Introduce shared compiler-rt builtins to libc and addtf3 builtin

Split from llvm#197950

Part of llvm#197824
@hulxv
hulxv merged commit cb4e7ff into llvm:main Jul 2, 2026
11 checks passed
hulxv added a commit that referenced this pull request Jul 23, 2026
…200196)

Add Pre-commit CI to test compiler-rt builtins with libc to check it
works without issues

Split from #197950

Part of #197824

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants