2408 Commits

Author SHA1 Message Date
Nick Desaulniers
606653091d
[libc] build with -Werror (#73966)
A recent commit introduced warnings observable when building unit tests.
If the
unit tests don't fail when warnings are introduced into the build, then
we
might fail to notice them in the stream of output from check-libc.

Link: https://github.com/llvm/llvm-project/pull/72763/files#r1410932348
2023-12-04 11:08:59 -08:00
Petr Hosek
c33e5d59e5
[libc] Add the missing math_extras.h include (#74259)
math_extras.h is used in integer_utils.h when building for 32-bit
platforms but the include is missing.
2023-12-04 11:02:03 -08:00
Schrodinger ZHU Yifan
a0eda10947
[libc][NFC] unify startup library's code style with the rest (#74041)
This PR unifies the startup library's code style with the rest of libc.
2023-12-04 10:31:18 -08:00
Guillaume Chatelet
8628ca29aa
[libc] Fix UB in memory utils (#74295)
The [standard](https://eel.is/c++draft/expr.add#4.3) forbids forming
pointers to invalid objects even if the pointer is never read from or
written to. This patch makes sure that we don't do pointer arithmetic on
invalid pointers.


Co-authored-by: Vitaly Buka <vitalybuka@google.com>
2023-12-04 10:57:35 +01:00
Joseph Huber
9553e156cb [libc] Allocate fine-grained memory for the RPC host symbol
Summary:
This pointer has been causing issues. Allocating and reading from coarse
memory on the CPU is not guaranteed and varies depending on the kernel
version and support. Previously we attempted to pin the memory but this
caused unexpected failures. This should be a legal operation and work
around the problem as fine-grained memory should be always legal to
write to by both sides.
2023-12-01 13:47:33 -06:00
Joseph Huber
8c1d476db0 Revert "[libc] Explicitly pin memory for the client symbol lookup (#73988)"
Summary:
This caused the bots to begin failing. Revert for now to get the bot
green.

This reverts commit 8bea804923a1b028e86b177caccb3258708ca01c.
This reverts commit e1395c7bdbe74b632ba7fbd90e2be2b4d82ee09e.
2023-12-01 13:04:49 -06:00
Joseph Huber
8bea804923
[libc] Move the pointer to pin off the stack to the heap (#74118)
Summary:
This may be problematic to pin a stack pointer. Allocate it via the OS
allocator instead as the documentation suggests.

For some reason, if you attempt to free this pointer after the memory
region has been unlocked, it will return an invalid pointer.
2023-12-01 12:31:34 -06:00
Caslyn Tonelli
3693f44fff
[libc] Exclude Fuchsia from float128 detection (#73985)
Following from https://github.com/llvm/llvm-project/pull/73372:

Fuchsia targets currently don't support `float128`. Add detection for
`LIBC_TARGET_OS_IS_FUCHSIA`, and exclude this OS from setting
`LIBC_COMPILER_HAS_FLOAT128_EXTENSION`.
2023-12-01 10:30:18 -08:00
Guillaume Chatelet
977af4252d
[libc][NFC] Rename SPECIAL_X86_LONG_DOUBLE in LIBC_LONG_DOUBLE_IS_X86_FLOAT80 (#73950) 2023-12-01 14:23:08 +01:00
Guillaume Chatelet
f1d0276e4c
[libc][NFC] Rename LIBC_LONG_DOUBLE_IS_IEEE754_BIN128 to LIBC_LONG_DOUBLE_IS_FLOAT128 (#74052)
To make it consistent with
https://github.com/llvm/llvm-project/pull/73948 and
https://github.com/llvm/llvm-project/pull/73950
2023-12-01 13:57:36 +01:00
Guillaume Chatelet
808b7d2203
[libc][NFC] rename LONG_DOUBLE_IS_DOUBLE into LIBC_LONG_DOUBLE_IS_FLOAT64 (#73948) 2023-12-01 13:55:31 +01:00
Guillaume Chatelet
bb98227db1
[libc][NFC] Remove named_pair (#73952)
`named_pair` does not provide enough value to deserve its own header.
2023-12-01 10:30:15 +01:00
Guillaume Chatelet
2c976a1fac
[libc] Fix _Float16 detection for x86 (#73947) 2023-12-01 09:47:26 +01:00
Guillaume Chatelet
9557fcca56
[libc] Fix lint message (#73956) 2023-12-01 09:32:22 +01:00
Schrodinger ZHU Yifan
54878b80f3
[libc] remove fragile test from mincore (#74026) 2023-11-30 23:35:35 -05:00
Schrodinger ZHU Yifan
985c0d1903
[libc][mincore] use correct page_size for test (#73984) 2023-11-30 20:36:28 -05:00
Joseph Huber
e1395c7bdb
[libc] Explicitly pin memory for the client symbol lookup (#73988)
Summary:
Previously, we determined that coarse grained memory cannot be used in
the general case. That removed the buffer used to transfer the memory,
however we still had this lookup. Though we do not access the symbol
directly, it still conflicts with the agents apparently. Pin this as
well.

This resolves the problems @lntue was having with the `libc` GPU build.
2023-11-30 15:35:33 -06:00
Nick Desaulniers
cc84a14197
[libc] fix getchar_unlocked (#73874)
A typo was leading to getc_unlocked.cpp.o being included into libc.a
twice.

I only noticed because I was trying to convert libc.a to a shared object
via

$ ld.lld -o libc.so --whole-archive libc.a

which errored since getc_unlocked was being defined twice.
2023-11-30 12:38:00 -08:00
Joseph Huber
0584e6c166
[libc] Explicitly pin memory for the HSA memory transfer (#73973)
Summary:
This portion of code handles mapping the RPC client memory over to the
device. HSA copies need to be between two slices of memory that HSA has
allocated. Previously we used coarse-grained memory to act as the host
source. However, the support for this varies depending on the kernel and
version and should not be relied upon. This patch changes that handling
to use the `hsa_amd_memory_lock` API to explicitly pin memory to a
location sufficient for a DMA transfer to the GPU.
2023-11-30 13:46:52 -06:00
Schrodinger ZHU Yifan
418a3a4577
[libc][SysMMan] implement mincore (#73704)
Implement `mincore` as specified in
https://man7.org/linux/man-pages/man2/mincore.2.html
2023-11-30 14:22:36 -05:00
Samira Bazuzi
3f505cd587
[libc] Mark operator== const to avoid ambiguity in C++20. (#73954)
C++20 will automatically generate an operator== with reversed operand
order, which is ambiguous with the written operator== when one argument
is marked const and the other isn't.

This operator currently triggers -Wambiguous-reversed-operator at
several usage sites in libc/test/src/__support/CPP/bitset_test.cpp,
starting with line 153.
2023-11-30 17:22:10 +01:00
Guillaume Chatelet
b703bd821d
[libc] Add more functions in CPP/bit.h (#73814)
Once this is submitted we can remove `include/__support/bit.h` that
duplicates some of this functionality.
2023-11-30 13:51:02 +01:00
Guillaume Chatelet
8b25381bb6
[libc] Add the digits property to numeric_limits (#73926) 2023-11-30 13:34:55 +01:00
Guillaume Chatelet
7eb3103123
[libc] Fix cast semantic in integer_to_string (#73804) 2023-11-29 16:40:25 +01:00
Joseph Huber
0468867c98
[libc] Fix the GPU build for the hashing support (#73799)
Summary:
For reasons unknown to me, this function is undefined only on the GPU
build if you use `uintptr_t` but not `uint64_t` directly. This patch
makes an ifdef to use this directly for the GPU build to fix the bots.
2023-11-29 09:04:36 -06:00
Guillaume Chatelet
e2a37e5130
[libc][NFC] Fix missing LIBC_INLINE + style (#73659) 2023-11-29 10:37:54 +01:00
Schrodinger ZHU Yifan
1886b1a580
[libc] add PREFER_GENERIC flag (#73744)
There are some basic vectorization features in standard architecture
specifications. Such as SSE/SSE2 for x86-64, or NEON for aarch64. Even
though such features are almost always available, we still need some
methods to test fallback routines without any vectorization.

Previous attempt in hsearch adds a DISABLE_SSE2_OPT flag that tries to
compile the code with -mno-sse2 in order to test specific table scanning
routines. However, it turns out that such flag may have some unwanted
side effects hindering portability.

This PR introduces PREFER_GENERIC as an alternative. When a target is
built with PREFER_GENERIC, cmake will define a macro
__LIBC_PREFER_GENERIC such that developers can selectively choose the
fallback routine based on the macro.
2023-11-28 23:47:48 -05:00
Schrodinger ZHU Yifan
e399a317ef
[libc] fix build on aarch64 (#73739)
* avoid implicit narrowing conversion
* move hsearch entrypoints to FULL_BUILD
2023-11-28 22:39:00 -05:00
Schrodinger ZHU Yifan
81e3e7e5d4
[libc] [search] implement hcreate(_r)/hsearch(_r)/hdestroy(_r) (#73469)
This patch implements `hcreate(_r)/hsearch(_r)/hdestroy(_r)` as
specified in https://man7.org/linux/man-pages/man3/hsearch.3.html.

Notice that `neon/asimd` extension is not yet added in this patch. 

- The implementation is largely simplified from rust's
[`hashbrown`](https://github.com/rust-lang/hashbrown/blob/master/src/raw/mod.rs)
as we only consider fix-sized insertion-only hashtables. Technical
details are provided in code comments.

- This patch also contains a portable string hash function, which is
derived from [`aHash`](https://github.com/tkaitchuck/aHash)'s fallback
routine. Not using any SIMD acceleration, it has a good enough quality
(passing all SMHasher tests) and is not too bad in speed.

- Some general functionalities are added, such as `memory_size`,
`offset_to`(alignment), `next_power_of_two`, `is_power_of_two`.
`ctz/clz` are extended to support shorter integers.
2023-11-28 21:02:25 -05:00
michaelrj-google
86b0ccaee1
[libc][NFC] unify nextafter and nexttoward code (#73698)
Previously the nextafter and nexttoward implementations were almost
identical, with the exception of whether or not the second argument was
a template or just long double. This patch unifies them by making the
two argument templates independent.
2023-11-28 15:14:15 -08:00
michaelrj-google
43f783ff66
[libc][docs] Update implementation docs (#73590)
Some of the files in the docs/ directory are from 2019 and haven't been
updated since. This patch updates implementation_standard.rst,
source_tree_layout.rst, and has some minor fixes for strings.rst. It
also marks the most severely out of date files with a warning. These
files will be updated in a later patch.
2023-11-28 10:14:12 -08:00
Nishant Mittal
18fd6df885
[libc][math] Add unit tests for raising excepts in nextafter (#73556)
Follow up to
https://github.com/llvm/llvm-project/pull/72763#discussion_r1398277962.

### Summary
- Add unit tests for raising excepts in `nextafter`. 
- Fixed a bug in testing code for `nexttoward`.  

cc: @lntue
2023-11-28 00:50:17 -05:00
michaelrj-google
f90f036efb
[libc] Move in_use into OptionalStorage (#73569)
The previous optional class would call the destructor on a non-trivially
destructible object regardless of if it had already been reset. This
patch fixes this by moving tracking for if the object exists into the
internal storage class for optional.
2023-11-27 13:31:10 -08:00
Guillaume Chatelet
c599b8eec0
[libc][NFC] Decouple FP properties from C++ types (#73517)
We simplify the floating point properties by splitting concerns:
 - We define all distinct floating point formats in the `FPType` enum.
 - We map them to properties via the `FPProperties` trait.
- We map from C++ type to `FPType` in the `getFPType` function so logic
is easier to understand and extend.
2023-11-27 17:05:49 +01:00
Guillaume Chatelet
fb23fabc19
[libc] Fix forward octal prefix (#73526)
To fix failing build bots after :
https://github.com/llvm/llvm-project/pull/73372

```
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/__support/macros/properties/float.h:40:71: error: invalid digit '9' in octal constant
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 0900)) || \
```
2023-11-27 16:21:20 +01:00
Guillaume Chatelet
9539cbf033
[libc] Add detection support for float16 (#73372) 2023-11-27 16:08:17 +01:00
Joseph Huber
bf02c84cb8
[libc] Use file lock to join newline on RPC puts call (#73373)
Summary:
The puts call appends a newline. With multiple threads, this can be done
out of order such that another thread puts something before we finish
appending the newline. Add a flockfile and funlockfile to ensure that
the whole string is printed before another string can appear.
2023-11-27 08:41:15 -06:00
Guillaume Chatelet
ee5749bf78
[libc] Provide compiler version properties (#73344)
This will be used to support conditional compilation based on compiler
version.
We adopt the same convention as
[libc++](https://github.com/llvm/llvm-project/blob/main/libcxx/include/__config)
- thx @legrosbuffle for the suggestion!
Usage:
```
#if defined(LIBC_COMPILER_CLANG_VER)
#  if LIBC_COMPILER_CLANG_VER < 1500
#    warning "Libc only supports Clang 15 and later"
#  endif
#elif defined(LIBC_COMPILER_GCC_VER)
#  if LIBC_COMPILER_GCC_VER < 1500
#    warning "Libc only supports GCC 15 and later"
#  endif
#elif defined(LIBC_COMPILER_MSC_VER)
#  if LIBC_COMPILER_MSC_VER < 1930
#    warning "Libc only supports Visual Studio 2022 RTW (17.0) and later"
#  endif
#endif
```
2023-11-24 17:02:11 +01:00
Guillaume Chatelet
89a832435d
[libc][cmake] Add missing dependencies for type_traits (#73339) 2023-11-24 16:29:29 +01:00
Guillaume Chatelet
5e5a22caf8
[libc][NFC] Move float macro into its own header / add target os detection (#73311)
Floating point properties are a combination of target OS, target
architecture and compiler support.
 - Adding target OS detection,
 - Moving floating point type detection to its own file.

This is in preparation of adding support for `_Float16` which requires
testing compiler **version** and target architecture.
2023-11-24 16:11:05 +01:00
Guillaume Chatelet
dc9787c872
[libc][NFC] Remove dead code (#73315) 2023-11-24 14:33:19 +01:00
Guillaume Chatelet
d924c5d721
[libc][NFC] Sink "PlatformDefs.h" into "FloatProperties.h" (#73226)
`PlatformDefs.h` does not bring a lot of value as a separate file.
It is transitively included in `FloatProperties.h` and `FPBits.h`. This
patch sinks it into `FloatProperties.h` and removes the associated build
targets.
2023-11-23 11:23:18 +01:00
Guillaume Chatelet
436f5f652b
[libc][NFC] Remove unused define (#73222) 2023-11-23 10:24:49 +01:00
Guillaume Chatelet
c444879313
[libc][NFC] Split builtin_wrapper into bit and math_extras (#73113)
Split `builtin_wrapper.h` into `bit.h` and `math_extras.h` to mimic LLVM
`llvm/ADT/Bit.h` and `llvm/Support/MathExtras.h`.
Also added unittest place holders.
2023-11-23 09:58:59 +01:00
Guillaume Chatelet
f12be145ec
[libc][bazel] Enable __support tests (#73125) 2023-11-22 16:36:37 +01:00
Joseph Huber
fa1e49cf37 [libc] Disable nexttoward tests on the GPU
Summary:

These tests are currently failing for some reason. A lot of math tests
on the GPU are disabled temporarily and need to be fixed.
2023-11-22 07:46:25 -06:00
Nishant Mittal
0c49fc4c68
[libc][math] Implement nexttoward functions (#72763)
Implements the `nexttoward`, `nexttowardf` and `nexttowardl` functions.
Also, raise excepts required by the standard in `nextafter` functions.

cc: @lntue
2023-11-21 09:02:51 -05:00
Joseph Huber
8341a40ec1
[libc] Update the AMDGPU implementation to use code object 5 (#72580)
Summary:
This patch includes the necessary changes to make the `libc` tests
running on AMD GPUs run using the newer code object version. The 'code
object version' is AMD's internal ABI for making kernel calls. The move
from 4 to 5 changed how we handle arguments for builtins such as
obtaining the grid size or setting up the size of the private stack.

Fixes: https://github.com/llvm/llvm-project/issues/72517
2023-11-21 07:14:10 -06:00
Joseph Huber
abd85cd473
[libc] Remove the optional arguments for NVPTX constructors (#69536)
Summary:
We call the global constructors by function pointer. For whatever reason
the NVPTX architecture relies very specifically on the arguments to the
function pointer invocation matching what the function is implemented
as. This is problematic as most of these constructors are generated
with no arguments. This patch removes the extended arguments that GNU
and LLVM use for the constructors optionally so that it can support the
common case.
2023-11-20 17:10:15 -06:00
michaelrj-google
4db99c8b54
[libc] Add base for target config within cmake (#72318)
Currently the only way to add or remove entrypoints is to modify the
entrypoints.txt file for the current target. This isn't ideal since
a user would have to carry a diff for this file when updating their
checkout. This patch adds a basic mechanism to allow the user to remove
entrypoints without modifying the repository.
2023-11-17 11:32:27 -08:00