Skip to content

Commit 16d11e2

Browse files
authored
[libc] Add GPU support for the 'system' function (#109687)
Summary: This function can easily be implemented by forwarding it to the host process. This shows up in a few places that we might want to test the GPU so it should be provided. Also, I find the idea of the GPU offloading work to the CPU via `system` very funny.
1 parent e44ecf7 commit 16d11e2

File tree

10 files changed

+90
-1
lines changed

10 files changed

+90
-1
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ set(TARGET_LIBC_ENTRYPOINTS
191191
libc.src.stdlib.at_quick_exit
192192
libc.src.stdlib.quick_exit
193193
libc.src.stdlib.getenv
194+
libc.src.stdlib.system
194195

195196
# TODO: Implement these correctly
196197
libc.src.stdlib.aligned_alloc

libc/docs/gpu/support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ atol |check|
176176
atoll |check|
177177
exit |check| |check|
178178
abort |check| |check|
179+
system |check| |check|
179180
labs |check|
180181
llabs |check|
181182
div |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_SYSTEM,
4142
RPC_LAST = 0xFFFF,
4243
} rpc_opcode_t;
4344

libc/newhdrgen/yaml/stdlib.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,9 @@ functions:
333333
- type: char **__restrict
334334
- type: int
335335
- type: locale_t
336+
- name: system
337+
standards:
338+
- stdc
339+
return_type: int
340+
arguments:
341+
- type: const char *

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,8 @@ def StdC : StandardSpec<"stdc"> {
13401340
FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
13411341
FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
13421342
FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
1343+
1344+
FunctionSpec<"system", RetValSpec<IntType>, [ArgSpec<ConstCharPtr>]>,
13431345
]
13441346
>;
13451347

libc/src/stdlib/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,10 @@ add_entrypoint_object(
621621
DEPENDS
622622
.${LIBC_TARGET_OS}.abort
623623
)
624+
625+
add_entrypoint_object(
626+
system
627+
ALIAS
628+
DEPENDS
629+
.${LIBC_TARGET_OS}.system
630+
)

libc/src/stdlib/gpu/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,16 @@ add_entrypoint_object(
6161
../abort.h
6262
DEPENDS
6363
libc.include.stdlib
64-
libc.src.__support.GPU.allocator
64+
libc.src.__support.RPC.rpc_client
65+
)
66+
67+
add_entrypoint_object(
68+
system
69+
SRCS
70+
system.cpp
71+
HDRS
72+
../system.h
73+
DEPENDS
74+
libc.include.stdlib
75+
libc.src.__support.RPC.rpc_client
6576
)

libc/src/stdlib/gpu/system.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- GPU implementation of system --------------------------------------===//
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/__support/RPC/rpc_client.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/string/string_utils.h"
13+
14+
#include "src/stdlib/system.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, system, (const char *command)) {
19+
int ret;
20+
rpc::Client::Port port = rpc::client.open<RPC_SYSTEM>();
21+
port.send_n(command, internal::string_length(command) + 1);
22+
port.recv(
23+
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
24+
port.close();
25+
26+
return ret;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/system.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for system ------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC_STDLIB_SYSTEM_H
10+
#define LLVM_LIBC_SRC_STDLIB_SYSTEM_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int system(const char *command);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_STDLIB_SYSTEM_H

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ rpc_status_t handle_server_impl(
392392
});
393393
break;
394394
}
395+
case RPC_SYSTEM: {
396+
uint64_t sizes[lane_size] = {0};
397+
void *args[lane_size] = {nullptr};
398+
port->recv_n(args, sizes, [&](uint64_t size) { return new char[size]; });
399+
port->send([&](rpc::Buffer *buffer, uint32_t id) {
400+
buffer->data[0] = static_cast<uint64_t>(
401+
system(reinterpret_cast<const char *>(args[id])));
402+
delete[] reinterpret_cast<uint8_t *>(args[id]);
403+
});
404+
break;
405+
}
395406
case RPC_NOOP: {
396407
port->recv([](rpc::Buffer *) {});
397408
break;

0 commit comments

Comments
 (0)