5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
- //
9
- // This file is intended to be used externally as part of the `shared/`
10
- // interface. For that purpose, we manually define a few options normally
11
- // handled by the libc build system.
12
- //
13
- // ===----------------------------------------------------------------------===//
14
-
15
- #ifndef LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
16
- #define LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
17
8
18
9
// Workaround for missing __has_builtin in < GCC 10.
19
10
#ifndef __has_builtin
20
11
#define __has_builtin (x ) 0
21
12
#endif
22
13
23
- // Configs for using the LLVM libc writer interface.
24
- #define LIBC_COPT_USE_C_ASSERT
25
- #define LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY
26
- #define LIBC_COPT_ARRAY_ARG_LIST
27
- #define LIBC_COPT_PRINTF_DISABLE_WRITE_INT
28
- #define LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
29
- #define LIBC_COPT_PRINTF_DISABLE_STRERROR
30
-
31
- // The 'long double' type is 8 byte
32
- #define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
33
-
34
14
#include " shared/rpc.h"
35
15
#include " shared/rpc_opcodes.h"
36
16
17
+ #include " src/__support/CPP/type_traits.h"
37
18
#include " src/__support/arg_list.h"
38
19
#include " src/stdio/printf_core/converter.h"
39
20
#include " src/stdio/printf_core/parser.h"
40
21
#include " src/stdio/printf_core/writer.h"
41
22
42
- #include " hdr/stdio_overlay.h "
43
- #include " hdr/stdlib_overlay.h "
23
+ #include < stdio.h >
24
+ #include < stdlib.h >
44
25
45
- namespace LIBC_NAMESPACE_DECL {
46
- namespace internal {
26
+ namespace LIBC_NAMESPACE {
47
27
48
28
// Minimal replacement for 'std::vector' that works for trivial types.
49
29
template <typename T> class TempVector {
@@ -55,66 +35,68 @@ template <typename T> class TempVector {
55
35
size_t capacity;
56
36
57
37
public:
58
- LIBC_INLINE TempVector () : data(nullptr ), current(0 ), capacity(0 ) {}
38
+ TempVector () : data(nullptr ), current(0 ), capacity(0 ) {}
59
39
60
- LIBC_INLINE ~TempVector () { free (data); }
40
+ ~TempVector () { free (data); }
61
41
62
- LIBC_INLINE void push_back (const T &value) {
42
+ void push_back (const T &value) {
63
43
if (current == capacity)
64
44
grow ();
65
45
data[current] = T (value);
66
46
++current;
67
47
}
68
48
69
- LIBC_INLINE void push_back (T &&value) {
49
+ void push_back (T &&value) {
70
50
if (current == capacity)
71
51
grow ();
72
52
data[current] = T (static_cast <T &&>(value));
73
53
++current;
74
54
}
75
55
76
- LIBC_INLINE void pop_back () { --current; }
56
+ void pop_back () { --current; }
77
57
78
- LIBC_INLINE bool empty () { return current == 0 ; }
58
+ bool empty () { return current == 0 ; }
79
59
80
- LIBC_INLINE size_t size () { return current; }
60
+ size_t size () { return current; }
81
61
82
- LIBC_INLINE T &operator [](size_t index) { return data[index]; }
62
+ T &operator [](size_t index) { return data[index]; }
83
63
84
- LIBC_INLINE T &back () { return data[current - 1 ]; }
64
+ T &back () { return data[current - 1 ]; }
85
65
86
66
private:
87
- LIBC_INLINE void grow () {
67
+ void grow () {
88
68
size_t new_capacity = capacity ? capacity * 2 : 1 ;
89
69
void *new_data = realloc (data, new_capacity * sizeof (T));
70
+ if (!new_data)
71
+ abort ();
90
72
data = static_cast <T *>(new_data);
91
73
capacity = new_capacity;
92
74
}
93
75
};
94
76
95
77
struct TempStorage {
96
- LIBC_INLINE char *alloc (size_t size) {
78
+ char *alloc (size_t size) {
97
79
storage.push_back (reinterpret_cast <char *>(malloc (size)));
98
80
return storage.back ();
99
81
}
100
82
101
- LIBC_INLINE ~TempStorage () {
83
+ ~TempStorage () {
102
84
for (size_t i = 0 ; i < storage.size (); ++i)
103
85
free (storage[i]);
104
86
}
105
87
106
88
TempVector<char *> storage;
107
89
};
108
90
109
- // Get the associated stream out of an encoded number.
110
- LIBC_INLINE static ::FILE *to_stream (uintptr_t f) {
111
- enum Stream {
112
- File = 0 ,
113
- Stdin = 1 ,
114
- Stdout = 2 ,
115
- Stderr = 3 ,
116
- };
91
+ enum Stream {
92
+ File = 0 ,
93
+ Stdin = 1 ,
94
+ Stdout = 2 ,
95
+ Stderr = 3 ,
96
+ };
117
97
98
+ // Get the associated stream out of an encoded number.
99
+ LIBC_INLINE ::FILE *to_stream (uintptr_t f) {
118
100
::FILE *stream = reinterpret_cast <FILE *>(f & ~0x3ull );
119
101
Stream type = static_cast <Stream>(f & 0x3ull );
120
102
if (type == Stdin)
@@ -127,8 +109,7 @@ LIBC_INLINE static ::FILE *to_stream(uintptr_t f) {
127
109
}
128
110
129
111
template <bool packed, uint32_t num_lanes>
130
- LIBC_INLINE static void handle_printf (rpc::Server::Port &port,
131
- TempStorage &temp_storage) {
112
+ static void handle_printf (rpc::Server::Port &port, TempStorage &temp_storage) {
132
113
FILE *files[num_lanes] = {nullptr };
133
114
// Get the appropriate output stream to use.
134
115
if (port.get_opcode () == LIBC_PRINTF_TO_STREAM ||
@@ -287,8 +268,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
287
268
}
288
269
}
289
270
290
- results[lane] = static_cast <int >(
291
- fwrite (buffer, 1 , writer.get_chars_written (), files[lane]));
271
+ results[lane] = fwrite (buffer, 1 , writer.get_chars_written (), files[lane]);
292
272
if (results[lane] != writer.get_chars_written () || ret == -1 )
293
273
results[lane] = -1 ;
294
274
}
@@ -302,7 +282,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
302
282
}
303
283
304
284
template <uint32_t num_lanes>
305
- LIBC_INLINE static rpc::Status handle_port_impl (rpc::Server::Port &port) {
285
+ rpc::Status handle_port_impl (rpc::Server::Port &port) {
306
286
TempStorage temp_storage;
307
287
308
288
switch (port.get_opcode ()) {
@@ -353,9 +333,8 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
353
333
void *data[num_lanes] = {nullptr };
354
334
port.recv ([&](rpc::Buffer *buffer, uint32_t id) {
355
335
data[id] = temp_storage.alloc (buffer->data [0 ]);
356
- const char *str = ::fgets (reinterpret_cast <char *>(data[id]),
357
- static_cast <int >(buffer->data [0 ]),
358
- to_stream (buffer->data [1 ]));
336
+ const char *str = fgets (reinterpret_cast <char *>(data[id]),
337
+ buffer->data [0 ], to_stream (buffer->data [1 ]));
359
338
sizes[id] = !str ? 0 : __builtin_strlen (str) + 1 ;
360
339
});
361
340
port.send_n (data, sizes);
@@ -374,9 +353,9 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
374
353
break ;
375
354
}
376
355
case LIBC_CLOSE_FILE: {
377
- port.recv_and_send ([&](rpc::Buffer *buffer, uint32_t ) {
356
+ port.recv_and_send ([&](rpc::Buffer *buffer, uint32_t id ) {
378
357
FILE *file = reinterpret_cast <FILE *>(buffer->data [0 ]);
379
- buffer->data [0 ] = :: fclose (file);
358
+ buffer->data [0 ] = fclose (file);
380
359
});
381
360
break ;
382
361
}
@@ -519,28 +498,21 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
519
498
return rpc::RPC_SUCCESS;
520
499
}
521
500
522
- } // namespace internal
523
- } // namespace LIBC_NAMESPACE_DECL
501
+ } // namespace LIBC_NAMESPACE
524
502
525
- namespace LIBC_NAMESPACE_DECL {
526
503
namespace rpc {
527
-
528
- // Handles any opcode generated from the 'libc' client code.
529
- LIBC_INLINE ::rpc::Status handle_libc_opcodes (::rpc::Server::Port &port,
530
- uint32_t num_lanes) {
504
+ // The implementation of this function currently lives in the utility directory
505
+ // at 'utils/gpu/server/rpc_server.cpp'.
506
+ rpc::Status handle_libc_opcodes (rpc::Server::Port &port, uint32_t num_lanes) {
531
507
switch (num_lanes) {
532
508
case 1 :
533
- return internal ::handle_port_impl<1 >(port);
509
+ return LIBC_NAMESPACE ::handle_port_impl<1 >(port);
534
510
case 32 :
535
- return internal ::handle_port_impl<32 >(port);
511
+ return LIBC_NAMESPACE ::handle_port_impl<32 >(port);
536
512
case 64 :
537
- return internal ::handle_port_impl<64 >(port);
513
+ return LIBC_NAMESPACE ::handle_port_impl<64 >(port);
538
514
default :
539
- return :: rpc::RPC_ERROR;
515
+ return rpc::RPC_ERROR;
540
516
}
541
517
}
542
-
543
518
} // namespace rpc
544
- } // namespace LIBC_NAMESPACE_DECL
545
-
546
- #endif // LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
0 commit comments