Skip to content

[CUDA][HIP] Add a __device__ version of std::__glibcxx_assert_fail() #136133

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,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
Expand Down
51 changes: 51 additions & 0 deletions clang/lib/Headers/cuda_wrappers/bits/c++config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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

#ifdef _GLIBCXX_VERBOSE_ASSERT
__attribute__((device, noreturn)) inline void
__glibcxx_assert_fail(const char *file, int line, const char *function,
const char *condition) noexcept {
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);
__builtin_abort();
}
#endif

#endif
__attribute__((device, noreturn, __always_inline__,
__visibility__("default"))) inline void
__glibcxx_assert_fail(...) noexcept {
__builtin_abort();
}
#ifdef _LIBCPP_END_NAMESPACE_STD
_LIBCPP_END_NAMESPACE_STD
#else
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_END_NAMESPACE_VERSION
#endif
} // namespace std
#endif

#endif