Skip to content

[libc] Partially implement 'errno' on the GPU #97107

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
Jul 1, 2024
Merged

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Jun 28, 2024

Summary:
The errno variable is expected to be thread_local by the standard.
However, the GPU targets do not support thread_local and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the errno symbol at all. However, to build some programs we at
least need to be able to link against errno. Many things that would
normally set errno completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the errno symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get libc++ to
build.

@llvmbot
Copy link
Member

llvmbot commented Jun 28, 2024

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

Summary:
The errno variable is expected to be thread_local by the standard.
However, the GPU targets do not support thread_local and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the errno symbol at all. However, to build some programs we at
least need to be able to link against errno. Many things that would
normally set errno completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the errno symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get libc++ to
build.


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

2 Files Affected:

  • (modified) libc/include/errno.h.def (+4-3)
  • (modified) libc/src/errno/libc_errno.cpp (+9-4)
diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index 3ffcd3fe4c721..1f7120e63bfc9 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -25,8 +25,9 @@
 #include "llvm-libc-macros/generic-error-number-macros.h"
 #endif
 
-#if !defined(__AMDGPU__) && !defined(__NVPTX__)
-
+#if defined(__AMDGPU__) || defined(__NVPTX__)
+extern int __llvmlibc_errno; // Not thread_local!
+#else
 #ifdef __cplusplus
 extern "C" {
 extern thread_local int __llvmlibc_errno;
@@ -34,8 +35,8 @@ extern thread_local int __llvmlibc_errno;
 #else
 extern _Thread_local int __llvmlibc_errno;
 #endif // __cplusplus
+#endif
 
 #define errno __llvmlibc_errno
-#endif
 
 #endif // LLVM_LIBC_ERRNO_H
diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp
index 64f9f522ca296..bd1438c226143 100644
--- a/libc/src/errno/libc_errno.cpp
+++ b/libc/src/errno/libc_errno.cpp
@@ -7,16 +7,21 @@
 //===----------------------------------------------------------------------===//
 
 #include "libc_errno.h"
+#include "src/__support/CPP/atomic.h"
 
 #ifdef LIBC_TARGET_ARCH_IS_GPU
-// LIBC_THREAD_LOCAL on GPU currently does nothing.  So essentially this is just
+// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
 // a global errno for gpu to use for now.
 extern "C" {
-LIBC_THREAD_LOCAL int __llvmlibc_gpu_errno;
+LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno;
 }
 
-void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_gpu_errno = a; }
-LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_gpu_errno; }
+void LIBC_NAMESPACE::Errno::operator=(int a) {
+  __llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED);
+}
+LIBC_NAMESPACE::Errno::operator int() {
+  return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);
+}
 
 #elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
 // This mode is for unit testing.  We just use our internal errno.

Summary:
The `errno` variable is expected to be `thread_local` by the standard.
However, the GPU targets do not support `thread_local` and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the `errno` symbol at all. However, to build some programs we at
least need to be able to link against `errno`. Many things that would
normally set `errno` completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the `errno` symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get `libc++` to
build.
Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on the libc side.

@jhuber6 jhuber6 merged commit 3c64a98 into llvm:main Jul 1, 2024
7 checks passed
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Summary:
The `errno` variable is expected to be `thread_local` by the standard.
However, the GPU targets do not support `thread_local` and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the `errno` symbol at all. However, to build some programs we at
least need to be able to link against `errno`. Many things that would
normally set `errno` completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the `errno` symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get `libc++` to
build.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
Summary:
The `errno` variable is expected to be `thread_local` by the standard.
However, the GPU targets do not support `thread_local` and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the `errno` symbol at all. However, to build some programs we at
least need to be able to link against `errno`. Many things that would
normally set `errno` completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the `errno` symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get `libc++` to
build.
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