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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 2 10:00:35 PDT 2026


Author: hulxv
Date: 2026-07-02T20:00:31+03:00
New Revision: cb4e7fff7d0e7fc2bf116302567f21405f63a748

URL: https://github.com/llvm/llvm-project/commit/cb4e7fff7d0e7fc2bf116302567f21405f63a748
DIFF: https://github.com/llvm/llvm-project/commit/cb4e7fff7d0e7fc2bf116302567f21405f63a748.diff

LOG: [compiler-rt][builtins] introduce libc math routines (#197950)

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

Added: 
    compiler-rt/lib/builtins/addtf3.cpp
    compiler-rt/lib/builtins/fp_libc_config.h

Modified: 
    compiler-rt/lib/builtins/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 0d52b582a8f26..e128fea6a3415 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -243,6 +243,30 @@ set(GENERIC_TF_SOURCES
   trunctfsf2.c
 )
 
+option(COMPILER_RT_USE_LIBC_MATH
+      "Use LLVM libc math routines for floating-point builtins" OFF)
+
+# Swap the legacy soft-float <name>.c builtin in <list> for the libc-backed
+# <name>.cpp implementation (which delegates to LIBC_NAMESPACE::shared::*).
+macro(use_libc_builtin list_var name)
+  list(REMOVE_ITEM ${list_var} ${name}.c)
+  list(APPEND ${list_var} ${name}.cpp)
+endmacro()
+
+if(COMPILER_RT_USE_LIBC_MATH)
+  include(FindLibcCommonUtils)
+  if(NOT TARGET llvm-libc-common-utilities)
+    message(FATAL_ERROR "LLVM libc is not found at ${libc_path}.")
+  endif()
+
+  get_target_property(_libc_include_dirs llvm-libc-common-utilities
+                      INTERFACE_INCLUDE_DIRECTORIES)
+  include_directories(SYSTEM ${_libc_include_dirs})
+  add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
+
+  use_libc_builtin(GENERIC_TF_SOURCES addtf3)
+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.cpp b/compiler-rt/lib/builtins/addtf3.cpp
new file mode 100644
index 0000000000000..ae4dbd5772cfb
--- /dev/null
+++ b/compiler-rt/lib/builtins/addtf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/addtf3.cpp - Quad-precision addition (libc-backed) --*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// __addtf3 implemented on top of LLVM-libc's shared::addtf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/addtf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __addtf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::addtf3(a, b);
+}

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..8264de2492d67
--- /dev/null
+++ b/compiler-rt/lib/builtins/fp_libc_config.h
@@ -0,0 +1,26 @@
+//===-- lib/fp_libc_config.h - LLVM-libc compile config --------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Compile-time configuration consumed by LLVM-libc's fputil headers when they
+// are included from compiler-rt builtins under COMPILER_RT_USE_LIBC_MATH.
+// Pins the libc namespace to __llvm_libc_rt to avoid clashing with libc's own
+// symbols, and disables errno / FP-exception bookkeeping inside libc routines
+// called from builtins.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FP_LIBC_CONFIG_H
+#define FP_LIBC_CONFIG_H
+
+#ifndef LIBC_NAMESPACE
+#define LIBC_NAMESPACE __llvm_libc_rt
+#endif
+
+#define LIBC_MATH (LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)
+
+#endif // FP_LIBC_CONFIG_H


        


More information about the llvm-commits mailing list