Commit Graph

2170 Commits

Author SHA1 Message Date
Guillaume Chatelet
74971db140
[libc] Add is_scalar (#65740)
Adds the is_scalar traits based on implementation in
https://en.cppreference.com/w/cpp/types/is_scalar
2023-09-08 12:45:17 +00:00
Guillaume Chatelet
eebf8faf3e
[libc] Add is_member_pointer_v (#65631)
Implementation from
https://en.cppreference.com/w/cpp/types/is_member_pointer
2023-09-08 11:36:19 +02:00
Michael Jones
dd51ae81d8 [libc] Fix printf %p format
The %p format wasn't correctly passing along flags and modifiers to the
integer conversion behind the scenes. This patch fixes that behavior, as
well as changing the nullptr behavior to be a string conversion behind
the scenes.

Reviewed By: lntue, jhuber6

Differential Revision: https://reviews.llvm.org/D159458
2023-09-07 14:13:35 -07:00
Mikhail R. Gadelha
9ff0a447d0 [libc] Fix setrlimit/getrlimit on 32-bit systems
libc uses SYS_prlimit64 (which takes a struct rlimit64) to implement
setrlimt and getrlimit (which take a struct rlimit). In 64-bit bits
systems this is not an issue since the members of struct rlimit64 and
struct rlimit are 64 bits long, however, in 32-bit systems the members
of struct rlimit are only 32 bits long, causing wrong values being
passed to SYS_prlimit64.

This patch changes rlim_t to be __UINT64_TYPE__ (which also changes
rlimit as a side-effect), fixing the problem of mismatching types in
the syscall.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159104
2023-09-07 16:02:32 -03:00
Joseph Huber
4792ae5cd5
[libc] Fix building the RPC server with LIBC_NAMESPACE (#65642)
A recent patch required the implementation to define `LIBC_NAMESPACE`.
For GPU offloading we provide a static library whose internal
implementation relies on the `libc` headers. This is a separate library
that is constructed during the "bootstrap" phase. This patch moves the
definition of the `LIBC_NAMESPACE` CMake variable up so its available
during bootstrapping and adds it to the definition of the RPC server.
2023-09-07 12:47:36 -05:00
Joseph Huber
d6cc3410ab
[libc] Fix missing GPU math implementations (#65616)
These functions were implemented by simply calling their `__builtin_*`
equivalents.
The builtins were resolving to the libc functions back again. This patch
adds explicit
vendor versions for these functions to avoid the recursion.
2023-09-07 11:48:44 -05:00
Guillaume Chatelet
260036ab1e
[libc] move in_place_t in utility (#65623)
This is needed because `cpp::in_place_t` is also used by `cpp::expected`
https://en.cppreference.com/w/cpp/utility/in_place
2023-09-07 18:12:50 +02:00
Guillaume Chatelet
a279bf0d78
[libc] Add is_null_pointer_v (#65627) 2023-09-07 18:07:24 +02:00
Guillaume Chatelet
f72d41b5b1
[libc] Add missing include in type_traits/remove_all_extents.h (#65626) 2023-09-07 17:57:47 +02:00
Guillaume Chatelet
26ddf2c935
[libc] fix missing default template parameter value in enable_if (#65622) 2023-09-07 17:42:00 +02:00
Tue Ly
f0d05bb699 [libc][math] Fix signed zeros for acosf, acoshf, and atanf in FE_DOWNWARD mode.
Fix signed zeros for acosf, acoshf, and atanf in FE_DOWNWARD mode.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159476
2023-09-07 15:21:33 +00:00
Guillaume Chatelet
9c4e005678
[libc] customizable namespace 2/4 (#65471)
This implements the second step of
https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079

Namely "Add a guard in `src/__support/common.h`"
2023-09-07 17:19:47 +02:00
Mikhail R. Gadelha
4151859770
[libc] Fix gmtime test on systems with sizeof(time_t) == 4 (#65388)
This test creates a time_t variable and assigns 0xfffffffffe1d7b01 which
overflows the maximum time_t value for 64-bit time_t, then checks if the
syscall fails and errno was set.

In systems with sizeof(time_t) == 4, the value is narrowed down to
0xfe1d7b01 and doesn't overflow, causing the test to fail.

This patch then disables the test on systems with 32 bits long time_t.
2023-09-07 09:28:47 -04:00
Mikhail R. Gadelha
80225af4c1
[libc] Fix overflow check for 32 bit long time_t (#65394)
This patch fixes the overflow check in update_from_seconds, used by
gmtime, gmtime_r and mktime.

In update_from_seconds, total_seconds is a int64_t and the previous
overflow check for when sizeof(time_t) == 4 would check if it was <
0x80000000 and > 0x7FFFFFFF, however, this check would cause the
following issues:

1. Valid negative numbers would be discarded, e.g., -1 is
0xffffffffffffffff as a int64_t, outside the range of the overflow
check.

2. Some valid positive numbers would be discarded because the hex
constants were being implicitly converted to int64_t, e.g., 0x80000000
would be implicitly converted to 2147483648, instead of -2147483648.

The fix for both cases was to static_cast total_seconds and the
constants to time_t if sizeof(time_t) == 4. The behaviour is not changed
in systems with sizeof(time_t) == 8.

---------

Signed-off-by: Mikhail R. Gadelha <mikhail@igalia.com>
2023-09-07 09:18:23 -04:00
Christopher Di Bella
b4d6c0f7c7
[libc] continues header cleanup process (#65556)
* replaces `add_rvalue_reference_t` with `is_rvalue_reference_t`
* includes `"stddef.h"` for `size_t` include

---------

Co-authored-by: Guillaume Chatelet <gchatelet@google.com>
2023-09-07 08:24:31 +02:00
Joseph Huber
cec1de3f35
[libc] Fix vendor implemented math functions not being exported (#65510)
Summary:
A previous introduced a new object type for the GPU functions
implemented by an external vendor library. This was done so they we did
not attempt to run tests on functions which we did not implement,
however this accidentally stopped them from being included in the actual
output. Fix this by checking the new type as well.

The long term goal is to remove this vendor handling altogether, but is
being used as a short-term solution to provide a math library on the
GPU which currently lacks one.
2023-09-06 14:43:23 -05:00
Mikhail R. Gadelha
6f387135ae
[libc] Fix failing mktime test case in 32-bit systems (#65390)
Previously, these tests expected that calling mktime with a struct tm
that caused overlow to succeed with return -1
(TimeConstants::OUT_OF_RANGE_RETURN_VALUE), however, the Succeeds call
expects the errno to be zero (no failure).

This patch fixes the expected calls to fail with EOVERFLOW. These tests
are only enabled to 32-bit systems, and are probably not being tested on
the arm32 buildbot, that's why this was not a problem before.
2023-09-06 14:29:31 -04:00
Mikhail R. Gadelha
10fb71bdff
[libc] Fix test passing negative value in timespec passed to nanosleep (#65346)
This test was setting tv_nsec to a negative value, which as per the
standard this is an EINVAL:

The value in the tv_nsec field was not in the range [0, 999999999] or
tv_sec was negative.

https://man7.org/linux/man-pages/man2/nanosleep.2.html
2023-09-06 14:28:31 -04:00
Mikhail R. Gadelha
ce3bade0cf
[libc] Fix call to clock_gettime (#65166)
The calls were missing the __llvm_libc:: namespace, which can allow the
test case to be linked to glibc's clock_gettime.
2023-09-06 14:26:20 -04:00
Mikhail R. Gadelha
8cd4ecfa60 [libc] Unify lseek implementations
In patch D157792, the calls to SYS_llseek/SYS_llseek for 32-bit systems
were fixed in lseek.cpp but there was another implementation in file.cpp
that was missed.

To reduce the code duplication, this patch unifies both call sites to
use a new lseekimpl function.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159208
2023-09-06 11:56:41 -03:00
Guillaume Chatelet
4acc3ffbb0 [reland][libc][NFC] split type_traits / utility in separate files (#65314)
`type_traits` and `utility` refer to each other in their
implementations. Also `type_traits` starts to become too big to be
manageable. This PR splits each function into individual files. FTR this
is [how libcxx handles large headers as
well](https://github.com/llvm/llvm-project/tree/main/libcxx/include/__type_traits).

The reland adds two missing functions : is_destructible_v and is_reference_v
2023-09-06 09:23:00 +00:00
Guillaume Chatelet
bcdbd0bd0b Revert "[libc][NFC] split type_traits / utility in separate files (#65314)"
This reverts commit b793c7e530.
It broke the libc-x86_64-debian-gcc-fullbuild-dbg build bot.
https://lab.llvm.org/buildbot/#/builders/250/builds/9776
2023-09-06 08:47:45 +00:00
Guillaume Chatelet
b793c7e530
[libc][NFC] split type_traits / utility in separate files (#65314)
`type_traits` and `utility` refer to each other in their
implementations. Also `type_traits` starts to become too big to be
manageable. This PR splits each function into individual files. FTR this
is [how libcxx handles large headers as
well](https://github.com/llvm/llvm-project/tree/main/libcxx/include/__type_traits).
2023-09-06 10:31:08 +02:00
Guillaume Chatelet
56426c6cdf
[libc] customizable namespace 1/4 (#65321)
This implements the first step of
https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079

Make required Bazel and CMake preparatory changes which define the var
for the customizable namespace and use it (pass it as a compile option
-DLIBC_NAMESPACE=<...>).

Differential Revision: https://reviews.llvm.org/D159112
2023-09-06 10:27:56 +02:00
Siva Chandra Reddy
0f1507af41 [libc] Add a JSON based config option system.
Few printf config options have been setup using this new config system
along with their baremetal overrides. A follow up patch will add generation
of doc/config.rst, which will contain the full list of libc config options
and short description explaining how they affect the libc.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D159158
2023-09-05 14:19:18 +00:00
Joseph Huber
ead92ae5fe [libc] Prevent system headers from being included for the GPU build
It's very important that the GPU build does not include any system
directories. We currently use `-ffreestanding` to disable a lot of these
features, but we can still accidentally include them if they are not
provided by `libc` yet. This patch changes this to use `-nostdinc` to
disable all standard search paths. Then we use the compiler's resource
directory to pick up the provided headers like `stdint.h`.

Differential Revision: https://reviews.llvm.org/D159445
2023-09-05 08:43:07 -05:00
Petr Hosek
89fc7e52ab [libc] Include (v)s(n)printf in baremetal configs
These are commonly used on baremetal targets. We disable float
support and other features to reduce the binary size. This would
ideally eventually be handled using the proposed config mechanism:
https://discourse.llvm.org/t/rfc-systematic-way-to-introduce-and-use-libc-config-options/72943
but for now we use a CMake conditional.

Differential Revision: https://reviews.llvm.org/D159067
2023-09-04 02:39:02 +00:00
Alfred Persson Forsberg
b12385f3aa
[libc] [NFC] wait4Impl: include <signal.h> instead of <sys/signal.h>
sys/signal.h currently does not exist in LLVM-libc. Both glibc and
musl provides this header with simply:

> #include <signal.h>
(and a warning in musl libc)

If you try to build LLVM-libc in a sysroot with only LLVM-libc headers
and linux-headers installed then this will fail the build due to
missing headers. However, if --sysroot is not passed, then the
LLVM-libc build will pick up the header from the system which in turn
includes LLVM-libc's signal.h.

LLVM-libc includes <signal.h> in all other cases, so this simply
matches that. Providing <sys/signal.h> could also be done (instead)
for glibc compatibility.
2023-09-04 01:51:08 +02:00
Joseph Huber
701e6f7630 [libc][fix] Fix buffer overrun in initialization of GPU return value
Summary:
The HSA API explicitly states that the size is a count of uint32_t's not
a byte count. This was erroneously being used as a simple memcpy,
causing some weird behaviour. Fix this by correctly passing `1` to
initialize a single integer to zero.
2023-09-02 17:59:01 -05:00
Joseph Huber
65642c7308 [libc][NFC] Support sm_89 and sm_90 NVIDIA GPUs
Summary:
These architectures were left out from the list and should be supported.
2023-09-02 16:23:41 -05:00
Kazu Hirata
5dd9568717 Fix typos in documentation 2023-09-02 09:32:48 -07:00
Fangrui Song
678e3ee123 [lldb] Fix duplicate word typos; NFC
Those fixes were taken from https://reviews.llvm.org/D137338
2023-09-01 21:32:24 -07:00
Joseph Huber
533145c458 [libc] Support 'assert.h' on the GPU
This patch adds the necessary support to provide `assert` functionality
through the GPU `libc` implementation. This implementation creates a
special-case GPU implementation rather than relying on the common
version. This is because the GPU has special considerings for printing.
The assertion is printed out in chunks with `write_to_stderr`, however
when combined with the GPU execution model this causes 32+ threads to
all execute in-lock step. Meaning that we'll get a horribly fragmented
message. Furthermore, potentially thousands of threads could hit the
assertion at once and try to print even if we had it all in one
`printf`.

This is solved by having a one-time lock that each thread group / wave /
warp will attempt to claim. We only let one thread group pass through
while the others simply stop executing. Finally only the first thread in
that group will do the printing until we finally abort execution.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159296
2023-08-31 15:04:43 -05:00
Joseph Huber
07102a1194 [libc] Implement the 'abort' function on the GPU
This function implements the `abort` function on the GPU. The
implementation here closely mirros the `exit` call where we first
synchornize with the RPC server to make sure it's listening and then we
exit on the GPU.

I was unsure if this should be a simple `__builtin_assert` on the GPU. I
elected to go with an RPC approach to make this a more "true" `abort`
call. That is, it should invoke some signal handlers and exit with the
proper code according to the implemented C library on the server.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D159210
2023-08-31 08:40:15 -05:00
Jon Chesterfield
1143da2245 [libc][gpu] Thread divergence fix on volta
The inbox/outbox loads are performed by the current warp, not a single thread.

The outbox load indicates whether a port has been successfully opened. If some
lanes in the warp think it has and others think the port open failed, as the
warp happened to be diverged when the load occurred, all the subsequent control
flow will be incorrect.

The inbox load indicates whether the machine on the other side of the RPC channel
has progressed. If lanes in the warp have different ideas about that, some will
try to progress their state transition while others won't. As far as the RPC layer
is concerned this is a performance problem and not a correctness one - none of the lanes
can start the transition early, only miss it and start late - but in practice the calls
layered on top of RPC do not have the interface required to detect this event and retry
the load on the stalled lanes, so the calls layered on top will be broken.

None of this is broken on amdgpu, but it's likely that the readfirstlane will have
beneficial performance properties there. Possible significant enough that it's
worth landing this ahead of fixing gpu::broadcast_value on volta.

Essentially volta wasn't adequately considered when writing this part of the protocol.
It's a bug present in the initial prototype and propagated thus far, because none of
the test cases push volta into a warp diverged state in the middle of the RPC sequence.

We should have some test cases for volta where port_open and equivalent are called
from diverged warps.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D159276
2023-08-31 14:34:02 +01:00
Joseph Huber
9730e87e13 [libc] Fix running the 'nanosleep' test on unsupported architectures
Summary:
This test is only supported on sm_70. We need to explicitly disable it
on `sm_60` which one of the build bots uses.
2023-08-30 21:20:45 -05:00
Joseph Huber
ca10bc4f41 [libc] Implement the 'nanosleep' function on the GPU
The GPU has the ability to sleep for very short periods of time. We can
map this to the existing `nanosleep` utility. This patch maps the
nanosleep utility to the existing hardware instructions as best as
possible.

Depends on D159118

Reviewed By: JonChesterfield, sivachandra

Differential Revision: https://reviews.llvm.org/D159225
2023-08-30 18:34:59 -05:00
Alex Brachet
1d1f230c92 [libc] Define long long limits if not defined
Some older gcc toolchains don't define these on 32 bit platforms. This
is a problem for pigweed which uses an older gcc toolchain and targets
32 bit.

Differential Revision: https://reviews.llvm.org/D157112
2023-08-30 23:03:36 +00:00
Joseph Huber
20f4f295da [libc] Fix 'clock()' testing on the GPU
Summary:
We should check for the GPU architectures first, since `__linux__` can
be set potentially during these compilations. Also the test needs to be
a hermetic test.
2023-08-30 16:58:11 -05:00
Joseph Huber
30307a7bb7 [libc] Implement the 'clock()' function on the GPU
This patch implements the `clock()` function on the GPU. This function
is supposed to return a timestamp that can be converted into seconds
using the `CLOCKS_PER_SEC` macro. The GPU has a fixed frequency timer
that can be used for this purpose. However, there are some
considerations.

First is that AMDGPU does not have a statically known fixed frequency. I
know internally that the gfx10xx and gfx11xx series use a 100 MHz clock
which will probably remain for the future. Gfx9xx typically uses a 25
MHz clock except for the Vega 10 GPU. The only way to know for sure is
to look it up from the runtime. For this purpose, I elected to default
it to some known values and assign these to an exteranlly visible symbol
that can be initialized if needed. If we do not have a good guess we
just return zero.

Second is that the `CLOCKS_PER_SEC` macro only gives about a microsecond
of resolution. POSIX demands that it's 1,000,000 so it's best that we
keep with this tradition as almost all targets seem to respect this. The
reason this is important is because on the GPU we will almost assuredly
be copying the host's macro value (see the wrapper header) so we should
go with the POSIX version that's most likely to be set. (We could
probably make a warning if the included header doesn't match the
expected value).

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D159118
2023-08-30 16:16:34 -05:00
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