[libc-commits] [libc] dc9baac - [libc] Partially implement `atexit` on the GPU
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Wed Apr 26 14:52:49 PDT 2023
Author: Joseph Huber
Date: 2023-04-26T16:52:32-05:00
New Revision: dc9baac1a0148ce0c2b8afdc75e3e208597f79c9
URL: https://github.com/llvm/llvm-project/commit/dc9baac1a0148ce0c2b8afdc75e3e208597f79c9
DIFF: https://github.com/llvm/llvm-project/commit/dc9baac1a0148ce0c2b8afdc75e3e208597f79c9.diff
LOG: [libc] Partially implement `atexit` on the GPU
The `atexit` function controls registering functions to call at the end
of the program. This is difficult to do in general on the GPU because of
the lack of a real mutex implementation. We primarily provide this for
testing where we can explicitly restrict how the `atexit` registration
functions are called. So we simply create a passthrough Mutex to get
past the usage of it as per @sivachandra's suggestion.
Depends on D149225
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D149226
Added:
libc/src/__support/threads/gpu/CMakeLists.txt
libc/src/__support/threads/gpu/mutex.h
Modified:
libc/config/gpu/api.td
libc/config/gpu/entrypoints.txt
libc/src/__support/threads/mutex.h
Removed:
################################################################################
diff --git a/libc/config/gpu/api.td b/libc/config/gpu/api.td
index 9036bbc89238..3e6927de9463 100644
--- a/libc/config/gpu/api.td
+++ b/libc/config/gpu/api.td
@@ -5,3 +5,10 @@ include "spec/stdc.td"
def StringAPI : PublicAPI<"string.h"> {
let Types = ["size_t"];
}
+
+def StdlibAPI : PublicAPI<"stdlib.h"> {
+ let Types = [
+ "size_t",
+ "__atexithandler_t",
+ ];
+}
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index d42a554577e6..c5ed1e3dd476 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -56,9 +56,11 @@ set(TARGET_LIBC_ENTRYPOINTS
# stdlib.h entrypoints
libc.src.stdlib.atoi
+ libc.src.stdlib.atexit
# Only implemented in the test suite
libc.src.stdlib.malloc
+ libc.src.stdlib.aligned_alloc
libc.src.stdlib.realloc
libc.src.stdlib.free
diff --git a/libc/src/__support/threads/gpu/CMakeLists.txt b/libc/src/__support/threads/gpu/CMakeLists.txt
new file mode 100644
index 000000000000..ea89feb0c5c6
--- /dev/null
+++ b/libc/src/__support/threads/gpu/CMakeLists.txt
@@ -0,0 +1,5 @@
+add_header_library(
+ mutex
+ HDRS
+ mutex.h
+)
diff --git a/libc/src/__support/threads/gpu/mutex.h b/libc/src/__support/threads/gpu/mutex.h
new file mode 100644
index 000000000000..c4a75907eed7
--- /dev/null
+++ b/libc/src/__support/threads/gpu/mutex.h
@@ -0,0 +1,31 @@
+//===--- Implementation of a GPU mutex class --------------------*- 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_SUPPORT_THREAD_GPU_MUTEX_H
+#define LLVM_LIBC_SRC_SUPPORT_THREAD_GPU_MUTEX_H
+
+#include "src/__support/macros/attributes.h"
+#include "src/__support/threads/mutex_common.h"
+
+namespace __llvm_libc {
+
+/// Implementation of a simple passthrough mutex which guards nothing. A
+/// complete Mutex locks in general cannot be implemented on the GPU. We simply
+/// define the Mutex interface and require that only a single thread executes
+/// code requiring a mutex lock.
+struct Mutex {
+ LIBC_INLINE constexpr Mutex(bool, bool, bool) {}
+
+ LIBC_INLINE MutexError lock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError reset() { return MutexError::NONE; }
+};
+
+} // namespace __llvm_libc
+
+#endif
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index 1015333b677a..3c2291d8f41f 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_THREAD_MUTEX_H
#define LLVM_LIBC_SRC_SUPPORT_THREAD_MUTEX_H
+#include "src/__support/macros/properties/architectures.h"
+
// Platform independent code will include this header file which pulls
// the platfrom specific specializations using platform macros.
//
@@ -35,8 +37,10 @@
// few global locks. So, to avoid static initialization order fiasco, we
// want the constructors of the Mutex classes to be constexprs.
-#ifdef __unix__
+#if defined(__unix__)
#include "linux/mutex.h"
+#elif defined(LIBC_TARGET_ARCH_IS_GPU)
+#include "gpu/mutex.h"
#endif // __unix__
namespace __llvm_libc {
More information about the libc-commits
mailing list