[libc] Ignore 'errno' on the GPU and support 'atoi'
authorJoseph Huber <jhuber6@vols.utk.edu>
Mon, 24 Apr 2023 23:28:54 +0000 (18:28 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Tue, 25 Apr 2023 17:41:20 +0000 (12:41 -0500)
commitd9f033146b47ceef94c1f041afcd339ef007279e
tree29558b7977891d6c2714cc2f18a6f74e799f7b05
parent329964769972ef3c8a68f2dfea30e66beb4ae3e2
[libc] Ignore 'errno' on the GPU and support 'atoi'

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
libc/config/gpu/entrypoints.txt
libc/config/gpu/headers.txt
libc/include/errno.h.def
libc/src/errno/libc_errno.cpp
libc/src/errno/libc_errno.h