Skip to content

Commit

Permalink
[libc] Implement 'errno' on the GPU as a global integer internally
Browse files Browse the repository at this point in the history
The C standard asserts that the `errno` value is an l-value thread local
integer. We cannot provide a generic thread local integer on the GPU
currently without some workarounds. Previously, we worked around this by
implementing the `errno` value as a special consumer class that made all
the writes disappear. However, this is problematic for internal tests.
Currently there are build failures because of this handling and it's
only likely to cause more problems the more we do this.

This patch instead makes the internal target used for testing export the
`errno` value as a simple global integer. This allows us to use and test
the `errno` interface correctly assuming we run with a single thread.
Because this is only used for the non-exported target we still do not
provide this feature in the version that users will use so we do not
need to worrk about it being incorrect in general.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D152015
  • Loading branch information
jhuber6 committed Jun 2, 2023
1 parent c6b2d25 commit cfde5f2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
4 changes: 0 additions & 4 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ ErrnoConsumer __llvmlibc_errno;
LIBC_THREAD_LOCAL int __llvmlibc_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#else
#ifdef LIBC_TARGET_ARCH_IS_GPU
ErrnoConsumer __llvmlibc_internal_errno;
#else
LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#endif
} // extern "C"

Expand Down
11 changes: 4 additions & 7 deletions libc/src/errno/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ extern "C" __llvm_libc::ErrnoConsumer __llvmlibc_errno;
#else
namespace __llvm_libc {

#ifdef LIBC_TARGET_ARCH_IS_GPU
extern "C" ErrnoConsumer __llvmlibc_internal_errno;
#else // LIBC_TARGET_ARCH_IS_GPU
extern "C" {
extern LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
} // extern "C"
#endif
// TODO: On the GPU build this will be mapped to a single global value. We need
// to ensure that tests are not run with multiple threads that depend on errno
// until we have true 'thread_local' support on the GPU.
extern "C" LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;

// TODO: After all of libc/src and libc/test are switched over to use
// libc_errno, this header file will be "shipped" via an add_entrypoint_object
Expand Down

0 comments on commit cfde5f2

Please sign in to comment.