1 /*===- __clang_openmp_device_functions.h - OpenMP device function declares -===
2  *
3  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4  * See https://llvm.org/LICENSE.txt for license information.
5  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6  *
7  *===-----------------------------------------------------------------------===
8  */
9 
10 #ifndef __CLANG_OPENMP_DEVICE_FUNCTIONS_H__
11 #define __CLANG_OPENMP_DEVICE_FUNCTIONS_H__
12 
13 #ifndef _OPENMP
14 #error "This file is for OpenMP compilation only."
15 #endif
16 
17 #pragma omp begin declare variant match(                                       \
18     device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define __CUDA__
25 #define __OPENMP_NVPTX__
26 
27 /// Include declarations for libdevice functions.
28 #include <__clang_cuda_libdevice_declares.h>
29 
30 /// Provide definitions for these functions.
31 #include <__clang_cuda_device_functions.h>
32 
33 #undef __OPENMP_NVPTX__
34 #undef __CUDA__
35 
36 #ifdef __cplusplus
37 } // extern "C"
38 #endif
39 
40 #pragma omp end declare variant
41 
42 // Ensure we make `_ZdlPv`, aka. `operator delete(void*)` available without the
43 // need to `include <new>` in C++ mode.
44 #ifdef __cplusplus
45 
46 // We require malloc/free.
47 #include <cstdlib>
48 
49 #pragma push_macro("OPENMP_NOEXCEPT")
50 #if __cplusplus >= 201103L
51 #define OPENMP_NOEXCEPT noexcept
52 #else
53 #define OPENMP_NOEXCEPT
54 #endif
55 
56 // Device overrides for non-placement new and delete.
new(__SIZE_TYPE__ size)57 inline void *operator new(__SIZE_TYPE__ size) {
58   if (size == 0)
59     size = 1;
60   return ::malloc(size);
61 }
62 
63 inline void *operator new[](__SIZE_TYPE__ size) { return ::operator new(size); }
64 
delete(void * ptr)65 inline void operator delete(void *ptr)OPENMP_NOEXCEPT { ::free(ptr); }
66 
67 inline void operator delete[](void *ptr) OPENMP_NOEXCEPT {
68   ::operator delete(ptr);
69 }
70 
71 // Sized delete, C++14 only.
72 #if __cplusplus >= 201402L
delete(void * ptr,__SIZE_TYPE__ size)73 inline void operator delete(void *ptr, __SIZE_TYPE__ size)OPENMP_NOEXCEPT {
74   ::operator delete(ptr);
75 }
76 inline void operator delete[](void *ptr, __SIZE_TYPE__ size) OPENMP_NOEXCEPT {
77   ::operator delete(ptr);
78 }
79 #endif
80 
81 #pragma pop_macro("OPENMP_NOEXCEPT")
82 #endif
83 
84 #endif
85