Skip to content

[libc] Add GPU support for the 'system' function #109687

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 23, 2024
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
1 change: 1 addition & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.at_quick_exit
libc.src.stdlib.quick_exit
libc.src.stdlib.getenv
libc.src.stdlib.system

# TODO: Implement these correctly
libc.src.stdlib.aligned_alloc
Expand Down
1 change: 1 addition & 0 deletions libc/docs/gpu/support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ atol |check|
atoll |check|
exit |check| |check|
abort |check| |check|
system |check| |check|
labs |check|
llabs |check|
div |check|
Expand Down
1 change: 1 addition & 0 deletions libc/include/llvm-libc-types/rpc_opcodes_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef enum {
RPC_PRINTF_TO_STDERR_PACKED,
RPC_PRINTF_TO_STREAM_PACKED,
RPC_REMOVE,
RPC_SYSTEM,
RPC_LAST = 0xFFFF,
} rpc_opcode_t;

Expand Down
6 changes: 6 additions & 0 deletions libc/newhdrgen/yaml/stdlib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,9 @@ functions:
- type: char **__restrict
- type: int
- type: locale_t
- name: system
standards:
- stdc
return_type: int
arguments:
- type: const char *
2 changes: 2 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,8 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,

FunctionSpec<"system", RetValSpec<IntType>, [ArgSpec<ConstCharPtr>]>,
]
>;

Expand Down
7 changes: 7 additions & 0 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,10 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.abort
)

add_entrypoint_object(
system
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.system
)
13 changes: 12 additions & 1 deletion libc/src/stdlib/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,16 @@ add_entrypoint_object(
../abort.h
DEPENDS
libc.include.stdlib
libc.src.__support.GPU.allocator
libc.src.__support.RPC.rpc_client
)

add_entrypoint_object(
system
SRCS
system.cpp
HDRS
../system.h
DEPENDS
libc.include.stdlib
libc.src.__support.RPC.rpc_client
)
29 changes: 29 additions & 0 deletions libc/src/stdlib/gpu/system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- GPU implementation of system --------------------------------------===//
//
// 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/__support/RPC/rpc_client.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/string_utils.h"

#include "src/stdlib/system.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, system, (const char *command)) {
int ret;
rpc::Client::Port port = rpc::client.open<RPC_SYSTEM>();
port.send_n(command, internal::string_length(command) + 1);
port.recv(
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
port.close();

return ret;
}

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/stdlib/system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for system ------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDLIB_SYSTEM_H
#define LLVM_LIBC_SRC_STDLIB_SYSTEM_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int system(const char *command);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDLIB_SYSTEM_H
11 changes: 11 additions & 0 deletions libc/utils/gpu/server/rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ rpc_status_t handle_server_impl(
});
break;
}
case RPC_SYSTEM: {
uint64_t sizes[lane_size] = {0};
void *args[lane_size] = {nullptr};
port->recv_n(args, sizes, [&](uint64_t size) { return new char[size]; });
port->send([&](rpc::Buffer *buffer, uint32_t id) {
buffer->data[0] = static_cast<uint64_t>(
system(reinterpret_cast<const char *>(args[id])));
delete[] reinterpret_cast<uint8_t *>(args[id]);
});
break;
}
case RPC_NOOP: {
port->recv([](rpc::Buffer *) {});
break;
Expand Down
Loading