Skip to content

Conversation

petrhosek
Copy link
Member

@petrhosek petrhosek commented Feb 7, 2025

__builtin_elementwise_fma doesn't consider errno and is thus more suitable for libc fma implementation.

__builtin_elementwise_fma doesn't consider errno and is thus more suitable
for libc fma implementation.

Fixes llvm#126025
@petrhosek petrhosek added the libc label Feb 7, 2025
@petrhosek petrhosek requested a review from lntue February 7, 2025 19:01
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

Changes

__builtin_elementwise_fma doesn't consider errno and is thus more suitable for libc fma implementation.

Fixes #126025


Full diff: https://github.com/llvm/llvm-project/pull/126288.diff

2 Files Affected:

  • (modified) libc/src/__support/FPUtil/FMA.h (+2-2)
  • (modified) libc/src/__support/FPUtil/multiply_add.h (+2-2)
diff --git a/libc/src/__support/FPUtil/FMA.h b/libc/src/__support/FPUtil/FMA.h
index 807d685239732e5..7b505d22c06b6ac 100644
--- a/libc/src/__support/FPUtil/FMA.h
+++ b/libc/src/__support/FPUtil/FMA.h
@@ -25,11 +25,11 @@ LIBC_INLINE OutType fma(InType x, InType y, InType z) {
 
 #ifdef LIBC_TARGET_CPU_HAS_FMA
 template <> LIBC_INLINE float fma(float x, float y, float z) {
-  return __builtin_fmaf(x, y, z);
+  return __builtin_elementwise_fma(x, y, z);
 }
 
 template <> LIBC_INLINE double fma(double x, double y, double z) {
-  return __builtin_fma(x, y, z);
+  return __builtin_elementwise_fma(x, y, z);
 }
 #endif // LIBC_TARGET_CPU_HAS_FMA
 
diff --git a/libc/src/__support/FPUtil/multiply_add.h b/libc/src/__support/FPUtil/multiply_add.h
index a86067c401873ba..763799f1f29d72b 100644
--- a/libc/src/__support/FPUtil/multiply_add.h
+++ b/libc/src/__support/FPUtil/multiply_add.h
@@ -47,11 +47,11 @@ namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
 
 LIBC_INLINE float multiply_add(float x, float y, float z) {
-  return __builtin_fmaf(x, y, z);
+  return __builtin_elementwise_fma(x, y, z);
 }
 
 LIBC_INLINE double multiply_add(double x, double y, double z) {
-  return __builtin_fma(x, y, z);
+  return __builtin_elementwise_fma(x, y, z);
 }
 
 } // namespace fputil

@petrhosek
Copy link
Member Author

Using __builtin_elementwise_fma was suggested in #126025 and in my limited testing does seem to be working but I don't know if there are any downsides to using it over __builtin_fma.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are borrowed from OpenCL I believe and aren't available in gcc, so you'd need a has_builtin guard. https://godbolt.org/z/s63hK9Y4T.

@jhuber6
Copy link
Contributor

jhuber6 commented Feb 7, 2025

Using __builtin_elementwise_fma was suggested in #126025 and in my limited testing does seem to be working but I don't know if there are any downsides to using it over __builtin_fma.

They're supposed to support LLVM's vector type AFAIK.

Copy link

github-actions bot commented Feb 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but I'll wait for @arsenm to chime in since he knows this stuff better than I do.

@petrhosek
Copy link
Member Author

I wonder if we should use the inline assembly version in the case where __builtin_elementwise_fma isn't available?

@nickdesaulniers
Copy link
Member

I'm fine with that; at least for 32b ARM (wasn't that the only arch that was problematic?).

@petrhosek petrhosek merged commit 2bdeeaa into llvm:main Feb 13, 2025
14 checks passed
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…26288)

__builtin_elementwise_fma doesn't consider errno and is thus more
suitable for libc fma implementation.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…26288)

__builtin_elementwise_fma doesn't consider errno and is thus more
suitable for libc fma implementation.
@petrhosek petrhosek deleted the libc-elementwise-fma branch February 24, 2025 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants