[libc-commits] [PATCH] D158365: [libc] Remove 'try_open' from the client interface

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Sun Aug 20 07:35:42 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: JonChesterfield, sivachandra, michaelrj.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
jhuber6 requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158365

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


Index: libc/src/__support/RPC/rpc.h
===================================================================
--- libc/src/__support/RPC/rpc.h
+++ libc/src/__support/RPC/rpc.h
@@ -517,15 +517,24 @@
   }
 }
 
-/// 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) {
+  return cpp::nullopt;
+}
+
+/// 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 +557,6 @@
     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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158365.551831.patch
Type: text/x-patch
Size: 2032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230820/6875ac7b/attachment.bin>


More information about the libc-commits mailing list