2130 Commits

Author SHA1 Message Date
Mikhail R. Gadelha
abacab6e0c [libc][riscv] Added support for rv32 in setjmp and longjmp
This patch adds two new macros to setjmp (STORE, STORE_FP) and two new
macros to longjmp (LOAD, LOAD_FP) that takes a register and a buff, then
select the correct asm instruction for rv32 and rv64.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158640
2023-08-30 12:21:46 -03:00
Mikhail R. Gadelha
b0272d8ec3 [libc] Fix set_thread_ptr call in rv32 start up code
This patch changes the instruction in set_thread_ptr from ld to mv,
as rv32 doesn't have the ld instruction, and mv is supported by both
rv32 and rv64.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159110
2023-08-30 11:30:56 -03:00
Mikhail R. Gadelha
9e34454519 [libc] Fix test case that expects time_t to be a 32-bit type
This patch changes a test case that tests for overflow when time_t is
32-bit long, however, it was checking size_t instead of time_t.

This in on par with other testcases that correctly check the size of
time_t (asctime_test.cpp, gmtime_r_test.cpp and gmtime_test.cpp).

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159113
2023-08-30 11:24:19 -03:00
Tue Ly
76bb278ebb [libc][math] Implement double precision exp10 function correctly rounded for all rounding modes.
Implement double precision exp10 function correctly rounded for all
rounding modes.  Using the same algorithm as double precision exp
(https://reviews.llvm.org/D158551) and exp2 (https://reviews.llvm.org/D158812)
functions.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D159143
2023-08-30 08:43:50 -04:00
Joseph Huber
6a6f3c99a2 [libc] Remove hard dependencies on linux from macros headers
Currently all of this logic expects to include and link with the headers
in the `linux/` directory. This patch adds a wrapper macro to optionally
add the appropriate dependency if it exists. This is preliminary to
adding some GPU specific versions of these files.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D159021
2023-08-29 11:38:34 -05:00
Guillaume Chatelet
ee252b9d5b [libc][NFC] Fix typo 2023-08-29 11:48:27 +00:00
Petr Hosek
f6259d9b9a [libc] Fix the remaining old style includes
These were omitted from previous cleanup changes.

Differential Revision: https://reviews.llvm.org/D159066
2023-08-29 05:35:05 +00:00
Petr Hosek
15b5ac38cf [libc] Add missing ${LIBC_INCLUDE_DIR} to object rule
This is breaking the runtimes build with llvm libc enabled.

Differential Revision: https://reviews.llvm.org/D158894
2023-08-29 05:22:14 +00:00
Petr Hosek
dd070394a3 [libc] Fix the libc header installation path
This results in headers being installed in the wrong location.

Differential Revision: https://reviews.llvm.org/D158895
2023-08-29 05:21:14 +00:00
Joseph Huber
ba77c160aa [libc] Add more test cases to the argument list tests
This patch adds some extra cases to the existing argument list test in
`libc`, mainly dealing with arguments of varying sizes and primitive
types.

The purpose of this patch is to provide a wider test area when we begin
to provide varargs support on the GPU as there is no other runtime code
that really tests it. So, running these tests more exhaustively in the
GPU libc project will serve as the runtime tests for GPU vararg support in
D158246.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158867
2023-08-28 12:15:29 -05:00
Tue Ly
8ca614aa22 [libc][math] Implement double precision exp2 function correctly rounded for all rounding modes.
Implement double precision exp2 function correctly rounded for all
rounding modes.  Using the same algorithm as double precision exp function in
https://reviews.llvm.org/D158551.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D158812
2023-08-25 10:15:08 -04:00
Siva Chandra
37f6e3c0e9 [libc] Add sys/time.h to the list of aarch64 headers.
Differential Revision: https://reviews.llvm.org/D158809
2023-08-24 21:03:15 -07:00
Siva Chandra
82f41192e2 [libc] Add abort and __llvm_libc_syscall to aarch64 fullbuild config.
Differential Revision: https://reviews.llvm.org/D158794
2023-08-24 16:26:15 -07:00
Tue Ly
6baee7f2b3 [libc] Fix fputil::multiply_add and fputil::polyeval template signatures.
Fix fputil::multiply_add and fputil::polyeval template function
signatures.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158746
2023-08-24 12:00:55 -04:00
Tue Ly
434bf16084 [libc][math] Implement double precision exp function correctly rounded for all rounding modes.
Implement double precision exp function correctly rounded for all
rounding modes.  Using 4 stages:
- Range reduction: reduce to `exp(x) = 2^hi * 2^mid1 * 2^mid2 * exp(lo)`.
- Use 64 + 64 LUT for 2^mid1 and 2^mid2, and use cubic Taylor polynomial to
approximate `(exp(lo) - 1) / lo` in double precision.  Relative error in this
step is bounded by 1.5 * 2^-63.
- If the rounding test fails, use degree-6 Taylor polynomial to approximate
`exp(lo)` in double-double precision.  Relative error in this step is bounded by
2^-99.
- If the rounding test still fails, use degree-7 Taylor polynomial to compute
`exp(lo)` in ~128-bit precision.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D158551
2023-08-24 10:17:17 -04:00
Tue Ly
5e3ec418ff [libc] Only add -ffreestanding flag in full build mode.
Only add -ffreestanding flag in full build mode.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158652
2023-08-24 10:15:42 -04:00
Joseph Huber
7fd9f0f4e0 [libc] Remove MAX_LANE_SIZE definition from the RPC server
This `MAX_LANE_SIZE` was a hack from the days when we used a single
instance of the server and had some GPU state handle it. Now that we
have everything templated this really shouldn't be used. This patch
removes its use and replaces it with template arguments.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D158633
2023-08-23 12:09:30 -05:00
Joseph Huber
a69340dd81 [libc] Add the 'cpp.new' as a dependency on atexit
The `atexit` function depends on the implementations in CPP/new.h but it
is not listed as a dependency. This causes the GPU build to not include
it in the `libcgpu.a` file and prevents us from using the startup code
externally. Simply add it.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158447
2023-08-21 15:08:04 -05:00
Joseph Huber
6970920ca6 [libc] Remove 'try_open' from the client interface
We previously provided the `try_open` facility to indicate if opening a
port failed. This is used on the server to continuously poll and quit if
there is no work. However the Client currently has no way to recover
from not finding a port and simply spins repeatedly. The abstraction
here costs us some resources. This patch changes the interface to only
allow `open` on the client side and merges the loops. This saves us a
branch and a good number of registers.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D158365
2023-08-21 06:43:38 -05:00
Mikhail R. Gadelha
cd96aa7e10 [libc] Use LIBC_HAS_BUILTIN instead of __has_builtin directly.
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158313
2023-08-21 08:36:42 -03:00
Kazu Hirata
11e2975810 Fx typos in documentation 2023-08-18 23:36:04 -07:00
Michael Jones
548789b071 [libc] Fix HPD on extremely long numbers
The fuzzer found that a 100,000 digit number could possibly return an
incorrect result. This patch fixes the issue.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D158118
2023-08-18 11:03:45 -07:00
Joseph Huber
b20a385422 [libc] Do not run tests on vendor implemented math
We currently remap vendor implementations of math functions to provide a
temporarily functional `libm.a` for the GPU. However, we should not run
tests on any files that depend on these vendor implementations as they
are not under our control and are not always present.

The goal in the future is to remove the need for this by replacing all
the vendor functionality, but for now this is a workaround.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D158213
2023-08-17 17:22:55 -05:00
Siva Chandra Reddy
0f6ef9bc2a [libc][NFC] Put definitions of stdout and friends into their own object files.
This is done so that tests which only require platform file but not the
platform streams can be run as unit tests. The tests which use platform
streams can only be hermetic tests to avoid conflicts with the
system-libc streams.

Fixes:
https://github.com/llvm/llvm-project/issues/64663
https://github.com/llvm/llvm-project/issues/63558

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D158193
2023-08-17 19:48:35 +00:00
Joseph Huber
def5905c45 [libc] Do not find system binaries for offloading tools
We use `find_program` to identify a few programs we use for offloading.
    Namely, `clang-offload-packger`, `amdgpu-arch`, and `nvptx-arch`.
    Currently the logic allows these to bind to any tool matching this name,
    so it will find it on the system. This meant that if the installation
    was deleted or it found a broken binary the compilation would fail. We
    should only pull these from the current LLVM binary directory.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D158203
2023-08-17 14:16:24 -05:00
Mikhail R. Gadelha
c86a0dd134 [libc] Extend optional to support non trivially destructible objects
This patch moves the storage from inside the libc's optional class to
its own set of class, so we can support non-trivially destructible
objects.

These new classes check if the class is or isn't non trivially
destructible and instantiate the correct base class, i.e., we explicitly
call the destructor if an object is not trivially destructible.

The motivation is to support cpp::optional<UInt<128>> (used by
UInt<T>::div), which is used when a platform does not support native
int128_t types (e.g., riscv32).

The code here is a trimmed-down version of llvm::optional.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D150211
2023-08-17 14:24:32 -03:00
Mikhail R. Gadelha
56ce7522d9 [libc][NFC] Add new is_trivially_destructible check to libc's type_traits
This patch adds new code to check if a given object is trivially
destructible. The ifdefs where necessary to implement it in gcc, as it
doesn't have the __is_trivially_destructible builtin like clang.

In gcc, it's not enough to only call __has_trivial_destructor, see:
https://stackoverflow.com/questions/20181702/which-type-traits-cannot-be-implemented-without-compiler-hooks

This patch only adds the new check, it will be used in D150211.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D158033
2023-08-17 14:24:26 -03:00
Alfred Persson Forsberg
d3e045934a
Revert "[libc] Add limits.h"
limits.h currently interferes with Clang's limits.h. include_next
emits a warning because it is a GNU extension. Will re add this once
we figure out a good solution.

This reverts commits 13bbca8d694b4ef2ad03ec1805f07da8640c1a31,
002cba03298a7e8f0c1cadcb7996379e9ea5feff, and
0fb3066873fdfbe28dbb6409c58a8bff7cf208c6.
2023-08-17 06:21:50 +02:00
Alfred Persson Forsberg
13bbca8d69
[libc] [NFC] explain compiler macros in limits.h
Differential Revision: https://reviews.llvm.org/D158128
2023-08-17 01:23:30 +02:00
Joseph Huber
0f386e693b [libc][fix] Fix test after changing logic for generic stdio
Summary:
The previous patch accidentally broke the logic for adding the `generic`
subdirectory. Fix this so the CPU build works properly.
2023-08-16 09:29:29 -05:00
Joseph Huber
1e573f378c [libc] Implement fopen, fclose, and fread on the GPU
This patch implements the `fopen`, `fclose`, and `fread` functions on
the GPU. These are pretty much re-implemented from what existed but
using the new interface. Having this subset allows us to test the
interface a bit more strenuously since we can write and read to a file.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157622
2023-08-16 09:14:38 -05:00
Mikhail R. Gadelha
ea70e6f9a9 [libc] Fix compilation on 32-bit systems
This fixes the following compilation error: no known conversion from 'off_t *'
(aka 'long long *') to 'long' for 5th argument.

Since pointers are 32-bit long anyway, casting it to long shouldn't be a
problem. Tested on rv32.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157792
2023-08-16 09:52:41 -03:00
Alfred Persson Forsberg
002cba0329 [libc] limits.h: include compiler limits if not already included
The LLVM-libc build itself will override include paths and prefer it's
own limits.h over the compiler's limits.h. Because we rely on the
compiler limits.h for numerical limits in LLVM-libc it needs to be
include_next:ed if not already included. The other method to work
around this is to define all numeric macros in place.

Signed-off-by: Alfred Persson Forsberg <cat@catcream.org>

Reviewed By: thesamesam

Differential Revision: https://reviews.llvm.org/D158040
2023-08-16 02:59:21 +01:00
Michael Jones
e7866ea212 [libc] Add fuzzing for printf floats
To guarantee accuracy for all potential float values, this patch adds a
fuzzer to compare the results for float conversions from our printf
against MPFR's.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156495
2023-08-15 16:23:24 -07:00
Michael Jones
cd06b9d98f [libc] Fix printf %e and %g bugs
Fuzzing revealed bugs in the %e and %g conversions. Since these are very
similar, they are grouped together. Again, most of the bugs were related
to rounding. As an example, previously the code to check if the number
was truncated only worked for digits below the decimal point, due to it
being originally designed for %f. This patch adds a mechanism to check
the digits above the decimal point for both %e and %g.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D157536
2023-08-15 16:23:19 -07:00
Michael Jones
b02e73a355 [libc] Fix printf %f bugs
Fuzzing revealed several bugs in the %f float conversion. This patch
fixes them. Most of these bugs are related to rounding, such as
1.999...999 being rounded to 2.999...999 instead of 2.000...000 due to
rounding up not properly changing the nines to zeros. Additionally, much
of the rounding infrastructure has been refactored out so it can be
shared with the other conversions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157535
2023-08-15 16:23:13 -07:00
Michael Jones
89cdaa8d49 [libc] Fix printf %a padding issue
The trailing zeroes were previously not counted when calculating the
padding, which caused a high-precision number to get too much padding.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157534
2023-08-15 16:23:08 -07:00
Michael Jones
455d678019 [libc] Add get_explicit_exponent to fpbits
In the same way that get_explicit_mantissa is used to get the mantissa
with all the implicit bits spelled out, get_explicit_exponent gives you
the exponent with the special cases handled. Mainly it handles the cases
where the exponent is zero, which causes the exponent to either be 1
higher than expected, or just 0.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D157156
2023-08-15 16:23:02 -07:00
Joseph Huber
bb830bca04 [libc][NFC] Suppress warnings on floating point conversions
Summary:
We implement round by implicitly converting these floating point values.
Sometimes this emits warnings that we should silence by making these
explicit casts.
2023-08-15 12:47:44 -05:00
Joseph Huber
5f276d3d33 [libc] Drop RPC port count and index to 32-bit numbers
The port count and index into the ports was originally written as a
64-bit number. This was with an abundance of caution, however it's
highly unlikely that any configuration will excede a 32-bit number as
most machines  will require something in the low-thousands. Because GPUs
are functionally 32-bit in many of their operations this costs us some
extra time and registers to do the 64-bit operations. Doing this saves
us about four registers in most tests.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D157980
2023-08-15 11:10:57 -05:00
Mikhail R. Gadelha
1ab42bac91 Revert "[libc] Extend optional to support non trivially destructible objects"
This reverts commit 7d06f59b60e6eaebd125dad351a20108f057940b.

This patch broke libc compilation with gcc as it doesn't seem to have
__is_trivially_destructible(T).
2023-08-14 19:43:56 -03:00
Mikhail R. Gadelha
7d06f59b60 [libc] Extend optional to support non trivially destructible objects
This patch moves the storage from inside the libc's optional class to
its own set of class, so we can support non-trivially destructible
objects.

These new classes check if the class is or isn't non trivially
destructible and instantiate the correct base class, i.e., we explicitly
call the destructor if an object is not trivially destructible.

The motivation is to support cpp::optional<UInt<128>> (used by
UInt<T>::div), which is used when a platform does not support native
int128_t types (e.g., riscv32).

The code here is a trimmed-down version of llvm::optional.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D150211
2023-08-14 19:32:34 -03:00
Chris Cotter
32870da3ba Fix typos in documentation 2023-08-13 23:46:44 -07:00
Alfred Persson Forsberg
1acbc21d46 [libc] Define __UTS_NAME_LENGTH for __APPLE__
Before cae84d8acf all __linux__ checks were incorrectly __unix__ checks. __unix__ being true on macOS systems therefore meant that macOS would use 65 as __UTS_NAME_LENGTH.

This commit correctly specifices __UTS_NAME_LENGTH to match XNU as 256.
https://opensource.apple.com/source/xnu/xnu-201/bsd/sys/utsname.h.auto.html

Reviewed By: thesamesam

Differential Revision: https://reviews.llvm.org/D157824
2023-08-14 01:56:32 +01:00
Alfred Persson Forsberg
0fb3066873 [libc] Add limits.h
This header contains implementation specific constants.

The compiler already provides its own limits.h with numerical limits
conforming to freestanding ISO C. But it is missing extensions like
POSIX, and does for example not include <linux/limits.h> which is
expected on a Linux system, therefore, an LLVM libc implementation of
limits.h is needed for hosted (__STDC_HOSTED__) environments.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156961
2023-08-14 01:35:44 +01:00
Michael Jones
222d7cf17f [libc][NFC] Fix warning in uint operator T
This code is creating a warning on Fuchsia, this patch should fix that
warning.

Reviewed By: mcgrathr

Differential Revision: https://reviews.llvm.org/D157546
2023-08-11 14:35:41 -07:00
Guillaume Chatelet
bc2b1193b5 [libc] add the CPP algorithm header for min/max
Reviewed By: jhuber6, sivachandra

Differential Revision: https://reviews.llvm.org/D157405
2023-08-10 08:00:18 +00:00
Joseph Huber
fe04baf1f7 [libc] Silence integer shortening warnings on NVPTX masks
Nvidia uses a 32-bit mask, but we store it in a common 64-bit integer to
provide it with a compatible ABI with the AMD implementaiton which may
use a 64-bit mask. Silence these warnings by explicitly casting to the
smaller value, we know this is always legal as the result will always
fit into the smaller value if it was generated on NVPTX.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157548
2023-08-09 17:18:07 -05:00
Joseph Huber
d2024bbce3 [libc][Fix] Move generic stdio implementations to a new directory
For whatever reason, the CMake did not like having the `generic_`
version live in the same directory. This patch pushes them to a new
directory, which is probably clearer anyway.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D157544
2023-08-09 15:32:34 -05:00
Joseph Huber
0c898b26b9 [libc][Obvious] Fix incorrect dependency for alias target
Summary:
This was not updated correctly, causing the alias to not be set as
expected. Fix this by using the correct `generic_` prefix.
2023-08-09 14:46:55 -05:00