[libc-commits] [libc] 4272d09 - [libc][NFC] Cleanup the RPC server implementation prior to installing

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Jun 21 09:14:27 PDT 2023


Author: Joseph Huber
Date: 2023-06-21T11:14:20-05:00
New Revision: 4272d09196ff6967ed9b10daf41f3ecfd97926c1

URL: https://github.com/llvm/llvm-project/commit/4272d09196ff6967ed9b10daf41f3ecfd97926c1
DIFF: https://github.com/llvm/llvm-project/commit/4272d09196ff6967ed9b10daf41f3ecfd97926c1.diff

LOG: [libc][NFC] Cleanup the RPC server implementation prior to installing

This does some simple cleanup prior to landing the patch to install
these.

Differential Revision: https://reviews.llvm.org/D153439

Added: 
    

Modified: 
    libc/utils/gpu/server/Server.cpp
    libc/utils/gpu/server/Server.h

Removed: 
    


################################################################################
diff  --git a/libc/utils/gpu/server/Server.cpp b/libc/utils/gpu/server/Server.cpp
index f37fc13851477..2edd0c0dacd59 100644
--- a/libc/utils/gpu/server/Server.cpp
+++ b/libc/utils/gpu/server/Server.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/RPC/rpc.h"
 #include <atomic>
 #include <cstdio>
+#include <cstring>
 #include <memory>
 #include <mutex>
 #include <unordered_map>
@@ -91,7 +92,8 @@ struct Server {
                 : (port->get_opcode() == RPC_WRITE_TO_STDERR ? stderr
                                                              : files[id]);
         int ret = fwrite(strs[id], sizes[id], 1, file);
-        reinterpret_cast<int *>(buffer->data)[0] = ret >= 0 ? sizes[id] : ret;
+        ret = ret >= 0 ? sizes[id] : ret;
+        std::memcpy(buffer->data, &ret, sizeof(int));
       });
       for (uint64_t i = 0; i < rpc::MAX_LANE_SIZE; ++i) {
         if (strs[i])
@@ -101,7 +103,9 @@ struct Server {
     }
     case RPC_EXIT: {
       port->recv([](rpc::Buffer *buffer) {
-        exit(reinterpret_cast<uint32_t *>(buffer->data)[0]);
+        int status = 0;
+        std::memcpy(&status, buffer->data, sizeof(int));
+        exit(status);
       });
       break;
     }
@@ -143,7 +147,7 @@ struct Server {
       break;
     }
     case RPC_NOOP: {
-      port->recv([](rpc::Buffer *buffer) {});
+      port->recv([](rpc::Buffer *) {});
       break;
     }
     default: {
@@ -215,6 +219,8 @@ rpc_status_t rpc_shutdown(void) {
 rpc_status_t rpc_server_init(uint32_t device_id, uint64_t num_ports,
                              uint32_t lane_size, rpc_alloc_ty alloc,
                              void *data) {
+  if (!state)
+    return RPC_STATUS_NOT_INITIALIZED;
   if (device_id >= state->num_devices)
     return RPC_STATUS_OUT_OF_RANGE;
 
@@ -250,6 +256,8 @@ rpc_status_t rpc_server_init(uint32_t device_id, uint64_t num_ports,
 
 rpc_status_t rpc_server_shutdown(uint32_t device_id, rpc_free_ty dealloc,
                                  void *data) {
+  if (!state)
+    return RPC_STATUS_NOT_INITIALIZED;
   if (device_id >= state->num_devices)
     return RPC_STATUS_OUT_OF_RANGE;
   if (!state->devices[device_id])
@@ -263,6 +271,8 @@ rpc_status_t rpc_server_shutdown(uint32_t device_id, rpc_free_ty dealloc,
 }
 
 rpc_status_t rpc_handle_server(uint32_t device_id) {
+  if (!state)
+    return RPC_STATUS_NOT_INITIALIZED;
   if (device_id >= state->num_devices)
     return RPC_STATUS_OUT_OF_RANGE;
   if (!state->devices[device_id])
@@ -280,6 +290,8 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
 rpc_status_t rpc_register_callback(uint32_t device_id, rpc_opcode_t opcode,
                                    rpc_opcode_callback_ty callback,
                                    void *data) {
+  if (!state)
+    return RPC_STATUS_NOT_INITIALIZED;
   if (device_id >= state->num_devices)
     return RPC_STATUS_OUT_OF_RANGE;
   if (!state->devices[device_id])
@@ -291,6 +303,8 @@ rpc_status_t rpc_register_callback(uint32_t device_id, rpc_opcode_t opcode,
 }
 
 void *rpc_get_buffer(uint32_t device_id) {
+  if (!state)
+    return nullptr;
   if (device_id >= state->num_devices)
     return nullptr;
   if (!state->devices[device_id])

diff  --git a/libc/utils/gpu/server/Server.h b/libc/utils/gpu/server/Server.h
index adc38e0961006..d27a0fe3bae41 100644
--- a/libc/utils/gpu/server/Server.h
+++ b/libc/utils/gpu/server/Server.h
@@ -28,6 +28,7 @@ typedef enum {
   RPC_STATUS_OUT_OF_RANGE = 0x1001,
   RPC_STATUS_UNHANDLED_OPCODE = 0x1002,
   RPC_STATUS_INVALID_LANE_SIZE = 0x1003,
+  RPC_STATUS_NOT_INITIALIZED = 0x1004,
 } rpc_status_t;
 
 /// A struct containing an opaque handle to an RPC port. This is what allows the


        


More information about the libc-commits mailing list