[libc] Fix the implementation of exit on the GPU
authorJoseph Huber <jhuber6@vols.utk.edu>
Thu, 29 Jun 2023 17:03:40 +0000 (12:03 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Thu, 29 Jun 2023 18:22:23 +0000 (13:22 -0500)
The RPC calls all have delays associated with them. Currently the `exit`
function does an async send and immediately exits the GPU. This can have
the effect that the RPC server never sees the exit call and we continue.
This patch changes that to first sync with the server before continuing
to perform its exit. There is still a hazard here, where the kernel can
complete before the RPC call reads back its response, but this is simply
multi-threaded hazards. This change ensures that the server *will*
always exit some time after the GPU exits.

Reviewed By: JonChesterfield

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

libc/docs/gpu/support.rst
libc/src/__support/OSUtil/gpu/quick_exit.cpp
libc/utils/gpu/server/rpc_server.cpp

index 816d961..a4ec0e6 100644 (file)
@@ -89,6 +89,7 @@ atoi           |check|
 atof           |check|
 atol           |check|
 atoll          |check|
+exit           |check|    |check|
 labs           |check|
 llabs          |check|
 strtod         |check|
index ec688f9..470b2bd 100644 (file)
@@ -17,7 +17,9 @@
 namespace __llvm_libc {
 
 void quick_exit(int status) {
+  // We want to first make sure the server is listening before we exit.
   rpc::Client::Port port = rpc::client.open<RPC_EXIT>();
+  port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
   port.send([&](rpc::Buffer *buffer) {
     reinterpret_cast<uint32_t *>(buffer->data)[0] = status;
   });
index a440efd..b5103f7 100644 (file)
@@ -102,6 +102,8 @@ private:
       break;
     }
     case RPC_EXIT: {
+      // Send a response to the client to signal that we are ready to exit.
+      port->recv_and_send([](rpc::Buffer *) {});
       port->recv([](rpc::Buffer *buffer) {
         int status = 0;
         std::memcpy(&status, buffer->data, sizeof(int));