[libc-commits] [libc] [libc] Provide 'signal.h' header for the GPU (PR #101996)
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Mon Aug 5 07:55:33 PDT 2024
https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/101996
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.
>From 2cf2190dd9066af3818d4a62a27f415ecc1786d3 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 09:51:32 -0500
Subject: [PATCH] [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.
---
libc/config/gpu/entrypoints.txt | 3 ++
libc/config/gpu/headers.txt | 1 +
.../llvm-libc-macros/gpu/CMakeLists.txt | 6 ++++
.../llvm-libc-macros/gpu/signal-macros.h | 24 +++++++++++++++
libc/include/llvm-libc-macros/signal-macros.h | 4 ++-
libc/include/llvm-libc-types/rpc_opcodes_t.h | 1 +
.../llvm-libc-types/struct_sigaction.h | 2 +-
libc/src/signal/gpu/CMakeLists.txt | 11 +++++++
libc/src/signal/gpu/raise.cpp | 30 +++++++++++++++++++
libc/test/src/signal/CMakeLists.txt | 3 +-
libc/utils/gpu/server/rpc_server.cpp | 7 +++++
11 files changed, 89 insertions(+), 3 deletions(-)
create mode 100644 libc/include/llvm-libc-macros/gpu/signal-macros.h
create mode 100644 libc/src/signal/gpu/CMakeLists.txt
create mode 100644 libc/src/signal/gpu/raise.cpp
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 8d29e7e2e253b..e38e366c77c1e 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -228,6 +228,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# wchar.h entrypoints
libc.src.wchar.wctob
+ # signal.h entrypoints
+ libc.src.signal.raise
+
# gpu/rpc.h entrypoints
libc.src.gpu.rpc_host_call
)
diff --git a/libc/config/gpu/headers.txt b/libc/config/gpu/headers.txt
index 1d4038d5eb45a..5a2283afad246 100644
--- a/libc/config/gpu/headers.txt
+++ b/libc/config/gpu/headers.txt
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.assert
libc.include.ctype
libc.include.string
+ libc.include.signal
libc.include.float
libc.include.stdint
libc.include.inttypes
diff --git a/libc/include/llvm-libc-macros/gpu/CMakeLists.txt b/libc/include/llvm-libc-macros/gpu/CMakeLists.txt
index ea08c63c00301..f3ee6af21d218 100644
--- a/libc/include/llvm-libc-macros/gpu/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/gpu/CMakeLists.txt
@@ -3,3 +3,9 @@ add_header(
HDR
time-macros.h
)
+
+add_header(
+ signal_macros
+ HDR
+ signal-macros.h
+)
diff --git a/libc/include/llvm-libc-macros/gpu/signal-macros.h b/libc/include/llvm-libc-macros/gpu/signal-macros.h
new file mode 100644
index 0000000000000..da095bdc8852f
--- /dev/null
+++ b/libc/include/llvm-libc-macros/gpu/signal-macros.h
@@ -0,0 +1,24 @@
+//===-- Definition of GPU signal number macros ----------------------------===//
+//
+// 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_MACROS_GPU_SIGNAL_MACROS_H
+#define LLVM_LIBC_MACROS_GPU_SIGNAL_MACROS_H
+
+#define SIGINT 2
+#define SIGILL 4
+#define SIGABRT 6
+#define SIGFPE 8
+#define SIGSEGV 11
+#define SIGTERM 15
+
+// Max signal number
+#define NSIG 64
+
+#define __NSIGSET_WORDS NSIG
+
+#endif // LLVM_LIBC_MACROS_GPU_SIGNAL_MACROS_H
diff --git a/libc/include/llvm-libc-macros/signal-macros.h b/libc/include/llvm-libc-macros/signal-macros.h
index 7ab605baa54c2..fbe929a0fea25 100644
--- a/libc/include/llvm-libc-macros/signal-macros.h
+++ b/libc/include/llvm-libc-macros/signal-macros.h
@@ -9,8 +9,10 @@
#ifndef LLVM_LIBC_MACROS_SIGNAL_MACROS_H
#define LLVM_LIBC_MACROS_SIGNAL_MACROS_H
-#ifdef __linux__
+#if defined(__linux__)
#include "linux/signal-macros.h"
+#elif defined(__NVPTX__) || defined(__AMDGPU__)
+#include "gpu/signal-macros.h"
#endif
#endif // LLVM_LIBC_MACROS_SIGNAL_MACROS_H
diff --git a/libc/include/llvm-libc-types/rpc_opcodes_t.h b/libc/include/llvm-libc-types/rpc_opcodes_t.h
index 45050e8521f7a..33aa6356c64e9 100644
--- a/libc/include/llvm-libc-types/rpc_opcodes_t.h
+++ b/libc/include/llvm-libc-types/rpc_opcodes_t.h
@@ -38,6 +38,7 @@ typedef enum {
RPC_PRINTF_TO_STDERR_PACKED,
RPC_PRINTF_TO_STREAM_PACKED,
RPC_REMOVE,
+ RPC_RAISE,
RPC_LAST = 0xFFFF,
} rpc_opcode_t;
diff --git a/libc/include/llvm-libc-types/struct_sigaction.h b/libc/include/llvm-libc-types/struct_sigaction.h
index ffce04d0f7e8c..945354105f493 100644
--- a/libc/include/llvm-libc-types/struct_sigaction.h
+++ b/libc/include/llvm-libc-types/struct_sigaction.h
@@ -17,7 +17,7 @@ struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
};
- sigset_t sa_mask;
+ struct sigset_t sa_mask;
int sa_flags;
#ifdef __linux__
// This field is present on linux for most targets.
diff --git a/libc/src/signal/gpu/CMakeLists.txt b/libc/src/signal/gpu/CMakeLists.txt
new file mode 100644
index 0000000000000..1115986f8d947
--- /dev/null
+++ b/libc/src/signal/gpu/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+ raise
+ SRCS
+ raise.cpp
+ HDRS
+ ../raise.h
+ DEPENDS
+ libc.hdr.types.sigset_t
+ libc.src.__support.RPC.rpc_client
+)
+
diff --git a/libc/src/signal/gpu/raise.cpp b/libc/src/signal/gpu/raise.cpp
new file mode 100644
index 0000000000000..07bf04791f004
--- /dev/null
+++ b/libc/src/signal/gpu/raise.cpp
@@ -0,0 +1,30 @@
+//===-- GPU implementation of signal --------------------------------------===//
+//
+// 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/signal/raise.h"
+
+#include "hdr/types/sigset_t.h"
+#include "llvm-libc-types/rpc_opcodes_t.h"
+#include "src/__support/RPC/rpc_client.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, raise, (int sig)) {
+ // We want to first make sure the server is listening before we exit.
+ rpc::Client::Port port = rpc::client.open<RPC_RAISE>();
+ int ret;
+ port.send_and_recv(
+ [=](rpc::Buffer *buf) { buf->data[0] = static_cast<uint64_t>(sig); },
+ [&](rpc::Buffer *buf) { ret = static_cast<int>(buf->data[0]); });
+ port.close();
+ return ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index edbd5c19edab3..ed7c89409b7ac 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -1,6 +1,6 @@
add_custom_target(libc_signal_unittests)
-add_libc_unittest(
+add_libc_test(
raise_test
SUITE
libc_signal_unittests
@@ -9,6 +9,7 @@ add_libc_unittest(
DEPENDS
libc.include.signal
libc.src.signal.raise
+ UNIT_TEST_ONLY # Requires death tests.
)
add_libc_unittest(
diff --git a/libc/utils/gpu/server/rpc_server.cpp b/libc/utils/gpu/server/rpc_server.cpp
index ed23d22f0bc36..f02a8dab17501 100644
--- a/libc/utils/gpu/server/rpc_server.cpp
+++ b/libc/utils/gpu/server/rpc_server.cpp
@@ -23,6 +23,7 @@
#include <algorithm>
#include <atomic>
#include <cstdio>
+#include <csignal>
#include <cstring>
#include <memory>
#include <mutex>
@@ -389,6 +390,12 @@ rpc_status_t handle_server_impl(
});
break;
}
+ case RPC_RAISE: {
+ port->recv_and_send([](rpc::Buffer *buffer) {
+ buffer->data[0] = raise(static_cast<int>(buffer->data[0]));
+ });
+ break;
+ }
case RPC_NOOP: {
port->recv([](rpc::Buffer *) {});
break;
More information about the libc-commits
mailing list