Skip to content

[libc] Remove dependency on cpp::function in rpc.h #112422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions libc/src/__support/GPU/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ namespace {
void *rpc_allocate(uint64_t size) {
void *ptr = nullptr;
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]);
});
port.send_and_recv(
[=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = size; },
[&](rpc::Buffer *buffer, uint32_t) {
ptr = reinterpret_cast<void *>(buffer->data[0]);
});
port.close();
return ptr;
}

void rpc_free(void *ptr) {
rpc::Client::Port port = rpc::client.open<RPC_FREE>();
port.send([=](rpc::Buffer *buffer) {
port.send([=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
});
port.close();
Expand Down
5 changes: 3 additions & 2 deletions libc/src/__support/OSUtil/gpu/exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ namespace internal {
[[noreturn]] void exit(int status) {
// We want to first make sure the server is listening before we exit.
rpc::Client::Port port = rpc::client.open<RPC_EXIT>();
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
port.send([&](rpc::Buffer *buffer) {
port.send_and_recv([](rpc::Buffer *, uint32_t) {},
[](rpc::Buffer *, uint32_t) {});
port.send([&](rpc::Buffer *buffer, uint32_t) {
reinterpret_cast<uint32_t *>(buffer->data)[0] = status;
});
port.close();
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/gpu/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
void write_to_stderr(cpp::string_view msg) {
rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDERR>();
port.send_n(msg.data(), msg.size());
port.recv([](rpc::Buffer *) { /* void */ });
port.recv([](rpc::Buffer *, uint32_t) { /* void */ });
port.close();
}

Expand Down
22 changes: 4 additions & 18 deletions libc/src/__support/RPC/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "rpc_util.h"
#include "src/__support/CPP/algorithm.h" // max
#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/functional.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/GPU/utils.h"
#include "src/__support/macros/config.h"
Expand Down Expand Up @@ -266,22 +265,9 @@ template <bool Invert> struct Process {
};

/// Invokes a function accross every active buffer across the total lane size.
static LIBC_INLINE void invoke_rpc(cpp::function<void(Buffer *)> fn,
uint32_t lane_size, uint64_t lane_mask,
Buffer *slot) {
if constexpr (is_process_gpu()) {
fn(&slot[gpu::get_lane_id()]);
} else {
for (uint32_t i = 0; i < lane_size; i += gpu::get_lane_size())
if (lane_mask & (1ul << i))
fn(&slot[i]);
}
}

/// Alternate version that also provides the index of the current lane.
static LIBC_INLINE void invoke_rpc(cpp::function<void(Buffer *, uint32_t)> fn,
uint32_t lane_size, uint64_t lane_mask,
Buffer *slot) {
template <typename F>
LIBC_INLINE static void invoke_rpc(F &&fn, uint32_t lane_size,
uint64_t lane_mask, Buffer *slot) {
if constexpr (is_process_gpu()) {
fn(&slot[gpu::get_lane_id()], gpu::get_lane_id());
} else {
Expand Down Expand Up @@ -444,7 +430,7 @@ template <bool T>
template <typename W>
LIBC_INLINE void Port<T>::recv_and_send(W work) {
recv(work);
send([](Buffer *) { /* no-op */ });
send([](Buffer *, uint32_t) { /* no-op */ });
}

/// Helper routine to simplify the interface when sending from the GPU using
Expand Down
4 changes: 2 additions & 2 deletions libc/src/gpu/rpc_host_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ LLVM_LIBC_FUNCTION(unsigned long long, rpc_host_call,
(void *fn, void *data, size_t size)) {
rpc::Client::Port port = rpc::client.open<RPC_HOST_CALL>();
port.send_n(data, size);
port.send([=](rpc::Buffer *buffer) {
port.send([=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = reinterpret_cast<uintptr_t>(fn);
});
unsigned long long ret;
port.recv([&](rpc::Buffer *buffer) {
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<unsigned long long>(buffer->data[0]);
});
port.close();
Expand Down
6 changes: 4 additions & 2 deletions libc/src/stdio/gpu/clearerr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void, clearerr, (::FILE * stream)) {
rpc::Client::Port port = rpc::client.open<RPC_CLEARERR>();
port.send_and_recv(
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
[&](rpc::Buffer *) {});
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
},
[&](rpc::Buffer *, uint32_t) {});
port.close();
}

Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdio/gpu/fclose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ LLVM_LIBC_FUNCTION(int, fclose, (::FILE * stream)) {
uint64_t ret = 0;
uintptr_t file = reinterpret_cast<uintptr_t>(stream);
rpc::Client::Port port = rpc::client.open<RPC_CLOSE_FILE>();
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = file; },
[&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
port.send_and_recv(
[=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = file; },
[&](rpc::Buffer *buffer, uint32_t) { ret = buffer->data[0]; });
port.close();

if (ret != 0)
Expand Down
8 changes: 6 additions & 2 deletions libc/src/stdio/gpu/feof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, feof, (::FILE * stream)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_FEOF>();
port.send_and_recv(
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
},
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
8 changes: 6 additions & 2 deletions libc/src/stdio/gpu/ferror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, ferror, (::FILE * stream)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_FERROR>();
port.send_and_recv(
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
},
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
8 changes: 6 additions & 2 deletions libc/src/stdio/gpu/fflush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_FFLUSH>();
port.send_and_recv(
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
},
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/gpu/fgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LLVM_LIBC_FUNCTION(char *, fgets,
uint64_t recv_size;
void *buf = nullptr;
rpc::Client::Port port = rpc::client.open<RPC_READ_FGETS>();
port.send([=](rpc::Buffer *buffer) {
port.send([=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = count;
buffer->data[1] = file::from_stream(stream);
});
Expand Down
8 changes: 4 additions & 4 deletions libc/src/stdio/gpu/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ LIBC_INLINE uint64_t write_impl(::FILE *file, const void *data, size_t size) {
rpc::Client::Port port = rpc::client.open<opcode>();

if constexpr (opcode == RPC_WRITE_TO_STREAM) {
port.send([&](rpc::Buffer *buffer) {
port.send([&](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
});
}

port.send_n(data, size);
port.recv([&](rpc::Buffer *buffer) {
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = reinterpret_cast<uint64_t *>(buffer->data)[0];
});
port.close();
Expand All @@ -81,12 +81,12 @@ LIBC_INLINE uint64_t read_from_stream(::FILE *file, void *buf, size_t size) {
uint64_t ret = 0;
uint64_t recv_size;
rpc::Client::Port port = rpc::client.open<RPC_READ_FROM_STREAM>();
port.send([=](rpc::Buffer *buffer) {
port.send([=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = size;
buffer->data[1] = from_stream(file);
});
port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; });
port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
port.recv([&](rpc::Buffer *buffer, uint32_t) { ret = buffer->data[0]; });
port.close();
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions libc/src/stdio/gpu/fopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ LLVM_LIBC_FUNCTION(::FILE *, fopen,
rpc::Client::Port port = rpc::client.open<RPC_OPEN_FILE>();
port.send_n(path, internal::string_length(path) + 1);
port.send_and_recv(
[=](rpc::Buffer *buffer) {
[=](rpc::Buffer *buffer, uint32_t) {
inline_memcpy(buffer->data, mode, internal::string_length(mode) + 1);
},
[&](rpc::Buffer *buffer) { file = buffer->data[0]; });
[&](rpc::Buffer *buffer, uint32_t) { file = buffer->data[0]; });
port.close();

return reinterpret_cast<FILE *>(file);
Expand Down
6 changes: 4 additions & 2 deletions libc/src/stdio/gpu/fseek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ LLVM_LIBC_FUNCTION(int, fseek, (::FILE * stream, long offset, int whence)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_FSEEK>();
port.send_and_recv(
[=](rpc::Buffer *buffer) {
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
buffer->data[1] = static_cast<uint64_t>(offset);
buffer->data[2] = static_cast<uint64_t>(whence);
},
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
8 changes: 6 additions & 2 deletions libc/src/stdio/gpu/ftell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(long, ftell, (::FILE * stream)) {
long ret;
rpc::Client::Port port = rpc::client.open<RPC_FSEEK>();
port.send_and_recv(
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
[&](rpc::Buffer *buffer) { ret = static_cast<long>(buffer->data[0]); });
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = file::from_stream(stream);
},
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<long>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdio/gpu/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ LLVM_LIBC_FUNCTION(int, remove, (const char *path)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_REMOVE>();
port.send_n(path, internal::string_length(path) + 1);
port.recv(
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdio/gpu/rename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
rpc::Client::Port port = rpc::client.open<RPC_RENAME>();
port.send_n(oldpath, internal::string_length(oldpath) + 1);
port.send_n(newpath, internal::string_length(newpath) + 1);
port.recv(
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();

return ret;
Expand Down
6 changes: 4 additions & 2 deletions libc/src/stdio/gpu/ungetc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ LLVM_LIBC_FUNCTION(int, ungetc, (int c, ::FILE *stream)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_UNGETC>();
port.send_and_recv(
[=](rpc::Buffer *buffer) {
[=](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = c;
buffer->data[1] = file::from_stream(stream);
},
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
[&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();
return ret;
}
Expand Down
6 changes: 3 additions & 3 deletions libc/src/stdio/gpu/vfprintf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ LIBC_INLINE int vfprintf_impl(::FILE *__restrict file,

if constexpr (opcode == RPC_PRINTF_TO_STREAM ||
opcode == RPC_PRINTF_TO_STREAM_PACKED) {
port.send([&](rpc::Buffer *buffer) {
port.send([&](rpc::Buffer *buffer, uint32_t) {
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
});
}

size_t args_size = 0;
port.send_n(format, format_size);
port.recv([&](rpc::Buffer *buffer) {
port.recv([&](rpc::Buffer *buffer, uint32_t) {
args_size = static_cast<size_t>(buffer->data[0]);
});
port.send_n(vlist, args_size);

uint32_t ret = 0;
for (;;) {
const char *str = nullptr;
port.recv([&](rpc::Buffer *buffer) {
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<uint32_t>(buffer->data[0]);
str = reinterpret_cast<const char *>(buffer->data[1]);
});
Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdlib/gpu/abort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void, abort, ()) {
// We want to first make sure the server is listening before we abort.
rpc::Client::Port port = rpc::client.open<RPC_ABORT>();
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
port.send([&](rpc::Buffer *) {});
port.send_and_recv([](rpc::Buffer *, uint32_t) {},
[](rpc::Buffer *, uint32_t) {});
port.send([&](rpc::Buffer *, uint32_t) {});
port.close();

gpu::end_program();
Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdlib/gpu/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ LLVM_LIBC_FUNCTION(int, system, (const char *command)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_SYSTEM>();
port.send_n(command, internal::string_length(command) + 1);
port.recv(
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
port.recv([&](rpc::Buffer *buffer, uint32_t) {
ret = static_cast<int>(buffer->data[0]);
});
port.close();

return ret;
Expand Down
Loading
Loading