2967 Commits

Author SHA1 Message Date
OverMighty
0eb9e021b1
[libc][math][c23] Add fabsf16 C23 math function (#93567)
cc @lntue
2024-05-30 15:37:15 -04:00
Hendrik Hübner
485f9f5895
Reland: [libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions (#93622)
New pull request for https://github.com/llvm/llvm-project/issues/89443

The previous PR was reverted after breaking fullbuild due to a missing
struct declaration, which I forgot to commit.

Reverts revert and adds the missing pthread_rwlockattr_getkind_np /
pthread_rwlockattr_setkind_np functions and tests respecitvely.
2024-05-30 11:27:50 -07:00
Guillaume Chatelet
662b130529
[libc][NFC] Tighten up guard conditions for sqrt and polyeval (#93791)
Found while investigating #93709
2024-05-30 14:24:05 +02:00
lntue
4486fcba75
[libc] Add proxy header for float.h. (#93504)
This is the continuation of
https://github.com/llvm/llvm-project/pull/88674.

Fixes #88433, #90496.

---------

Co-authored-by: aniplcc <aniplccode@gmail.com>
2024-05-28 19:14:26 -04:00
Schrodinger ZHU Yifan
0694552cb7
[libc] clean up MutexLock (#93619) 2024-05-28 15:56:17 -07:00
Michael Kruse
e14f5f225a
Revise IDE folder structure (#89755)
Update the folder titles for targets in the monorepository that have not
seen taken care of for some time. These are the folders that targets are
organized in Visual Studio and XCode
(`set_property(TARGET <target> PROPERTY FOLDER "<title>")`)
when using the respective CMake's IDE generator.

 * Ensure that every target is in a folder
 * Use a folder hierarchy with each LLVM subproject as a top-level folder
 * Use consistent folder names between subprojects
 * When using target-creating functions from AddLLVM.cmake, automatically
deduce the folder. This reduces the number of
`set_property`/`set_target_property`, but are still necessary when
`add_custom_target`, `add_executable`, `add_library`, etc. are used. A
LLVM_SUBPROJECT_TITLE definition is used for that in each subproject's
root CMakeLists.txt.
2024-05-25 17:41:21 +02:00
Michael Flanders
0f6c4d8b06
[libc][docs] adds macro handling, POSIX status, and validation to docgen (#89421)
docgen now lists macro implementation status in the generated rst files.

Adds POSIX definition link property to docgen json API (`posix-definition`) and
changes the `defined` property of docgen json API to `c-definition`. Now that
docgen's api is getting more specified, adds validation checks to docgen to
start codifying the docgen api spec.

To make sure this all looks good, I've added POSIX definition links to signal.h
as a tester.
2024-05-23 10:48:16 -07:00
Michael Jones
a5bab70d06
[libc] Fix scudo integration test (#92967)
When scudo is built with LLVM-libc's headers, certain functions also
need to be linked from LLVM-libc. This patch adds those functions to the
list to be linked into the specific scudo test, which uses a minimal
subset of libc.

Fixes #92861 and #59453
2024-05-21 16:06:20 -07:00
Nick Desaulniers (paternity leave)
5442e15a15
[libc][__support] move CndVar to __support (#89329)
We should be able to reuse this between the implementation of C11 cnd_t
condition variables and POSIX pthread_cond_t condition variables.

The current implementation is hyper linux specific, making use of Futex. That
obviously wont work outside of linux, so split the OS specific functions off
into their own source outside of the header.

Modifies the return values of the to-be-shared impl to return 0 on success and
-1 on error.  This pattern was shamelessly stolen from Bionic's
[__bionic_thrd_error](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/bits/threads_inlines.h#41).

Fixes: #88580
Link: #88583
2024-05-21 15:49:42 -07:00
Petr Hosek
b3e71ec9f7
[libc] Provide __libc_{init,fini}_array for baremetal (#90828)
These are provided by newlib and many baremetal projects assume they're
available rather than providing their own implementation.
2024-05-21 15:46:04 -07:00
Joseph Huber
00d7e67f83 [libc] Fix constant address space on global clock
Summary:
I did this wrong in the first version, because `extern "C"` doesn't
imply it's extern when used directly.
2024-05-20 18:00:05 -05:00
Nick Desaulniers (paternity leave)
dce197ac92
[libc][errno] remove mips+sparc specific errnos (#92798)
These are untested and unsupported platforms. The pattern used makes sense for
platform specific error numbers, but these are platforms we do not support.
Excise this code.

Link: #91150
2024-05-20 14:15:24 -07:00
Nick Desaulniers (paternity leave)
51ba7a816c
[libc][setjmp] disable -ftrivial-auto-var-init=pattern for now (#92796)
This would consistently fail for me locally, to the point where I could not run
ninja libc-unit-tests without ninja libc_setjmp_unittests failing.

Turns out that since I enabled -ftrivial-auto-var-init=pattern in
commit 1d5c16d ("[libc] default enable -ftrivial-auto-var-init=pattern (#78776)")
this has been a problem. Our x86_64 setjmp definition disabled -Wuninitialized,
so we wound up clobbering these registers and instead backing up
0xAAAAAAAAAAAAAAAA rather than the actual register value.

The implemenation should be rewritten entirely. I've proposed three different
ways to do so (linked below). Until we decide which way to go, at least disable
this hardening feature for this function for now so that the unit tests go back
to green.

Link: #87837
Link: #88054
Link: #88157
Fixes: #91164
2024-05-20 14:05:49 -07:00
Joseph Huber
61565abcdc [libc] Fix constant variable initializer 2024-05-16 15:24:23 -05:00
Joseph Huber
9d0a8eda0b [libc][NFC] Replace address space with constant GPU utility
Summary:
This was written before we had the address space helpers. Using
numerical address spaces is bad so this should be removed.
2024-05-16 15:13:24 -05:00
Joseph Huber
64b3cdc022
[libc] Fix GPU handling for unsupported backends (#92271)
Summary:
If the user does not have the selected backend enabled, we should still
be able to build the LLVM-IR an ddistribute it. This patch makes logic
to suppress tests if the backend can't build it, as well as removing a
flag for the building that's only present int he NVPTX backend.
2024-05-15 14:13:50 -05:00
Joseph Huber
398162ddbc [libc] Fix typo in test message 2024-05-15 07:12:54 -05:00
Jay Foad
1650f1b3d7
Fix typo "indicies" (#92232) 2024-05-15 13:10:16 +01:00
Jan Patrick Lehr
ccbf908b08
[libc] Fix GPU test build error (#92235)
This fixes a build error on the AMDGPU buildbot introduced in PR
https://github.com/llvm/llvm-project/pull/92172
2024-05-15 07:06:40 -05:00
Robin Caloudis
0980f715cf
[libc][errno] Remove previously added errno numbers (#92163)
Introduced in https://github.com/llvm/llvm-project/pull/91150. Not
needed anymore as https://github.com/llvm/llvm-project/pull/92041 fixed
the root cause. `ENAMETOOLONG` and `EOVERFLOW` are well defined in
`<linux/errno.h>`.

Post mortem: Due to the previously missing inclusion of
`<linux/errno.h>` (fixed with
https://github.com/llvm/llvm-project/pull/92041), I misinterpreted an
undefined macro issue during the development of
https://github.com/llvm/llvm-project/pull/91150 as being caused by a
missing definition rather than by the missing inclusion of the linux
header. I realized too late that `ENAMETOOLONG` and `EOVERFLOW` were
correctly defined in `<linux/errno.h>` and that it was my missing
inclusion that caused the problem.
2024-05-15 01:26:23 -04:00
Cyuria
d3455f4ddd
[libc][docs] Fix outdated code review section, as per #91934 (#92051)
As in the title, fixes #91934
2024-05-15 00:13:05 -04:00
Robin Caloudis
a71e2b9d0f
[libc][errno] Remove non asm generic error number (#92172)
The following small thing caught my eye:

1) `EILSEQ` is not part of the generic asm error number macros. See the
[full list of generic asm errno
codes](4b95dc8736/include/uapi/asm-generic/errno-base.h).
AFAIK the generic asm errno numbers are common between different
operating systems and architectures. `EILSEQ` is not part of this common
set of errno's.

2) `EILSEQ`'s value is wrong. During the addition of `EILSEQ` in
https://reviews.llvm.org/D151129, the value `35` was probably chosen as
its the consecutive number. This is not correct. The actual values can
be looked up for example here:
* [For Linux
kernel](https://github.com/search?q=repo%3Atorvalds%2Flinux+EILSEQ&type=code&p=1):
`EILSEQ = 84` (uapi; i.e. x86_64), `EILSEQ = 88` (mips), `EILSEQ = 47`
(parisc)
* [For Darwin
kernel](https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/errno.h#L237):
`EILSEQ = 92`
2024-05-15 00:02:46 -04:00
Guillaume Chatelet
292b300c51
[libc][bug] Fix out of bound write in memcpy w/ software prefetching (#90591)
This patch adds tests for `memcpy` and `memset` making sure that we
don't access buffers out of bounds. It relies on POSIX `mmap` /
`mprotect` and works only when FULL_BUILD_MODE is disabled.

The bug showed up while enabling software prefetching.
`loop_and_tail_offset` is always running at least one iteration but in
some configurations loop unrolled prefetching was actually needing only
the tail operation and no loop iterations at all.
2024-05-14 13:55:24 +02:00
Robin Caloudis
cd45bb2e43
[libc][errno] Remove unnecessary include (#92063)
Since https://github.com/llvm/llvm-project/pull/91150, a proxy header
for the errno macros is available and gets included in `libc_errno.h`
since then.

As `libc_errno.cpp` includes `libc_errno.h`, which already includes the
proxy header `hdr/errno_macros.h`, there's no need to include it in
`libc_errno.cpp` if we are in overlay mode, because the proxy header
takes care to either include our header from libc/include/ (fullbuild)
or the corresponding underlying system header (overlay).
2024-05-14 00:58:13 -04:00
Schrodinger ZHU Yifan
96c23af8b3
[libc] fix 32bit arm build (casting time_t) (#92065) 2024-05-14 00:46:17 -04:00
Schrodinger ZHU Yifan
b342d18a8f
[libc] add timeout and clock conversion utilities (#91905)
This PR:

- Make `clock_gettime` a header-only library
- Add `clock_conversion` header library to allow conversion between
clocks relative to the time of call
- Add `timeout` header library to manage the absolute timeout used in
POSIX's timed locking/waiting APIs
2024-05-13 19:00:19 -04:00
lntue
8960078765
[libc][errno] Include <linux/errno.h> for Linux in full build mode. (#92041) 2024-05-13 18:31:35 -04:00
Robin Caloudis
561c42df57
[libc][errno] Use macro instead of system header (#91150)
## Why
Currently, the system header `errno.h` is included in `libc_errno.h`,
which is supposed to be consumed by internal implementations only. As
unit and hermetic tests should never use `#include <errno.h>` but
instead use `#include "src/errno/libc_errno.h"`, we do not want to
implicitly include `errno.h`. In order to have a clear seperation
between those two, we want to pull out the definitions of errno numbers
from `errno.h`.

## What
* Extract the definitions of errno numbers from
[include/errno.h.def](https://github.com/llvm/llvm-project/pull/91150/files#diff-ed38ed463ed50571b498a5b69039cab58dc9d145da7f751a24da9d77f07781cd)
and place it under
[include/llvm-libc-macros/linux/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-d6192866629690ebb7cefa1f0a90b6675073e9642f3279df08a04dcdb05fd892)
* Provide mips-specific errno numbers in
[include/llvm-libc-macros/linux/mips/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-3fd35a4c94e0cc359933e497b10311d857857b2e173e8afebc421b04b7527743)
* Find definition of mips errno numbers in glibc
[here](ea73eb5f58/sysdeps/unix/sysv/linux/mips/bits/errno.h (L32-L50))
(equally defined in the Linux kernel)
* Provide sparc-specific errno numbers in
[include/llvm-libc-macros/linux/sparc/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-5f16ffb2a51a6f72ebd4403aca7e1edea48289c99dd5978a1c84385bec4f226b)
* Find definition of sparc errno numbers in glibc
[here](ea73eb5f58/sysdeps/unix/sysv/linux/sparc/bits/errno.h (L33-L51))
(equally defined in the Linux kernel)
* Include proxy header `errno_macros.h` instead of the system header
`errno.h` in `libc_errno.h`/`libc_errno.cpp`

Closes https://github.com/llvm/llvm-project/issues/80172
2024-05-13 17:56:01 -04:00
Schrodinger ZHU Yifan
27595c4bef
Revert "[libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions" (#91966)
Reverts llvm/llvm-project#90249

Fullbuild is broken:
https://lab.llvm.org/buildbot/#/builders/163/builds/56501
2024-05-13 09:43:14 -04:00
Hendrik Hübner
d2676a7333
[libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions (#90249)
Closes #89443

I added the two missing functions and respective test cases. Let me know
if anything needs changing.
2024-05-13 09:35:17 -04:00
Joseph Huber
fb3f4b013c
[libc] Add memory fence utility to the GPU utilities (#91756)
Summary:
GPUs like to execute instructions in the background until something
excplitely consumes them. We are working on adding some
microbenchmarking code, which requires flushing the pending memory
operations beforehand. This patch simply adds these utility functions
that will be used in the near future.
2024-05-10 16:38:13 -05:00
Schrodinger ZHU Yifan
d8e73752a5
Reland "[libc][NFC] adjust time related implementations"" (#91687)
Reverts llvm/llvm-project#91657 and Relands #91485
2024-05-10 15:32:04 -04:00
Schrodinger ZHU Yifan
b8f4f39d3d
[libc] avoid cmpxchg on the fastpath of callonce (#91748)
Avoid `cmpxchg` operation if the function has already been called.
The destination operand of `cmpxchg` may receive a write cycle without
regard to the result of the comparison
2024-05-10 11:55:02 -04:00
lntue
92dfe20acd
[libc][math] Fix exact cases for powf. (#91488)
It was reported from the CORE-MATH project that the `powf`
implementation did not round correctly when `x^y` is either exact for
exactly half-way.

This PR deals with the potential exact cases when `y` is an integer `2 <
y < 25`. In such cases, the results of `x^y` is exactly representable in
double precision.
2024-05-10 11:07:41 -04:00
Schrodinger ZHU Yifan
5a0e0b659f
Revert "[libc][NFC] adjust time related implementations" (#91657)
Reverts llvm/llvm-project#91485. It breaks GPU and fuchisa.
2024-05-09 17:27:59 -04:00
Schrodinger ZHU Yifan
8ac928fea8
[libc][NFC] adjust time related implementations (#91485) 2024-05-09 16:34:45 -04:00
lntue
e1f279e92d
[libc] Use __builtin_fma(f) by default if LIBC_TARGET_CPU_HAS_FMA is defined. (#91535) 2024-05-09 13:10:43 -04:00
lntue
6f1013a5b3
[libc] Add template deduction guide for cpp::lock_guard. (#91589)
Fix ctad-maybe-unsupported warnings for `cpp::lock_guard`.
2024-05-09 09:31:06 -04:00
Joseph Huber
a7ee81e827 [libc] Remove unused variable causing build errors 2024-05-09 07:00:25 -05:00
Vlad Mishel
c4a3d184db
[libc] Replace MutexLock with cpp::lock_guard (#89340)
This PR address issue #89002.

#### Changes in this PR

* Added a simple implementation of `cpp::lock_guard` (an equivalent of
`std::lock_guard`) in libc/src/__support/CPP inspired by the libstdc++
implementation
* Added tests for `cpp::lock_guard` in
/libc/test/src/__support/CPP/mutex_test.cpp
* Replaced all references to `MutexLock` with `cpp::lock_guard`

---------

Co-authored-by: Guillaume Chatelet <gchatelet@google.com>
2024-05-09 09:06:18 +02:00
lntue
e37bd6c68b
[libc][fenv] Add missing FE_* definitions for some environment. (#91519) 2024-05-08 17:32:05 -04:00
Schrodinger ZHU Yifan
a5044e6d50
[libc] fix typo due to futex renaming (#91379) 2024-05-07 17:36:58 -04:00
Joseph Huber
873431a68a
[libc] Add __FE_DENORM to the fenv macros (#91353)
Summary:
Some targets support denormals as floating point exceptions. This is
provided as an extension in the GNU headers as __FE_DENORM.

This provides it in our headers, however I'm unsure if we should make it
internal or external. I do not think it should be in all exception as it
doesn't represent an exceptional behavior as far as the standard is
concerned, but I'm not an expert.
2024-05-07 12:57:54 -05:00
Schrodinger ZHU Yifan
ab3a9e724d
[libc] clean up futex usage (#91163)
# Motivation

Futex syscalls are widely used in our codebase as synchronization
mechanism. Hence, it may be worthy to abstract out the most common
routines (wait and wake). On the other hand, C++20 also provides
`std::atomic_notify_one/std::atomic_wait/std::atomic_notify_all` which
align with such functionalities. This PR introduces `Futex` as a subtype
of `cpp::Atomic<FutexWordType>` with additional
`notify_one/notify_all/wait` operations.

Providing such wrappers also make future porting easier. For example,
FreeBSD's `_umtx_op` and Darwin's `ulock` can be wrapped in a similar
manner.

### Similar Examples

1. [bionic
futex](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/bionic/bionic_futex.cpp)
2. [futex in Rust's
std](8cef37dbb6/library/std/src/sys/pal/unix/futex.rs (L21))
2024-05-07 10:47:41 -04:00
Benjamin Kramer
1d87465a0a [libc][math] fmod: clear exceptions before the test instead of after
The test has no control over the CPU state before the test runs.

This test checks whether no exception flags are set, which may not be
true at the start of the test. This used to be not a problem because the
check was broken but that was fixed in ecfb5d9951554d8bdb6a499c958f48cc35f78a88
2024-05-07 15:27:55 +02:00
Schrodinger ZHU Yifan
096f85e827
[libc] add more APIs of cmgxchg variants (#91208)
Such APIs are useful in lock implementations
2024-05-06 18:55:18 -04:00
Michael Flanders
ecfb5d9951
[libc][math] fix loose except check in {EXPECT,ASSERT}_FP_EXCEPTION macros (#88816)
Adds more FP test macros for the upcoming test adds for #61092 and the
issues opened from it: #88768, #88769, #88770, #88771, #88772.

Fix bug in `{EXPECT,ASSERT}_FP_EXCEPTION`. `EXPECT_FP_EXCEPTION(0)`
seems to be used to test that an exception did not happen, but it always
does `EXPECT_GE(... & 0, 0)` which never fails.

Update and refactor tests that break after the above bug fix. An
interesting way things broke after the above change is that
`ForceRoundingMode` and `quick_get_round()` were raising the inexact
exception, breaking a lot of the `atan*` tests.

The changes for all files other than `FPMatcher.h` and
`libc/test/src/math/smoke/RoundToIntegerTest.h` should have the same
semantics as before. For `RoundToIntegerTest.h`, lines 56-58 before the
changes do not always hold since this test is used for functions with
different exception and errno behavior like `lrint` and `lround`. I've
deleted those lines for now, but tests for those cases should be added
for the different nearest int functions to account for this.

Adding @nickdesaulniers for review.
2024-05-06 09:05:22 -04:00
luolent
a98a6e95be
Add clarifying parenthesis around non-trivial conditions in ternary expressions. (#90391)
Fixes [#85868](https://github.com/llvm/llvm-project/issues/85868)

Parenthesis are added as requested on ternary operators with non trivial conditions.

I used this [precedence table](https://en.cppreference.com/w/cpp/language/operator_precedence) for reference, to make sure we get the expected behavior on each change.
2024-05-04 18:38:45 +01:00
Joseph Huber
1022636b0c
[libc] Fix assert dependency on macro header (#91036)
Summary:
This file was missing a dependency so it wasn't being installed.
2024-05-03 20:57:34 -05:00
Schrodinger ZHU Yifan
0e5ff6251f
[libc] add hashtable fuzzing (#87949) 2024-05-02 15:36:10 -04:00