Skip to content

Conversation

yuxuanchen1997
Copy link
Member

An older PR #102036 suggested that LLVM libc declares lgamma_r as noexcept and is incompatible with this redeclaration. However, I recently discovered that glibc also declares the math functions to be noexcept under C++ mode.

This line usually don't cause issues because both the glibc and this file are included as "system headers". According to this godbolt, both GCC and clang ignore the different exception specification between multiple declarations if they are in system headers.

However, this seems not the case for NVCC/EDG, so a fix for this redeclaration is still desirable. This patch proposes that we should declare the function as noexcept under known libc integrations to keep the declared function consistent.

@yuxuanchen1997 yuxuanchen1997 requested a review from a team as a code owner September 2, 2025 22:19
Copy link

github-actions bot commented Sep 2, 2025

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

@yuxuanchen1997 yuxuanchen1997 force-pushed the users/yuxuanchen1997/libcxx-noexcept-on-lgamma-r branch from 6844b07 to ae1da16 Compare September 2, 2025 22:46
@frederick-vs-ja frederick-vs-ja added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 3, 2025
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

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

I think #153631 (comment) is rather what we should go for.

@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-libcxx

Author: Yuxuan Chen (yuxuanchen1997)

Changes

An older PR #102036 suggested that LLVM libc declares lgamma_r as noexcept and is incompatible with this redeclaration. However, I recently discovered that glibc also declares the math functions to be noexcept under C++ mode.

This line usually don't cause issues because both the glibc and this file are included as "system headers". According to this godbolt, both GCC and clang ignore the different exception specification between multiple declarations if they are in system headers.

However, this seems not the case for NVCC/EDG, so a fix for this redeclaration is still desirable. This patch proposes that we should declare the function as noexcept under known libc integrations to keep the declared function consistent.


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

1 Files Affected:

  • (modified) libcxx/include/__random/binomial_distribution.h (+10-4)
diff --git a/libcxx/include/__random/binomial_distribution.h b/libcxx/include/__random/binomial_distribution.h
index b4b4340827761..80a910b611adf 100644
--- a/libcxx/include/__random/binomial_distribution.h
+++ b/libcxx/include/__random/binomial_distribution.h
@@ -97,13 +97,19 @@ class binomial_distribution {
   }
 };
 
-// The LLVM C library provides this with conflicting `noexcept` attributes.
-#if !defined(_LIBCPP_MSVCRT_LIKE) && !defined(__LLVM_LIBC__)
-extern "C" double lgamma_r(double, int*);
+// Some libc declares the math functions to be `noexcept`.
+#if _LIBCPP_GLIBC_PREREQ(2, 8) || defined(__LLVM_LIBC__)
+#  define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
+#else
+#  define _LIBCPP_LGAMMA_R_NOEXCEPT
+#endif
+
+#if !defined(_LIBCPP_MSVCRT_LIKE)
+    extern "C" double lgamma_r(double, int*) _LIBCPP_LGAMMA_R_NOEXCEPT;
 #endif
 
 inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
-#if defined(_LIBCPP_MSVCRT_LIKE) || defined(__LLVM_LIBC__)
+#if defined(_LIBCPP_MSVCRT_LIKE)
   return lgamma(__d);
 #else
   int __sign;

@yuxuanchen1997
Copy link
Member Author

I think #153631 (comment) is rather what we should go for.

Hi. Would appreciate progress on that work. If no one is working on it, I can also contribute some cycles. In the meantime, this patch can unblock some libc++ CUDA usages.

@yuxuanchen1997 yuxuanchen1997 force-pushed the users/yuxuanchen1997/libcxx-noexcept-on-lgamma-r branch 2 times, most recently from c8a7ec2 to b8be27e Compare September 3, 2025 22:24
@yuxuanchen1997 yuxuanchen1997 force-pushed the users/yuxuanchen1997/libcxx-noexcept-on-lgamma-r branch from b8be27e to a549e73 Compare September 5, 2025 19:26
@yuxuanchen1997 yuxuanchen1997 reopened this Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants