From 49052d0bac78ff2e77701f56ccd97a231f0bdc80 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Mon, 22 Sep 2025 16:32:42 -0500 Subject: [PATCH] [libc] Remove separate RPC test handling 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. --- .../llvm-libc-types/test_rpc_opcodes_t.h | 21 ------- libc/shared/rpc_opcodes.h | 7 ++- libc/src/__support/RPC/rpc_server.h | 49 +++++++++++++++++ .../startup/gpu/rpc_interface_test.cpp | 3 +- .../integration/startup/gpu/rpc_lane_test.cpp | 5 +- .../startup/gpu/rpc_stream_test.cpp | 7 +-- .../test/integration/startup/gpu/rpc_test.cpp | 3 +- llvm/tools/llvm-gpu-loader/server.h | 55 ------------------- 8 files changed, 62 insertions(+), 88 deletions(-) delete mode 100644 libc/include/llvm-libc-types/test_rpc_opcodes_t.h 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(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(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(); + LIBC_NAMESPACE::rpc::client.open(); 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(); + LIBC_NAMESPACE::rpc::client.open(); port.send_and_recv( [=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) { reinterpret_cast(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(); + LIBC_NAMESPACE::rpc::client.open(); 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(); + LIBC_NAMESPACE::rpc::client.open(); 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(); + LIBC_NAMESPACE::rpc::client.open(); port.send_and_recv( [=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) { reinterpret_cast(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 #include -#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(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(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(alloc(buffer->data[0]));