Skip to content

[libc][NFC] Cleanup the GPU file I/O utility header #65680

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
Sep 8, 2023
Merged
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
69 changes: 21 additions & 48 deletions libc/src/stdio/gpu/file.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- GPU helper functions--------------------===//
//===--- GPU helper functions for file I/O using RPC ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -14,35 +14,17 @@
namespace __llvm_libc {
namespace file {

LIBC_INLINE uint64_t write_to_stdout(const void *data, size_t size) {
template <uint16_t opcode>
LIBC_INLINE uint64_t write_impl(::FILE *file, const void *data, size_t size) {
uint64_t ret = 0;
rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDOUT>();
port.send_n(data, size);
port.recv([&](rpc::Buffer *buffer) {
ret = reinterpret_cast<uint64_t *>(buffer->data)[0];
});
port.close();
return ret;
}
rpc::Client::Port port = rpc::client.open<opcode>();

LIBC_INLINE uint64_t write_to_stderr(const void *data, size_t size) {
uint64_t ret = 0;
rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDERR>();
port.send_n(data, size);
port.recv([&](rpc::Buffer *buffer) {
ret = reinterpret_cast<uint64_t *>(buffer->data)[0];
});
port.close();
return ret;
}
if constexpr (opcode == RPC_WRITE_TO_STREAM) {
port.send([&](rpc::Buffer *buffer) {
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
});
}

LIBC_INLINE uint64_t write_to_stream(uintptr_t file, const void *data,
size_t size) {
uint64_t ret = 0;
rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STREAM>();
port.send([&](rpc::Buffer *buffer) {
reinterpret_cast<uintptr_t *>(buffer->data)[0] = file;
});
port.send_n(data, size);
port.recv([&](rpc::Buffer *buffer) {
ret = reinterpret_cast<uint64_t *>(buffer->data)[0];
Expand All @@ -51,45 +33,36 @@ LIBC_INLINE uint64_t write_to_stream(uintptr_t file, const void *data,
return ret;
}

LIBC_INLINE uint64_t write(FILE *f, const void *data, size_t size) {
LIBC_INLINE uint64_t write(::FILE *f, const void *data, size_t size) {
if (f == stdout)
return write_to_stdout(data, size);
return write_impl<RPC_WRITE_TO_STDOUT>(f, data, size);
else if (f == stderr)
return write_to_stderr(data, size);
return write_impl<RPC_WRITE_TO_STDERR>(f, data, size);
else
return write_to_stream(reinterpret_cast<uintptr_t>(f), data, size);
}

LIBC_INLINE uint64_t read_from_stdin(void *buf, size_t size) {
uint64_t ret = 0;
uint64_t recv_size;
rpc::Client::Port port = rpc::client.open<RPC_READ_FROM_STDIN>();
port.send([=](rpc::Buffer *buffer) { buffer->data[0] = size; });
port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; });
port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
port.close();
return ret;
return write_impl<RPC_WRITE_TO_STREAM>(f, data, size);
}

LIBC_INLINE uint64_t read_from_stream(uintptr_t file, void *buf, size_t size) {
template <uint16_t opcode>
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>();
rpc::Client::Port port = rpc::client.open<opcode>();
port.send([=](rpc::Buffer *buffer) {
buffer->data[0] = size;
buffer->data[1] = file;
if constexpr (opcode == RPC_READ_FROM_STREAM)
buffer->data[1] = reinterpret_cast<uintptr_t>(file);
});
port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; });
port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
port.close();
return ret;
}

LIBC_INLINE uint64_t read(FILE *f, void *data, size_t size) {
LIBC_INLINE uint64_t read(::FILE *f, void *data, size_t size) {
if (f == stdin)
return read_from_stdin(data, size);
return read_from_stream<RPC_READ_FROM_STDIN>(f, data, size);
else
return read_from_stream(reinterpret_cast<uintptr_t>(f), data, size);
return read_from_stream<RPC_READ_FROM_STREAM>(f, data, size);
}

} // namespace file
Expand Down