[libc-commits] [PATCH] D150115: [libc] Make the opcode parameter a compile time constant

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon May 8 06:39:55 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: JonChesterfield, tianshilei1992, sivachandra, jdoerfert, lntue, michaelrj.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
jhuber6 requested review of this revision.

Currently the opcode is only valid if it is the same between all of the
ports. This is possible to violate if the opcode is places into a memory
location and then read in a uniform manner by the warp / wavefront.
Moving this to a compile time constant makes it impossible to break this
invariant.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150115

Files:
  libc/src/__support/OSUtil/gpu/io.cpp
  libc/src/__support/OSUtil/gpu/quick_exit.cpp
  libc/src/__support/RPC/rpc.h
  libc/test/integration/startup/gpu/rpc_test.cpp


Index: libc/test/integration/startup/gpu/rpc_test.cpp
===================================================================
--- libc/test/integration/startup/gpu/rpc_test.cpp
+++ libc/test/integration/startup/gpu/rpc_test.cpp
@@ -17,7 +17,7 @@
       10 + 10 * gpu::get_thread_id() + 10 * gpu::get_block_id();
   uint64_t cnt = 0;
   for (uint32_t i = 0; i < num_additions; ++i) {
-    rpc::Client::Port port = rpc::client.open(rpc::TEST_INCREMENT);
+    rpc::Client::Port port = rpc::client.open<rpc::TEST_INCREMENT>();
     port.send_and_recv(
         [=](rpc::Buffer *buffer) {
           reinterpret_cast<uint64_t *>(buffer->data)[0] = cnt;
@@ -32,7 +32,7 @@
 
 // Test to ensure that the RPC mechanism doesn't hang on divergence.
 static void test_noop(uint8_t data) {
-  rpc::Client::Port port = rpc::client.open(rpc::NOOP);
+  rpc::Client::Port port = rpc::client.open<rpc::NOOP>();
   port.send([=](rpc::Buffer *buffer) { buffer->data[0] = data; });
   port.close();
 }
Index: libc/src/__support/RPC/rpc.h
===================================================================
--- libc/src/__support/RPC/rpc.h
+++ libc/src/__support/RPC/rpc.h
@@ -273,8 +273,8 @@
   LIBC_INLINE ~Client() = default;
 
   using Port = rpc::Port<false>;
-  LIBC_INLINE cpp::optional<Port> try_open(uint16_t opcode);
-  LIBC_INLINE Port open(uint16_t opcode);
+  template <uint16_t opcode> LIBC_INLINE cpp::optional<Port> try_open();
+  template <uint16_t opcode> LIBC_INLINE Port open();
 };
 
 /// The RPC server used to respond to the client.
@@ -411,10 +411,9 @@
 /// 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.
-/// Opening a port is only valid if the `opcode` is the sam accross every
-/// participating thread.
+template <uint16_t opcode>
 [[clang::convergent]] LIBC_INLINE cpp::optional<Client::Port>
-Client::try_open(uint16_t opcode) {
+Client::try_open() {
   // Perform a naive linear scan for a port that can be opened to send data.
   for (uint64_t index = 0; index < port_count; ++index) {
     // Attempt to acquire the lock on this index.
@@ -445,9 +444,9 @@
   return cpp::nullopt;
 }
 
-LIBC_INLINE Client::Port Client::open(uint16_t opcode) {
+template <uint16_t opcode> LIBC_INLINE Client::Port Client::open() {
   for (;;) {
-    if (cpp::optional<Client::Port> p = try_open(opcode))
+    if (cpp::optional<Client::Port> p = try_open<opcode>())
       return cpp::move(p.value());
     sleep_briefly();
   }
Index: libc/src/__support/OSUtil/gpu/quick_exit.cpp
===================================================================
--- libc/src/__support/OSUtil/gpu/quick_exit.cpp
+++ libc/src/__support/OSUtil/gpu/quick_exit.cpp
@@ -17,7 +17,7 @@
 namespace __llvm_libc {
 
 void quick_exit(int status) {
-  rpc::Client::Port port = rpc::client.open(rpc::EXIT);
+  rpc::Client::Port port = rpc::client.open<rpc::EXIT>();
   port.send([&](rpc::Buffer *buffer) {
     reinterpret_cast<uint32_t *>(buffer->data)[0] = status;
   });
Index: libc/src/__support/OSUtil/gpu/io.cpp
===================================================================
--- libc/src/__support/OSUtil/gpu/io.cpp
+++ libc/src/__support/OSUtil/gpu/io.cpp
@@ -15,7 +15,7 @@
 namespace __llvm_libc {
 
 void write_to_stderr(cpp::string_view msg) {
-  rpc::Client::Port port = rpc::client.open(rpc::PRINT_TO_STDERR);
+  rpc::Client::Port port = rpc::client.open<rpc::PRINT_TO_STDERR>();
   port.send_n(msg.data(), msg.size());
   port.close();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150115.520353.patch
Type: text/x-patch
Size: 3603 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230508/4334e22a/attachment.bin>


More information about the libc-commits mailing list