Skip to content

Commit fe6a3d4

Browse files
authored
[libc] Implement the 'rename' function on the GPU (#109814)
Summary: Straightforward implementation like the other `stdio.h` functions.
1 parent 7773243 commit fe6a3d4

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ set(TARGET_LIBC_ENTRYPOINTS
240240
libc.src.stdio.putchar
241241
libc.src.stdio.puts
242242
libc.src.stdio.remove
243+
libc.src.stdio.rename
243244
libc.src.stdio.stderr
244245
libc.src.stdio.stdin
245246
libc.src.stdio.stdout

libc/docs/gpu/support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ fputs |check| |check|
240240
fputc |check| |check|
241241
fwrite |check| |check|
242242
remove |check| |check|
243+
rename |check| |check|
243244
putc |check| |check|
244245
printf |check| |check|
245246
vprintf |check| |check|

libc/include/llvm-libc-types/rpc_opcodes_t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef enum {
3838
RPC_PRINTF_TO_STDERR_PACKED,
3939
RPC_PRINTF_TO_STREAM_PACKED,
4040
RPC_REMOVE,
41+
RPC_RENAME,
4142
RPC_SYSTEM,
4243
RPC_LAST = 0xFFFF,
4344
} rpc_opcode_t;

libc/src/stdio/gpu/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ add_entrypoint_object(
294294
.vfprintf_utils
295295
)
296296

297+
add_entrypoint_object(
298+
rename
299+
SRCS
300+
rename.cpp
301+
HDRS
302+
../rename.h
303+
DEPENDS
304+
libc.hdr.types.FILE
305+
.gpu_file
306+
)
307+
297308
add_entrypoint_object(
298309
stdin
299310
SRCS

libc/src/stdio/gpu/rename.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- GPU Implementation of rename --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/rename.h"
10+
#include "src/__support/CPP/string_view.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/stdio/gpu/file.h"
13+
14+
#include "hdr/types/FILE.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
19+
int ret;
20+
rpc::Client::Port port = rpc::client.open<RPC_RENAME>();
21+
port.send_n(oldpath, internal::string_length(oldpath) + 1);
22+
port.send_n(newpath, internal::string_length(newpath) + 1);
23+
port.recv(
24+
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
25+
port.close();
26+
27+
return ret;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,24 @@ rpc_status_t handle_server_impl(
392392
});
393393
break;
394394
}
395+
case RPC_RENAME: {
396+
uint64_t oldsizes[lane_size] = {0};
397+
uint64_t newsizes[lane_size] = {0};
398+
void *oldpath[lane_size] = {nullptr};
399+
void *newpath[lane_size] = {nullptr};
400+
port->recv_n(oldpath, oldsizes,
401+
[&](uint64_t size) { return new char[size]; });
402+
port->recv_n(newpath, newsizes,
403+
[&](uint64_t size) { return new char[size]; });
404+
port->send([&](rpc::Buffer *buffer, uint32_t id) {
405+
buffer->data[0] = static_cast<uint64_t>(
406+
rename(reinterpret_cast<const char *>(oldpath[id]),
407+
reinterpret_cast<const char *>(newpath[id])));
408+
delete[] reinterpret_cast<uint8_t *>(oldpath[id]);
409+
delete[] reinterpret_cast<uint8_t *>(newpath[id]);
410+
});
411+
break;
412+
}
395413
case RPC_SYSTEM: {
396414
uint64_t sizes[lane_size] = {0};
397415
void *args[lane_size] = {nullptr};

0 commit comments

Comments
 (0)