Skip to content

Commit 59012b6

Browse files
Artem-Btstellar
authored andcommitted
[CUDA] Another attempt to fix early inclusion of <new> from libstdc++
Previous patch (9a46505) did not fix the problem. https://bugs.llvm.org/show_bug.cgi?id=48228 If the <new> is included too early, before CUDA-specific defines are available, just include-next the standard <new> and undo the include guard. CUDA-specific variants of operator new/delete will be declared if/when <new> is used from the CUDA source itself, when all CUDA-related macros are available. Differential Revision: https://reviews.llvm.org/D91807 (cherry picked from commit 4326792)
1 parent aa29049 commit 59012b6

File tree

1 file changed

+22
-24
lines changed
  • clang/lib/Headers/cuda_wrappers

1 file changed

+22
-24
lines changed

clang/lib/Headers/cuda_wrappers/new

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,83 +26,81 @@
2626

2727
#include_next <new>
2828

29+
#if !defined(__device__)
30+
// The header has been included too early from the standard C++ library
31+
// and CUDA-specific macros are not available yet.
32+
// Undo the include guard and try again later.
33+
#undef __CLANG_CUDA_WRAPPERS_NEW
34+
#else
35+
2936
#pragma push_macro("CUDA_NOEXCEPT")
3037
#if __cplusplus >= 201103L
3138
#define CUDA_NOEXCEPT noexcept
3239
#else
3340
#define CUDA_NOEXCEPT
3441
#endif
3542

36-
#pragma push_macro("__DEVICE__")
37-
#if defined __device__
38-
#define __DEVICE__ __device__
39-
#else
40-
// <new> has been included too early from the standard libc++ headers and the
41-
// standard CUDA macros are not available yet. We have to define our own.
42-
#define __DEVICE__ __attribute__((device))
43-
#endif
44-
4543
// Device overrides for non-placement new and delete.
46-
__DEVICE__ inline void *operator new(__SIZE_TYPE__ size) {
44+
__device__ inline void *operator new(__SIZE_TYPE__ size) {
4745
if (size == 0) {
4846
size = 1;
4947
}
5048
return ::malloc(size);
5149
}
52-
__DEVICE__ inline void *operator new(__SIZE_TYPE__ size,
50+
__device__ inline void *operator new(__SIZE_TYPE__ size,
5351
const std::nothrow_t &) CUDA_NOEXCEPT {
5452
return ::operator new(size);
5553
}
5654

57-
__DEVICE__ inline void *operator new[](__SIZE_TYPE__ size) {
55+
__device__ inline void *operator new[](__SIZE_TYPE__ size) {
5856
return ::operator new(size);
5957
}
60-
__DEVICE__ inline void *operator new[](__SIZE_TYPE__ size,
58+
__device__ inline void *operator new[](__SIZE_TYPE__ size,
6159
const std::nothrow_t &) {
6260
return ::operator new(size);
6361
}
6462

65-
__DEVICE__ inline void operator delete(void* ptr) CUDA_NOEXCEPT {
63+
__device__ inline void operator delete(void* ptr) CUDA_NOEXCEPT {
6664
if (ptr) {
6765
::free(ptr);
6866
}
6967
}
70-
__DEVICE__ inline void operator delete(void *ptr,
68+
__device__ inline void operator delete(void *ptr,
7169
const std::nothrow_t &) CUDA_NOEXCEPT {
7270
::operator delete(ptr);
7371
}
7472

75-
__DEVICE__ inline void operator delete[](void* ptr) CUDA_NOEXCEPT {
73+
__device__ inline void operator delete[](void* ptr) CUDA_NOEXCEPT {
7674
::operator delete(ptr);
7775
}
78-
__DEVICE__ inline void operator delete[](void *ptr,
76+
__device__ inline void operator delete[](void *ptr,
7977
const std::nothrow_t &) CUDA_NOEXCEPT {
8078
::operator delete(ptr);
8179
}
8280

8381
// Sized delete, C++14 only.
8482
#if __cplusplus >= 201402L
85-
__DEVICE__ inline void operator delete(void *ptr,
83+
__device__ inline void operator delete(void *ptr,
8684
__SIZE_TYPE__ size) CUDA_NOEXCEPT {
8785
::operator delete(ptr);
8886
}
89-
__DEVICE__ inline void operator delete[](void *ptr,
87+
__device__ inline void operator delete[](void *ptr,
9088
__SIZE_TYPE__ size) CUDA_NOEXCEPT {
9189
::operator delete(ptr);
9290
}
9391
#endif
9492

9593
// Device overrides for placement new and delete.
96-
__DEVICE__ inline void *operator new(__SIZE_TYPE__, void *__ptr) CUDA_NOEXCEPT {
94+
__device__ inline void *operator new(__SIZE_TYPE__, void *__ptr) CUDA_NOEXCEPT {
9795
return __ptr;
9896
}
99-
__DEVICE__ inline void *operator new[](__SIZE_TYPE__, void *__ptr) CUDA_NOEXCEPT {
97+
__device__ inline void *operator new[](__SIZE_TYPE__, void *__ptr) CUDA_NOEXCEPT {
10098
return __ptr;
10199
}
102-
__DEVICE__ inline void operator delete(void *, void *) CUDA_NOEXCEPT {}
103-
__DEVICE__ inline void operator delete[](void *, void *) CUDA_NOEXCEPT {}
100+
__device__ inline void operator delete(void *, void *) CUDA_NOEXCEPT {}
101+
__device__ inline void operator delete[](void *, void *) CUDA_NOEXCEPT {}
104102

105-
#pragma pop_macro("__DEVICE__")
106103
#pragma pop_macro("CUDA_NOEXCEPT")
107104

105+
#endif // __device__
108106
#endif // include guard

0 commit comments

Comments
 (0)