Skip to content

Commit a602d4f

Browse files
committed
[CUDA][HIP] Add a __device__ version of std::__glibcxx_assert_fail()
libstdc++ 15 uses the non-constexpr function std::__glibcxx_assert_fail() to trigger compilation errors when the __glibcxx_assert(cond) macro is used in a constantly evaluated 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. This patch proposes a cuda wrapper header "bits/c++config.h" which adds a __device__ version of std::__glibcxx_assert_fail().
1 parent db2315a commit a602d4f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ set(cuda_wrapper_files
333333
)
334334

335335
set(cuda_wrapper_bits_files
336+
cuda_wrappers/bits/c++config.h
336337
cuda_wrappers/bits/shared_ptr_base.h
337338
cuda_wrappers/bits/basic_string.h
338339
cuda_wrappers/bits/basic_string.tcc
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// libstdc++ uses the non-constexpr function std::__glibcxx_assert_fail()
2+
// to trigger compilation errors when the __glibcxx_assert(cond) macro
3+
// is used in a constexpr context.
4+
// Compilation fails when using code from the libstdc++ (such as std::array) on
5+
// device code, since these assertions invoke a non-constexpr host function from
6+
// device code.
7+
//
8+
// To work around this issue, we declare our own device version of the function
9+
10+
#ifndef __CLANG_CUDA_WRAPPERS_BITS_CPP_CONFIG
11+
#define __CLANG_CUDA_WRAPPERS_BITS_CPP_CONFIG
12+
13+
#include_next <bits/c++config.h>
14+
15+
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD
16+
_LIBCPP_BEGIN_NAMESPACE_STD
17+
#else
18+
namespace std {
19+
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
20+
_GLIBCXX_BEGIN_NAMESPACE_VERSION
21+
#endif
22+
23+
#ifdef _GLIBCXX_VERBOSE_ASSERT
24+
__attribute__((device, noreturn)) inline void
25+
__glibcxx_assert_fail(const char *file, int line, const char *function,
26+
const char *condition) noexcept {
27+
if (file && function && condition)
28+
__builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", file, line,
29+
function, condition);
30+
else if (function)
31+
__builtin_printf("%s: Undefined behavior detected.\n", function);
32+
__builtin_abort();
33+
}
34+
#endif
35+
36+
#endif
37+
__attribute__((device, noreturn, __always_inline__,
38+
__visibility__("default"))) inline void
39+
__glibcxx_assert_fail(...) noexcept {
40+
__builtin_abort();
41+
}
42+
#ifdef _LIBCPP_END_NAMESPACE_STD
43+
_LIBCPP_END_NAMESPACE_STD
44+
#else
45+
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
46+
_GLIBCXX_END_NAMESPACE_VERSION
47+
#endif
48+
} // namespace std
49+
#endif
50+
51+
#endif

0 commit comments

Comments
 (0)