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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc/docs/gpu/motivation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Limitations

We only implement a subset of the standard C library. The GPU does not
currently support thread local variables in all cases, so variables like
``errno`` are not provided. Furthermore, the GPU under the OpenCL execution
``errno`` are atomic and global. Furthermore, the GPU under the OpenCL execution
model cannot safely provide a mutex interface. This means that features like
file buffering are not implemented on the GPU. We can also not easily provide
threading features on the GPU due to the execution model so these will be
Expand Down
7 changes: 4 additions & 3 deletions libc/include/errno.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
#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;
}
#else
extern _Thread_local int __llvmlibc_errno;
#endif // __cplusplus
#endif

#define errno __llvmlibc_errno
#endif

#endif // LLVM_LIBC_ERRNO_H
13 changes: 9 additions & 4 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading