Commit Graph

373 Commits

Author SHA1 Message Date
Joseph Huber
443d71527b [libc] Implement exit for the GPU partially
This patch implements the `exit` function on the GPU. This required
breaking the entrypoints calling eachother on `linux` since this doesn't
work with a non-aliased target. This is only partial support because
full support requires a malloc / free implementation for the exit
callbacks array.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149363
2023-04-27 20:32:00 -05:00
Joseph Huber
f7d5e9bc4c [libc] Support the abs functions in the GPU libc.
This patch simply enables building the integer `abs` functions for the
GPU.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149317
2023-04-27 20:31:59 -05:00
Joseph Huber
72bfe2c05a [libc] Support the string conversion methods on the GPU
This patch enables us to use the existing `libc` support for string
conversion functions on the GPU. This required setting the `fenv_t` and
long double configuration. As far as I am aware, long doubles are
converted to doubles on the GPU and the floating point environment is
just an `uint32_t`.

This code is still untested as we are still working out how to run the
unit tests on the GPU.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D149306
2023-04-27 20:31:58 -05:00
Joseph Huber
dc9baac1a0 [libc] Partially implement atexit on the GPU
The `atexit` function controls registering functions to call at the end
of the program. This is difficult to do in general on the GPU because of
the lack of a real mutex implementation. We primarily provide this for
testing where we can explicitly restrict how the `atexit` registration
functions are called. So we simply create a passthrough Mutex to get
past the usage of it as per @sivachandra's suggestion.

Depends on D149225

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149226
2023-04-26 16:52:32 -05:00
Joseph Huber
fab84dcd0a [libc] Enable the GPU build to build the UnitTest library
The previous patch in D149216 allows us to use the internal `<stdlib.h>`
include for the GPU build. However, we currently don't provide the
memory functions so the header wasn't resolving them. This patch adds
these as entrypoints. They don't cause any entrypoints to be emitted
because they are not implemented, but they provide it in the header so
that we can rely on the test's implementation of them.

Depends on D149216

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149217
2023-04-26 16:52:30 -05:00
Joseph Huber
16c2a906b8 [libc][fix] Add missing 'errno' header for the GPU libc
Summary:
The previous GPU `libc` patch added support for `atoi`. However, it
didn't also enable generating the errno header. This caused including it
to use the system's header which invariably will cause compiler
problems.
2023-04-25 13:25:45 -05:00
Joseph Huber
d9f033146b [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
2023-04-25 12:41:20 -05:00
Noah Goldstein
0432b85d8e [LIBC] Implement remainder of posix 'sched.h' minus SCHED_SPORADIC
Includes macros:
    linux/SCHED_OTHER // posix req
    linux/SCHED_FIFO // posix req
    linux/SCHED_RR // posix req
    linux/SCHED_BATCH
    linux/SCHED_ISO
    linux/SCHED_IDLE
    linux/SCHED_DEADLINE

Includes types:
    struct sched_param { int sched_priority; }

Includes functions:
    sched_setparam
    sched_getparam
    sched_setscheduler
    sched_getscheduler
    sched_get_priority_max
    sched_get_priority_min
    sched_rr_get_interval

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148069
2023-04-20 14:53:41 -05:00
Tue Ly
f79264b5f6 [libc][math] Remove placeholder implementations of asin and pow.
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D148781
2023-04-20 01:28:16 -04:00
Mikhail R. Gadelha
3abae1c884 [libc] Partial revert of "[libc] Update supported riscv libs"
Partial revert of commit ee91799670.

I can't reproduce the error locally, so let's revert it to keep the bots
happy.
2023-04-17 16:47:47 -03:00
Mikhail R. Gadelha
ee91799670 [libc] Update supported riscv libs
This patch updates the list of supported libs according to the latest changes to x86_64

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D147961
2023-04-17 16:13:48 -03:00
Michael Jones
1c261e360f [libc] Add implementation of getchar
added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
2023-04-14 15:40:05 -07:00
Noah Goldstein
203aff2df3 [LIBC] Implement sched_yield()
Implements: https://linux.die.net/man/2/sched_yield

Possibly we don't need the return value check / errno as according to
both the manpage (and current linux source) `sched_yield` cannot fail.

Reviewed By: sivachandra, michaelrj

Differential Revision: https://reviews.llvm.org/D147985
2023-04-12 19:04:37 -05:00
Caslyn Tonelli
718729e997 [libc] Add memmem implementation
Introduce the `memmem` libc string function.

`memmem_implementation` performs shared logic for `strstr`,
`strcasestr`, and `memmem`; essentially reconfiguring what was the
`strstr_implementation` to support length parameters.

Differential Revision: https://reviews.llvm.org/D147822
2023-04-11 20:49:25 +00:00
Caslyn Tonelli
95ca2e2cf9 [libc] Fix swab placement
Per https://reviews.llvm.org/D147970#4256889, swab.cpp is moved out of
the /linux subdirectory and cmake specifications are amended to reflect
that swab is not OS-specific.

Differential Revision: https://reviews.llvm.org/D147988
2023-04-11 18:06:13 +00:00
Alex Brachet
9c7a370505 Reland "[libc] Add {,r}index"
Differential Revision: https://reviews.llvm.org/D147464
2023-04-11 04:30:50 +00:00
Alex Brachet
42294bf16f Revert "[libc] Add {,r}index"
This reverts commit a0a141fcbe.
2023-04-11 01:26:42 +00:00
Alex Brachet
a0a141fcbe [libc] Add {,r}index
Differential Revision: https://reviews.llvm.org/D147464
2023-04-11 01:17:11 +00:00
Caslyn Tonelli
1a3e760eda [libc] Add swab implementation
Swab implementation is added to libc/src/unistd.

Differential Revision: https://reviews.llvm.org/D147970
2023-04-10 23:37:51 +00:00
Caslyn Tonelli
a1e4f1d006 [libc] Add strxfrm Implementation
Introduce `strxfrm` and unit tests. The current implementation is
introduced without locale support.

The simplified function performs a `memcpy` if the `n` value is large
enough to store the source len + '\0', otherwise `dest` is unmodified.

Ticket: https://fxbug.dev/124217

Differential Revision: https://reviews.llvm.org/D147478
2023-04-07 00:09:22 +00:00
Alex Brachet
b4ab398cb1 [libc] Implement strsep
Differential Revision: https://reviews.llvm.org/D147503
2023-04-06 17:48:28 +00:00
Caslyn Tonelli
bc2b161408 [libc] Add strchrnul implementation
Introduce strchrnul implementation and unit tests.

Submitting on behalf of Caslyn@

Differential Revision: https://reviews.llvm.org/D147346
2023-04-03 11:08:28 -07:00
Mikhail R. Gadelha
b1e5d0d671 [libc] Enable more headers for riscv
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146203
2023-03-31 13:25:04 -03:00
Michael Jones
e0de24cb0d [libc] Re-enable wctob with fixes
The stdio test failures were due to headers potentially not being built
in the correct order. This should set up the dependencies correctly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146551
2023-03-29 12:49:29 -07:00
Mikhail R. Gadelha
0f6fd1b704 [libc] Add support for setjmp and longjmp in riscv
This patch implements setjmp and longjmp in riscv using inline asm. The
following changes were required:

* Omit frame pointer: otherwise gcc won't allow us to use s0
* Use __attribute__((naked)): otherwise both gcc and clang will generate
function prologue and epilogue in both functions. This doesn't happen
in x86_64, so we guard it to only riscv

Furthermore, using __attribute__((naked)) causes two problems: we
can't use `return 0` (both gcc and clang) and the function arguments in
the function body (clang only), so we had to use a0 and a1 directly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145584
2023-03-24 16:16:31 -03:00
Michael Jones
de939c6cd8 [libc] enable printf using system FILE
The printf and fprintf implementations use our internal implementation
to improve performance when it's available, but this patch enables using
the public FILE API for overlay mode.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146001
2023-03-23 09:56:45 -07:00
Michael Jones
9e2b164570 [libc] temporarily disable wctob entrypoint
Differential Revision: https://reviews.llvm.org/D146484
2023-03-20 17:42:00 -07:00
Michael Jones
46b5087227 [libc] add basic wide char functions
This patch adds the wchar header, as well as the functions to convert to
and from wide chars. The header also sets up the definitions for wint
and wchar.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145995
2023-03-20 16:36:21 -07:00
Mikhail R. Gadelha
1990ce74dc [libc] Enable __llvm_libc_syscall and fork
This patch enables the remaining calls from unistd.

The test cases had to be updated to:
1. Use SYS_symlinkat if SYS_symlink is not available
2. Use SYS_readlinkat if SYS_readlink is not available
3. Use SYS_unlinkat if SYS_unlink is not available
4. Use SYS_openat if SYS_open is not available

We also abort compilation if neither of the syscalls mentioned above are
available.

Differential Revision: https://reviews.llvm.org/D146161
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
fe99de31d9 [libc] Enable spawn lib in riscv
In this patch we add support for the spawn lib in riscv.

Only small changes were required, the biggest one was to use of dup3
instead of dup2, if the latter is not available. This follows our
implementation of dup2.

Differential Revision: https://reviews.llvm.org/D146145
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
63ed8ab4db [libc] Update supported riscv libs
This patch removes some duplicated libs added to entrypoints.txt, adds
new libs supported to entrypoints.txt and updates header.txt

Differential Revision: https://reviews.llvm.org/D146065
2023-03-16 06:16:14 -03:00
Siva Chandra
537e6e7095 [libc] Enable more functions on riscv64.
The list of headers has also been updated. Some duplicated entrypoints
have been removed.
2023-03-14 06:33:08 +00:00
Siva Chandra Reddy
71825a889a [libc][NFC] Add string.h header to various platform headers.txt. 2023-03-13 15:34:58 +00:00
Siva Chandra
cf90633cf2 [libc] Extend the linux thread implementation for riscv64.
Also, added riscv64 startup code for static linking which is used
by the integration tests. Functions from the C standard threads
library have been enabled.

Reviewed By: mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D145670
2023-03-09 16:58:48 +00:00
Siva Chandra
6822156a58 [libc] Widen the riscv64 full build config. 2023-03-08 23:05:00 +00:00
Siva Chandra
dea96e7dca [libc] Remove log10 from the list of riscv64 entrypoints.
Its test is currently failing of real riscv64 hardware.
2023-03-08 20:05:13 +00:00
Mikhail R. Gadelha
4d94a0080e [libc] Support more functions in riscv
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145444
2023-03-08 14:58:51 -03:00
Siva Chandra
6f8dfeee06 [libc] Add riscv64 fenv implementation and enable the fenv.h functions.
Reviewed By: mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D145560
2023-03-08 10:49:24 -03:00
Mikhail R. Gadelha
0ffea21893 [libc] Small improvements to libc cmake
This patch includes:
1. Better error message when cmake finds incompatible triple
2. Added missing header dependencies libc/include/CMakeLists.txt as per
   app.td
3. Removed unused $LLVM_LIBC_INCLUDE_DIRS cmake variable

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145496
2023-03-08 09:02:02 -03:00
Siva Chandra
9ca2fb8217 [libc] Add riscv64 syscall implementation.
All syscall wrapper functions which have unit tests have been enabled.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D145452
2023-03-07 21:47:58 -03:00
Siva Chandra Reddy
439eebab81 [libc] Add fenv functions to arm32 baremetal config.
Also, an "arm" subfolder for baremetal config has been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145476
2023-03-07 18:11:20 +00:00
Siva Chandra Reddy
9edef3e93d [libc] Move math.h and fenv.h macro definitions to llvm-libc-macros.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145475
2023-03-07 18:08:52 +00:00
Siva Chandra Reddy
f3e4ece4e0 [libc] Add a headers.txt for linux/arm config. 2023-03-07 07:56:15 +00:00
Siva Chandra
f7e91f2b82 [libc] Add riscv64 config.
Memory functions get the basic implementation. They can be tuned
as a follow up.

Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D145433
2023-03-06 23:03:43 +00:00
Siva Chandra Reddy
ae8e1b8fc3 [libc] Add arm 32 FEnvImpl.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145347
2023-03-06 15:57:37 +00:00
Tue Ly
3735d209a2 [libc][Obvious] Add errno entrypoint for macOS ARM64. 2023-03-06 00:06:03 -05:00
Siva Chandra Reddy
4052bc8674 [libc] Make errno an entrypoint.
The entrypoint has been added to the various entrypoint lists. The libc
code style doc has been updated with information on how errno should be
set from the libc runtime code.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145179
2023-03-03 06:59:19 +00:00
Michael Jones
effd56b0a0 [libc] add basic Intel MacOS configuration
The config is based on the ARM MacOS config, but with fenv and math
functions disabled.

This should unblock this bug: https://github.com/llvm/llvm-project/issues/60910

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145099
2023-03-01 15:33:16 -08:00
Jeff Bailey
8aec3b126b [libc] Introduce sys/socket.h
This adds sys/socket.h and some definitions on Linux.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144415
2023-02-23 07:11:20 +00:00
Raman Tenneti
d0d6d78bbd [libc] Implement ntohl and ntohs
Per spec:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohs.html

Co-authored-by: Jeff Bailey <jbailey@google.com>

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144506
2023-02-22 10:40:38 -08:00