|
| 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)) |
| 25 | +inline void |
| 26 | +__glibcxx_assert_fail( |
| 27 | + const char* file, int line, |
| 28 | + const char* function, const char* condition) noexcept { |
| 29 | + if (file && function && condition) |
| 30 | + __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", file, line, function, condition); |
| 31 | + else if (function) |
| 32 | + __builtin_printf("%s: Undefined behavior detected.\n", function); |
| 33 | + __builtin_abort(); |
| 34 | +} |
| 35 | +#endif |
| 36 | + |
| 37 | +#endif |
| 38 | +__attribute__((device, noreturn, __always_inline__, __visibility__("default"))) |
| 39 | +inline void |
| 40 | +__glibcxx_assert_fail(...) noexcept { |
| 41 | + __builtin_abort(); |
| 42 | +} |
| 43 | +#ifdef _LIBCPP_END_NAMESPACE_STD |
| 44 | +_LIBCPP_END_NAMESPACE_STD |
| 45 | +#else |
| 46 | +#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 47 | +_GLIBCXX_END_NAMESPACE_VERSION |
| 48 | +#endif |
| 49 | +} // namespace std |
| 50 | +#endif |
| 51 | + |
| 52 | +#endif |
0 commit comments