Skip to content

Commit 0b9d816

Browse files
authored
[libc] Remove separate RPC test handling (#160206)
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.
1 parent 28d68f9 commit 0b9d816

File tree

8 files changed

+62
-88
lines changed

8 files changed

+62
-88
lines changed

libc/include/llvm-libc-types/test_rpc_opcodes_t.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

libc/shared/rpc_opcodes.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "rpc.h"
1313

1414
#define LLVM_LIBC_RPC_BASE 'c'
15-
#define LLVM_LIBC_OPCODE(n) (LLVM_LIBC_RPC_BASE << 24 | n)
15+
#define LLVM_LIBC_OPCODE(n) (((LLVM_LIBC_RPC_BASE << 24) | n))
1616

1717
typedef enum {
1818
LIBC_NOOP = LLVM_LIBC_OPCODE(0),
@@ -45,6 +45,11 @@ typedef enum {
4545
LIBC_REMOVE = LLVM_LIBC_OPCODE(27),
4646
LIBC_RENAME = LLVM_LIBC_OPCODE(28),
4747
LIBC_SYSTEM = LLVM_LIBC_OPCODE(29),
48+
49+
// Internal opcodes for testing.
50+
LIBC_TEST_INCREMENT = LLVM_LIBC_OPCODE(1 << 15),
51+
LIBC_TEST_INTERFACE = LLVM_LIBC_OPCODE((1 << 15) + 1),
52+
LIBC_TEST_STREAM = LLVM_LIBC_OPCODE((1 << 15) + 2),
4853
LIBC_LAST = 0xFFFFFFFF,
4954
} rpc_opcode_t;
5055

libc/src/__support/RPC/rpc_server.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,55 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
517517
});
518518
break;
519519
}
520+
case LIBC_TEST_INCREMENT: {
521+
port.recv_and_send([](rpc::Buffer *buffer, uint32_t) {
522+
reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
523+
});
524+
break;
525+
}
526+
case LIBC_TEST_INTERFACE: {
527+
bool end_with_recv;
528+
uint64_t cnt;
529+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
530+
end_with_recv = buffer->data[0];
531+
});
532+
port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
533+
port.send([&](rpc::Buffer *buffer, uint32_t) {
534+
buffer->data[0] = cnt = cnt + 1;
535+
});
536+
port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
537+
port.send([&](rpc::Buffer *buffer, uint32_t) {
538+
buffer->data[0] = cnt = cnt + 1;
539+
});
540+
port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
541+
port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
542+
port.send([&](rpc::Buffer *buffer, uint32_t) {
543+
buffer->data[0] = cnt = cnt + 1;
544+
});
545+
port.send([&](rpc::Buffer *buffer, uint32_t) {
546+
buffer->data[0] = cnt = cnt + 1;
547+
});
548+
if (end_with_recv)
549+
port.recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
550+
else
551+
port.send([&](rpc::Buffer *buffer, uint32_t) {
552+
buffer->data[0] = cnt = cnt + 1;
553+
});
554+
555+
break;
556+
}
557+
case LIBC_TEST_STREAM: {
558+
uint64_t sizes[num_lanes] = {0};
559+
void *dst[num_lanes] = {nullptr};
560+
port.recv_n(dst, sizes,
561+
[](uint64_t size) -> void * { return new char[size]; });
562+
port.send_n(dst, sizes);
563+
for (uint64_t i = 0; i < num_lanes; ++i) {
564+
if (dst[i])
565+
delete[] reinterpret_cast<uint8_t *>(dst[i]);
566+
}
567+
break;
568+
}
520569
case LIBC_NOOP: {
521570
port.recv([](rpc::Buffer *, uint32_t) {});
522571
break;

libc/test/integration/startup/gpu/rpc_interface_test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
109
#include "src/__support/GPU/utils.h"
1110
#include "src/__support/RPC/rpc_client.h"
1211
#include "test/IntegrationTest/test.h"
@@ -18,7 +17,7 @@ using namespace LIBC_NAMESPACE;
1817
static void test_interface(bool end_with_send) {
1918
uint64_t cnt = 0;
2019
LIBC_NAMESPACE::rpc::Client::Port port =
21-
LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INTERFACE>();
20+
LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INTERFACE>();
2221
port.send([&](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
2322
buffer->data[0] = end_with_send;
2423
});

libc/test/integration/startup/gpu/rpc_lane_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
109
#include "src/__support/GPU/utils.h"
1110
#include "src/__support/RPC/rpc_client.h"
1211
#include "test/IntegrationTest/test.h"
@@ -16,7 +15,7 @@ using namespace LIBC_NAMESPACE;
1615
static void test_add() {
1716
uint64_t cnt = gpu::get_lane_id();
1817
LIBC_NAMESPACE::rpc::Client::Port port =
19-
LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INCREMENT>();
18+
LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INCREMENT>();
2019
port.send_and_recv(
2120
[=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
2221
reinterpret_cast<uint64_t *>(buffer->data)[0] = cnt;
@@ -29,7 +28,7 @@ static void test_add() {
2928
EXPECT_EQ(gpu::get_thread_id(), gpu::get_lane_id());
3029
}
3130

32-
TEST_MAIN(int argc, char **argv, char **envp) {
31+
TEST_MAIN(int, char **, char **) {
3332
test_add();
3433

3534
return 0;

libc/test/integration/startup/gpu/rpc_stream_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
109
#include "src/__support/GPU/utils.h"
1110
#include "src/__support/RPC/rpc_client.h"
1211
#include "src/__support/integer_to_string.h"
@@ -36,7 +35,7 @@ static void test_stream() {
3635
inline_memcpy(send_ptr, str, send_size);
3736
ASSERT_TRUE(inline_memcmp(send_ptr, str, send_size) == 0 && "Data mismatch");
3837
LIBC_NAMESPACE::rpc::Client::Port port =
39-
LIBC_NAMESPACE::rpc::client.open<RPC_TEST_STREAM>();
38+
LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_STREAM>();
4039
port.send_n(send_ptr, send_size);
4140
port.recv_n(&recv_ptr, &recv_size,
4241
[](uint64_t size) { return malloc(size); });
@@ -80,7 +79,7 @@ static void test_divergent() {
8079
ASSERT_TRUE(inline_memcmp(buffer, &data[offset], offset) == 0 &&
8180
"Data mismatch");
8281
LIBC_NAMESPACE::rpc::Client::Port port =
83-
LIBC_NAMESPACE::rpc::client.open<RPC_TEST_STREAM>();
82+
LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_STREAM>();
8483
port.send_n(buffer, offset);
8584
inline_memset(buffer, 0, offset);
8685
port.recv_n(&recv_ptr, &recv_size, [&](uint64_t) { return buffer; });
@@ -91,7 +90,7 @@ static void test_divergent() {
9190
ASSERT_TRUE(recv_size == offset && "Data size mismatch");
9291
}
9392

94-
TEST_MAIN(int argc, char **argv, char **envp) {
93+
TEST_MAIN(int, char **, char **) {
9594
test_stream();
9695
test_divergent();
9796

libc/test/integration/startup/gpu/rpc_test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
109
#include "src/__support/GPU/utils.h"
1110
#include "src/__support/RPC/rpc_client.h"
1211
#include "test/IntegrationTest/test.h"
@@ -19,7 +18,7 @@ static void test_add_simple() {
1918
uint64_t cnt = 0;
2019
for (uint32_t i = 0; i < num_additions; ++i) {
2120
LIBC_NAMESPACE::rpc::Client::Port port =
22-
LIBC_NAMESPACE::rpc::client.open<RPC_TEST_INCREMENT>();
21+
LIBC_NAMESPACE::rpc::client.open<LIBC_TEST_INCREMENT>();
2322
port.send_and_recv(
2423
[=](LIBC_NAMESPACE::rpc::Buffer *buffer, uint32_t) {
2524
reinterpret_cast<uint64_t *>(buffer->data)[0] = cnt;

llvm/tools/llvm-gpu-loader/server.h

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <cstddef>
1313
#include <cstdint>
1414

15-
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
16-
1715
#include "shared/rpc.h"
1816
#include "shared/rpc_opcodes.h"
1917
#include "shared/rpc_server.h"
@@ -28,59 +26,6 @@ inline uint32_t handle_server(rpc::Server &server, uint32_t index,
2826

2927
int status = rpc::RPC_SUCCESS;
3028
switch (port->get_opcode()) {
31-
case RPC_TEST_INCREMENT: {
32-
port->recv_and_send([](rpc::Buffer *buffer, uint32_t) {
33-
reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
34-
});
35-
break;
36-
}
37-
case RPC_TEST_INTERFACE: {
38-
bool end_with_recv;
39-
uint64_t cnt;
40-
port->recv([&](rpc::Buffer *buffer, uint32_t) {
41-
end_with_recv = buffer->data[0];
42-
});
43-
port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
44-
port->send([&](rpc::Buffer *buffer, uint32_t) {
45-
buffer->data[0] = cnt = cnt + 1;
46-
});
47-
port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
48-
port->send([&](rpc::Buffer *buffer, uint32_t) {
49-
buffer->data[0] = cnt = cnt + 1;
50-
});
51-
port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
52-
port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
53-
port->send([&](rpc::Buffer *buffer, uint32_t) {
54-
buffer->data[0] = cnt = cnt + 1;
55-
});
56-
port->send([&](rpc::Buffer *buffer, uint32_t) {
57-
buffer->data[0] = cnt = cnt + 1;
58-
});
59-
if (end_with_recv)
60-
port->recv([&](rpc::Buffer *buffer, uint32_t) { cnt = buffer->data[0]; });
61-
else
62-
port->send([&](rpc::Buffer *buffer, uint32_t) {
63-
buffer->data[0] = cnt = cnt + 1;
64-
});
65-
66-
break;
67-
}
68-
case RPC_TEST_STREAM: {
69-
uint64_t sizes[num_lanes] = {0};
70-
void *dst[num_lanes] = {nullptr};
71-
port->recv_n(dst, sizes,
72-
[](uint64_t size) -> void * { return new char[size]; });
73-
port->send_n(dst, sizes);
74-
for (uint64_t i = 0; i < num_lanes; ++i) {
75-
if (dst[i])
76-
delete[] reinterpret_cast<uint8_t *>(dst[i]);
77-
}
78-
break;
79-
}
80-
case RPC_TEST_NOOP: {
81-
port->recv([&](rpc::Buffer *, uint32_t) {});
82-
break;
83-
}
8429
case LIBC_MALLOC: {
8530
port->recv_and_send([&](rpc::Buffer *buffer, uint32_t) {
8631
buffer->data[0] = reinterpret_cast<uintptr_t>(alloc(buffer->data[0]));

0 commit comments

Comments
 (0)