Skip to content

Commit 94b2eae

Browse files
authored
Reapply "[CUDA][HIP] Add a __device__ version of std::__glibcxx_assert_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]>
1 parent eeb5d84 commit 94b2eae

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ CUDA/HIP Language Changes
12741274
- Fixed a bug about overriding a constexpr pure-virtual member function with a non-constexpr virtual member function which causes compilation failure when including standard C++ header `format`.
12751275
- Added initial support for version 3 of the compressed offload bundle format, which uses 64-bit fields for Total File Size and Uncompressed Binary Size. This enables support for files larger than 4GB. The support is currently experimental and can be enabled by setting the environment variable `COMPRESSED_BUNDLE_FORMAT_VERSION=3`.
12761276

1277+
* Provide a __device__ version of std::__glibcxx_assert_fail() in a header wrapper.
1278+
12771279
CUDA Support
12781280
^^^^^^^^^^^^
12791281
- Clang now supports CUDA SDK up to 12.6

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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
#pragma push_macro("CUDA_NOEXCEPT")
24+
#if __cplusplus >= 201103L
25+
#define CUDA_NOEXCEPT noexcept
26+
#else
27+
#define CUDA_NOEXCEPT
28+
#endif
29+
30+
__attribute__((device, noreturn)) inline void
31+
__glibcxx_assert_fail(const char *file, int line, const char *function,
32+
const char *condition) CUDA_NOEXCEPT {
33+
#ifdef _GLIBCXX_VERBOSE_ASSERT
34+
if (file && function && condition)
35+
__builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", file, line,
36+
function, condition);
37+
else if (function)
38+
__builtin_printf("%s: Undefined behavior detected.\n", function);
39+
#endif
40+
__builtin_abort();
41+
}
42+
43+
#endif
44+
__attribute__((device, noreturn, __always_inline__,
45+
__visibility__("default"))) inline void
46+
__glibcxx_assert_fail() CUDA_NOEXCEPT {
47+
__builtin_abort();
48+
}
49+
50+
#pragma pop_macro("CUDA_NOEXCEPT")
51+
52+
#ifdef _LIBCPP_END_NAMESPACE_STD
53+
_LIBCPP_END_NAMESPACE_STD
54+
#else
55+
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
56+
_GLIBCXX_END_NAMESPACE_VERSION
57+
#endif
58+
} // namespace std
59+
#endif
60+
61+
#endif

0 commit comments

Comments
 (0)