55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===----------------------------------------------------------------------===//
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
178
189// Workaround for missing __has_builtin in < GCC 10.
1910#ifndef __has_builtin
2011#define __has_builtin (x ) 0
2112#endif
2213
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-
3414#include " shared/rpc.h"
3515#include " shared/rpc_opcodes.h"
3616
17+ #include " src/__support/CPP/type_traits.h"
3718#include " src/__support/arg_list.h"
3819#include " src/stdio/printf_core/converter.h"
3920#include " src/stdio/printf_core/parser.h"
4021#include " src/stdio/printf_core/writer.h"
4122
42- #include " hdr/stdio_overlay.h "
43- #include " hdr/stdlib_overlay.h "
23+ #include < stdio.h >
24+ #include < stdlib.h >
4425
45- namespace LIBC_NAMESPACE_DECL {
46- namespace internal {
26+ namespace LIBC_NAMESPACE {
4727
4828// Minimal replacement for 'std::vector' that works for trivial types.
4929template <typename T> class TempVector {
@@ -55,66 +35,68 @@ template <typename T> class TempVector {
5535 size_t capacity;
5636
5737public:
58- LIBC_INLINE TempVector () : data(nullptr ), current(0 ), capacity(0 ) {}
38+ TempVector () : data(nullptr ), current(0 ), capacity(0 ) {}
5939
60- LIBC_INLINE ~TempVector () { free (data); }
40+ ~TempVector () { free (data); }
6141
62- LIBC_INLINE void push_back (const T &value) {
42+ void push_back (const T &value) {
6343 if (current == capacity)
6444 grow ();
6545 data[current] = T (value);
6646 ++current;
6747 }
6848
69- LIBC_INLINE void push_back (T &&value) {
49+ void push_back (T &&value) {
7050 if (current == capacity)
7151 grow ();
7252 data[current] = T (static_cast <T &&>(value));
7353 ++current;
7454 }
7555
76- LIBC_INLINE void pop_back () { --current; }
56+ void pop_back () { --current; }
7757
78- LIBC_INLINE bool empty () { return current == 0 ; }
58+ bool empty () { return current == 0 ; }
7959
80- LIBC_INLINE size_t size () { return current; }
60+ size_t size () { return current; }
8161
82- LIBC_INLINE T &operator [](size_t index) { return data[index]; }
62+ T &operator [](size_t index) { return data[index]; }
8363
84- LIBC_INLINE T &back () { return data[current - 1 ]; }
64+ T &back () { return data[current - 1 ]; }
8565
8666private:
87- LIBC_INLINE void grow () {
67+ void grow () {
8868 size_t new_capacity = capacity ? capacity * 2 : 1 ;
8969 void *new_data = realloc (data, new_capacity * sizeof (T));
70+ if (!new_data)
71+ abort ();
9072 data = static_cast <T *>(new_data);
9173 capacity = new_capacity;
9274 }
9375};
9476
9577struct TempStorage {
96- LIBC_INLINE char *alloc (size_t size) {
78+ char *alloc (size_t size) {
9779 storage.push_back (reinterpret_cast <char *>(malloc (size)));
9880 return storage.back ();
9981 }
10082
101- LIBC_INLINE ~TempStorage () {
83+ ~TempStorage () {
10284 for (size_t i = 0 ; i < storage.size (); ++i)
10385 free (storage[i]);
10486 }
10587
10688 TempVector<char *> storage;
10789};
10890
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+ };
11797
98+ // Get the associated stream out of an encoded number.
99+ LIBC_INLINE ::FILE *to_stream (uintptr_t f) {
118100 ::FILE *stream = reinterpret_cast <FILE *>(f & ~0x3ull );
119101 Stream type = static_cast <Stream>(f & 0x3ull );
120102 if (type == Stdin)
@@ -127,8 +109,7 @@ LIBC_INLINE static ::FILE *to_stream(uintptr_t f) {
127109}
128110
129111template <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) {
132113 FILE *files[num_lanes] = {nullptr };
133114 // Get the appropriate output stream to use.
134115 if (port.get_opcode () == LIBC_PRINTF_TO_STREAM ||
@@ -287,8 +268,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
287268 }
288269 }
289270
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]);
292272 if (results[lane] != writer.get_chars_written () || ret == -1 )
293273 results[lane] = -1 ;
294274 }
@@ -302,7 +282,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
302282}
303283
304284template <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) {
306286 TempStorage temp_storage;
307287
308288 switch (port.get_opcode ()) {
@@ -353,9 +333,8 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
353333 void *data[num_lanes] = {nullptr };
354334 port.recv ([&](rpc::Buffer *buffer, uint32_t id) {
355335 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 ]));
359338 sizes[id] = !str ? 0 : __builtin_strlen (str) + 1 ;
360339 });
361340 port.send_n (data, sizes);
@@ -374,9 +353,9 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
374353 break ;
375354 }
376355 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 ) {
378357 FILE *file = reinterpret_cast <FILE *>(buffer->data [0 ]);
379- buffer->data [0 ] = :: fclose (file);
358+ buffer->data [0 ] = fclose (file);
380359 });
381360 break ;
382361 }
@@ -519,28 +498,21 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
519498 return rpc::RPC_SUCCESS;
520499}
521500
522- } // namespace internal
523- } // namespace LIBC_NAMESPACE_DECL
501+ } // namespace LIBC_NAMESPACE
524502
525- namespace LIBC_NAMESPACE_DECL {
526503namespace 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) {
531507 switch (num_lanes) {
532508 case 1 :
533- return internal ::handle_port_impl<1 >(port);
509+ return LIBC_NAMESPACE ::handle_port_impl<1 >(port);
534510 case 32 :
535- return internal ::handle_port_impl<32 >(port);
511+ return LIBC_NAMESPACE ::handle_port_impl<32 >(port);
536512 case 64 :
537- return internal ::handle_port_impl<64 >(port);
513+ return LIBC_NAMESPACE ::handle_port_impl<64 >(port);
538514 default :
539- return :: rpc::RPC_ERROR;
515+ return rpc::RPC_ERROR;
540516 }
541517}
542-
543518} // namespace rpc
544- } // namespace LIBC_NAMESPACE_DECL
545-
546- #endif // LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
0 commit comments