6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
- #ifndef LLVM_LIBC_SRC___SUPPORT_RPC_RPC_UTIL_H
10
- #define LLVM_LIBC_SRC___SUPPORT_RPC_RPC_UTIL_H
11
-
12
- #include " src/__support/macros/attributes.h"
13
- #include " src/__support/macros/config.h"
9
+ #ifndef LLVM_LIBC_SHARED_RPC_UTIL_H
10
+ #define LLVM_LIBC_SHARED_RPC_UTIL_H
14
11
15
12
#include < stddef.h>
16
13
#include < stdint.h>
20
17
#define RPC_TARGET_IS_GPU
21
18
#endif
22
19
23
- namespace LIBC_NAMESPACE_DECL {
20
+ #ifndef RPC_INLINE
21
+ #define RPC_INLINE inline
22
+ #endif
23
+
24
24
namespace rpc {
25
25
26
26
template <typename T> struct type_identity {
@@ -40,26 +40,26 @@ template <class T> struct is_const<const T> : type_constant<bool, true> {};
40
40
41
41
// / Freestanding implementation of std::move.
42
42
template <class T >
43
- LIBC_INLINE constexpr typename remove_reference<T>::type &&move(T &&t) {
43
+ RPC_INLINE constexpr typename remove_reference<T>::type &&move(T &&t) {
44
44
return static_cast <typename remove_reference<T>::type &&>(t);
45
45
}
46
46
47
47
// / Freestanding implementation of std::forward.
48
48
template <typename T>
49
- LIBC_INLINE constexpr T &&forward(typename remove_reference<T>::type &value) {
49
+ RPC_INLINE constexpr T &&forward(typename remove_reference<T>::type &value) {
50
50
return static_cast <T &&>(value);
51
51
}
52
52
template <typename T>
53
- LIBC_INLINE constexpr T &&forward(typename remove_reference<T>::type &&value) {
53
+ RPC_INLINE constexpr T &&forward(typename remove_reference<T>::type &&value) {
54
54
return static_cast <T &&>(value);
55
55
}
56
56
57
57
struct in_place_t {
58
- LIBC_INLINE explicit in_place_t () = default;
58
+ RPC_INLINE explicit in_place_t () = default;
59
59
};
60
60
61
61
struct nullopt_t {
62
- LIBC_INLINE constexpr explicit nullopt_t () = default;
62
+ RPC_INLINE constexpr explicit nullopt_t () = default;
63
63
};
64
64
65
65
constexpr inline in_place_t in_place{};
@@ -75,15 +75,15 @@ template <typename T> class optional {
75
75
76
76
bool in_use = false ;
77
77
78
- LIBC_INLINE ~OptionalStorage () { reset (); }
78
+ RPC_INLINE ~OptionalStorage () { reset (); }
79
79
80
- LIBC_INLINE constexpr OptionalStorage () : empty() {}
80
+ RPC_INLINE constexpr OptionalStorage () : empty() {}
81
81
82
82
template <typename ... Args>
83
- LIBC_INLINE constexpr explicit OptionalStorage (in_place_t , Args &&...args)
83
+ RPC_INLINE constexpr explicit OptionalStorage (in_place_t , Args &&...args)
84
84
: stored_value(forward<Args>(args)...) {}
85
85
86
- LIBC_INLINE constexpr void reset () {
86
+ RPC_INLINE constexpr void reset () {
87
87
if (in_use)
88
88
stored_value.~U ();
89
89
in_use = false ;
@@ -93,60 +93,54 @@ template <typename T> class optional {
93
93
OptionalStorage<T> storage;
94
94
95
95
public:
96
- LIBC_INLINE constexpr optional () = default;
97
- LIBC_INLINE constexpr optional (nullopt_t ) {}
96
+ RPC_INLINE constexpr optional () = default;
97
+ RPC_INLINE constexpr optional (nullopt_t ) {}
98
98
99
- LIBC_INLINE constexpr optional (const T &t) : storage(in_place, t) {
99
+ RPC_INLINE constexpr optional (const T &t) : storage(in_place, t) {
100
100
storage.in_use = true ;
101
101
}
102
- LIBC_INLINE constexpr optional (const optional &) = default;
102
+ RPC_INLINE constexpr optional (const optional &) = default;
103
103
104
- LIBC_INLINE constexpr optional (T &&t) : storage(in_place, move(t)) {
104
+ RPC_INLINE constexpr optional (T &&t) : storage(in_place, move(t)) {
105
105
storage.in_use = true ;
106
106
}
107
- LIBC_INLINE constexpr optional (optional &&O) = default;
107
+ RPC_INLINE constexpr optional (optional &&O) = default;
108
108
109
- LIBC_INLINE constexpr optional &operator =(T &&t) {
109
+ RPC_INLINE constexpr optional &operator =(T &&t) {
110
110
storage = move (t);
111
111
return *this ;
112
112
}
113
- LIBC_INLINE constexpr optional &operator =(optional &&) = default ;
113
+ RPC_INLINE constexpr optional &operator =(optional &&) = default ;
114
114
115
- LIBC_INLINE constexpr optional &operator =(const T &t) {
115
+ RPC_INLINE constexpr optional &operator =(const T &t) {
116
116
storage = t;
117
117
return *this ;
118
118
}
119
- LIBC_INLINE constexpr optional &operator =(const optional &) = default ;
119
+ RPC_INLINE constexpr optional &operator =(const optional &) = default ;
120
120
121
- LIBC_INLINE constexpr void reset () { storage.reset (); }
121
+ RPC_INLINE constexpr void reset () { storage.reset (); }
122
122
123
- LIBC_INLINE constexpr const T &value () const & {
124
- return storage.stored_value ;
125
- }
123
+ RPC_INLINE constexpr const T &value () const & { return storage.stored_value ; }
126
124
127
- LIBC_INLINE constexpr T &value () & { return storage.stored_value ; }
125
+ RPC_INLINE constexpr T &value () & { return storage.stored_value ; }
128
126
129
- LIBC_INLINE constexpr explicit operator bool () const {
130
- return storage.in_use ;
131
- }
132
- LIBC_INLINE constexpr bool has_value () const { return storage.in_use ; }
133
- LIBC_INLINE constexpr const T *operator ->() const {
127
+ RPC_INLINE constexpr explicit operator bool () const { return storage.in_use ; }
128
+ RPC_INLINE constexpr bool has_value () const { return storage.in_use ; }
129
+ RPC_INLINE constexpr const T *operator ->() const {
134
130
return &storage.stored_value ;
135
131
}
136
- LIBC_INLINE constexpr T *operator ->() { return &storage.stored_value ; }
137
- LIBC_INLINE constexpr const T &operator *() const & {
132
+ RPC_INLINE constexpr T *operator ->() { return &storage.stored_value ; }
133
+ RPC_INLINE constexpr const T &operator *() const & {
138
134
return storage.stored_value ;
139
135
}
140
- LIBC_INLINE constexpr T &operator *() & { return storage.stored_value ; }
136
+ RPC_INLINE constexpr T &operator *() & { return storage.stored_value ; }
141
137
142
- LIBC_INLINE constexpr T &&value() && { return move (storage.stored_value ); }
143
- LIBC_INLINE constexpr T &&operator *() && {
144
- return move (storage.stored_value );
145
- }
138
+ RPC_INLINE constexpr T &&value() && { return move (storage.stored_value ); }
139
+ RPC_INLINE constexpr T &&operator *() && { return move (storage.stored_value ); }
146
140
};
147
141
148
142
// / Suspend the thread briefly to assist the thread scheduler during busy loops.
149
- LIBC_INLINE void sleep_briefly () {
143
+ RPC_INLINE void sleep_briefly () {
150
144
#if defined(LIBC_TARGET_ARCH_IS_NVPTX)
151
145
if (__nvvm_reflect (" __CUDA_ARCH" ) >= 700 )
152
146
asm (" nanosleep.u32 64;" ::: " memory" );
@@ -164,7 +158,7 @@ LIBC_INLINE void sleep_briefly() {
164
158
}
165
159
166
160
// / Conditional to indicate if this process is running on the GPU.
167
- LIBC_INLINE constexpr bool is_process_gpu () {
161
+ RPC_INLINE constexpr bool is_process_gpu () {
168
162
#ifdef RPC_TARGET_IS_GPU
169
163
return true ;
170
164
#else
@@ -173,14 +167,14 @@ LIBC_INLINE constexpr bool is_process_gpu() {
173
167
}
174
168
175
169
// / Wait for all lanes in the group to complete.
176
- LIBC_INLINE void sync_lane (uint64_t lane_mask) {
170
+ RPC_INLINE void sync_lane (uint64_t lane_mask) {
177
171
#ifdef RPC_TARGET_IS_GPU
178
172
return __gpu_sync_lane (lane_mask);
179
173
#endif
180
174
}
181
175
182
176
// / Copies the value from the first active thread to the rest.
183
- LIBC_INLINE uint32_t broadcast_value (uint64_t lane_mask, uint32_t x) {
177
+ RPC_INLINE uint32_t broadcast_value (uint64_t lane_mask, uint32_t x) {
184
178
#ifdef RPC_TARGET_IS_GPU
185
179
return __gpu_read_first_lane_u32 (lane_mask, x);
186
180
#else
@@ -189,7 +183,7 @@ LIBC_INLINE uint32_t broadcast_value(uint64_t lane_mask, uint32_t x) {
189
183
}
190
184
191
185
// / Returns the number lanes that participate in the RPC interface.
192
- LIBC_INLINE uint32_t get_num_lanes () {
186
+ RPC_INLINE uint32_t get_num_lanes () {
193
187
#ifdef RPC_TARGET_IS_GPU
194
188
return __gpu_num_lanes ();
195
189
#else
@@ -198,7 +192,7 @@ LIBC_INLINE uint32_t get_num_lanes() {
198
192
}
199
193
200
194
// / Returns the id of the thread inside of an AMD wavefront executing together.
201
- LIBC_INLINE uint64_t get_lane_mask () {
195
+ RPC_INLINE uint64_t get_lane_mask () {
202
196
#ifdef RPC_TARGET_IS_GPU
203
197
return __gpu_lane_mask ();
204
198
#else
@@ -207,7 +201,7 @@ LIBC_INLINE uint64_t get_lane_mask() {
207
201
}
208
202
209
203
// / Returns the id of the thread inside of an AMD wavefront executing together.
210
- LIBC_INLINE uint32_t get_lane_id () {
204
+ RPC_INLINE uint32_t get_lane_id () {
211
205
#ifdef RPC_TARGET_IS_GPU
212
206
return __gpu_lane_id ();
213
207
#else
@@ -216,7 +210,7 @@ LIBC_INLINE uint32_t get_lane_id() {
216
210
}
217
211
218
212
// / Conditional that is only true for a single thread in a lane.
219
- LIBC_INLINE bool is_first_lane (uint64_t lane_mask) {
213
+ RPC_INLINE bool is_first_lane (uint64_t lane_mask) {
220
214
#ifdef RPC_TARGET_IS_GPU
221
215
return __gpu_is_first_in_lane (lane_mask);
222
216
#else
@@ -225,7 +219,7 @@ LIBC_INLINE bool is_first_lane(uint64_t lane_mask) {
225
219
}
226
220
227
221
// / Returns a bitmask of threads in the current lane for which \p x is true.
228
- LIBC_INLINE uint64_t ballot (uint64_t lane_mask, bool x) {
222
+ RPC_INLINE uint64_t ballot (uint64_t lane_mask, bool x) {
229
223
#ifdef RPC_TARGET_IS_GPU
230
224
return __gpu_ballot (lane_mask, x);
231
225
#else
@@ -235,22 +229,22 @@ LIBC_INLINE uint64_t ballot(uint64_t lane_mask, bool x) {
235
229
236
230
// / Return \p val aligned "upwards" according to \p align.
237
231
template <typename V, typename A>
238
- LIBC_INLINE constexpr V align_up (V val, A align) {
232
+ RPC_INLINE constexpr V align_up (V val, A align) {
239
233
return ((val + V (align) - 1 ) / V (align)) * V (align);
240
234
}
241
235
242
236
// / Utility to provide a unified interface between the CPU and GPU's memory
243
237
// / model. On the GPU stack variables are always private to a lane so we can
244
238
// / simply use the variable passed in. On the CPU we need to allocate enough
245
239
// / space for the whole lane and index into it.
246
- template <typename V> LIBC_INLINE V &lane_value (V *val, uint32_t id) {
240
+ template <typename V> RPC_INLINE V &lane_value (V *val, uint32_t id) {
247
241
if constexpr (is_process_gpu ())
248
242
return *val;
249
243
return val[id];
250
244
}
251
245
252
246
// / Advance the \p p by \p bytes.
253
- template <typename T, typename U> LIBC_INLINE T *advance (T *ptr, U bytes) {
247
+ template <typename T, typename U> RPC_INLINE T *advance (T *ptr, U bytes) {
254
248
if constexpr (is_const<T>::value)
255
249
return reinterpret_cast <T *>(reinterpret_cast <const uint8_t *>(ptr) +
256
250
bytes);
@@ -259,15 +253,14 @@ template <typename T, typename U> LIBC_INLINE T *advance(T *ptr, U bytes) {
259
253
}
260
254
261
255
// / Wrapper around the optimal memory copy implementation for the target.
262
- LIBC_INLINE void rpc_memcpy (void *dst, const void *src, size_t count) {
256
+ RPC_INLINE void rpc_memcpy (void *dst, const void *src, size_t count) {
263
257
__builtin_memcpy (dst, src, count);
264
258
}
265
259
266
- template <class T > LIBC_INLINE constexpr const T &max (const T &a, const T &b) {
260
+ template <class T > RPC_INLINE constexpr const T &max (const T &a, const T &b) {
267
261
return (a < b) ? b : a;
268
262
}
269
263
270
264
} // namespace rpc
271
- } // namespace LIBC_NAMESPACE_DECL
272
265
273
- #endif // LLVM_LIBC_SRC___SUPPORT_RPC_RPC_UTIL_H
266
+ #endif // LLVM_LIBC_SHARED_RPC_UTIL_H
0 commit comments