Skip to content

Commit

Permalink
[libc] Ignore 'errno' on the GPU and support 'atoi'
Browse files Browse the repository at this point in the history
The 'errno' value is most likely not useful on the GPU and it prevents
us from providing certain functions on the GPU that depend on it, like
`atoi`. This patch makes the necessary changes to support `errno` by
simple replacing it with a consumer class.

Supporting `errno` on the GPU is possible in some aspects. The first
approach would be to use a buffer of shared memory that has enough space
for all threads. Another option would be to change code generation to
support `thread_local` using `address_space(5)` memory allocated at
kernel launch. The former could look like the following, which could be
implemented in a later patch:

```
template <typename T>
using SharedBuffer = T[gpu::MAX_THREADS] [[clang::address_space(3)]];
template <typename T> struct ErrnoSetter {
  constexpr ErrnoSetter(SharedBuffer<T> &storage) : storage(storage) {}
  SharedBuffer<T> &storage;
  void operator=(const T &val) { storage[gpu::get_thread_id()] = val; }
};

static SharedBuffer<int> thread_local_buffer [[clang::loader_uninitialized]];
ErrnoSetter<int> __llvmlibc_internal_errno(thread_local_buffer);
```

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149107
  • Loading branch information
jhuber6 committed Apr 25, 2023
1 parent 3299647 commit d9f0331
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strstr
libc.src.string.strtok
libc.src.string.strtok_r

# stdlib.h entrypoints
libc.src.stdlib.atoi
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/headers.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.ctype
libc.include.string
libc.include.stdlib
)
2 changes: 2 additions & 0 deletions libc/include/errno.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
#include <llvm-libc-macros/generic-error-number-macros.h>
#endif

#if !defined(__AMDGPU__) && !defined(__NVPTX__)
extern _Thread_local int __llvmlibc_errno;
#define errno __llvmlibc_errno
#endif

#endif // LLVM_LIBC_ERRNO_H
10 changes: 10 additions & 0 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/macros/properties/architectures.h"

namespace __llvm_libc {

extern "C" {
Expand All @@ -18,9 +20,17 @@ extern "C" {
// macro defined in LLVM libc's public errno.h header file.
// TODO: Use a macro to distinguish full build and overlay build which can be
// used to exclude __llvmlibc_errno under overlay build.
#ifdef LIBC_TARGET_ARCH_IS_GPU
ErrnoConsumer __llvmlibc_errno;
#else
thread_local int __llvmlibc_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#else
#ifdef LIBC_TARGET_ARCH_IS_GPU
ErrnoConsumer __llvmlibc_internal_errno;
#else
thread_local int __llvmlibc_internal_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#endif
} // extern "C"

Expand Down
21 changes: 21 additions & 0 deletions libc/src/errno/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,42 @@
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H

#include "src/__support/macros/properties/architectures.h"

#include <errno.h>

// If we are targeting the GPU we currently don't support 'errno'. We simply
// consume it.
#ifdef LIBC_TARGET_ARCH_IS_GPU
namespace __llvm_libc {
struct ErrnoConsumer {
void operator=(int) {}
};
} // namespace __llvm_libc
#endif

// All of the libc runtime and test code should use the "libc_errno" macro. They
// should not refer to the "errno" macro directly.
#ifdef LIBC_COPT_PUBLIC_PACKAGING
#ifdef LIBC_TARGET_ARCH_IS_GPU
extern "C" __llvm_libc::ErrnoConsumer __llvmlibc_errno;
#define libc_errno __llvmlibc_errno
#else
// This macro will resolve to errno from the errno.h file included above. Under
// full build, this will be LLVM libc's errno. In overlay build, it will be
// system libc's errno.
#define libc_errno errno
#endif
#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 thread_local int __llvmlibc_internal_errno;
} // extern "C"
#endif

// 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 d9f0331

Please sign in to comment.