Commit Graph

2206 Commits

Author SHA1 Message Date
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 13bbca8d69,
002cba0329, and
0fb3066873.
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 7d06f59b60.

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
Roland McGrath
5207451dd0 [libc] Compile with -Wconversion -Wno-sign-conversion
This more closely matches the stricter warnings used for
this same code in the Fuchsia build.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156630
2023-08-09 12:46:20 -07:00
Joseph Huber
d04494ccc9 [libc] Rework the file handling for the GPU
The GPU has much tighter requirements for handling IO functions.
Previously we attempted to define the GPU as one of the platform files.
Using a common interface allowed us to easily define these functions
without much extra work. However, it became more clear that this was a
poor fit for the GPU. The file interface uses function pointers, which
prevented inlining and caused bad perfromance and resource usage on the
GPU. Further, using an actual `FILE` type rather than referring to it as
a host stub prevented us from usin files coming from the host on the GPU
device.

After talking with @sivachandra, the approach now is to simply define
GPU specific versions of the functions we intend to support. Also, we
are ignoring `errno` for the time being as it is unlikely we will ever
care about supporting it fully.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157427
2023-08-09 14:42:20 -05:00
Michael Jones
8b37431024 [libc] Fix vfprintf test colliding with fprintf
Sometimes the vfprintf test was failing, I suspect that's due to it
using the same filename as the fprintf test. This patch fixes that
problem by changing the filename of the vfprintf output file.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D157523
2023-08-09 10:31:58 -07:00
Michael Jones
e328d19302 [libc] Fix final conversion warnings
This patch fixes the floating point conversion warnings found with
`-Wconversion` and `-Wno-sign-conversion`. These were the last warnings
I found, meaning that once this lands https://reviews.llvm.org/D156630
should be unblocked.

Reviewed By: mcgrathr, lntue

Differential Revision: https://reviews.llvm.org/D157449
2023-08-09 10:24:03 -07:00
Mikhail R. Gadelha
860d38bf57 [libc] Support poky system
This patch adds support for yocto images, which are custom Linux-base
systems created by yocto.

$CMAKE_HOST_SYSTEM_NAME returns "poky" as the system name, but it is a
linux image, so we just replace the name with "linux", so libc can use
the correct path.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D157404
2023-08-09 10:40:19 -03:00
Guillaume Chatelet
b555912e70 [libc] Better IntegerToString API
This patch is an alternative to D155902. It provides the following benefits:
 - No buffer manual allocation and error handling for the general case
 - More flexible API : width specifier, sign and prefix handling
 - Simpler code

The more flexible API removes the need for manually tweaking the buffer afterwards, and so prevents relying on implementation details of IntegerToString.

Reviewed By: michaelrj, jhuber6

Differential Revision: https://reviews.llvm.org/D156981
2023-08-09 07:33:29 +00:00
Guillaume Chatelet
6632b25841 [libc] Make add_libc_unittest compile with -ffreestanding
tests not compile with `-ffreestanding` can pull unwanted dependencies like `limits.h` which defines `PTHREAD_STACK_MIN`.
This is what caused the build bot failure in https://reviews.llvm.org/D156981#4570776.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157444
2023-08-09 07:31:59 +00:00
Guillaume Chatelet
98ab87f44d Revert "[libc] Better IntegerToString API"
This reverts commit 910cc05aae.
2023-08-08 20:52:50 +00:00
Guillaume Chatelet
910cc05aae [libc] Better IntegerToString API
This patch is an alternative to D155902. It provides the following benefits:
 - No buffer manual allocation and error handling for the general case
 - More flexible API : width specifier, sign and prefix handling
 - Simpler code

The more flexible API removes the need for manually tweaking the buffer afterwards, and so prevents relying on implementation details of IntegerToString.

Reviewed By: michaelrj, jhuber6

Differential Revision: https://reviews.llvm.org/D156981
2023-08-08 20:39:13 +00:00
Guillaume Chatelet
0abce1e4a5
[libc] add <limits> specialization for signed char 2023-08-08 17:04:55 +02:00
Joseph Huber
e74281a3c3 [libc] Allow NVPTX to use aliases
Summrary:
Following D156014 we can now use aliases for NVPTX, removing this source
of divergence. We require at least +ptx63 and at least sm_30 for
`.alias` but this is already within what we build for with `libc`
support.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157323
2023-08-08 08:59:35 -05:00
Guillaume Chatelet
575310330b [libc][doc] Update macros documentation
Update documentaiton now that macros are laid out in a more structured way.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143911
2023-08-08 08:02:30 +00:00
Vitaly Buka
9abc1e080d [test][libc] Fix aligned_alloc argument
Size must be multiple of Alignment.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D157247
2023-08-07 16:36:20 -07:00
Michael Jones
f0a3954ef1 [libc][cleanup] Fix most conversion warnings
This patch is large, but is almost entirely just adding casts to calls
to syscall_impl. Much of the work was done programatically, with human
checking when the syntax or types got confusing.

Reviewed By: mcgrathr

Differential Revision: https://reviews.llvm.org/D156950
2023-08-07 15:03:01 -07:00
Michael Jones
f6ba352988 [libc] Add nullptr check option to printf %s
Some printf implementations perform a null check on pointers passed to
%s. While that's not in the standard, this patch adds it as an option
for compatibility. It also puts a similar check in %n behind the same
flag.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156923
2023-08-07 14:47:09 -07:00
Roland McGrath
019a477c88 [libc] Clean up required LIBC_INLINE uses in src/string
This was generated using clang-tidy and clang-apply-replacements,
on src/string/*.cpp for just the llvmlibc-inline-function-decl
check, after applying https://reviews.llvm.org/D157164, and then
some manual fixup.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D157169
2023-08-07 12:21:22 -07:00
Michael Jones
16d5c24226 [libc] Add v variants of printf functions
The v variants of the printf functions take their variadic arguments as
a va_list instead of as individual arguments. They are otherwise
identical to the corresponding printf variants. This patch adds them
(vprintf, vfprintf, vsprintf, and vsnprintf) as well as tests.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D157138
2023-08-04 14:50:24 -07:00
Guillaume Chatelet
cdbc3ab7d6 [libc] Add support for cpp::make_signed 2023-08-03 13:45:04 +00:00
Mikhail R. Gadelha
29dec35212 [libc] NFC: Fix build on systems that don't have linux/time_types.h
This broke some bots that don't have linux/time_types.h available
(libc-x86_64-debian-*).

The header is needed because of __kernel_timespec, and since this is
only needed when SYS_sched_rr_get_interval_time64 is available, guarding
the include should fix the broken bot.
2023-08-03 10:30:21 -03:00
Guillaume Chatelet
51f91e1045 [libc] Add string construct/assign from string_view 2023-08-03 13:25:53 +00:00
Mikhail R. Gadelha
c9783d2bda [libc] Add support to compile some syscalls on 32 bit platform
This patch adds a bunch of ifdefs to handle the 32 bit versions of
some syscalls, which often only append a 64 to the name of the syscall
(with exception of SYS_lseek -> SYS_llseek and SYS_futex ->
SYS_futex_time64)

This patch also tries to handle cases where wait4 is not available
(as in riscv32): to implement wait, wait4 and waitpid when wait4 is
not available, we check for alternative wait calls and ultimately rely
on waitid to implement them all.

In riscv32, only waitid is available, so we need it to support this
platform.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148371
2023-08-03 10:08:01 -03:00
Roland McGrath
cd328c10ad [libc] Use ASSERT_DEATH for EXPECT_DEATH under Fuchsia zxtest
The Fuchsia zxtest library has ASSERT_DEATH but not EXPECT_DEATH.
The latter may be added in the future, but for now just use the
former as substitute.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D156940
2023-08-02 17:57:44 -07:00
Michael Jones
ad844632b9 [libc] Support underscores in NaN char sequences
Other libc implementations support underscores in NaN(n-char-sequence)
strings. Us not supporting that is causing fuzz failures, so this patch
solves the problem.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156927
2023-08-02 13:02:47 -07:00
Mikhail R. Gadelha
44bf4c8944 [libc] Fix float to string conversion in 32-bit systems
In 32-bit systems, sizeof(size_t) is 4, so we fail to build an 128-bit
integer in mul_shift_mod_1e9, which ends up ignoring the top bits in the
mantissa.

This patch fixes the issue by calling the Uint constructor directly. If
it's a system that supports 128-bit integers, the constructor that takes
a value will be called, if the system doesn't support 128-bit integers
(like rv32), mantissa is already a UInt.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D156813
2023-08-02 09:50:45 -03:00
Joseph Huber
a82b154eb6 [libc] Disable nextafter tests for NVPTX
Summary:
These tests pass just fine on my local system, but the buildbot doesn't
like it. Disable these until we can figure out an alternative.
2023-08-01 16:52:50 -05:00
Anton Rydahl
53f5bfdb58 [libc][libm][GPU] Populating 'libmgpu.a' for math on the GPU
This commit populates `libmgpu.a` with wrappers for the following built-ins
- modf, modff
- nearbyint, nearbyintf
- remainder, remainderf
- remquo, remquof
- rint, rintf
- scalbn, scalbnf
- sqrt, sqrtf
- tan, tanf
- tanh, tanhf
- trunc, truncf
and wrappers the following vendor implementations
- nextafter, nextafterf
- sincos, sincosf
- sinh, sinhf
- sinf
- tan, tanf
- tanh, tanhf

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D153395
2023-08-01 13:34:43 -07:00
Michael Jones
15994529e0 [libc][NFC] Fix str_to_float and uint warnings
More Wconversion and Wno-sign-conversion warning fixes.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156812
2023-08-01 11:08:10 -07:00
Mikhail R. Gadelha
943d194fd9 [libc] Fix sched_getscheduler return value
This patch fixes the return time for sched_getscheduler which was set to
always zero. The syscall documentation, however, defines:

On success, sched_getscheduler() returns the policy for the thread (a
nonnegative integer).

I also changed the return type for sched_setscheduler, but this change
didn't impact and test case.

This patch also removes the duplicated code from param_and_scheduler_test.cpp
and adds SCHED_BATCH and SCHED_IDLE to the tests.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156700
2023-08-01 14:26:41 -03:00
Joseph Huber
3926feb84e [libc] Add basic wrappers for the backend address spaces
The GPU makes use of different address spaces. We generally work with
global memory, thread private memory, and thread shared memory. This
patch simply adds a few preliminary wrappers to map these concepts to
the numerical values the backend uses. Obviously casts between these
will need to be checked by the user.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D156731
2023-08-01 07:41:10 -05:00
Roland McGrath
0ed4b17707 [libc] Quiet initializer warnings in getopt
This prevents -Wmissing-braces warnings.

Reviewed By: michaelrj, Caslyn

Differential Revision: https://reviews.llvm.org/D156629
2023-07-31 16:50:40 -07:00
Michael Jones
e9bdf4afa0 [libc][NFC] clean up type warnings in printf
In preparation for https://reviews.llvm.org/D156630 this patch cleans up
all integer size and sign conversion warnings in printf.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156723
2023-07-31 13:19:34 -07:00
Joseph Huber
9ffde19d11 [libc] Add PAUSE instruction for the RPC spin loop
The other architectures use a brief sleep to defer work during this spin
loop that checks the RPC mailboxes. This patch adds one for x64 to
improve usage when running the server.

Reviewed By: tianshilei1992

Differential Revision: https://reviews.llvm.org/D156566
2023-07-28 18:09:01 -05:00
Michael Jones
63f8922f49 [libc] Fix printf g conversion with high precision
The number of trailing zeroes was being calculated incorrectly. It was
assuming that it could add all of the implicit leading zeroes in the
final block, not accounting for the number of digits actually reqested
by the precision.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156489
2023-07-28 14:07:00 -07:00
Alfred Persson Forsberg
fe9c3c7868 [libc] _INCLUDE_SCUDO: check for compiler-rt in enabled RUNTIMES too
Previously including SCUDO in a libc build with runtimes/ as root was
not possible since this code only checked for compiler-rt enabled via
LLVM_ENABLED_PROJECTS.

Reviewed By: thesamesam

Differential Revision: https://reviews.llvm.org/D156388
2023-07-27 05:11:54 +01:00
Joseph Huber
334bbc0d67 [libc] Add support for the 'fread' function on the GPU
This patch adds support for `fread` on the GPU via the RPC mechanism.
Here we simply pass the size of the read to the server and then copy it
back to the client via the RPC channel. This should allow us to do the
basic operations on files now. This will obviously be slow for large
sizes due ot the number of RPC calls involved, this could be optimized
further by having a special RPC call that can initiate a memcpy between
the two pointers.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D155121
2023-07-26 13:51:35 -05:00
Michael Jones
91eb99b841 [libc][NFC] fix sprintf test on 32 bit systems
The expected number for the max ptrdiff value was expected to be exactly
4294967296 (2**32) for 32 bit systems, when it should be
4294967295 (2**32 - 1). This also adds a second test to check for this
case on non-32 bit systems.

Reviewed By: lntue, mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D156257
2023-07-26 11:33:53 -07:00
Joseph Huber
be9a4926d2 [libc] Disable ilogb functions on NVPTX
Summary:
These fail tests, disbale them until we can figure out how to get them
passing.
2023-07-26 12:59:15 -05:00
Joseph Huber
a42c1f8d97 [libc][Obvious] Fix use of fwrite in the RPC server
Summary:
The RPC server used the size field which meant we didn't get the correct
return value for partial reads. We fix that here.
2023-07-26 11:13:38 -05:00
Joseph Huber
d1417f431f [libc] Fix missing bitcode flags passed to GPU vendor math
Summary:
A previous patch missed adding these to all the definitions.
2023-07-26 09:53:38 -05:00
Joseph Huber
b06617f1ec [libc] Fix GPU tests failing after recent changes
Summray:
We landed some extra math support, which is apparently broken on the
max / min functions. the `mod` functions cannot be tested as they use
`std::limits` which don't exist in a freestanding environment. Also the
`blockstore` test seems to be broken. We will need to fix these in the
future but for now we need something in a workable state.

Reviewed By: jplehr

Differential Revision: https://reviews.llvm.org/D156329
2023-07-26 09:06:19 -05:00
Ethan Luis McDonough
546c9b3f6a
[libc] Add math functions to AMD/NVPTX libm
Related to D152486.  The following functions are included in this revision: `acosf`, `acoshf`, `asinf`, `asinhf`, `atanf`, `atanhf`, `ceil`, `ceilf`, `copysign`, `copysignf`, `cos`, `cosf`, `cosh`, `coshf`, `exp10f`, `exp2f`, `expf`, `expm1f`, `fabs`, `fabsf`, `fdim`, `fdimf`, `floor`, `floorf`, `fma`, `fmaf`, `fmax`, `fmaxf`, `fmin`, `fminf`, `fmod`, `fmodf`, `frexp`, `frexpf`, `hypot`, `hypotf`, `ilogb`, `ilogbf`, `ldexp`, `ldexpf`, `llrint`, `llrintf`, `llround`, `llroundf`, `pow`, and `powf`.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D152603
2023-07-26 03:02:24 -05:00
Alfred Persson Forsberg
c1ce7c8341 [libc] pthread.h.def: add PTHREAD_MUTEX_INITIALIZER
Adds PTHREAD_MUTEX_INITIALIZER for compiler-rt's builtins/emutls.c

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156276
2023-07-25 16:47:15 -07:00
Michael Jones
49eb1aed89 [libc] Add back report_assertion_failure
It's necessary for the assert_fail function, so it needs to stay in for
the moment.

Reviewed By: alfredfo

Differential Revision: https://reviews.llvm.org/D156275
2023-07-25 16:34:53 -07:00
Michael Jones
65fe1ac30a [libc][NFC] Simplify assert message generation
Previously displaying a failed assert would involve a runtime integer to
string conversion. This patch changes that to be a compile time string
conversion.

This was inspired by a comment by JonChesterfield on https://reviews.llvm.org/D155899

Reviewed By: lntue, sivachandra, JonChesterfield

Differential Revision: https://reviews.llvm.org/D156168
2023-07-25 11:45:45 -07:00
Joseph Huber
7c8a52f90c [libc][Obvious] Fix AMDGPU control constant for vendor sqrt
Summary:
This is supposed to be enabled to say that we want correct sqrt by
default.
2023-07-25 07:59:23 -05:00
Joseph Huber
c381a94753 [libc] Remove test RPC opcodes from the exported header
This patch does the noisy work of removing the test opcodes from the
exported interface to an interface that is only visible in `libc`. The
benefit of this is that we both test the exported RPC registration more
directly, and we do not need to give this interface to users.

I have decided to export any opcode that is not a "core" libc feature as
having its MSB set in the opcode. We can think of these as non-libc
"extensions".

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154848
2023-07-21 15:36:36 -05:00
Joseph Huber
69bd7fae2b [libc] Disable 'DecodeInOtherBases` test on GPU targets
This test is excessively slow on GPU targets, taking anywhere beween 5
and 60 seconds to complete each time it's run. See
https://lab.llvm.org/buildbot/#/builders/55/builds/52203/steps/12/logs/stdio
for an example on the NVPTX buildbot. Simply disable testing this on the
GPU for now.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D155979
2023-07-21 13:15:09 -05:00
Joseph Huber
d3aabeb7b5 [libc] Treat the locks array as a bitfield
Currently we keep an internal buffer of device memory that is used to
indicate ownership of a port. Since we only use this as a single bit we
can simply turn this into a bitfield. I did this manually rather than
having a separate type as we need very special handling of the masks
used to interact with the locks.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D155511
2023-07-21 10:49:11 -05:00
Jon Chesterfield
bf0992c718 [libc] Fix line reporting in assertion failure
Was passing zeros to the string print function.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D155899
2023-07-20 23:44:33 +01:00
Michael Jones
aeec4627e6 [libc][NFC] mark vprintf functions as inline
The functions are in a header, and so must be marked inline to avoid
symbol conflicts.

Differential Revision: https://reviews.llvm.org/D155892
2023-07-20 14:50:18 -07:00
Michael Jones
b9f6c20876 [libc] Move printf writer to new design
The new printf writer design focuses on optimizing the fast path. It
inlines any write to a buffer or string, and by handling buffering
itself can more effectively work with both internal and external file
implementations. The overflow hook should allow for expansion to
asprintf with minimal extra code.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D153999
2023-07-20 11:08:20 -07:00
Joseph Huber
26a1849cca [libc] Warn on use of global constructors in libc
Clang supports the `-Wglobal-constructors` flag which will indicate if a
global constructor is being used. The current goal in `libc` is to make
the constructors `constexpr` to prevent this from happening with
straight construction. However, there are many other cases where we can
emit a constructor that this won't catch. This should give warning if
someone accidentally introduces a global constructor.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D155721
2023-07-20 11:29:58 -05:00
Joseph Huber
cf269417b2 [libc] Add an override option for specifying the loader implementation
There are some cases when testing we want to override the logic for not
building tests if the loader is not present. This allows users to
specify an external binary that fulfils the same duties which will force
the tests to be built even without meeting the dependencies.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D155837
2023-07-20 08:44:58 -05:00
Jon Chesterfield
095e69404a [libc][amdgpu] Accept deadstripped clock_freq global
If the clock_freq symbol isn't used, and is removed,
we don't need to abort the loader. Can instead just not set it.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D155832
2023-07-20 14:23:08 +01:00
Joseph Huber
ed7ade9cfa [libc] Remove global constructor in getopt implementation
This file required a global constructor due to copying the file stream
and have a non-constexpr constructor for the wrapper type. Also, I
changes the `opterr` to be a pointer, because it seemed like it wasn't
being set correctly as an externally visibile variable if we just
captured it by value.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D155766
2023-07-20 08:11:19 -05:00
Joseph Huber
cc92212d75 [libc] Remove global constructors on File type
The `File` interface currently has a destructor to delete the buffer if
it is owned by the file. This is problematic for the globally allocated
`stdout`, `stdin`, and `stderr` files. This causes the file interface to
have global constructors to initialize the destructors to use these.
However, these never use the destructors because they don't own the
buffer. This patch removes the destructor and calls in manually in the
close implementation. The platform close should never need to access the
buffer and it needs to be done before clearing the whole thing, so this
should work.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D155762
2023-07-20 08:11:18 -05:00
Jon Chesterfield
d483824fc8 [libc][amdgpu] Tolerate different install directories for hsa.h
HSA headers might be under a hsa/ directory or might not.
This scheme matches the one used by the openmp amdgpu plugin.

Reviewed By: jhuber6, jplehr

Differential Revision: https://reviews.llvm.org/D155812
2023-07-20 13:43:17 +01:00
Joseph Huber
480e3e353c [libc][NFC] Move the RPC types to the rpc_client.h header
Summary:
Simple cleanup of the interface so we do not depend on the installed
headers and get everything we need just including rpc_client.h.
2023-07-19 15:26:14 -05:00
Joseph Huber
90eea57d75 [libc] Fix global constructor being emitted for the RPC client
The indirection here is for some reason causing an unnecessary
constructor. If we leave this uninitialized we will get the default
constructor which simply zero initliaizes the global. I've checked the
output and confirmed that it uses the `zeroinitializer` so this should
be safe.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D155720
2023-07-19 11:38:46 -05:00
Joseph Huber
e537c83975 [libc] Add basic support for calling host functions from the GPU
This patch adds the `rpc_host_call` function as a GPU extension. This is
exported from the `libc` project to use the RPC interface to call a
function pointer via RPC any copying the arguments by-value. The
interface can only support a single void pointer argument much like
pthreads. The function call here is the bare-bones version of what's
required for OpenMP reverse offloading. Full support will require
interfacing with the mapping table, nowait support, etc.

I decided to test this interface in `libomptarget` as that will be the
primary consumer and it would be more difficult to make a test in `libc`
due to the testing infrastructure not really having a concept of the
"host" as it runs directly on the GPU as if it were a CPU target.

Reviewed By: jplehr

Differential Revision: https://reviews.llvm.org/D155003
2023-07-19 10:11:46 -05:00
Joseph Huber
979fb95021 Revert "[libc] Treat the locks array as a bitfield"
Summary:
This caused test failures on the gfx90a buildbot. This works on my
gfx1030 and the Nvidia buildbots, so we'll need to investigate what is
going wrong here. For now revert it to get the bots green.

This reverts commit 05abcc5792.
2023-07-19 09:27:08 -05:00
Guillaume Chatelet
1f5783474f [libc][NFC] Rename files
This patch mostly renames files so it better reflects the function they declare.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D155607
2023-07-19 09:06:29 +00:00
Guillaume Chatelet
bc8c3f4998 [libc][memfunctions] Explicit error when platform in not supported
Reviewed By: JonChesterfield, jhuber6

Differential Revision: https://reviews.llvm.org/D155597
2023-07-19 08:45:04 +00:00
Michael Jones
4d4e18f4ac [libc][NFC] reuse variable in printf string conv
The amount of spaces to pad with is stored in the variable
padding_spaces, previously the actual write calls used the same formula
to calculate the value. This simplifies and clarifies the values by just
reusing the variable.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D155113
2023-07-18 16:30:57 -07:00
Michael Jones
2ff8094e32 [libc] Set min precision for strtofloat fuzz
MPFR has a minimum precision of 2, but the strtofloat fuzz sometimes
would request a precision of 1 for the case of the minimum subnormal.
This patch tells the fuzzer to ignore any case where the precision would
go below 2.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D155130
2023-07-18 16:28:22 -07:00
Joseph Huber
05abcc5792 [libc] Treat the locks array as a bitfield
Currently we keep an internal buffer of device memory that is used to
indicate ownership of a port. Since we only use this as a single bit we
can simply turn this into a bitfield. I did this manually rather than
having a separate type as we need very special handling of the masks
used to interact with the locks.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D155511
2023-07-18 11:34:21 -05:00
Joseph Huber
8759f1b030 Revert "[libc] Default the GPU build to the default memory utilities"
This reverts commit eca8b54a5f.

Another user reverted the patch this was based on leaving this one in a
broken state.
2023-07-18 11:01:38 -05:00
Joseph Huber
eca8b54a5f [libc] Default the GPU build to the default memory utilities
A previous patch made this cause an error on the GPU. We have not yet
dedicated time towards an optimial implementaiton there but we do not
want it to cause an error. We simply use the fallback routines.

Differential Revision: https://reviews.llvm.org/D155615
2023-07-18 10:49:51 -05:00
Jon Chesterfield
f717c2d4f2 Revert "[libc][memfunctions] Explicit error when platform in not supported"
Broke amdgpu libc bot

This reverts commit a39c951730.
2023-07-18 16:41:47 +01:00
Guillaume Chatelet
a39c951730 [libc][memfunctions] Explicit error when platform in not supported
Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D155597
2023-07-18 13:53:03 +00:00
Guillaume Chatelet
23dcdbfba7 [libc][NFC] Split memmove implementations per platform
This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155515
2023-07-18 12:20:23 +00:00
Jay Foad
92542f2a40 [AMDGPU] Add targets gfx1150 and gfx1151
This is the target definition only. Currently they are treated the same
as GFX 11.0.x.

Differential Revision: https://reviews.llvm.org/D155429
2023-07-17 13:06:12 +01:00
Guillaume Chatelet
b38dda74fa [libc][NFC] Split memcmp implementations per platform
This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155181
2023-07-17 11:35:31 +00:00
Guillaume Chatelet
83f3920854 [libc][NFC] Split memset implementations per platform
This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155174
2023-07-17 11:12:19 +00:00
Guillaume Chatelet
8cc440b3e7 [libc][NFC] Split memcpy implementations per platform
This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155099
2023-07-13 10:30:38 +00:00
Guillaume Chatelet
1c4e4e03bd [libc][NFC] Split bcmp implementations per platform
This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155076
2023-07-13 10:19:00 +00:00
Dominic Chen
50414422ac [libc][math] Fix floating-point test support on x86_64 Apple machines
Provide platform-specific x87 FPU definitions and operations

Differential Revision: https://reviews.llvm.org/D153823
2023-07-12 00:38:45 -07:00
Joseph Huber
a608076726 [libc][Obvious] Check if the state hasn't already been destroyed on shutdown
This ensures that if someone calls the `rpc_shutdown` method multiple
times it will not segfault and gracefully continue. This was causing
problems in the OpenMP usage. This could point to other issues, but for
now this is a safe fix.

Differential Revision: https://reviews.llvm.org/D155005
2023-07-11 14:35:38 -05:00
Michael Jones
2cb4731902 [libc] adjust strtofloat precision for subnormals
Subnormal floating point numbers have a lower effective precision than
normal floating point numbers. This can cause issues for the fuzz test
since the MPFR floats have a constant precision regardless of the
exponent, and the precision must match exactly or else create rounding
errors. To solve this problem, the precision of the MPFR floats is
dynamically calculated.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D154909
2023-07-11 11:27:19 -07:00
Joseph Huber
a4f553fcde [libc] Fix using the libcgpu.a for NVPTX in non-LTO builds
CUDA requires a PTX feature to be compiled generally, because the
`libcgpu.a` archive contains LLVM-IR we need to have one present to
compile it. Currently, the wrapper fatbinary format we use to
incorporate these into single-source offloading languages has a special
option to provide this. Since this was not present in the builds, if the
user did not specify it via `-foffload-lto` it would not compile from
CUDA or OpenMP due to the missing PTX features. Fix this by passing it
to the packager invocation.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D154864
2023-07-10 13:54:47 -05:00
Joseph Huber
b454e7aa7c [libc] Remove GPU string functions incompatible with C++
These functions have definitions differing between C and C++. GNU
respects the C++ definitions while the LLVM libc does not. This causes
many bugs and the current hack creates other issues. Rather than hack
around this I'd rather temporarily disable these than regress with the
integration into other offloading languages. We lose test support for
them but we should be able to re-enable these once the `libc` headers
provide these correctly.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154850
2023-07-10 10:40:10 -05:00
Petr Hosek
0ab14951db [NFC][libc] Use the new style includes for tests
This was accidentally omitted from D154746.
2023-07-10 07:42:13 +00:00
Petr Hosek
36c15be20b [libc] Use LIBC_INCLUDE_DIR in CMake rules
D152592 introduced LIBC_INCLUDE_DIR for the location of the include
directory, use it in relevant CMake rules.

Differential Revision: https://reviews.llvm.org/D154278
2023-07-10 07:32:24 +00:00
Guillaume Chatelet
bfd94882f2 [libc][NFC] Move aligned access implementations to separate header
Follow up on https://reviews.llvm.org/D154770

Differential Revision: https://reviews.llvm.org/D154800
2023-07-09 22:17:05 +00:00
Guillaume Chatelet
dbaa5838c1 [libc][NFC] Move memfunction's byte per byte implementations to a separate header
There will be subsequent patches to move things around and make the file layout more principled.

Differential Revision: https://reviews.llvm.org/D154770
2023-07-09 07:21:58 +00:00
Petr Hosek
fb149e4beb [libc] Use the new style includes for tests
This is a follow up to D154529 covering tests.

Differential Revision: https://reviews.llvm.org/D154746
2023-07-08 05:15:44 +00:00
Petr Hosek
9654bc3960 Revert "[libc] Set include directories for the str_to_float test"
This reverts commit 147c0640a3 since
it broke GPU builds.
2023-07-07 21:25:23 +00:00
Joseph Huber
2a65d0388c [libc] Add support for creating wrapper headers for offloading in clang
This is an alternate approach to the patches proposed in D153897 and
D153794. Rather than exporting a single header that can be included on
the GPU in all circumstances, this patch chooses to instead generate a
separate set of headers that only provides the declarations. This can
then be used by external tooling to set up what's on the GPU. This
leaves room for header hacks for offloading languages without needing to
worry about the `libc` implementation.

Currently this generates a set of headers that only contain the
declarations. These will then be installed to a new clang resource
directory called `llvm_libc_wrappers/` which will house the shim code.
We can then automaticlaly include this from `clang` when offloading to
wrap around the headers while specifying what's on the GPU.

Reviewed By: jdoerfert, JonChesterfield

Differential Revision: https://reviews.llvm.org/D154036
2023-07-07 16:02:33 -05:00
Petr Hosek
bf171aaa7a Revert "[libc] Use LIBC_INCLUDE_DIR in CMake rules"
This reverts commit 6e821f0b3a since
it broke the libc-aarch64-ubuntu-fullbuild-dbg bot.
2023-07-07 20:52:54 +00:00
Petr Hosek
6e821f0b3a [libc] Use LIBC_INCLUDE_DIR in CMake rules
D152592 introduced LIBC_INCLUDE_DIR for the location of the include
directory, use it in relevant CMake rules.

Differential Revision: https://reviews.llvm.org/D154278
2023-07-07 20:42:25 +00:00
Petr Hosek
147c0640a3 [libc] Set include directories for the str_to_float test
This test uses libc headers and need to explicitly include them.

Differential Revision: https://reviews.llvm.org/D154277
2023-07-07 20:33:54 +00:00
Joseph Huber
691dc2d10d [Libomptarget] Begin implementing support for RPC services
This patch adds the intial support for running an RPC server in
libomptarget to handle host services. We interface with the library
provided by the `libc` project to stand up a basic server. We introduce
a new type that is controlled by the plugin and has each device
intialize its interface. We then run a basic server to check the RPC
buffer.

This patch does not fully implement the interface. In the future each
plugin will want to define special handlers via the interface to support
things like malloc or H2D copies coming from RPC. We will also want to
allow the plugin to specify t he number of ports. This is currently
capped in the implementation but will be adjusted soon.

Right now running the server is handled by whatever thread ends up doing
the waiting. This is probably not a completely sound solution but I am
not overly familiar with the behaviour of OpenMP tasks and what would be
required here. This works okay with synchrnous regions, and somewhat
fine with `nowait` regions, but I've observed some weird behavior when
one of those regions calls `exit`.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D154312
2023-07-07 12:36:46 -05:00
Joseph Huber
c012eb79e2 [libc] Enable aliasing on AMDGPU targets
AMDGPU supports aliases now, so we can drop this case and leave it only
for the NVPTX target. Unfortunately it's unlikely that NVPTX will be
able to support this in the future due to their PTX language being very
limited.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D154704
2023-07-07 11:49:16 -05:00
Guillaume Chatelet
cb1468d3cb [libc] Adding a version of memcpy w/ software prefetching
For machines with a lot of cores, hardware prefetchers can saturate the memory bus when utilization is high.
In this case it is desirable to turn off the hardware prefetcher completely.
This has a big impact on the performance of memory functions such as `memcpy` that rely on the fact that the next cache line will be readily available.

This patch adds the 'LIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING' compile time option that generates a version of memcpy with software prefetching. While not fully restoring the original performances it mitigates the impact to an acceptable level.

Reviewed By: rtenneti

Differential Revision: https://reviews.llvm.org/D154494
2023-07-07 10:37:32 +00:00
Joseph Huber
6ca6cdb23e Revert "[libc] Add support for creating wrapper headers for offloading in clang"
This reverts commit a4a26374aa.

This was causing some problems with the CPU build and CUDA buildbot.
Revert until I can figure out what those issues are and fix them. I
believe it is just some CMake.
2023-07-06 18:26:41 -05:00
Joseph Huber
a4a26374aa [libc] Add support for creating wrapper headers for offloading in clang
This is an alternate approach to the patches proposed in D153897 and
D153794. Rather than exporting a single header that can be included on
the GPU in all circumstances, this patch chooses to instead generate a
separate set of headers that only provides the declarations. This can
then be used by external tooling to set up what's on the GPU. This
leaves room for header hacks for offloading languages without needing to
worry about the `libc` implementation.

Currently this generates a set of headers that only contain the
declarations. These will then be installed to a new clang resource
directory called `llvm_libc_wrappers/` which will house the shim code.
We can then automaticlaly include this from `clang` when offloading to
wrap around the headers while specifying what's on the GPU.

Reviewed By: jdoerfert, JonChesterfield

Differential Revision: https://reviews.llvm.org/D154036
2023-07-06 18:10:49 -05:00
Joseph Huber
c850ea1498 [libc] Support fopen / fclose on the GPU
This patch adds the necessary support for the fopen and fclose functions
to work on the GPU via RPC. I added a new test that enables testing this
with the minimal features we have on the GPU. I will update it once we
have `fread` and `fwrite` to actually check the outputted strings. For
now I just relied on checking manually via the outpuot temp file.

Reviewed By: JonChesterfield, sivachandra

Differential Revision: https://reviews.llvm.org/D154519
2023-07-05 18:31:58 -05:00
Joseph Huber
7e88e26d38 [libc] Add GPU support for the 'inttypes.h' functions
Another low hanging fruit we can put on the GPU, this ports the tests
over to the hermetic framework so we can run them on the GPU.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D154540
2023-07-05 17:47:10 -05:00
Joseph Huber
515bd1c9b8 [libc][Obvious] Fix timing on AMDGPU not being initialized
Summary:
Reviewer requested that this routine not be a macro, however that means
that it was not being intitialized as the static initializer was done
before the memcpy from the device. Fix this so we can get timing
information.
2023-07-05 16:08:37 -05:00
Joseph Huber
80504b06ad [libc][Obvious] Fix bad macro check on NVPTX tests
Summary:
I forgot to add the `defined()` check on NVPTX.
2023-07-05 15:54:12 -05:00
Joseph Huber
5db39796bf [libc] Support timing information in libc tests
This patch adds the necessary support to provide timing information in
`libc` tests. This is useful for determining which tests look what
amount of time. We also can use this as a test basis for providing more
fine-grained timing when implementing things on the GPU.

The main difficulty with this is the fact that the AMDGPU fixed
frequency clock operates at an unknown frequency. We need to read this
on a per-card basis from the driver and then copy it in. NVPTX on the
other hand has a fixed clock at a resolution of 1ns. I have also
increased the resolution of the print-outs as the majority of these are
below a millisecond for me.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154446
2023-07-05 14:27:08 -05:00
Michael Jones
cfbcbc8f88 [libc] fix MPFR rounding problems in fuzz test
The accuracy for the MPFR numbers in the strtofloat fuzz test was set
too high, causing rounding issues when rounding to a smaller final
result.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D154150
2023-07-05 10:53:40 -07:00
Petr Hosek
8910cc2742 [libc] Use the new style includes
We should be using the standard includes.

Differential Revision: https://reviews.llvm.org/D154529
2023-07-05 17:51:41 +00:00
Petr Hosek
e1cb5924cb Revert "[libc] Use LIBC_INCLUDE_DIR in CMake rules"
This reverts commit 046deabd93 since
it broke libc-aarch64-ubuntu-fullbuild-dbg.
2023-07-05 17:20:11 +00:00
Petr Hosek
046deabd93 [libc] Use LIBC_INCLUDE_DIR in CMake rules
D152592 introduced LIBC_INCLUDE_DIR for the location of the include
directory, use it in relevant CMake rules.

Differential Revision: https://reviews.llvm.org/D154278
2023-07-05 17:16:19 +00:00
Petr Hosek
80368a104e [libc] Check if the hermetic test target exists
When crt1 isn't available, which is typical on baremetal, hermetic tests
aren't created and the hermetic test target won't be available.

Differential Revision: https://reviews.llvm.org/D154279
2023-07-05 17:09:01 +00:00
Siva Chandra
3db36d6a9b [libc] Initiliaze the global pointer in riscv startup code.
Reviewed By: mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D151539
2023-07-05 07:32:31 +00:00
Joseph Huber
f8cf210576 [libc] Remove flaky static assert from RPC interface
Summary:
This function is intended to only be used on the GPU as a shorthand. The
static assert should only fire if it's called ,but it seems that its
precence can sometimes cause issues and other times not. Simply remove
it as it's causing build problems.
2023-07-04 11:06:06 -05:00
Alfred Persson Forsberg
cae84d8acf [libc] Correct usage of __unix__ and __linux__
Reviewed By: michaelrj, thesamesam

Differential Revision: https://reviews.llvm.org/D153729
2023-07-03 01:08:15 +01:00
Petr Hosek
1c241bb791 [libc] Missing FEnvImpl.h dependency on math.h
FEnvImpl.h includes math.h and so needs an explicit dependency.

Differential Revision: https://reviews.llvm.org/D154044
2023-07-01 18:27:36 +00:00
Roland McGrath
5bf8efd269 [libc] Fix more inline definitions
Fix a bunch more instances of incorrect use of the `static`
keyword and missing use of LIBC_INLINE and LIBC_INLINE_VAR
macros. Note that even forward declarations and generic template
declarations must follow the prescribed patterns for libc code so
that they match every definition, all template specializations.

Reviewed By: Caslyn

Differential Revision: https://reviews.llvm.org/D154260
2023-06-30 14:46:25 -07:00
Roland McGrath
dbd38b1219 [libc] Add missing cast in x86 big_endian_cmp_mask
Implicit narrowing conversions from int to uint16_t
get a compiler warning with the warning settings used
in the Fuchsia build.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D154256
2023-06-30 14:15:59 -07:00
Joseph Huber
df52a22b1b [libc] Make the RPC server target always available
This patch makes sure that we always build the RPC server. The proposed
used for this is to begin integrating this server implementation into
`libomptarget`. That requires that we build this server ahead of time
when using a `LLVM_ENABLE_PROJECTS` build. Make a few tweaks to ensure
that the GCC compiler which may be used for this build doesn't complain.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154105
2023-06-30 11:30:57 -05:00
Joseph Huber
62f57bc9b0 [libc] Add other RPC callback methods to the RPC server
This patch adds the other two methods to the server so the external
users can use the interface through the obfuscated interface.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154224
2023-06-30 11:29:37 -05:00
Guillaume Chatelet
1c814c99aa [libc] Improve memcmp latency and codegen
This is based on ideas from @nafi to:
 - use a branchless version of 'cmp' for 'uint32_t',
 - completely resolve the lexicographic comparison through vector
   operations when wide types are available. We also get rid of byte
   reloads and serializing '__builtin_ctzll'.

I did not include the suggestion to replace comparisons of 'uint16_t'
with two 'uint8_t' as it did not seem to help the codegen. This can
be revisited in sub-sequent patches.

The code been rewritten to reduce nested function calls, making the
job of the inliner easier and preventing harmful code duplication.

Reviewed By: nafi3000

Differential Revision: https://reviews.llvm.org/D148717
2023-06-30 13:00:58 +00:00
Joseph Huber
b15ac1fd89 [libc] Enable the 'div' routines on the GPU
This patch simply enables the `div`, `ldiv,` and, `lldiv` functions on
the GPU. This should be straightforward enough.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D154143
2023-06-29 15:42:46 -05:00
Joseph Huber
667c10353e [libc] Fix the implementation of exit on the GPU
The RPC calls all have delays associated with them. Currently the `exit`
function does an async send and immediately exits the GPU. This can have
the effect that the RPC server never sees the exit call and we continue.
This patch changes that to first sync with the server before continuing
to perform its exit. There is still a hazard here, where the kernel can
complete before the RPC call reads back its response, but this is simply
multi-threaded hazards. This change ensures that the server *will*
always exit some time after the GPU exits.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154112
2023-06-29 13:22:23 -05:00
Guillaume Chatelet
177583c914 [libc][NFC] Use SIZE_MAX instead of size_t(-1) 2023-06-29 12:21:43 +00:00
Tue Ly
de19101e33 [libc][NFC] Set rounding mode for sincosf exhaustive test. 2023-06-28 20:30:54 -04:00
Tue Ly
f320fefc4a [libc][math] Implement erff function correctly rounded to all rounding modes.
Implement correctly rounded `erff` functions.

For `x >= 4`, `erff(x) = 1` for `FE_TONEAREST` or `FE_UPWARD`, `0x1.ffffep-1` for `FE_DOWNWARD` or `FE_TOWARDZERO`.

For `0 <= x < 4`, we divide into 32 sub-intervals of length `1/8`, and use a degree-15 odd polynomial to approximate `erff(x)` in each sub-interval:
```
  erff(x) ~ x * (c0 + c1 * x^2 + c2 * x^4 + ... + c7 * x^14).
```

For `x < 0`, we can use the same formula as above, since the odd part is factored out.

Performance tested with `perf.sh` tool from the CORE-MATH project on AMD Ryzen 9 5900X:

Reciprocal throughput (clock cycles / op)
```
$ ./perf.sh erff --path2
GNU libc version: 2.35
GNU libc release: stable
-- CORE-MATH reciprocal throughput --  with -march=native      (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 11.790 + 0.182 clc/call; Median-Min = 0.154 clc/call; Max = 12.255 clc/call;
-- CORE-MATH reciprocal throughput --  with -march=x86-64-v2      (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 14.205 + 0.151 clc/call; Median-Min = 0.159 clc/call; Max = 15.893 clc/call;

-- System LIBC reciprocal throughput --
[####################] 100 %
Ntrial = 20 ; Min = 45.519 + 0.445 clc/call; Median-Min = 0.552 clc/call; Max = 46.345 clc/call;

-- LIBC reciprocal throughput --  with -mavx2 -mfma     (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 9.595 + 0.214 clc/call; Median-Min = 0.220 clc/call; Max = 9.887 clc/call;
-- LIBC reciprocal throughput --  with -msse4.2     (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 10.223 + 0.190 clc/call; Median-Min = 0.222 clc/call; Max = 10.474 clc/call;
```

and latency (clock cycles / op):
```
$ ./perf.sh erff --path2
GNU libc version: 2.35
GNU libc release: stable
-- CORE-MATH latency --  with -march=native      (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 38.566 + 0.391 clc/call; Median-Min = 0.503 clc/call; Max = 39.170 clc/call;
-- CORE-MATH latency --  with -march=x86-64-v2      (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 43.223 + 0.667 clc/call; Median-Min = 0.680 clc/call; Max = 43.913 clc/call;

-- System LIBC latency --
[####################] 100 %
Ntrial = 20 ; Min = 111.613 + 1.267 clc/call; Median-Min = 1.696 clc/call; Max = 113.444 clc/call;

-- LIBC latency --  with -mavx2 -mfma     (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 40.138 + 0.410 clc/call; Median-Min = 0.536 clc/call; Max = 40.729 clc/call;
-- LIBC latency --  with -msse4.2     (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 44.858 + 0.872 clc/call; Median-Min = 0.814 clc/call; Max = 46.019 clc/call;
```

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D153683
2023-06-28 13:58:37 -04:00
Guillaume Chatelet
b3b54131d0 [libc][NFC] Separate avx/no-avx x86 memcpy implementations
Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D153958
2023-06-28 13:56:56 +00:00
Tue Ly
e9074d019e [libc] Fix missing dependency and linking option for sqrtf exhaustive test. 2023-06-28 08:13:53 -04:00
Tue Ly
9532074a9d [libc][math] Clean up exhaustive tests implementations.
Clean up exhaustive tests.  Let check functions return number of failures instead of passed/failed.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D153682
2023-06-28 07:58:46 -04:00
Joseph Huber
31c154881c [libc] Allow the RPC client to be initialized via a H2D memcpy
The RPC client must be initialized to set a pointer to the underlying
buffer. This is currently done with the `reset` method which may not be
ideal for the use-case. We want runtimes to be able to initialize this
without needing to call a kernel. Recent changes allowed the `Client`
type to be trivially copyable. That means we can create a client on the
server side and then copy it over. To that end we take the existing
externally visible symbol and initialize it to the client's pointer.
Therefore we can look up the symbol and copy it over once loaded.

No test currently, I tested with a demo OpenMP application but couldn't think of
how to put that in-tree.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D153633
2023-06-26 10:41:32 -05:00