Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Use -fno-math-errno to for __builtin_fma* to generate fma instructions. #124200

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jan 23, 2025

Fixes #123387

@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2025

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

Fixes #123387


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

2 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+7)
  • (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+5)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 1795639011936c..12420db331961b 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -28,6 +28,13 @@ function(_get_compile_options_from_flags output_var)
       elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
         list(APPEND compile_options "-D__LIBC_RISCV_USE_FMA")
       endif()
+      # For clang, we will build the math functions with `-fno-math-errno` so that
+      # __builtin_fma* will generate the fused-mutliply-add instructions.  We
+      # don't put the control flag to the public config yet, and see if it makes
+      # sense to just enable this flag by default.
+      if(LIBC_ADD_FNO_MATH_ERRNO)
+        list(APPEND compile_options "-fno-math-errno")
+      endif()
     endif()
     if(ADD_ROUND_OPT_FLAG)
       if(LIBC_TARGET_ARCHITECTURE_IS_X86_64)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 96fa6c3a707e47..10bb9c9487d636 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -1,6 +1,11 @@
 function(_get_common_test_compile_options output_var c_test flags)
   _get_compile_options_from_flags(compile_flags ${flags})
 
+  # Remove -fno-math-errno if it was added.
+  if(LIBC_ADD_FNO_MATH_ERRNO)
+    list(REMOVE_ITEM compile_options "-fno-math-errno")
+  endif()
+
   set(compile_options
       ${LIBC_COMPILE_OPTIONS_DEFAULT}
       ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT}

@jhuber6
Copy link
Contributor

jhuber6 commented Jan 23, 2025

@arsenm I thought that builitins implied no errno?

@arsenm
Copy link
Contributor

arsenm commented Jan 23, 2025

@arsenm I thought that builitins implied no errno?

I don't believe it does

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.

Hm, I thought that the whole point of the llvm instrinsics was they they didn't have errno behavior. I'm guessing we could enable this by default, but we can see later.

@arsenm
Copy link
Contributor

arsenm commented Jan 24, 2025

Hm, I thought that the whole point of the llvm instrinsics was they they didn't have errno behavior. I'm guessing we could enable this by default, but we can see later.

The intrinsics, sure. The builtins are defined as "do what GCC does". Unfortunately that doesn't mean raw access to llvm intrinsics

@lntue lntue merged commit b11529b into llvm:main Jan 24, 2025
13 checks passed
@lntue lntue deleted the errno branch January 24, 2025 01:38
# __builtin_fma* will generate the fused-mutliply-add instructions. We
# don't put the control flag to the public config yet, and see if it makes
# sense to just enable this flag by default.
if(LIBC_ADD_FNO_MATH_ERRNO)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be LIBC_MATH_NO_ERRNO? Where is LIBC_ADD_FNO_MATH_ERRNO being set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was planning to use the flag LIBC_MATH_NO_ERRNO differently, in the sense that our math functions entry points won't set or change errno, regardless of what math_errhandling is set to. And another quite likely possibility that we can just add -fno-math-errno flag by default. So right now I don't add it to our config generator config.json yet. Maybe I should add it to config.json?

petrhosek added a commit to petrhosek/llvm-project that referenced this pull request Feb 5, 2025
This partially reverts llvm#124200. Rather than using a CMake option to
control whether to enable `-fno-math-errno`, use LIBC_MATH_NO_ERRNO
configuration option. While there might be other cases when we want
to set `-fno-math-errno`, having LIBC_MATH_NO_ERRNO imply it should
be always safe and represents a reasonable starting point.
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.

fmaf is an empty unreachable function with optimizations on cortex-m33
5 participants