Skip to content

[libc] Provide 'signal.h' header for the GPU #101996

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
Aug 5, 2024
Merged

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Aug 5, 2024

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.

@llvmbot llvmbot added the libc label Aug 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/101996.diff

11 Files Affected:

  • (modified) libc/config/gpu/entrypoints.txt (+3)
  • (modified) libc/config/gpu/headers.txt (+1)
  • (modified) libc/include/llvm-libc-macros/gpu/CMakeLists.txt (+6)
  • (added) libc/include/llvm-libc-macros/gpu/signal-macros.h (+24)
  • (modified) libc/include/llvm-libc-macros/signal-macros.h (+3-1)
  • (modified) libc/include/llvm-libc-types/rpc_opcodes_t.h (+1)
  • (modified) libc/include/llvm-libc-types/struct_sigaction.h (+1-1)
  • (added) libc/src/signal/gpu/CMakeLists.txt (+11)
  • (added) libc/src/signal/gpu/raise.cpp (+30)
  • (modified) libc/test/src/signal/CMakeLists.txt (+2-1)
  • (modified) libc/utils/gpu/server/rpc_server.cpp (+7)
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;

Copy link

github-actions bot commented Aug 5, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@jhuber6 jhuber6 force-pushed the signal branch 2 times, most recently from 1d5c218 to cdd198e Compare August 5, 2024 15:47
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.
@jhuber6 jhuber6 merged commit bde5123 into llvm:main Aug 5, 2024
4 of 5 checks passed
@jhuber6 jhuber6 deleted the signal branch October 14, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants