[libc-commits] [libc] 6970920 - [libc] Remove 'try_open' from the client interface

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Mon Aug 21 04:43:47 PDT 2023


Author: Joseph Huber
Date: 2023-08-21T06:43:38-05:00
New Revision: 6970920ca6ab318616a13df8967093de9dbedca2

URL: https://github.com/llvm/llvm-project/commit/6970920ca6ab318616a13df8967093de9dbedca2
DIFF: https://github.com/llvm/llvm-project/commit/6970920ca6ab318616a13df8967093de9dbedca2.diff

LOG: [libc] Remove 'try_open' from the client interface

We previously provided the `try_open` facility to indicate if opening a
port failed. This is used on the server to continuously poll and quit if
there is no work. However the Client currently has no way to recover
from not finding a port and simply spins repeatedly. The abstraction
here costs us some resources. This patch changes the interface to only
allow `open` on the client side and merges the loops. This saves us a
branch and a good number of registers.

Reviewed By: JonChesterfield

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

Added: 
    

Modified: 
    libc/src/__support/RPC/rpc.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/RPC/rpc.h b/libc/src/__support/RPC/rpc.h
index 33694cd7ceed8c..519faa0d00ecd7 100644
--- a/libc/src/__support/RPC/rpc.h
+++ b/libc/src/__support/RPC/rpc.h
@@ -348,7 +348,6 @@ struct Client {
   LIBC_INLINE ~Client() = default;
 
   using Port = rpc::Port<false, Packet<gpu::LANE_SIZE>>;
-  template <uint16_t opcode> LIBC_INLINE cpp::optional<Port> try_open();
   template <uint16_t opcode> LIBC_INLINE Port open();
 
   LIBC_INLINE void reset(uint32_t port_count, void *buffer) {
@@ -517,15 +516,19 @@ LIBC_INLINE void Port<T, S>::recv_n(void **dst, uint64_t *size, A &&alloc) {
   }
 }
 
-/// Attempts to open a port to use as the client. The client can only open a
-/// port if we find an index that is in a valid sending state. That is, there
-/// are send operations pending that haven't been serviced on this port. Each
-/// port instance uses an associated \p opcode to tell the server what to do.
-template <uint16_t opcode>
-[[clang::convergent]] LIBC_INLINE cpp::optional<Client::Port>
-Client::try_open() {
-  // Perform a naive linear scan for a port that can be opened to send data.
-  for (uint32_t index = 0; index < process.port_count; ++index) {
+/// Continually attempts to open a port to use as the client. The client can
+/// only open a port if we find an index that is in a valid sending state. That
+/// is, there are send operations pending that haven't been serviced on this
+/// port. Each port instance uses an associated \p opcode to tell the server
+/// what to do.
+template <uint16_t opcode> LIBC_INLINE Client::Port Client::open() {
+  // Repeatedly perform a naive linear scan for a port that can be opened to
+  // send data.
+  for (uint32_t index = 0;; ++index) {
+    // Start from the beginning if we run out of ports to check.
+    if (index >= process.port_count)
+      index = 0;
+
     // Attempt to acquire the lock on this index.
     uint64_t lane_mask = gpu::get_lane_mask();
     if (!process.try_lock(lane_mask, index))
@@ -548,15 +551,6 @@ Client::try_open() {
     gpu::sync_lane(lane_mask);
     return Port(process, lane_mask, index, out);
   }
-  return cpp::nullopt;
-}
-
-template <uint16_t opcode> LIBC_INLINE Client::Port Client::open() {
-  for (;;) {
-    if (cpp::optional<Client::Port> p = try_open<opcode>())
-      return cpp::move(p.value());
-    sleep_briefly();
-  }
 }
 
 /// Attempts to open a port to use as the server. The server can only open a


        


More information about the libc-commits mailing list