[compiler-rt][builtins] introduce libc math routines - #197950
Merged
hulxv merged 8 commits intoJul 2, 2026
Merged
Conversation
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
3 times, most recently
from
May 16, 2026 23:13
b0980c2 to
01e9016
Compare
hulxv
marked this pull request as ready for review
May 16, 2026 23:18
|
@llvm/pr-subscribers-libc @llvm/pr-subscribers-github-workflow Author: hulxv (hulxv) ChangesIntroduce using libc math routines in compiler-rt builtins. this pr adds Part of #197824 Full diff: https://github.com/llvm/llvm-project/pull/197950.diff 11 Files Affected:
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
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
from
May 16, 2026 23:39
2c25c38 to
d89166f
Compare
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
from
May 17, 2026 00:01
d89166f to
06ff185
Compare
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
4 times, most recently
from
May 17, 2026 22:10
a6ea507 to
a5f6493
Compare
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
2 times, most recently
from
May 17, 2026 22:33
4ff9013 to
03c61ee
Compare
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
from
May 17, 2026 22:43
03c61ee to
5c618aa
Compare
lntue
reviewed
May 24, 2026
statham-arm
reviewed
May 26, 2026
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
2 times, most recently
from
May 27, 2026 23:14
d4701c4 to
456f013
Compare
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
from
May 27, 2026 23:17
456f013 to
1277b1c
Compare
statham-arm
approved these changes
May 28, 2026
compnerd
reviewed
May 28, 2026
…iltins/init-libc-integration-project
hulxv
force-pushed
the
compiler_rt/builtins/init-libc-integration-project
branch
from
May 31, 2026 22:06
76d5f5a to
31501d8
Compare
hulxv
added a commit
that referenced
this pull request
Jun 26, 2026
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
lntue
approved these changes
Jul 2, 2026
hulxv
added a commit
that referenced
this pull request
Jul 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce using libc math routines in compiler-rt builtins. this pr adds
COMPILER_RT_USE_LIBC_MATHflag and uses math routines in basic arithmetic operations for Quad-Precision valuesRFC: https://discourse.llvm.org/t/rfc-explore-using-llvm-libc-math-routines-for-compiler-rt-floating-point-builtins/89670
Part of #197824