2427 Commits

Author SHA1 Message Date
Guillaume Chatelet
31316b3f85
[reland][libc] Make BigInt bit_cast-able to compatible types (#74862)
Fix #74258
This is a reland of #74837, the error went unnoticed because it compiles
fine on
clang-16 but not on clang-12 which is the version used on the buildbots.

The fix was to explicitly initialize `BigInt` variables in `constexpr`
operations: `BigInt<Bits, Signed> result(0);` instead of `BigInt<Bits,
Signed> result;`
2023-12-08 18:03:24 +01:00
Guillaume Chatelet
d5199b43be
Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74856)
Reverts llvm/llvm-project#74837

Some build bot are failing because of missing constexpr.

https://lab.llvm.org/buildbot/#/builders/138/builds/56468/steps/7/logs/stdio
2023-12-08 17:15:10 +01:00
Guillaume Chatelet
a2b7ded40c
[libc] Make BigInt bit_cast-able to compatible types (#74837)
Fix #74258
2023-12-08 17:11:05 +01:00
lntue
b8f7c2ce89
[libc][NFC] Clean up conversion warnings in math function implementations. (#74697) 2023-12-08 09:54:21 -05:00
Nick Desaulniers
1ee6a1e38a
[libc] fix -Wshift-count-overflow in UInt.h (#74649)
Not that I'm very good at SFINAE, but it seems that conversion operators
are
perhaps difficult to compose with SFINAE. I saw an example that used one
layer
of indirection to have an explicit return type that could then be used
with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: #74623
2023-12-07 08:20:46 -08:00
Nick Desaulniers
079ca05eea
[libc] fix -Wtype-limits in wctob (#74511)
GCC warns:
```
libc/src/__support/wctype_utils.h:33:20: error: comparison of unsigned
expression in ‘< 0’ is always false [-Werror=type-limits]
   33 |   if (c > 127 || c < 0)
      |                  ~~^~~
```

Looking into the signedness of wint_t, it looks like depending on the
platform,
__WINT_TYPE__ is defined to int or unsigned int depending on the
platform.

Link:
https://lab.llvm.org/buildbot/#/builders/250/builds/14891/steps/6/logs/stdio
2023-12-05 15:37:27 -08:00
Schrodinger ZHU Yifan
86bde5adc8
[libc] implement prctl (#74386)
Implement `prctl` as specified in
https://man7.org/linux/man-pages/man2/prctl.2.html.

This patch also includes test cases covering two simple use cases:

1. `PR_GET_NAME/PR_SET_NAME`: where userspace data is passed via arg2.
2. `PR_GET_THP_DISABLE`: where return value is passed via syscal retval.
2023-12-05 12:31:00 -08:00
Nick Desaulniers
9c2e5449f4
[libc] remove spurious LIBC_INLINE (#74505)
This function is marked noinline; the inline keyword (or whatever
LIBC_INLINE
resolves to) will have no effect.
2023-12-05 10:53:48 -08:00
Schrodinger ZHU Yifan
86e99e11e5
[libc] [search] improve hsearch robustness (#73896)
Following up the discussion at
https://github.com/llvm/llvm-project/pull/73469#discussion_r1409593911
by @nickdesaulniers.

According to FreeBSD implementation
(https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/upstream-freebsd/lib/libc/stdlib/hcreate.c),
`hsearch` is able to handle the cases where the global table is not
properly initialized. To do this, FreeBSD actually allows hash table to
be dynamically resized. If the global table is uninitialized at the
first call, the table will be initialized with a minimal size; hence
subsequent insertion will be reasonable as the table grows
automatically.

This patch mimic such behaviors. More precisely, this patch introduces:

1. a full table iterator that scans each element in the table,
2. a resize routine that is automatically triggered whenever the load
factor is reached where it iterates the old table and insert the entries
into a new one,
3. more tests that cover the growth of the table.
2023-12-05 09:06:48 -08:00
Joseph Huber
a28e4eac26
[libc] Default to a single threaded thread pool for GPU tests (#74486)
Summary:
The GPU tests tend to fail when run massively in parallel. This is why
we use a CMake job pool to limit it to 1 in most cases. We should
default to the configuration that is most likely to work, that being a
single thread. There aren't enough GPU tests for this to be a massive
increase in test time on the bots, so we should default to what works
guaranteed.
2023-12-05 15:25:50 +00:00
Guillaume Chatelet
21b986637b
[libc] Fix arm32 tests (#74457)
`ASSERT_EQ` requires that both operands have the same type but on arm32
`size_t` is `unsigned int` instead of `unsigned long`. Using `size_t`
explicitely to avoid "conflicting types for parameter 'ValType"
2023-12-05 13:53:19 +01:00
Guillaume Chatelet
1d89478830
[reland][libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) (#74446)
Same as #73939 but also fix `libc/src/string/memory_utils/op_aarch64.h`
that was still using `deferred_static_assert`.
2023-12-05 11:35:13 +01:00
Guillaume Chatelet
de7fdc5b54
Revert "[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead" (#74444)
Reverts llvm/llvm-project#73939

This broke libc-aarch64-ubuntu build bot 
https://lab.llvm.org/buildbot/#/builders/138/builds/56186
2023-12-05 11:25:39 +01:00
Guillaume Chatelet
b140948850
[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) 2023-12-05 11:21:07 +01:00
michaelrj-google
ab65c9c3bb
[libc][NFC] fix int warnings in float conversion (#74379)
The printf float to string conversion functions had some implicit
integer conversion warnings on gcc. This patch adds explicit casts to
these places.
2023-12-04 16:34:24 -08:00
Schrodinger ZHU Yifan
6fd1c1b8ef
[libc] fix HashTable warnings and build problems (#74371)
According to https://lab.llvm.org/buildbot/#/builders/163/builds/48002,
the generic build on HashTable fails with two major issues with
`werror`:
1. warnings on `error: suggest braces around initialization of
subobject`.
2. `__support/HashTable` tests are built regardless of its entrypoints`

This PR attempts to fix such issues.
2023-12-04 14:06:57 -08:00
Nick Desaulniers
0d59cfc7a3
[libc] fix -Wconversion in float_to_string.h (#74369)
Fixes:
libc/src/__support/float_to_string.h:551:48: error: conversion from
‘long
unsigned int’ to ‘int32_t’ {aka ‘int’} may change value
[-Werror=conversion]
551 | const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE *
idx);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Observed in gcc fullbuilds.

IDX_SIZE is a size_t (aka 'long unsigned int'), but has the value 128,
so the
expression is undergoing implicit promotion.

Link: https://lab.llvm.org/buildbot/#/builders/250/builds/14891
2023-12-04 13:12:51 -08:00
Schrodinger ZHU Yifan
ff51b60b18
[libc] Revert #73704 and subsequent fixes #73984, #74026 (#74355)
The test cases of mincore require getting correct page size from OS. As
`sysconf` is not functioning correctly, these patches are implemented in
a somewhat confusing way. We revert such patches and will reintroduce
mincore after we correct sysconf.

This reverts 54878b8, 985c0d1 and 418a3a4.
2023-12-04 12:49:12 -08:00
Nick Desaulniers
6886a52d6d Revert "[libc] build with -Werror (#73966)"
This reverts commit 606653091d1a66d1a83a1bfdea2883cc8d46687e.

Post submit buildbots are now red. We can use these explicit errors to better
clean up existing warnings, then reland this.

Link: #73966
2023-12-04 11:31:59 -08:00
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