Skip to content

Commit

Permalink
[libc][NFC] Clean up some code in the RPC implementation.
Browse files Browse the repository at this point in the history
Small cleanup of the server code and fixes a constant name not following
the naming convention.

Differential Revision: https://reviews.llvm.org/D150361
  • Loading branch information
jhuber6 committed May 11, 2023
1 parent 9dd2af0 commit 4a2e50e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
4 changes: 2 additions & 2 deletions libc/src/__support/RPC/rpc.h
Expand Up @@ -74,7 +74,7 @@ struct alignas(64) Packet {
// of thumb is that you should have at least as many ports as possible
// concurrent work items on the GPU to mitigate the lack offorward
// progress guarantees on the GPU.
constexpr uint64_t default_port_count = 64;
constexpr uint64_t DEFAULT_PORT_COUNT = 64;

/// A common process used to synchronize communication between a client and a
/// server. The process contains an inbox and an outbox used for signaling
Expand Down Expand Up @@ -111,7 +111,7 @@ template <bool InvertInbox> struct Process {
cpp::Atomic<uint32_t> *outbox;
Packet *packet;

cpp::Atomic<uint32_t> lock[default_port_count] = {0};
cpp::Atomic<uint32_t> lock[DEFAULT_PORT_COUNT] = {0};

/// Initialize the communication channels.
LIBC_INLINE void reset(uint64_t port_count, uint32_t lane_size, void *state) {
Expand Down
63 changes: 25 additions & 38 deletions libc/utils/gpu/loader/Server.h
Expand Up @@ -22,80 +22,67 @@ static __llvm_libc::rpc::Server server;
/// Queries the RPC client at least once and performs server-side work if there
/// are any active requests.
void handle_server() {
using namespace __llvm_libc;

// Continue servicing the client until there is no work left and we return.
for (;;) {
auto port = server.try_open();
if (!port)
return;

switch (port->get_opcode()) {
case __llvm_libc::rpc::Opcode::PRINT_TO_STDERR: {
uint64_t str_size[__llvm_libc::rpc::MAX_LANE_SIZE] = {0};
char *strs[__llvm_libc::rpc::MAX_LANE_SIZE] = {nullptr};
case rpc::Opcode::PRINT_TO_STDERR: {
uint64_t str_size[rpc::MAX_LANE_SIZE] = {0};
char *strs[rpc::MAX_LANE_SIZE] = {nullptr};
port->recv_n([&](uint64_t size, uint32_t id) {
str_size[id] = size;
strs[id] = new char[size];
return strs[id];
});
for (uint64_t i = 0; i < __llvm_libc::rpc::MAX_LANE_SIZE; ++i) {
for (uint64_t i = 0; i < rpc::MAX_LANE_SIZE; ++i) {
if (strs[i]) {
fwrite(strs[i], str_size[i], 1, stderr);
delete[] strs[i];
}
}
break;
}
case __llvm_libc::rpc::Opcode::EXIT: {
port->recv([](__llvm_libc::rpc::Buffer *buffer) {
case rpc::Opcode::EXIT: {
port->recv([](rpc::Buffer *buffer) {
exit(reinterpret_cast<uint32_t *>(buffer->data)[0]);
});
break;
}
case __llvm_libc::rpc::Opcode::TEST_INCREMENT: {
port->recv_and_send([](__llvm_libc::rpc::Buffer *buffer) {
case rpc::Opcode::TEST_INCREMENT: {
port->recv_and_send([](rpc::Buffer *buffer) {
reinterpret_cast<uint64_t *>(buffer->data)[0] += 1;
});
break;
}
case __llvm_libc::rpc::Opcode::TEST_INTERFACE: {
case rpc::Opcode::TEST_INTERFACE: {
uint64_t cnt = 0;
bool end_with_recv;
port->recv([&](__llvm_libc::rpc::Buffer *buffer) {
end_with_recv = buffer->data[0];
});
port->recv(
[&](__llvm_libc::rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](__llvm_libc::rpc::Buffer *buffer) {
buffer->data[0] = cnt = cnt + 1;
});
port->recv(
[&](__llvm_libc::rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](__llvm_libc::rpc::Buffer *buffer) {
buffer->data[0] = cnt = cnt + 1;
});
port->recv(
[&](__llvm_libc::rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->recv(
[&](__llvm_libc::rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](__llvm_libc::rpc::Buffer *buffer) {
buffer->data[0] = cnt = cnt + 1;
});
port->send([&](__llvm_libc::rpc::Buffer *buffer) {
buffer->data[0] = cnt = cnt + 1;
});
port->recv([&](rpc::Buffer *buffer) { end_with_recv = buffer->data[0]; });
port->recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
port->recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
port->recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->send([&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
port->send([&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
if (end_with_recv)
port->recv(
[&](__llvm_libc::rpc::Buffer *buffer) { cnt = buffer->data[0]; });
port->recv([&](rpc::Buffer *buffer) { cnt = buffer->data[0]; });
else
port->send([&](__llvm_libc::rpc::Buffer *buffer) {
buffer->data[0] = cnt = cnt + 1;
});
port->send(
[&](rpc::Buffer *buffer) { buffer->data[0] = cnt = cnt + 1; });
break;
}
default:
port->recv([](__llvm_libc::rpc::Buffer *buffer) {});
port->recv([](rpc::Buffer *buffer) {});
}
port->close();
}
}

#endif

0 comments on commit 4a2e50e

Please sign in to comment.