Skip to content

[libc] Change the puts implementation on the GPU #67189

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 25, 2023
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
21 changes: 11 additions & 10 deletions libc/include/llvm-libc-types/rpc_opcodes_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ typedef enum : unsigned short {
RPC_WRITE_TO_STDOUT = 2,
RPC_WRITE_TO_STDERR = 3,
RPC_WRITE_TO_STREAM = 4,
RPC_READ_FROM_STREAM = 5,
RPC_OPEN_FILE = 6,
RPC_CLOSE_FILE = 7,
RPC_MALLOC = 8,
RPC_FREE = 9,
RPC_HOST_CALL = 10,
RPC_ABORT = 11,
RPC_FEOF = 12,
RPC_FERROR = 13,
RPC_CLEARERR = 14,
RPC_WRITE_TO_STDOUT_NEWLINE = 5,
RPC_READ_FROM_STREAM = 6,
RPC_OPEN_FILE = 7,
RPC_CLOSE_FILE = 8,
RPC_MALLOC = 9,
RPC_FREE = 10,
RPC_HOST_CALL = 11,
RPC_ABORT = 12,
RPC_FEOF = 13,
RPC_FERROR = 14,
RPC_CLEARERR = 15,
} rpc_opcode_t;

#endif // __LLVM_LIBC_TYPES_RPC_OPCODE_H__
8 changes: 3 additions & 5 deletions libc/src/stdio/gpu/puts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, puts, (const char *__restrict str)) {
cpp::string_view str_view(str);
auto written = file::write(stdout, str, str_view.size());
if (written != str_view.size())
return EOF;
written = file::write(stdout, "\n", 1);
if (written != 1)
auto written = file::write_impl<RPC_WRITE_TO_STDOUT_NEWLINE>(stdout, str,
str_view.size());
if (written != str_view.size() + 1)
return EOF;
return 0;
}
Expand Down
22 changes: 13 additions & 9 deletions libc/utils/gpu/server/rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,27 @@ struct Server {
switch (port->get_opcode()) {
case RPC_WRITE_TO_STREAM:
case RPC_WRITE_TO_STDERR:
case RPC_WRITE_TO_STDOUT: {
case RPC_WRITE_TO_STDOUT:
case RPC_WRITE_TO_STDOUT_NEWLINE: {
uint64_t sizes[lane_size] = {0};
void *strs[lane_size] = {nullptr};
FILE *files[lane_size] = {nullptr};
if (port->get_opcode() == RPC_WRITE_TO_STREAM)
if (port->get_opcode() == RPC_WRITE_TO_STREAM) {
port->recv([&](rpc::Buffer *buffer, uint32_t id) {
files[id] = reinterpret_cast<FILE *>(buffer->data[0]);
});
} else if (port->get_opcode() == RPC_WRITE_TO_STDERR) {
std::fill(files, files + lane_size, stderr);
} else {
std::fill(files, files + lane_size, stdout);
}

port->recv_n(strs, sizes, [&](uint64_t size) { return new char[size]; });
port->send([&](rpc::Buffer *buffer, uint32_t id) {
FILE *file =
port->get_opcode() == RPC_WRITE_TO_STDOUT
? stdout
: (port->get_opcode() == RPC_WRITE_TO_STDERR ? stderr
: files[id]);
uint64_t ret = fwrite(strs[id], 1, sizes[id], file);
std::memcpy(buffer->data, &ret, sizeof(uint64_t));
buffer->data[0] = fwrite(strs[id], 1, sizes[id], files[id]);
if (port->get_opcode() == RPC_WRITE_TO_STDOUT_NEWLINE &&
buffer->data[0] == sizes[id])
buffer->data[0] += fwrite("\n", 1, 1, files[id]);
delete[] reinterpret_cast<uint8_t *>(strs[id]);
});
break;
Expand Down
2 changes: 1 addition & 1 deletion openmp/libomptarget/test/libc/puts.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ int main() {
// CHECK: PASS
#pragma omp target teams num_teams(4)
#pragma omp parallel num_threads(2)
{ fputs("PASS\n", stdout); }
{ puts("PASS\n"); }
}