Skip to content

Commit 07102a1

Browse files
committed
[libc] Implement the 'abort' function on the GPU
This function implements the `abort` function on the GPU. The implementation here closely mirros the `exit` call where we first synchornize with the RPC server to make sure it's listening and then we exit on the GPU. I was unsure if this should be a simple `__builtin_assert` on the GPU. I elected to go with an RPC approach to make this a more "true" `abort` call. That is, it should invoke some signal handlers and exit with the proper code according to the implemented C library on the server. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D159210
1 parent 8f469bf commit 07102a1

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ set(TARGET_LIBC_ENTRYPOINTS
6565
libc.src.stdlib.strtoll
6666
libc.src.stdlib.strtoul
6767
libc.src.stdlib.strtoull
68-
69-
# stdlib.h entrypoints
7068
libc.src.stdlib._Exit
7169
libc.src.stdlib.atexit
7270
libc.src.stdlib.exit
71+
libc.src.stdlib.abort
7372

7473
# Only implemented in the test suite
7574
libc.src.stdlib.malloc

libc/docs/gpu/support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ atof |check|
9090
atol |check|
9191
atoll |check|
9292
exit |check| |check|
93+
abort |check| |check|
9394
labs |check|
9495
llabs |check|
9596
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
@@ -22,6 +22,7 @@ typedef enum : unsigned short {
2222
RPC_MALLOC = 9,
2323
RPC_FREE = 10,
2424
RPC_HOST_CALL = 11,
25+
RPC_ABORT = 12,
2526
} rpc_opcode_t;
2627

2728
#endif // __LLVM_LIBC_TYPES_RPC_OPCODE_H__

libc/src/stdlib/gpu/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ add_entrypoint_object(
1919
libc.include.stdlib
2020
libc.src.__support.RPC.rpc_client
2121
)
22+
23+
add_entrypoint_object(
24+
abort
25+
SRCS
26+
abort.cpp
27+
HDRS
28+
../abort.h
29+
DEPENDS
30+
libc.include.stdlib
31+
libc.src.__support.RPC.rpc_client
32+
)

libc/src/stdlib/gpu/abort.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- GPU implementation of abort ---------------------------------------===//
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+
12+
#include "src/stdlib/abort.h"
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(void, abort, ()) {
17+
// We want to first make sure the server is listening before we abort.
18+
rpc::Client::Port port = rpc::client.open<RPC_ABORT>();
19+
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
20+
port.send([&](rpc::Buffer *) {});
21+
port.close();
22+
23+
#if defined(LIBC_TARGET_ARCH_IS_NVPTX)
24+
LIBC_INLINE_ASM("exit;" ::: "memory");
25+
#elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
26+
__builtin_amdgcn_endpgm();
27+
#endif
28+
__builtin_unreachable();
29+
}
30+
31+
} // namespace __llvm_libc

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ struct Server {
145145
});
146146
break;
147147
}
148+
case RPC_ABORT: {
149+
// Send a response to the client to signal that we are ready to abort.
150+
port->recv_and_send([](rpc::Buffer *) {});
151+
port->recv([](rpc::Buffer *) {});
152+
abort();
153+
break;
154+
}
148155
case RPC_HOST_CALL: {
149156
uint64_t sizes[lane_size] = {0};
150157
void *args[lane_size] = {nullptr};

0 commit comments

Comments
 (0)