[libc-commits] [libc] [libc] Proof of concept of aliasing long double math functions. (PR #132627)

via libc-commits libc-commits at lists.llvm.org
Tue Mar 25 13:25:08 PDT 2025


================
@@ -37,21 +37,44 @@
 
 #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
 
-// MacOS needs to be excluded because it does not support aliasing.
-#if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
-#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
+// MacOS needs to be excluded because it does not support [[gnu::aliasing]].
+#ifndef __APPLE__
+
+#if defined(LIBC_COPT_PUBLIC_PACKAGING)
+#define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
   LLVM_LIBC_ATTR(name)                                                         \
   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
-      __##name##_impl__ __asm__(#name);                                        \
+      __##name##_impl__ asm(#name);                                            \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
   type __##name##_impl__ arglist
-#else
-#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
-#endif
 
-// This extra layer of macro allows `name` to be a macro to rename a function.
+#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
+  decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name [[gnu::alias(#func)]];   \
+  asm(#name " = " #func);                                                      \
----------------
lntue wrote:

So right now (before this patch), we export 2 public symbols in the library:
- The C++ namespaced function: `LIBC_NAMESPACE::copysign`
- The C standard function: `copysign`
and they are aliased to each other.
And the same for `LIBC_NAMESPACE::copysignl` and `copysignl`.
So in total we have 4 exported symbols, with 2 separate implementations.

With this PR, we still export all those 4 symbols, but making all of them aliased to the same implementation (`copysign`).

And for the testing (aka `.__internal__` targets), previously we have exported only C++ namespaced symbols `LIBC_NAMESPACE::copysign` and `LIBC_NAMESPACE::copysignl` with 2 separate implementations.  So now to maintain the same structure / implementation, with this PR, we also alias `LIBC_NAMESPACE::copysignl` with `LIBC_NAMESPACE::copysign`.

But it's a bit tricky to directly aliasing 2 C++ symbols in a general and portable way.  So instead added a C symbol `__copysign_impl__` and aliased both C++ symbols to it, following the same construct for the `.__internal__` targets.    And it's actually cleaner this way.

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


More information about the libc-commits mailing list