Skip to content

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Sep 22, 2025

Summary:
This was originally kept separate so it didn't pollute the name space,
but now I'm thinking it's just easier to bundle it in with the default
interface. This means that we'll have a bit of extra code for people
using the server.h file to handle libc opcodes, but it's minimal (3
functions) and it simplifies this.

I'm doing this because I'm hoping to move the GPU tester binary to
liboffload which handles libc opcodes internally except these. This is
the easier option compared to adding a hook to register custom handlers
there.

Summary:
This was originally kept separate so it didn't pollute the name space,
but now I'm thinking it's just easier to bundle it in with the default
interface. This means that we'll have a bit of extra code for people
using the server.h file to handle libc opcodes, but it's minimal (3
functions) and it simplifies this.

I'm doing this because I'm hoping to move the GPU tester binary to
liboffload which handles `libc` opcodes internally except these. This is
the easier option compared to adding a hook to register custom handlers
there.
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2025

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

Summary:
This was originally kept separate so it didn't pollute the name space,
but now I'm thinking it's just easier to bundle it in with the default
interface. This means that we'll have a bit of extra code for people
using the server.h file to handle libc opcodes, but it's minimal (3
functions) and it simplifies this.

I'm doing this because I'm hoping to move the GPU tester binary to
liboffload which handles libc opcodes internally except these. This is
the easier option compared to adding a hook to register custom handlers
there.


Full diff: https://github.com/llvm/llvm-project/pull/160206.diff

8 Files Affected:

  • (removed) libc/include/llvm-libc-types/test_rpc_opcodes_t.h (-21)
  • (modified) libc/shared/rpc_opcodes.h (+6-1)
  • (modified) libc/src/__support/RPC/rpc_server.h (+49)
  • (modified) libc/test/integration/startup/gpu/rpc_interface_test.cpp (+1-2)
  • (modified) libc/test/integration/startup/gpu/rpc_lane_test.cpp (+2-3)
  • (modified) libc/test/integration/startup/gpu/rpc_stream_test.cpp (+3-4)
  • (modified) libc/test/integration/startup/gpu/rpc_test.cpp (+1-2)
  • (modified) llvm/tools/llvm-gpu-loader/server.h (-55)
diff --git a/libc/include/llvm-libc-types/test_rpc_opcodes_t.h b/libc/include/llvm-libc-types/test_rpc_opcodes_t.h
deleted file mode 100644
index 7129768dc8b98..0000000000000
--- a/libc/include/llvm-libc-types/test_rpc_opcodes_t.h
+++ /dev/null
@@ -1,21 +0,0 @@
-//===-- Definition of RPC opcodes used for internal tests -----------------===//
-//
-// 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_TEST_RPC_OPCODES_T_H
-#define LLVM_LIBC_TYPES_TEST_RPC_OPCODES_T_H
-
-// We consider the first 32768 opcodes as reserved for libc purposes. We allow
-// extensions to use any other number without conflicting with anything else.
-typedef enum : unsigned short {
-  RPC_TEST_NOOP = 1 << 15,
-  RPC_TEST_INCREMENT,
-  RPC_TEST_INTERFACE,
-  RPC_TEST_STREAM,
-} rpc_test_opcode_t;
-
-#endif // LLVM_LIBC_TYPES_TEST_RPC_OPCODES_T_H
diff --git a/libc/shared/rpc_opcodes.h b/libc/shared/rpc_opcodes.h
index 6de41cd1899e7..583d622e1fa0d 100644
--- a/libc/shared/rpc_opcodes.h
+++ b/libc/shared/rpc_opcodes.h
@@ -12,7 +12,7 @@
 #include "rpc.h"
 
 #define LLVM_LIBC_RPC_BASE 'c'
-#define LLVM_LIBC_OPCODE(n) (LLVM_LIBC_RPC_BASE << 24 | n)
+#define LLVM_LIBC_OPCODE(n) (((LLVM_LIBC_RPC_BASE << 24) | n))
 
 typedef enum {
   LIBC_NOOP = LLVM_LIBC_OPCODE(0),
@@ -45,6 +45,11 @@ typedef enum {
   LIBC_REMOVE = LLVM_LIBC_OPCODE(27),
   LIBC_RENAME = LLVM_LIBC_OPCODE(28),
   LIBC_SYSTEM = LLVM_LIBC_OPCODE(29),
+
+  // Internal opcodes for testing.
+  LIBC_TEST_INCREMENT = LLVM_LIBC_OPCODE(1 << 15),
+  LIBC_TEST_INTERFACE = LLVM_LIBC_OPCODE((1 << 15) + 1),
+  LIBC_TEST_STREAM = LLVM_LIBC_OPCODE((1 << 15) + 2),
   LIBC_LAST = 0xFFFFFFFF,
 } rpc_opcode_t;
 
diff --git a/libc/src/__support/RPC/rpc_server.h b/libc/src/__support/RPC/rpc_server.h
index beea4dd975ea7..4bd8a93526e3a 100644
--- a/libc/src/__support/RPC/rpc_server.h
+++ b/libc/src/__support/RPC/rpc_server.h
@@ -517,6 +517,55 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
     });
     break;
   }
+  case LIBC_TEST_INCREMENT: {
+    port.recv_and_send([](rpc::Buffer *buffer, uint32_t) {
+      reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
+    });
+    break;
+  }
+  case LIBC_TEST_INTERFACE: {
+    bool end_with_recv;
+    uint64_t cnt;
+    port.recv([&](rpc::Buffer *buffer, uint32_t) {
+      end_with_recv = buffer->data[0];
+    });
+    port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
+    port.send([&](rpc::Buffer *buffer, uint32_t) {
+      buffer->data[0] = cnt = cnt + 1;
+    });
+    port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
+    port.send([&](rpc::Buffer *buffer, uint32_t) {
+      buffer->data[0] = cnt = cnt + 1;
+    });
+    port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
+    port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
+    port.send([&](rpc::Buffer *buffer, uint32_t) {
+      buffer->data[0] = cnt = cnt + 1;
+    });
+    port.send([&](rpc::Buffer *buffer, uint32_t) {
+      buffer->data[0] = cnt = cnt + 1;
+    });
+    if (end_with_recv)
+      port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
+    else
+      port.send([&](rpc::Buffer *buffer, uint32_t) {
+        buffer->data[0] = cnt = cnt + 1;
+      });
+
+    break;
+  }
+  case LIBC_TEST_STREAM: {
+    uint64_t sizes[num_lanes] = {0};
+    void *dst[num_lanes] = {nullptr};
+    port.recv_n(dst, sizes,
+                [](uint64_t size) -> void * { return new char[size]; });
+    port.send_n(dst, sizes);
+    for (uint64_t i = 0; i < num_lanes; ++i) {
+      if (dst[i])
+        delete[] reinterpret_cast<uint8_t *>(dst[i]);
+    }
+    break;
+  }
   case LIBC_NOOP: {
     port.recv([](rpc::Buffer *, uint32_t) {});
     break;
diff --git a/libc/test/integration/startup/gpu/rpc_interface_test.cpp b/libc/test/integration/startup/gpu/rpc_interface_test.cpp
index b05ffb92699bf..8957b15b780a7 100644
--- a/libc/test/integration/startup/gpu/rpc_interface_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_interface_test.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
 #include "src/__support/GPU/utils.h"
 #include "src/__support/RPC/rpc_client.h"
 #include "test/IntegrationTest/test.h"
@@ -18,7 +17,7 @@ using namespace LIBC_NAMESPACE;
 static void test_interface(bool end_with_send) {
   uint64_t cnt = 0;
   LIBC_NAMESPACE::rpc::Client::Port port =
-      LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INTERFACE>();
+      LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INTERFACE>();
   port.send([&](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
     buffer->data[0] = end_with_send;
   });
diff --git a/libc/test/integration/startup/gpu/rpc_lane_test.cpp b/libc/test/integration/startup/gpu/rpc_lane_test.cpp
index 72c7109eecfd6..e80b438829b44 100644
--- a/libc/test/integration/startup/gpu/rpc_lane_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_lane_test.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
 #include "src/__support/GPU/utils.h"
 #include "src/__support/RPC/rpc_client.h"
 #include "test/IntegrationTest/test.h"
@@ -16,7 +15,7 @@ using namespace LIBC_NAMESPACE;
 static void test_add() {
   uint64_t cnt = gpu::get_lane_id();
   LIBC_NAMESPACE::rpc::Client::Port port =
-      LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INCREMENT>();
+      LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INCREMENT>();
   port.send_and_recv(
       [=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
         reinterpret_cast<uint64_t *>(buffer->data)[0] = cnt;
@@ -29,7 +28,7 @@ static void test_add() {
   EXPECT_EQ(gpu::get_thread_id(), gpu::get_lane_id());
 }
 
-TEST_MAIN(int argc, char **argv, char **envp) {
+TEST_MAIN(int, char **, char **) {
   test_add();
 
   return 0;
diff --git a/libc/test/integration/startup/gpu/rpc_stream_test.cpp b/libc/test/integration/startup/gpu/rpc_stream_test.cpp
index 3e81328629145..b8c37926d2d42 100644
--- a/libc/test/integration/startup/gpu/rpc_stream_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_stream_test.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
 #include "src/__support/GPU/utils.h"
 #include "src/__support/RPC/rpc_client.h"
 #include "src/__support/integer_to_string.h"
@@ -36,7 +35,7 @@ static void test_stream() {
   inline_memcpy(send_ptr, str, send_size);
   ASSERT_TRUE(inline_memcmp(send_ptr, str, send_size) == 0 && "Data mismatch");
   LIBC_NAMESPACE::rpc::Client::Port port =
-      LIBC_NAMESPACE::rpc::client.open<RPC_TEST_STREAM>();
+      LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_STREAM>();
   port.send_n(send_ptr, send_size);
   port.recv_n(&recv_ptr, &recv_size,
               [](uint64_t size) { return malloc(size); });
@@ -80,7 +79,7 @@ static void test_divergent() {
   ASSERT_TRUE(inline_memcmp(buffer, &data[offset], offset) == 0 &&
               "Data mismatch");
   LIBC_NAMESPACE::rpc::Client::Port port =
-      LIBC_NAMESPACE::rpc::client.open<RPC_TEST_STREAM>();
+      LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_STREAM>();
   port.send_n(buffer, offset);
   inline_memset(buffer, 0, offset);
   port.recv_n(&recv_ptr, &recv_size, [&](uint64_t) { return buffer; });
@@ -91,7 +90,7 @@ static void test_divergent() {
   ASSERT_TRUE(recv_size == offset && "Data size mismatch");
 }
 
-TEST_MAIN(int argc, char **argv, char **envp) {
+TEST_MAIN(int, char **, char **) {
   test_stream();
   test_divergent();
 
diff --git a/libc/test/integration/startup/gpu/rpc_test.cpp b/libc/test/integration/startup/gpu/rpc_test.cpp
index d20f39600598f..d46a1adf28570 100644
--- a/libc/test/integration/startup/gpu/rpc_test.cpp
+++ b/libc/test/integration/startup/gpu/rpc_test.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
 #include "src/__support/GPU/utils.h"
 #include "src/__support/RPC/rpc_client.h"
 #include "test/IntegrationTest/test.h"
@@ -19,7 +18,7 @@ static void test_add_simple() {
   uint64_t cnt = 0;
   for (uint32_t i = 0; i < num_additions; ++i) {
     LIBC_NAMESPACE::rpc::Client::Port port =
-        LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INCREMENT>();
+        LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INCREMENT>();
     port.send_and_recv(
         [=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
           reinterpret_cast<uint64_t *>(buffer->data)[0] = cnt;
diff --git a/llvm/tools/llvm-gpu-loader/server.h b/llvm/tools/llvm-gpu-loader/server.h
index bc54b4b74915a..da73cc007f5d5 100644
--- a/llvm/tools/llvm-gpu-loader/server.h
+++ b/llvm/tools/llvm-gpu-loader/server.h
@@ -12,8 +12,6 @@
 #include <cstddef>
 #include <cstdint>
 
-#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
-
 #include "shared/rpc.h"
 #include "shared/rpc_opcodes.h"
 #include "shared/rpc_server.h"
@@ -28,59 +26,6 @@ inline uint32_t handle_server(rpc::Server &server, uint32_t index,
 
   int status = rpc::RPC_SUCCESS;
   switch (port->get_opcode()) {
-  case RPC_TEST_INCREMENT: {
-    port->recv_and_send([](rpc::Buffer *buffer, uint32_t) {
-      reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
-    });
-    break;
-  }
-  case RPC_TEST_INTERFACE: {
-    bool end_with_recv;
-    uint64_t cnt;
-    port->recv([&](rpc::Buffer *buffer, uint32_t) {
-      end_with_recv = buffer->data[0];
-    });
-    port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
-    port->send([&](rpc::Buffer *buffer, uint32_t) {
-      buffer->data[0] = cnt = cnt + 1;
-    });
-    port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
-    port->send([&](rpc::Buffer *buffer, uint32_t) {
-      buffer->data[0] = cnt = cnt + 1;
-    });
-    port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
-    port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
-    port->send([&](rpc::Buffer *buffer, uint32_t) {
-      buffer->data[0] = cnt = cnt + 1;
-    });
-    port->send([&](rpc::Buffer *buffer, uint32_t) {
-      buffer->data[0] = cnt = cnt + 1;
-    });
-    if (end_with_recv)
-      port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
-    else
-      port->send([&](rpc::Buffer *buffer, uint32_t) {
-        buffer->data[0] = cnt = cnt + 1;
-      });
-
-    break;
-  }
-  case RPC_TEST_STREAM: {
-    uint64_t sizes[num_lanes] = {0};
-    void *dst[num_lanes] = {nullptr};
-    port->recv_n(dst, sizes,
-                 [](uint64_t size) -> void * { return new char[size]; });
-    port->send_n(dst, sizes);
-    for (uint64_t i = 0; i < num_lanes; ++i) {
-      if (dst[i])
-        delete[] reinterpret_cast<uint8_t *>(dst[i]);
-    }
-    break;
-  }
-  case RPC_TEST_NOOP: {
-    port->recv([&](rpc::Buffer *, uint32_t) {});
-    break;
-  }
   case LIBC_MALLOC: {
     port->recv_and_send([&](rpc::Buffer *buffer, uint32_t) {
       buffer->data[0] = reinterpret_cast<uintptr_t>(alloc(buffer->data[0]));

@jhuber6 jhuber6 merged commit 0b9d816 into llvm:main Sep 23, 2025
21 checks passed
@jhuber6 jhuber6 deleted the rpc_tests branch September 23, 2025 00:44
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 23, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-windows running on linaro-armv8-windows-msvc-05 while building libc,llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/11734

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/3/12 (2299 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/4/12 (2300 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/5/12 (2301 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/6/12 (2302 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/7/12 (2303 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/8/12 (2304 of 2308)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/9/12 (2305 of 2308)
PASS: lldb-unit :: tools/lldb-server/tests/./LLDBServerTests.exe/0/2 (2306 of 2308)
PASS: lldb-unit :: tools/lldb-server/tests/./LLDBServerTests.exe/1/2 (2307 of 2308)
TIMEOUT: lldb-unit :: Host/./HostTests.exe/1/10 (2308 of 2308)
******************** TEST 'lldb-unit :: Host/./HostTests.exe/1/10' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\tools\lldb\unittests\Host\.\HostTests.exe-lldb-unit-15756-1-10.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=10 GTEST_SHARD_INDEX=1 C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\tools\lldb\unittests\Host\.\HostTests.exe
--

Note: This is test shard 2 of 10.

[==========] Running 8 tests from 7 test suites.

[----------] Global test environment set-up.

[----------] 1 test from ConnectionFileDescriptorTest

[ RUN      ] ConnectionFileDescriptorTest.TCPGetURIv6

[       OK ] ConnectionFileDescriptorTest.TCPGetURIv6 (2 ms)

[----------] 1 test from ConnectionFileDescriptorTest (2 ms total)



[----------] 1 test from FileSystemTest

[ RUN      ] FileSystemTest.MakeAbsolute

[       OK ] FileSystemTest.MakeAbsolute (0 ms)

[----------] 1 test from FileSystemTest (0 ms total)



[----------] 1 test from HostInfoTestInitialization

[ RUN      ] HostInfoTestInitialization.InitTwice

[       OK ] HostInfoTestInitialization.InitTwice (1 ms)

[----------] 1 test from HostInfoTestInitialization (1 ms total)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants