Skip to content

[libc] Implement the 'rename' function on the GPU #109814

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 24, 2024
Merged

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Sep 24, 2024

Summary:
Straightforward implementation like the other stdio.h functions.

Summary:
Straightforward implementation like the other `stdio.h` functions.
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2024

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

Summary:
Straightforward implementation like the other stdio.h functions.


Full diff: https://github.com/llvm/llvm-project/pull/109814.diff

6 Files Affected:

  • (modified) libc/config/gpu/entrypoints.txt (+1)
  • (modified) libc/docs/gpu/support.rst (+1)
  • (modified) libc/include/llvm-libc-types/rpc_opcodes_t.h (+1)
  • (modified) libc/src/stdio/gpu/CMakeLists.txt (+11)
  • (added) libc/src/stdio/gpu/rename.cpp (+30)
  • (modified) libc/utils/gpu/server/rpc_server.cpp (+18)
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 9fb89e6fd8d28a..b4cfe47f4505fd 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 44c21c7b4c1ff9..9c151a5fbac1f6 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 3b388de6888c5d..1a6c0cd9bc4a14 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 86470b8425e956..9cac42ed71fb76 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 00000000000000..1087228835842e
--- /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<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.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 8708f946b310ee..aa65dfe69c385c 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<uint64_t>(
+          rename(reinterpret_cast<const char *>(oldpath[id]),
+                 reinterpret_cast<const char *>(newpath[id])));
+      delete[] reinterpret_cast<uint8_t *>(oldpath[id]);
+      delete[] reinterpret_cast<uint8_t *>(newpath[id]);
+    });
+    break;
+  }
   case RPC_SYSTEM: {
     uint64_t sizes[lane_size] = {0};
     void *args[lane_size] = {nullptr};

@jhuber6 jhuber6 merged commit fe6a3d4 into llvm:main Sep 24, 2024
8 of 9 checks passed
@jhuber6 jhuber6 deleted the rename branch October 14, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants