Skip to content

Commit 1d5c218

Browse files
committed
[libc] Provide 'signal.h' header for the GPU
Summary: This header is practically useless, but we provide it mostly for the macros so that applications can compile. I'm only doing this for the `libc++` unittests that want it, and it is part of the C standard technically. I just made an RPC call to do `raise`. Anything more isn't going to work since it'd be way too annoying to make the CPU call into some signal handler the GPU registered.
1 parent 9fea731 commit 1d5c218

File tree

11 files changed

+89
-3
lines changed

11 files changed

+89
-3
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ set(TARGET_LIBC_ENTRYPOINTS
228228
# wchar.h entrypoints
229229
libc.src.wchar.wctob
230230

231+
# signal.h entrypoints
232+
libc.src.signal.raise
233+
231234
# gpu/rpc.h entrypoints
232235
libc.src.gpu.rpc_host_call
233236
)

libc/config/gpu/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
22
libc.include.assert
33
libc.include.ctype
44
libc.include.string
5+
libc.include.signal
56
libc.include.float
67
libc.include.stdint
78
libc.include.inttypes

libc/include/llvm-libc-macros/gpu/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ add_header(
33
HDR
44
time-macros.h
55
)
6+
7+
add_header(
8+
signal_macros
9+
HDR
10+
signal-macros.h
11+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Definition of GPU signal number macros ----------------------------===//
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_MACROS_GPU_SIGNAL_MACROS_H
10+
#define LLVM_LIBC_MACROS_GPU_SIGNAL_MACROS_H
11+
12+
#define SIGINT 2
13+
#define SIGILL 4
14+
#define SIGABRT 6
15+
#define SIGFPE 8
16+
#define SIGSEGV 11
17+
#define SIGTERM 15
18+
19+
// Max signal number
20+
#define NSIG 64
21+
22+
#define __NSIGSET_WORDS NSIG
23+
24+
#endif // LLVM_LIBC_MACROS_GPU_SIGNAL_MACROS_H

libc/include/llvm-libc-macros/signal-macros.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#ifndef LLVM_LIBC_MACROS_SIGNAL_MACROS_H
1010
#define LLVM_LIBC_MACROS_SIGNAL_MACROS_H
1111

12-
#ifdef __linux__
12+
#if defined(__linux__)
1313
#include "linux/signal-macros.h"
14+
#elif defined(__NVPTX__) || defined(__AMDGPU__)
15+
#include "gpu/signal-macros.h"
1416
#endif
1517

1618
#endif // LLVM_LIBC_MACROS_SIGNAL_MACROS_H

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_RAISE,
4142
RPC_LAST = 0xFFFF,
4243
} rpc_opcode_t;
4344

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct sigaction {
1717
void (*sa_handler)(int);
1818
void (*sa_sigaction)(int, siginfo_t *, void *);
1919
};
20-
sigset_t sa_mask;
20+
struct sigset_t sa_mask;
2121
int sa_flags;
2222
#ifdef __linux__
2323
// This field is present on linux for most targets.

libc/src/signal/gpu/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_entrypoint_object(
2+
raise
3+
SRCS
4+
raise.cpp
5+
HDRS
6+
../raise.h
7+
DEPENDS
8+
libc.hdr.types.sigset_t
9+
libc.src.__support.RPC.rpc_client
10+
)
11+

libc/src/signal/gpu/raise.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- GPU implementation of signal --------------------------------------===//
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/signal/raise.h"
10+
11+
#include "hdr/types/sigset_t.h"
12+
#include "llvm-libc-types/rpc_opcodes_t.h"
13+
#include "src/__support/RPC/rpc_client.h"
14+
#include "src/__support/common.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(int, raise, (int sig)) {
20+
// We want to first make sure the server is listening before we exit.
21+
rpc::Client::Port port = rpc::client.open<RPC_RAISE>();
22+
int ret;
23+
port.send_and_recv(
24+
[=](rpc::Buffer *buf) { buf->data[0] = static_cast<uint64_t>(sig); },
25+
[&](rpc::Buffer *buf) { ret = static_cast<int>(buf->data[0]); });
26+
port.close();
27+
return ret;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/signal/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_custom_target(libc_signal_unittests)
22

3-
add_libc_unittest(
3+
add_libc_test(
44
raise_test
55
SUITE
66
libc_signal_unittests
@@ -9,6 +9,7 @@ add_libc_unittest(
99
DEPENDS
1010
libc.include.signal
1111
libc.src.signal.raise
12+
UNIT_TEST_ONLY # Requires death tests.
1213
)
1314

1415
add_libc_unittest(

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "src/stdio/gpu/file.h"
2323
#include <algorithm>
2424
#include <atomic>
25+
#include <csignal>
2526
#include <cstdio>
2627
#include <cstring>
2728
#include <memory>
@@ -389,6 +390,12 @@ rpc_status_t handle_server_impl(
389390
});
390391
break;
391392
}
393+
case RPC_RAISE: {
394+
port->recv_and_send([](rpc::Buffer *buffer) {
395+
buffer->data[0] = raise(static_cast<int>(buffer->data[0]));
396+
});
397+
break;
398+
}
392399
case RPC_NOOP: {
393400
port->recv([](rpc::Buffer *) {});
394401
break;

0 commit comments

Comments
 (0)