From 2fae79b4da48c8d531c14b6749bb6cb2c794dad0 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Tue, 24 Sep 2024 10:12:44 -0500 Subject: [PATCH] [libc] Implement the 'rename' function on the GPU Summary: Straightforward implementation like the other `stdio.h` functions. --- libc/config/gpu/entrypoints.txt | 1 + libc/docs/gpu/support.rst | 1 + libc/include/llvm-libc-types/rpc_opcodes_t.h | 1 + libc/src/stdio/gpu/CMakeLists.txt | 11 +++++++ libc/src/stdio/gpu/rename.cpp | 30 ++++++++++++++++++++ libc/utils/gpu/server/rpc_server.cpp | 18 ++++++++++++ 6 files changed, 62 insertions(+) create mode 100644 libc/src/stdio/gpu/rename.cpp diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt index 9fb89e6fd8d28..b4cfe47f4505f 100644 --- a/libc/config/gpu/entrypoints.txt +++ b/libc/config/gpu/entrypoints.txt @@ -240,6 +240,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdio.putchar libc.src.stdio.puts libc.src.stdio.remove + libc.src.stdio.rename libc.src.stdio.stderr libc.src.stdio.stdin libc.src.stdio.stdout diff --git a/libc/docs/gpu/support.rst b/libc/docs/gpu/support.rst index 44c21c7b4c1ff..9c151a5fbac1f 100644 --- a/libc/docs/gpu/support.rst +++ b/libc/docs/gpu/support.rst @@ -240,6 +240,7 @@ fputs |check| |check| fputc |check| |check| fwrite |check| |check| remove |check| |check| +rename |check| |check| putc |check| |check| printf |check| |check| vprintf |check| |check| diff --git a/libc/include/llvm-libc-types/rpc_opcodes_t.h b/libc/include/llvm-libc-types/rpc_opcodes_t.h index 3b388de6888c5..1a6c0cd9bc4a1 100644 --- a/libc/include/llvm-libc-types/rpc_opcodes_t.h +++ b/libc/include/llvm-libc-types/rpc_opcodes_t.h @@ -38,6 +38,7 @@ typedef enum { RPC_PRINTF_TO_STDERR_PACKED, RPC_PRINTF_TO_STREAM_PACKED, RPC_REMOVE, + RPC_RENAME, RPC_SYSTEM, RPC_LAST = 0xFFFF, } rpc_opcode_t; diff --git a/libc/src/stdio/gpu/CMakeLists.txt b/libc/src/stdio/gpu/CMakeLists.txt index 86470b8425e95..9cac42ed71fb7 100644 --- a/libc/src/stdio/gpu/CMakeLists.txt +++ b/libc/src/stdio/gpu/CMakeLists.txt @@ -294,6 +294,17 @@ add_entrypoint_object( .vfprintf_utils ) +add_entrypoint_object( + rename + SRCS + rename.cpp + HDRS + ../rename.h + DEPENDS + libc.hdr.types.FILE + .gpu_file +) + add_entrypoint_object( stdin SRCS diff --git a/libc/src/stdio/gpu/rename.cpp b/libc/src/stdio/gpu/rename.cpp new file mode 100644 index 0000000000000..1087228835842 --- /dev/null +++ b/libc/src/stdio/gpu/rename.cpp @@ -0,0 +1,30 @@ +//===-- GPU Implementation of rename --------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/rename.h" +#include "src/__support/CPP/string_view.h" +#include "src/__support/macros/config.h" +#include "src/stdio/gpu/file.h" + +#include "hdr/types/FILE.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) { + int ret; + rpc::Client::Port port = rpc::client.open(); + 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(buffer->data[0]); }); + port.close(); + + return ret; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/utils/gpu/server/rpc_server.cpp b/libc/utils/gpu/server/rpc_server.cpp index 8708f946b310e..aa65dfe69c385 100644 --- a/libc/utils/gpu/server/rpc_server.cpp +++ b/libc/utils/gpu/server/rpc_server.cpp @@ -392,6 +392,24 @@ rpc_status_t handle_server_impl( }); break; } + case RPC_RENAME: { + uint64_t oldsizes[lane_size] = {0}; + uint64_t newsizes[lane_size] = {0}; + void *oldpath[lane_size] = {nullptr}; + void *newpath[lane_size] = {nullptr}; + port->recv_n(oldpath, oldsizes, + [&](uint64_t size) { return new char[size]; }); + port->recv_n(newpath, newsizes, + [&](uint64_t size) { return new char[size]; }); + port->send([&](rpc::Buffer *buffer, uint32_t id) { + buffer->data[0] = static_cast( + rename(reinterpret_cast(oldpath[id]), + reinterpret_cast(newpath[id]))); + delete[] reinterpret_cast(oldpath[id]); + delete[] reinterpret_cast(newpath[id]); + }); + break; + } case RPC_SYSTEM: { uint64_t sizes[lane_size] = {0}; void *args[lane_size] = {nullptr};