Skip to content
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
1 change: 1 addition & 0 deletions libc/include/llvm-libc-types/rpc_opcodes_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef enum {
RPC_WRITE_TO_STREAM,
RPC_WRITE_TO_STDOUT_NEWLINE,
RPC_READ_FROM_STREAM,
RPC_READ_FGETS,
RPC_OPEN_FILE,
RPC_CLOSE_FILE,
RPC_MALLOC,
Expand Down
28 changes: 12 additions & 16 deletions libc/src/stdio/gpu/fgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ LLVM_LIBC_FUNCTION(char *, fgets,
if (count < 1)
return nullptr;

// This implementation is very slow as it makes multiple RPC calls.
unsigned char c = '\0';
int i = 0;
for (; i < count - 1 && c != '\n'; ++i) {
auto r = file::read(stream, &c, 1);
if (r != 1)
break;

str[i] = c;
}

bool has_error = LIBC_NAMESPACE::ferror(stream);
bool has_eof = LIBC_NAMESPACE::feof(stream);

if (has_error || (i == 0 && has_eof))
uint64_t recv_size;
void *buf = nullptr;
rpc::Client::Port port = rpc::client.open<RPC_READ_FGETS>();
port.send([=](rpc::Buffer *buffer) {
buffer->data[0] = count;
buffer->data[1] = file::from_stream(stream);
});
port.recv_n(&buf, &recv_size,
[&](uint64_t) { return reinterpret_cast<void *>(str); });
port.close();

if (recv_size == 0)
return nullptr;

str[i] = '\0';
return str;
}

Expand Down
16 changes: 16 additions & 0 deletions libc/utils/gpu/server/rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ struct Server {
});
break;
}
case RPC_READ_FGETS: {
uint64_t sizes[lane_size] = {0};
void *data[lane_size] = {nullptr};
port->recv([&](rpc::Buffer *buffer, uint32_t id) {
data[id] = new char[buffer->data[0]];
const char *str =
fgets(reinterpret_cast<char *>(data[id]), buffer->data[0],
file::to_stream(buffer->data[1]));
sizes[id] = !str ? 0 : std::strlen(str) + 1;
});
port->send_n(data, sizes);
for (uint32_t id = 0; id < lane_size; ++id)
if (data[id])
delete[] reinterpret_cast<uint8_t *>(data[id]);
break;
}
case RPC_OPEN_FILE: {
uint64_t sizes[lane_size] = {0};
void *paths[lane_size] = {nullptr};
Expand Down