Skip to content

Conversation

@jmmartinez
Copy link
Contributor

Modifications to reapply the commit:

  • Add noexcept only after C++11 on __glibcxx_assert_fail
  • Remove vararg version of __glibcxx_assert_fail

@jmmartinez jmmartinez requested review from Artem-B and yxsamliu June 19, 2025 12:05
@jmmartinez jmmartinez self-assigned this Jun 19, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Jun 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

Modifications to reapply the commit:

  • Add noexcept only after C++11 on __glibcxx_assert_fail
  • Remove vararg version of __glibcxx_assert_fail

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

2 Files Affected:

  • (modified) clang/lib/Headers/CMakeLists.txt (+1)
  • (added) clang/lib/Headers/cuda_wrappers/bits/c++config.h (+61)
diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt
index c1c9d2e8c7b79..c96d209c1fc0c 100644
--- a/clang/lib/Headers/CMakeLists.txt
+++ b/clang/lib/Headers/CMakeLists.txt
@@ -341,6 +341,7 @@ set(cuda_wrapper_files
 )
 
 set(cuda_wrapper_bits_files
+  cuda_wrappers/bits/c++config.h
   cuda_wrappers/bits/shared_ptr_base.h
   cuda_wrappers/bits/basic_string.h
   cuda_wrappers/bits/basic_string.tcc
diff --git a/clang/lib/Headers/cuda_wrappers/bits/c++config.h b/clang/lib/Headers/cuda_wrappers/bits/c++config.h
new file mode 100644
index 0000000000000..27083253181d2
--- /dev/null
+++ b/clang/lib/Headers/cuda_wrappers/bits/c++config.h
@@ -0,0 +1,61 @@
+// libstdc++ uses the non-constexpr function std::__glibcxx_assert_fail()
+// to trigger compilation errors when the __glibcxx_assert(cond) macro
+// is used in a constexpr context.
+// Compilation fails when using code from the libstdc++ (such as std::array) on
+// device code, since these assertions invoke a non-constexpr host function from
+// device code.
+//
+// To work around this issue, we declare our own device version of the function
+
+#ifndef __CLANG_CUDA_WRAPPERS_BITS_CPP_CONFIG
+#define __CLANG_CUDA_WRAPPERS_BITS_CPP_CONFIG
+
+#include_next <bits/c++config.h>
+
+#ifdef _LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_BEGIN_NAMESPACE_STD
+#else
+namespace std {
+#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+#endif
+
+#pragma push_macro("CUDA_NOEXCEPT")
+#if __cplusplus >= 201103L
+#define CUDA_NOEXCEPT noexcept
+#else
+#define CUDA_NOEXCEPT
+#endif
+
+__attribute__((device, noreturn)) inline void
+__glibcxx_assert_fail(const char *file, int line, const char *function,
+                      const char *condition) CUDA_NOEXCEPT {
+#ifdef _GLIBCXX_VERBOSE_ASSERT
+  if (file && function && condition)
+    __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", file, line,
+                     function, condition);
+  else if (function)
+    __builtin_printf("%s: Undefined behavior detected.\n", function);
+#endif
+  __builtin_abort();
+}
+
+#endif
+__attribute__((device, noreturn, __always_inline__,
+               __visibility__("default"))) inline void
+__glibcxx_assert_fail() CUDA_NOEXCEPT {
+  __builtin_abort();
+}
+
+#pragma pop_macro("CUDA_NOEXCEPT")
+
+#ifdef _LIBCPP_END_NAMESPACE_STD
+_LIBCPP_END_NAMESPACE_STD
+#else
+#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
+_GLIBCXX_END_NAMESPACE_VERSION
+#endif
+} // namespace std
+#endif
+
+#endif

@jmmartinez jmmartinez merged commit 8ec0552 into llvm:main Jun 24, 2025
11 checks passed
@jmmartinez
Copy link
Contributor Author

Thanks !

DrSergei pushed a commit to DrSergei/llvm-project that referenced this pull request Jun 24, 2025
…t_fail() (llvm#144886)

Modifications to reapply the commit:
* Add noexcept only after C++11 on __glibcxx_assert_fail
* Remove vararg version of __glibcxx_assert_fail
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
…t_fail() (llvm#144886)

Modifications to reapply the commit:
* Add noexcept only after C++11 on __glibcxx_assert_fail
* Remove vararg version of __glibcxx_assert_fail
jmmartinez added a commit to jmmartinez/llvm-project that referenced this pull request Jul 10, 2025
The patch [CUDA][HIP] Add a __device__ version of std::__glibcxx_assert_fail() (llvm#144886)
missed a changelog line.
jmmartinez added a commit that referenced this pull request Jul 11, 2025
The patch [CUDA][HIP] Add a __device__ version of
std::__glibcxx_assert_fail() (#144886) missed a changelog line.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Aug 14, 2025
…t_fail() (llvm#144886) (llvm#3189)

Modifications to reapply the commit:
* Add noexcept only after C++11 on __glibcxx_assert_fail
* Remove vararg version of __glibcxx_assert_fail

And doc CP.

Issue
[SWDEV-518041](https://ontrack-internal.amd.com/browse/SWDEV-518041)
& doc task
[SWDEV-538485](https://ontrack-internal.amd.com/browse/SWDEV-538485)

---------

Co-authored-by: Juan Manuel Martinez Caamaño <[email protected]>
rahulc-gh pushed a commit to ROCm/llvm-project that referenced this pull request Sep 11, 2025
…lvm#144886)

Modifications to reapply the commit:

Add noexcept only after C++11 on __glibcxx_assert_fail
Remove vararg version of __glibcxx_assert_fail
And doc CP.

Issue SWDEV-518041
& doc task SWDEV-538485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants