[libc-commits] [libc] dcdfc96 - [libc] Export GPU extensions to `libc` for external use

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Thu Jun 15 09:02:32 PDT 2023


Author: Joseph Huber
Date: 2023-06-15T11:02:24-05:00
New Revision: dcdfc963d7934a1313094b6fe9ce7aa04debe495

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

LOG: [libc] Export GPU extensions to `libc` for external use

The GPU port of the LLVM C library needs to export a few extensions to
the interface such that users can interface with it. This patch adds the
necessary logic to define a GPU extension. Currently, this only exports
a `rpc_reset_client` function. This allows us to use the server in
D147054 to set up the RPC interface outside of `libc`.

Depends on https://reviews.llvm.org/D147054

Reviewed By: sivachandra

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

Added: 
    libc/include/gpu/rpc.h.def
    libc/include/llvm-libc-types/rpc_opcodes_t.h
    libc/spec/gpu_ext.td
    libc/src/gpu/CMakeLists.txt
    libc/src/gpu/rpc_reset.cpp
    libc/src/gpu/rpc_reset.h

Modified: 
    libc/config/gpu/api.td
    libc/config/gpu/entrypoints.txt
    libc/config/gpu/headers.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/src/CMakeLists.txt
    libc/src/__support/File/gpu/file.cpp
    libc/src/__support/OSUtil/gpu/io.cpp
    libc/src/__support/OSUtil/gpu/quick_exit.cpp
    libc/src/__support/RPC/CMakeLists.txt
    libc/src/__support/RPC/rpc.h
    libc/src/__support/RPC/rpc_client.h
    libc/src/stdlib/gpu/free.cpp
    libc/src/stdlib/gpu/malloc.cpp
    libc/test/integration/startup/gpu/rpc_interface_test.cpp
    libc/test/integration/startup/gpu/rpc_stream_test.cpp
    libc/test/integration/startup/gpu/rpc_test.cpp
    libc/utils/gpu/loader/CMakeLists.txt
    libc/utils/gpu/server/CMakeLists.txt
    libc/utils/gpu/server/Server.cpp
    libc/utils/gpu/server/Server.h

Removed: 
    


################################################################################
diff  --git a/libc/config/gpu/api.td b/libc/config/gpu/api.td
index f6209c4bebe56..1268a6f315b0a 100644
--- a/libc/config/gpu/api.td
+++ b/libc/config/gpu/api.td
@@ -1,6 +1,7 @@
 include "config/public_api.td"
 
 include "spec/stdc.td"
+include "spec/gpu_ext.td"
 
 def StringAPI : PublicAPI<"string.h"> {
   let Types = ["size_t"];

diff  --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index f9d194475c0e9..64a19a9c5183d 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -90,6 +90,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.stdin
     libc.src.stdio.stdout
     libc.src.stdio.stderr
+
+    # gpu/rpc.h entrypoints
+    libc.src.gpu.rpc_reset
 )
 
 set(TARGET_LIBM_ENTRYPOINTS

diff  --git a/libc/config/gpu/headers.txt b/libc/config/gpu/headers.txt
index b11ae18b5131c..cd7585afe3c39 100644
--- a/libc/config/gpu/headers.txt
+++ b/libc/config/gpu/headers.txt
@@ -6,4 +6,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.errno
     libc.include.stdlib
     libc.include.stdio
+
+    # Header for RPC extensions
+    libc.include.gpu_rpc
 )

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index f7b949fb7eb54..dd24d01ea386a 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -488,6 +488,19 @@ add_gen_header(
     .llvm-libc-types.wchar_t
 )
 
+if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
+  file(MAKE_DIRECTORY "gpu")
+
+  add_gen_header(
+    gpu_rpc
+    DEF_FILE gpu/rpc.h.def
+    GEN_HDR gpu/rpc.h
+    DEPENDS
+      .llvm_libc_common_h
+      .llvm-libc-types.rpc_opcodes_t
+  )
+endif()
+
 if(NOT LLVM_LIBC_FULL_BUILD)
   # We don't install headers in non-fullbuild mode.
   return()

diff  --git a/libc/include/gpu/rpc.h.def b/libc/include/gpu/rpc.h.def
new file mode 100644
index 0000000000000..0438cd65e7be2
--- /dev/null
+++ b/libc/include/gpu/rpc.h.def
@@ -0,0 +1,18 @@
+//===-- GPU header rpc.h --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_GPU_RPC_H
+#define LLVM_LIBC_GPU_RPC_H
+
+#include <__llvm-libc-common.h>
+
+#include <llvm-libc-types/rpc_opcodes_t.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_GPU_RPC_H

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index ae9b3355b25be..006986fd98fc8 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -90,3 +90,4 @@ add_header(wchar_t HDR wchar_t.h)
 add_header(wint_t HDR wint_t.h)
 add_header(sa_family_t HDR sa_family_t.h)
 add_header(struct_sockaddr HDR struct_sockaddr.h)
+add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)

diff  --git a/libc/include/llvm-libc-types/rpc_opcodes_t.h b/libc/include/llvm-libc-types/rpc_opcodes_t.h
new file mode 100644
index 0000000000000..8a2ad5bda7542
--- /dev/null
+++ b/libc/include/llvm-libc-types/rpc_opcodes_t.h
@@ -0,0 +1,25 @@
+//===-- Definition of RPC opcodes -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __LLVM_LIBC_TYPES_RPC_OPCODE_H__
+#define __LLVM_LIBC_TYPES_RPC_OPCODE_H__
+
+typedef enum : unsigned short {
+  RPC_NOOP = 0,
+  RPC_EXIT = 1,
+  RPC_WRITE_TO_STDOUT = 2,
+  RPC_WRITE_TO_STDERR = 3,
+  RPC_WRITE_TO_STREAM = 4,
+  RPC_MALLOC = 5,
+  RPC_FREE = 6,
+  RPC_TEST_INCREMENT = 7,
+  RPC_TEST_INTERFACE = 8,
+  RPC_TEST_STREAM = 9,
+} rpc_opcode_t;
+
+#endif // __LLVM_LIBC_TYPES_RPC_OPCODE_H__

diff  --git a/libc/spec/gpu_ext.td b/libc/spec/gpu_ext.td
new file mode 100644
index 0000000000000..69117bbde4993
--- /dev/null
+++ b/libc/spec/gpu_ext.td
@@ -0,0 +1,18 @@
+def GPUExtensions : StandardSpec<"GPUExtensions"> {
+  HeaderSpec RPC = HeaderSpec<
+    "gpu/rpc.h",
+    [], // Macros
+    [], // Types
+    [], // Enumerations
+    [
+        FunctionSpec<
+            "rpc_reset",
+            RetValSpec<VoidType>,
+            [ArgSpec<UnsignedIntType>, ArgSpec<VoidPtr>]
+        >,
+    ]
+  >;
+  let Headers = [
+    RPC,
+  ];
+}

diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 9f2f45165e408..88838eecc53c9 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -20,6 +20,10 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_subdirectory(unistd)
 endif()
 
+if(${LIBC_TARGET_OS} STREQUAL "gpu")
+  add_subdirectory(gpu)
+endif()
+
 if(NOT LLVM_LIBC_FULL_BUILD)
   return()
 endif()

diff  --git a/libc/src/__support/File/gpu/file.cpp b/libc/src/__support/File/gpu/file.cpp
index 2db56d7ce5e5f..f31bcb53292cb 100644
--- a/libc/src/__support/File/gpu/file.cpp
+++ b/libc/src/__support/File/gpu/file.cpp
@@ -37,7 +37,7 @@ namespace {
 
 int write_to_stdout(const void *data, size_t size) {
   int ret = 0;
-  rpc::Client::Port port = rpc::client.open<rpc::WRITE_TO_STDOUT>();
+  rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDOUT>();
   port.send_n(data, size);
   port.recv([&](rpc::Buffer *buffer) {
     ret = reinterpret_cast<int *>(buffer->data)[0];
@@ -48,7 +48,7 @@ int write_to_stdout(const void *data, size_t size) {
 
 int write_to_stderr(const void *data, size_t size) {
   int ret = 0;
-  rpc::Client::Port port = rpc::client.open<rpc::WRITE_TO_STDERR>();
+  rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDERR>();
   port.send_n(data, size);
   port.recv([&](rpc::Buffer *buffer) {
     ret = reinterpret_cast<int *>(buffer->data)[0];
@@ -59,7 +59,7 @@ int write_to_stderr(const void *data, size_t size) {
 
 int write_to_stream(uintptr_t file, const void *data, size_t size) {
   int ret = 0;
-  rpc::Client::Port port = rpc::client.open<rpc::WRITE_TO_STREAM>();
+  rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STREAM>();
   port.send([&](rpc::Buffer *buffer) {
     reinterpret_cast<uintptr_t *>(buffer->data)[0] = file;
   });

diff  --git a/libc/src/__support/OSUtil/gpu/io.cpp b/libc/src/__support/OSUtil/gpu/io.cpp
index eb074a479deac..fcbadf2a8fd51 100644
--- a/libc/src/__support/OSUtil/gpu/io.cpp
+++ b/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::WRITE_TO_STDERR>();
+  rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDERR>();
   port.send_n(msg.data(), msg.size());
   port.recv([](rpc::Buffer *) { /* void */ });
   port.close();

diff  --git a/libc/src/__support/OSUtil/gpu/quick_exit.cpp b/libc/src/__support/OSUtil/gpu/quick_exit.cpp
index 6432965a741f0..ec688f932b4f5 100644
--- a/libc/src/__support/OSUtil/gpu/quick_exit.cpp
+++ b/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;
   });

diff  --git a/libc/src/__support/RPC/CMakeLists.txt b/libc/src/__support/RPC/CMakeLists.txt
index 96bfd8e729549..6d8dd7f257b64 100644
--- a/libc/src/__support/RPC/CMakeLists.txt
+++ b/libc/src/__support/RPC/CMakeLists.txt
@@ -22,6 +22,7 @@ add_object_library(
   HDRS
     rpc_client.h
   DEPENDS
+    libc.include.gpu_rpc
     libc.src.__support.GPU.utils
     .rpc
 )

diff  --git a/libc/src/__support/RPC/rpc.h b/libc/src/__support/RPC/rpc.h
index 3c4613b36a973..0d7911403cf48 100644
--- a/libc/src/__support/RPC/rpc.h
+++ b/libc/src/__support/RPC/rpc.h
@@ -30,20 +30,6 @@
 namespace __llvm_libc {
 namespace rpc {
 
-/// A list of opcodes that we use to invoke certain actions on the server.
-enum Opcode : uint16_t {
-  NOOP = 0,
-  EXIT = 1,
-  WRITE_TO_STDOUT = 2,
-  WRITE_TO_STDERR = 3,
-  WRITE_TO_STREAM = 4,
-  MALLOC = 5,
-  FREE = 6,
-  TEST_INCREMENT = 7,
-  TEST_INTERFACE = 8,
-  TEST_STREAM = 9,
-};
-
 /// A fixed size channel used to communicate between the RPC client and server.
 struct Buffer {
   uint64_t data[8];

diff  --git a/libc/src/__support/RPC/rpc_client.h b/libc/src/__support/RPC/rpc_client.h
index 509ec2f4185b9..71ead0b38c878 100644
--- a/libc/src/__support/RPC/rpc_client.h
+++ b/libc/src/__support/RPC/rpc_client.h
@@ -11,6 +11,8 @@
 
 #include "rpc.h"
 
+#include <gpu/rpc.h>
+
 namespace __llvm_libc {
 namespace rpc {
 

diff  --git a/libc/src/gpu/CMakeLists.txt b/libc/src/gpu/CMakeLists.txt
new file mode 100644
index 0000000000000..8994fe05f7c7e
--- /dev/null
+++ b/libc/src/gpu/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_entrypoint_object(
+  rpc_reset
+  SRCS
+    rpc_reset.cpp
+  HDRS
+    rpc_reset.h
+  DEPENDS
+    libc.src.__support.RPC.rpc_client
+    libc.src.__support.GPU.utils
+)

diff  --git a/libc/src/gpu/rpc_reset.cpp b/libc/src/gpu/rpc_reset.cpp
new file mode 100644
index 0000000000000..949df7ccd4e32
--- /dev/null
+++ b/libc/src/gpu/rpc_reset.cpp
@@ -0,0 +1,25 @@
+//===---------- GPU implementation of the external RPC functionion --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/gpu/rpc_reset.h"
+
+#include "src/__support/GPU/utils.h"
+#include "src/__support/RPC/rpc_client.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// This is the external interface to initialize the RPC client with the
+// shared buffer.
+LLVM_LIBC_FUNCTION(void, rpc_reset,
+                   (unsigned int num_ports, void *rpc_shared_buffer)) {
+  __llvm_libc::rpc::client.reset(num_ports, __llvm_libc::gpu::get_lane_size(),
+                                 rpc_shared_buffer);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/gpu/rpc_reset.h b/libc/src/gpu/rpc_reset.h
new file mode 100644
index 0000000000000..5d6a6632760f8
--- /dev/null
+++ b/libc/src/gpu/rpc_reset.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for RPC functions -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_GPU_RPC_H
+#define LLVM_LIBC_SRC_GPU_RPC_H
+
+namespace __llvm_libc {
+
+void rpc_reset(unsigned int num_ports, void *buffer);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_GPU_RPC_H

diff  --git a/libc/src/stdlib/gpu/free.cpp b/libc/src/stdlib/gpu/free.cpp
index 6889349d52b98..64b60384581f7 100644
--- a/libc/src/stdlib/gpu/free.cpp
+++ b/libc/src/stdlib/gpu/free.cpp
@@ -13,7 +13,7 @@
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(void, free, (void *ptr)) {
-  rpc::Client::Port port = rpc::client.open<rpc::FREE>();
+  rpc::Client::Port port = rpc::client.open<RPC_FREE>();
   port.send([=](rpc::Buffer *buffer) {
     buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
   });

diff  --git a/libc/src/stdlib/gpu/malloc.cpp b/libc/src/stdlib/gpu/malloc.cpp
index e9c37b85e709c..c63b8383f6b16 100644
--- a/libc/src/stdlib/gpu/malloc.cpp
+++ b/libc/src/stdlib/gpu/malloc.cpp
@@ -14,7 +14,7 @@ namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
   void *ptr = nullptr;
-  rpc::Client::Port port = rpc::client.open<rpc::MALLOC>();
+  rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
   port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
                      [&](rpc::Buffer *buffer) {
                        ptr = reinterpret_cast<void *>(buffer->data[0]);

diff  --git a/libc/test/integration/startup/gpu/rpc_interface_test.cpp b/libc/test/integration/startup/gpu/rpc_interface_test.cpp
index b4b03ead31c10..784f183fb3aaf 100644
--- a/libc/test/integration/startup/gpu/rpc_interface_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_interface_test.cpp
@@ -16,7 +16,7 @@ using namespace __llvm_libc;
 // as long as they are mirrored.
 static void test_interface(bool end_with_send) {
   uint64_t cnt = 0;
-  rpc::Client::Port port = rpc::client.open<rpc::TEST_INTERFACE>();
+  rpc::Client::Port port = rpc::client.open<RPC_TEST_INTERFACE>();
   port.send([&](rpc::Buffer *buffer) { buffer->data[0] = end_with_send; });
   port.send([&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
   port.recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });

diff  --git a/libc/test/integration/startup/gpu/rpc_stream_test.cpp b/libc/test/integration/startup/gpu/rpc_stream_test.cpp
index 442ff415067c3..3b1548649e403 100644
--- a/libc/test/integration/startup/gpu/rpc_stream_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_stream_test.cpp
@@ -33,7 +33,7 @@ static void test_stream() {
 
   inline_memcpy(send_ptr, str, send_size);
   ASSERT_TRUE(inline_memcmp(send_ptr, str, send_size) == 0 && "Data mismatch");
-  rpc::Client::Port port = rpc::client.open<rpc::TEST_STREAM>();
+  rpc::Client::Port port = rpc::client.open<RPC_TEST_STREAM>();
   port.send_n(send_ptr, send_size);
   port.recv_n(&recv_ptr, &recv_size,
               [](uint64_t size) { return malloc(size); });
@@ -76,7 +76,7 @@ static void test_divergent() {
   inline_memcpy(buffer, &data[offset], offset);
   ASSERT_TRUE(inline_memcmp(buffer, &data[offset], offset) == 0 &&
               "Data mismatch");
-  rpc::Client::Port port = rpc::client.open<rpc::TEST_STREAM>();
+  rpc::Client::Port port = rpc::client.open<RPC_TEST_STREAM>();
   port.send_n(buffer, offset);
   inline_memset(buffer, offset, 0);
   port.recv_n(&recv_ptr, &recv_size, [&](uint64_t) { return buffer; });

diff  --git a/libc/test/integration/startup/gpu/rpc_test.cpp b/libc/test/integration/startup/gpu/rpc_test.cpp
index 0f051bd72c975..a95004a010443 100644
--- a/libc/test/integration/startup/gpu/rpc_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_test.cpp
@@ -17,7 +17,7 @@ static void test_add_simple() {
       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 @@ static void test_add_simple() {
 
 // 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();
 }

diff  --git a/libc/utils/gpu/loader/CMakeLists.txt b/libc/utils/gpu/loader/CMakeLists.txt
index a05aa91b9c658..65f346dd0b8ab 100644
--- a/libc/utils/gpu/loader/CMakeLists.txt
+++ b/libc/utils/gpu/loader/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_library(gpu_loader OBJECT Main.cpp)
 target_include_directories(gpu_loader PUBLIC
   ${CMAKE_CURRENT_SOURCE_DIR}
+  ${LIBC_SOURCE_DIR}/include
   ${LIBC_SOURCE_DIR}
 )
 

diff  --git a/libc/utils/gpu/server/CMakeLists.txt b/libc/utils/gpu/server/CMakeLists.txt
index fa006b36ea16c..2d5f9207f94cf 100644
--- a/libc/utils/gpu/server/CMakeLists.txt
+++ b/libc/utils/gpu/server/CMakeLists.txt
@@ -3,4 +3,6 @@ add_library(rpc_server STATIC Server.cpp)
 # Include the RPC implemenation from libc.
 add_dependencies(rpc_server libc.src.__support.RPC.rpc)
 target_include_directories(rpc_server PRIVATE ${LIBC_SOURCE_DIR})
+# TODO: This is for the opcodes, we will copy the file here when installed.
+target_include_directories(rpc_server PUBLIC ${LIBC_SOURCE_DIR}/include)
 target_include_directories(rpc_server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

diff  --git a/libc/utils/gpu/server/Server.cpp b/libc/utils/gpu/server/Server.cpp
index f3ce2fd9b2851..11781b90114cc 100644
--- a/libc/utils/gpu/server/Server.cpp
+++ b/libc/utils/gpu/server/Server.cpp
@@ -101,23 +101,23 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
       return RPC_STATUS_SUCCESS;
 
     switch (port->get_opcode()) {
-    case rpc::Opcode::WRITE_TO_STREAM:
-    case rpc::Opcode::WRITE_TO_STDERR:
-    case rpc::Opcode::WRITE_TO_STDOUT: {
+    case RPC_WRITE_TO_STREAM:
+    case RPC_WRITE_TO_STDERR:
+    case RPC_WRITE_TO_STDOUT: {
       uint64_t sizes[rpc::MAX_LANE_SIZE] = {0};
       void *strs[rpc::MAX_LANE_SIZE] = {nullptr};
       FILE *files[rpc::MAX_LANE_SIZE] = {nullptr};
-      if (port->get_opcode() == rpc::Opcode::WRITE_TO_STREAM)
+      if (port->get_opcode() == RPC_WRITE_TO_STREAM)
         port->recv([&](rpc::Buffer *buffer, uint32_t id) {
           files[id] = reinterpret_cast<FILE *>(buffer->data[0]);
         });
       port->recv_n(strs, sizes, [&](uint64_t size) { return new char[size]; });
       port->send([&](rpc::Buffer *buffer, uint32_t id) {
-        FILE *file = port->get_opcode() == rpc::Opcode::WRITE_TO_STDOUT
-                         ? stdout
-                         : (port->get_opcode() == rpc::Opcode::WRITE_TO_STDERR
-                                ? stderr
-                                : files[id]);
+        FILE *file =
+            port->get_opcode() == RPC_WRITE_TO_STDOUT
+                ? stdout
+                : (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;
       });
@@ -127,20 +127,20 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
       }
       break;
     }
-    case rpc::Opcode::EXIT: {
+    case RPC_EXIT: {
       port->recv([](rpc::Buffer *buffer) {
         exit(reinterpret_cast<uint32_t *>(buffer->data)[0]);
       });
       break;
     }
     // TODO: Move handling of these  test cases to the loader implementation.
-    case rpc::Opcode::TEST_INCREMENT: {
+    case RPC_TEST_INCREMENT: {
       port->recv_and_send([](rpc::Buffer *buffer) {
         reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
       });
       break;
     }
-    case rpc::Opcode::TEST_INTERFACE: {
+    case RPC_TEST_INTERFACE: {
       uint64_t cnt = 0;
       bool end_with_recv;
       port->recv([&](rpc::Buffer *buffer) { end_with_recv = buffer->data[0]; });
@@ -159,7 +159,7 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
             [&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
       break;
     }
-    case rpc::Opcode::TEST_STREAM: {
+    case RPC_TEST_STREAM: {
       uint64_t sizes[rpc::MAX_LANE_SIZE] = {0};
       void *dst[rpc::MAX_LANE_SIZE] = {nullptr};
       port->recv_n(dst, sizes, [](uint64_t size) { return new char[size]; });
@@ -170,7 +170,7 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
       }
       break;
     }
-    case rpc::Opcode::NOOP: {
+    case RPC_NOOP: {
       port->recv([](rpc::Buffer *buffer) {});
       break;
     }

diff  --git a/libc/utils/gpu/server/Server.h b/libc/utils/gpu/server/Server.h
index 5a15371ff77ce..81162ccc164d8 100644
--- a/libc/utils/gpu/server/Server.h
+++ b/libc/utils/gpu/server/Server.h
@@ -11,6 +11,8 @@
 
 #include <stdint.h>
 
+#include "llvm-libc-types/rpc_opcodes_t.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -18,17 +20,6 @@ extern "C" {
 /// The maxium number of ports that can be opened for any server.
 const uint64_t RPC_MAXIMUM_PORT_COUNT = 64;
 
-// TODO: Move these to a header exported by the C library.
-typedef enum : uint16_t {
-  RPC_NOOP = 0,
-  RPC_EXIT = 1,
-  RPC_WRITE_TO_STDOUT = 2,
-  RPC_WRITE_TO_STDERR = 3,
-  RPC_WRITE_TO_STREAM = 4,
-  RPC_MALLOC = 5,
-  RPC_FREE = 6,
-} rpc_opcode_t;
-
 /// status codes.
 typedef enum {
   RPC_STATUS_SUCCESS = 0x0,


        


More information about the libc-commits mailing list