11535 Commits

Author SHA1 Message Date
Hristo Hristov
80f9458cf3
[libc++][sstream] P2495R3: Interfacing stringstreams with string_view (#80552)
Implements P2495R3 <https://wg21.link/P2495R3>
- https://eel.is/c++draft/version.syn#headerref:%3csstream%3e
- https://eel.is/c++draft/stringbuf
- https://eel.is/c++draft/stringbuf.general
- https://eel.is/c++draft/stringbuf.cons
- https://eel.is/c++draft/stringbuf.members
- https://eel.is/c++draft/istringstream
- https://eel.is/c++draft/istringstream.general
- https://eel.is/c++draft/istringstream.cons
- https://eel.is/c++draft/istringstream.members
- https://eel.is/c++draft/ostringstream
- https://eel.is/c++draft/ostringstream.general
- https://eel.is/c++draft/ostringstream.cons
- https://eel.is/c++draft/ostringstream.members
- https://eel.is/c++draft/stringstream
- https://eel.is/c++draft/stringstream.general
- https://eel.is/c++draft/stringstream.cons
- https://eel.is/c++draft/stringstream.members

References:
- https://eel.is/c++draft/string.streams
2024-03-05 08:49:49 +02:00
Louis Dionne
40081a45a1
[libc++] Fix diagnostic for <stdatomic.h> before C++23 (#83351)
We normally try to issue a reasonable diagnostic when mixing
<stdatomic.h> and <atomic> before C++23. However, after granularizing
the <atomic> header, the check and the #error message was moved to
*after* the point where mixing both causes problems. When mixing both
headers, we would hence get the diagnostic burried under a pile of
previous diagnostics in e.g. __atomic/kill_dependency.h.

This patch moves the check earlier to restore the intended behavior. It
also switches from `#ifdef kill_dependency` to an explicit check of the
inclusion of the header and the Standard version, which seems to be more
reliable than checking whether a macro is defined.
2024-03-04 18:24:51 -05:00
Louis Dionne
5174b38025
[libc++] Use __wrap_iter in string_view and array in the unstable ABI (#74482)
std::string_view and std::array iterators don't have to be raw pointers,
and in fact other implementations don't represent them as raw pointers.
Them being raw pointers in libc++ makes it easier for users to write
non-portable code. This is bad in itself, but this is even worse when
considering efforts like hardening where we want an easy ability to
swap for a different iterator type. If users depend on iterators being
raw pointers, this becomes a build break.

Hence, this patch enables the use of __wrap_iter in the unstable ABI,
creating a long term path towards making this the default. This patch
may break code that assumes these iterators are raw pointers for
people compiling with the unstable ABI.

This patch also removes several assumptions that array iterators are
raw pointers in the code base and in the test suite.
2024-03-04 18:23:53 -05:00
Louis Dionne
4d80df0922
[libc++] Do not forward-declare syncstream outside experimental (#82511)
We only define the classes in `<syncstream>` when experimental library
features are enabled, but we would forward-declare them in `<iosfwd>`
even when they are disabled. This led to confusing error messages about
being unable to instantiate an undefined template.
2024-03-04 18:16:52 -05:00
Louis Dionne
6fd27d5b03
[libc++] Don't generate the modulemap file (#80352)
We actually didn't generate anything in that file, so generating it via
CMake is useless.
2024-03-04 18:16:16 -05:00
Jake Egan
98418c27bc
[libc++][AIX] Use input redirection instead of piping for cin tests (#83184)
When echo is used for piping, lit uses the system echo rather than the
builtin echo. The system echo on AIX doesn't support the `-n` option,
which causes these tests to fail. Use input redirection, so the builtin
echo can be used.
2024-03-04 13:06:05 -05:00
Louis Dionne
d0521484c8
[libc++] Remove leftover .fail.cpp matcher in Lit test format (#83583)
This should have been removed in 8dcb8ea75cef, which removed support for
.fail.cpp tests in the libc++ test suite.
2024-03-04 10:20:42 -05:00
Po-yao Chang
b29301cd40
[libc++][format] Handle range-underlying-spec (#81914)
An immediate colon signifeis that the range-format-spec contains only
range-underlying-spec.

This patch allows this code to compile and run:
```c++
std::println("{::<<9?}", std::span<const char>{"Hello", sizeof "Hello"});
```
2024-03-04 08:05:01 +08:00
Nikolas Klauser
33de5a316c
[libc++] Rename __fwd/hash.h to __fwd/functional.h and add reference_wrapper (#81445)
We forward declare `reference_wrapper` in multiple places already. This
moves the declaration to the canonical place and removes unnecessary
includes of `__functional/reference_wrapper.h`.
2024-03-03 20:45:17 +01:00
Nikolas Klauser
0c90e8837a [libc++][NFC] Replace _ALIGNAS_TYPE with alignas in iostream.cpp 2024-03-03 19:29:51 +01:00
Nikolas Klauser
55357160d0
[libc++] Use GCC type traits builtins for remove_cv and remove_cvref (#81386)
They have been added recently to GCC without support for mangling. This
patch uses them in structs and adds aliases to these structs instead of
the builtins directly.
2024-03-03 18:26:57 +01:00
Mark de Wever
a5d3a1dbc8
[libc++] Refactors fstream open. (#76617)
This moves the duplicated code to one new function.

This is a preparation to fix
https://github.com/llvm/llvm-project/issues/60509
2024-03-03 13:23:41 +01:00
Hui
1f613bce19
[libc++] refactor cxx_atomic_wait to make it reusable for atomic_ref (#81427)
The goal of this patch is to make `atomic`'s wait functions to be
reusable by `atomic_ref`.
https://github.com/llvm/llvm-project/pull/76647

First, this patch is built on top of
https://github.com/llvm/llvm-project/pull/80596 , to reduce the future
merge conflicts.

This patch made the following functions as "API"s to be used by
`atomic`, `atomic_flag`, `semaphore`, `latch`, `atomic_ref`

```
__atomic_wait
__atomic_wait_unless
__atomic_notify_one
__atomic_notify_all
```

These functions are made generic to support `atomic` type and
`atomic_ref`. There are two customisation points.

```
// How to load the value from the given type (with a memory order)
__atomic_load
```


```
// what is the contention address that the platform `wait` function is going to monitor
__atomic_contention_address
```


For `atomic_ref` (not implemented in this patch), the `load` and
`address` function will be different, because
- it does not use the "atomic abstraction layer" so the `load` operation
will be some gcc builtin
- the contention address will be the user's actual type that the
`atomic_ref` is pointing to
2024-03-02 14:50:52 +00:00
Hui
1a0c988ebd [libc++][NFC] rename variable in atomic_sync
As discussed in https://github.com/llvm/llvm-project/pull/81427/files#r1509266817
rename the variable as a separate commit
2024-03-01 19:52:14 +00:00
ZijunZhaoCCK
7d7d4752a8
[libc++] Set feature test macros __cpp_lib_ranges_contains and__cpp_lib_ranges_starts_ends_with (#81816)
ranges::contains: fdd089b50063
ranges::starts_with: 205175578e0d
ranges::ends_with: 0218ea4aaa54

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2024-02-29 16:06:32 -08:00
Fangrui Song
8157847764
[libc++] Include missing <__assert> after #80091 (#83480)
_LIBCPP_ASSERT_SHIM used by the -fno-exceptions and 
LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS=on configuration
needs _LIBCPP_ASSERT from <__assert>.
2024-02-29 16:05:59 -05:00
Louis Dionne
58e476f757
[libc++] Map forward declaration headers for iostreams to <iosfwd> (#83327)
This seems more appropriate than mapping them to the headers that
contain actual definitions.
2024-02-29 10:48:23 -05:00
Louis Dionne
37dca605c9
[libc++] Clean up includes of <__assert> (#80091)
Originally, we used __libcpp_verbose_abort to handle assertion failures.
That function was declared from all public headers. Since we don't use
that mechanism anymore, we don't need to declare __libcpp_verbose_abort
from all public headers, and we can clean up a lot of unnecessary
includes.

This patch also moves the definition of the various assertion categories
to the <__assert> header, since we now rely on regular IWYU for these
assertion macros.

rdar://105510916
2024-02-29 10:12:22 -05:00
Dominik Wójt
d1a461dbb2
[libc++] tests with picolibc: prevent looking for unneeded "rt" library (#82262)
Picolibc does not provide the clock_gettime function nor the "rt" library.
check_library_exists was invalidly detecting the "rt" library due to cmake issue 
present, when cross-compiling[1]. This resulted with "chrono.cpp" trying to link
to the "rt" library and following error:
    unable to find library from dependent library specifier: rt

[1] https://gitlab.kitware.com/cmake/cmake/-/issues/18121
2024-02-29 08:54:58 +01:00
Nikolas Klauser
1530034166
[libc++] Remove unnecessary includes from <atomic> (#82880)
This reduces the include time of `<atomic>` from 135ms to 88ms.
2024-02-28 21:14:35 +01:00
Nikolas Klauser
21c7bc51e9
[libc++] Use __integer_pack to implement integer_sequence on GCC (#82983)
This significantly simplifies the implementation.
2024-02-28 20:33:20 +01:00
Louis Dionne
3761ad01e1
[libc++] Remove _LIBCPP_ATOMIC_ONLY_USE_BUILTINS (#82000)
As discussed in #76647, _LIBCPP_ATOMIC_ONLY_USE_BUILTINS is a
questionable configuration option. It makes our implementation of
std::atomic even more complicated than it already is for a limited
benefit.

Indeed, the original goal of that setting was to decouple libc++ from
libraries like compiler-rt and libatomic in Freestanding mode. We didn't
have a clear understanding of goals and non-goals of Freestanding back
then, but nowadays we do have a better understanding that removing all
dependencies of libc++ in Freestanding is a non-goal. We should still be
able to depend on builtins like those defined in compiler-rt for
implementing our atomic operations in Freestanding. Freestanding means
that there is no underlying operating system, not that there is no
toolchain available.

This patch removes the configuration option. This should have a very
limited fallout since that configuration was only enabled with
-ffreestanding, and libc++ basically doesn't work out of the box on
Freestanding platforms today.

The benefits are a slightly simpler implementation of std::atomic,
getting rid of one of the ABI-incompatible representations of
std::atomic, and clearing the way for proper Freestanding support to
eventually land in the library.

Fixes #81286
2024-02-27 17:31:02 -05:00
Mark de Wever
b50bcc7ffb
[libc++][modules] Fixes naming inconsistency. (#83036)
The modules used is-standard-library and is-std-library. The latter is
the name used in the SG15 proposal,

Fixes: https://github.com/llvm/llvm-project/issues/82879
2024-02-27 12:10:53 -05:00
Dominik Wójt
c19a81931c
[libc++] protect absent atomic types with ifdef (#82351)
Otherwise modules.std-module.sh.cpp test fails with following error:
    error: no member named 'atomic_signed_lock_free' in namespace 'std'
when the types are not available.
2024-02-27 07:27:02 +01:00
Louis Dionne
6a884a9aef
[libc++] Always keep libc++abi re-exports up-to-date (#79012)
Previously, the list of libc++abi symbols that we re-export from libc++
would be partly encoded in libc++abi (and re-exported automatically via
the cxxabi-reexports target), and partly hard-coded in
libcxx/lib/libc++abi.exp. The duplication of information led to symbols
not being exported from libc++ after being added to libc++abi when they
should have been.

This patch removes the duplication of information. After this patch, the
full list of symbols to re-export from libc++abi is handled by the
cxxabi-reexports target and is stored in libcxxabi.

The symbols newly re-exported from libc++ are mainly new fundamental
typeinfos and a bunch of functions and classes that are part of
libc++abi but are most likely implementation details. In the future, it
would be possible to try to trim down the set of what we export from
libc++abi (and hence what we re-export from libc++) to remove some
implementation detail symbols.

Fixes #79008
2024-02-26 14:55:10 -05:00
Louis Dionne
5e6f50eaa9
[libc++] Remove LIBCXX_EXECUTOR and LIBCXXABI_EXECUTOR (#79886)
Those were deprecated in LLVM 18 and their removal was planned for LLVM 19.
2024-02-26 14:46:15 -05:00
Louis Dionne
deaf53e632
[libc++][NFC] Reorder availability macros in introduction order (#82531)
Reorder the availability macros in __availability to respect the order
in which they were introduced in the dylib. This simple refactor will
greatly simplify an upcoming change I am working on.
2024-02-26 14:34:43 -05:00
Louis Dionne
1f8b7e3c0b
[libc++] Fix non-uglified name in scoped_allocator_adaptor (#80706)
As mentioned in #78754, the 'base' typedef in scoped_allocator_adaptor
was not uglified properly.
2024-02-26 14:33:58 -05:00
Nikolas Klauser
d0c99f4b1d
[libc++] Remove __member_pointer_traits_imp (#82081)
They aren't ever used, so they can be removed.
2024-02-26 12:04:41 +01:00
Abhina Sree
1197fcabc4
[libcxx][test] Change UNSUPPORTED to XFAIL for target-related failures (#81513)
This is a followup from this discussion
https://github.com/llvm/llvm-project/pull/80735#discussion_r1486586017
to mark targets that were initially marked as UNSUPPORTED with an XFAIL
instead.
2024-02-23 08:01:56 -05:00
Hui
69279a8413
[libc++][test] add benchmarks for std::atomic::wait (#70571)
For the mutex vs atomic  test:

Old: `unique_lock<mutex>`
New: a lock implemented with `atomic::wait`

On 10 years old Intel Macbook, `atomic::wait` is 50% slower than `mutex`

```
Benchmark                                             Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------
BM_multi_thread_lock_unlock/1024                   +0.3735         +2.4497       1724726       2368935        153159        528354
BM_multi_thread_lock_unlock/2048                   +0.4174         +1.2487       3410538       4834012        435062        978311
BM_multi_thread_lock_unlock/4096                   +0.5256         +1.9824       6903783      10532681        590266       1760405
BM_multi_thread_lock_unlock/8192                   +0.5415         +0.4578      14536391      22408399       1456328       2123075
BM_multi_thread_lock_unlock/16384                  +0.5663         +0.0513      30181991      47275023       3316850       3486950
BM_multi_thread_lock_unlock/32768                  +0.5635         -0.2081      62027663      96977726       6477076       5129190
BM_multi_thread_lock_unlock/65536                  +0.5228         -0.3273     129637761     197408739      11341630       7628955
BM_multi_thread_lock_unlock/131072                 +0.4825         -0.1070     266256295     394712193      10379800       9269200
BM_multi_thread_lock_unlock/262144                 +0.4793         +0.2795     539732340     798409253      10802200      13821100
BM_multi_thread_lock_unlock/524288                 +0.5272         +0.2847    1070035132    1634124353      14523000      18657800
BM_multi_thread_lock_unlock/1048576                +0.4799         +0.3353    2125510441    3145636119      13404200      17899000
OVERALL_GEOMEAN                                    +0.4970         +0.3886             0             0             0             0
```

On Apple Arm, `atomic::wait` is 200% slower than `mutex`. And
`atomic::wait` is even slower than my 10 years old Intel CPU Macbook

```
Benchmark                                             Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------
BM_multi_thread_lock_unlock/1024                   +2.1811         +3.9854       2036726       6478993        119817        597334
BM_multi_thread_lock_unlock/2048                   +1.6736         +1.4301       3162161       8454415        426201       1035727
BM_multi_thread_lock_unlock/4096                   +1.1017         +0.6456       6620503      13914159        893019       1469578
BM_multi_thread_lock_unlock/8192                   +0.6688         +0.2148      12089392      20174635       1489000       1808799
BM_multi_thread_lock_unlock/16384                  +1.4217         -0.2436      19365999      46899345       2068266       1564530
BM_multi_thread_lock_unlock/32768                  +2.6161         -0.4927      31371052     113440165       3715100       1884540
BM_multi_thread_lock_unlock/65536                  +2.6286         -0.3967      54314581     197086847       5912764       3567410
BM_multi_thread_lock_unlock/131072                 +2.3554         +0.4990     103176565     346201425       9260407      13880900
BM_multi_thread_lock_unlock/262144                 +2.8780         +0.4995     182355400     707170733      16335852      24496000
BM_multi_thread_lock_unlock/524288                 +3.0280         +0.3001     360953079    1453902595      32548700      42316364
BM_multi_thread_lock_unlock/1048576                +3.7480         +1.2374     714500462    3392470417      48603455     108747000
OVERALL_GEOMEAN                                    +2.0791         +0.3874             0             0             0             0
```











For the atomic_wait test:

On my 2013 MacBook with Intel CPU

```
Run on (8 X 2300 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x4)
  L1 Instruction 32 KiB (x4)
  L2 Unified 256 KiB (x4)
  L3 Unified 6144 KiB (x1)
Load Average: 1.95, 3.77, 4.13
-----------------------------------------------------------------------------------------------------
Benchmark                                                           Time             CPU   Iterations
-----------------------------------------------------------------------------------------------------
BM_atomic_wait_one_thread_one_atomic_wait/1024                 184455 ns       183979 ns         3760
BM_atomic_wait_one_thread_one_atomic_wait/2048                 361607 ns       360917 ns         1912
BM_atomic_wait_one_thread_one_atomic_wait/4096                 709055 ns       708326 ns          929
BM_atomic_wait_one_thread_one_atomic_wait/8192                1469063 ns      1467430 ns          488
BM_atomic_wait_one_thread_one_atomic_wait/16384               2865332 ns      2863473 ns          237
BM_atomic_wait_one_thread_one_atomic_wait/32768               5839429 ns      5834708 ns          113
BM_atomic_wait_one_thread_one_atomic_wait/65536              11460822 ns     11453183 ns           60
BM_atomic_wait_one_thread_one_atomic_wait/131072             23052804 ns     23035000 ns           30
BM_atomic_wait_one_thread_one_atomic_wait/262144             46958743 ns     46712733 ns           15
BM_atomic_wait_one_thread_one_atomic_wait/524288             93151904 ns     92977429 ns            7
BM_atomic_wait_one_thread_one_atomic_wait/1048576           186100011 ns    185888500 ns            4
BM_atomic_wait_one_thread_one_atomic_wait/2097152           364548135 ns    364280000 ns            2
BM_atomic_wait_one_thread_one_atomic_wait/4194304           747181672 ns    745056000 ns            1
BM_atomic_wait_one_thread_one_atomic_wait/8388608          1473070400 ns   1471165000 ns            1
BM_atomic_wait_one_thread_one_atomic_wait/16777216         2950352547 ns   2947373000 ns            1
BM_atomic_wait_multi_thread_one_atomic_wait/1024               668544 ns       167233 ns         4496
BM_atomic_wait_multi_thread_one_atomic_wait/2048              1384668 ns       369750 ns         1941
BM_atomic_wait_multi_thread_one_atomic_wait/4096              2851627 ns       768559 ns          995
BM_atomic_wait_multi_thread_one_atomic_wait/8192              5797669 ns      1476876 ns          526
BM_atomic_wait_multi_thread_one_atomic_wait/16384            11597952 ns      2692792 ns          260
BM_atomic_wait_multi_thread_one_atomic_wait/32768            23528028 ns      5291465 ns          142
BM_atomic_wait_multi_thread_one_atomic_wait/65536            46287247 ns      8547713 ns           87
BM_atomic_wait_multi_thread_one_atomic_wait/131072           90315848 ns     13294492 ns           61
BM_atomic_wait_multi_thread_one_atomic_wait/262144          190722393 ns     16193917 ns           36
BM_atomic_wait_multi_thread_one_atomic_wait/524288          408456684 ns     23641600 ns           10
BM_atomic_wait_multi_thread_one_atomic_wait/1048576         708809670 ns     36361900 ns           10
BM_atomic_wait_multi_thread_wait_different_atomics/1024       2116444 ns        11669 ns        10000
BM_atomic_wait_multi_thread_wait_different_atomics/2048      12435259 ns        21905 ns         1000
BM_atomic_wait_multi_thread_wait_different_atomics/4096       6393816 ns        17819 ns         1000
BM_atomic_wait_multi_thread_wait_different_atomics/8192      11930400 ns        28637 ns         1000
BM_atomic_wait_multi_thread_wait_different_atomics/16384     20987224 ns        35272 ns         1000
BM_atomic_wait_multi_thread_wait_different_atomics/32768     44335820 ns        66660 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/65536     91395912 ns       129030 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/131072   145440007 ns       165960 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/262144   368219935 ns       420800 ns           10
BM_atomic_wait_multi_thread_wait_different_atomics/524288   630106863 ns       809500 ns           10
BM_atomic_wait_multi_thread_wait_different_atomics/1048576 1138174673 ns      1093000 ns           10
```

On apple arm

```
Run on (8 X 24.1208 MHz CPU s)
CPU Caches:
  L1 Data 64 KiB (x8)
  L1 Instruction 128 KiB (x8)
  L2 Unified 4096 KiB (x2)
Load Average: 1.34, 1.58, 1.66
-----------------------------------------------------------------------------------------------------
Benchmark                                                           Time             CPU   Iterations
-----------------------------------------------------------------------------------------------------
BM_atomic_wait_one_thread_one_atomic_wait/1024                  61602 ns        61602 ns         8701
BM_atomic_wait_one_thread_one_atomic_wait/2048                 123148 ns       123146 ns         5688
BM_atomic_wait_one_thread_one_atomic_wait/4096                 246248 ns       246249 ns         2888
BM_atomic_wait_one_thread_one_atomic_wait/8192                 480373 ns       480359 ns         1455
BM_atomic_wait_one_thread_one_atomic_wait/16384                974725 ns       974721 ns          724
BM_atomic_wait_one_thread_one_atomic_wait/32768               1922185 ns      1922115 ns          355
BM_atomic_wait_one_thread_one_atomic_wait/65536               3940632 ns      3940608 ns          181
BM_atomic_wait_one_thread_one_atomic_wait/131072              7886302 ns      7886102 ns           88
BM_atomic_wait_one_thread_one_atomic_wait/262144             15393156 ns     15393000 ns           45
BM_atomic_wait_one_thread_one_atomic_wait/524288             30833221 ns     30832174 ns           23
BM_atomic_wait_one_thread_one_atomic_wait/1048576            62551936 ns     62551909 ns           11
BM_atomic_wait_one_thread_one_atomic_wait/2097152           123155625 ns    123155667 ns            6
BM_atomic_wait_one_thread_one_atomic_wait/4194304           252468180 ns    252458667 ns            3
BM_atomic_wait_one_thread_one_atomic_wait/8388608           505075604 ns    505075500 ns            2
BM_atomic_wait_one_thread_one_atomic_wait/16777216          992977209 ns    992935000 ns            1
BM_atomic_wait_multi_thread_one_atomic_wait/1024               531411 ns       239695 ns         2783
BM_atomic_wait_multi_thread_one_atomic_wait/2048              1030592 ns       484868 ns         1413
BM_atomic_wait_multi_thread_one_atomic_wait/4096              1951896 ns       922357 ns          631
BM_atomic_wait_multi_thread_one_atomic_wait/8192              3759893 ns      1952074 ns          390
BM_atomic_wait_multi_thread_one_atomic_wait/16384             7417929 ns      3458309 ns          233
BM_atomic_wait_multi_thread_one_atomic_wait/32768            14386361 ns      5590830 ns          100
BM_atomic_wait_multi_thread_one_atomic_wait/65536            29725536 ns      6521887 ns          115
BM_atomic_wait_multi_thread_one_atomic_wait/131072           60023797 ns     10766795 ns           73
BM_atomic_wait_multi_thread_one_atomic_wait/262144          120782267 ns     17532091 ns           44
BM_atomic_wait_multi_thread_one_atomic_wait/524288          242539333 ns     27506920 ns           25
BM_atomic_wait_multi_thread_one_atomic_wait/1048576         482833787 ns     53721600 ns           10
BM_atomic_wait_multi_thread_wait_different_atomics/1024       2230048 ns       626042 ns         1000
BM_atomic_wait_multi_thread_wait_different_atomics/2048       3931958 ns       837540 ns          884
BM_atomic_wait_multi_thread_wait_different_atomics/4096       6506887 ns      1127922 ns          586
BM_atomic_wait_multi_thread_wait_different_atomics/8192      10528008 ns      1651254 ns          456
BM_atomic_wait_multi_thread_wait_different_atomics/16384     18055829 ns      2066379 ns          317
BM_atomic_wait_multi_thread_wait_different_atomics/32768     29878496 ns      2875600 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/65536     50523799 ns      3193170 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/131072    85926943 ns      4121950 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/262144   154602296 ns      5879050 ns          100
BM_atomic_wait_multi_thread_wait_different_atomics/524288   279121754 ns     10063400 ns           10
BM_atomic_wait_multi_thread_wait_different_atomics/1048576  522796900 ns     12370300 ns           10
```
2024-02-21 13:43:35 +00:00
Tacet
7661ade5d1
[libc++] Add details about string annotations (#80912)
This commit adds information that only long strings are annotated, and
with all allocators by default.

To read why short string annotations are not turned on yet, read comments in a related
PR: https://github.com/llvm/llvm-project/pull/79536

---------

Co-authored-by: Mark de Wever <zar-rpg@xs4all.nl>
2024-02-20 07:20:21 +01:00
Nikolas Klauser
76a2472715
[libc++] Refactor more __enable_ifs to the canonical style (#81457)
This brings the code base closer to having only a single style of
`enable_if`s.
2024-02-20 01:47:38 +01:00
Jan Kokemüller
95ebf2be0e
[libc++] Refactor the predicate taking variant of __cxx_atomic_wait (#80596)
This is a follow-up PR to
<https://github.com/llvm/llvm-project/pull/79265>. It aims to be a
gentle refactoring of the `__cxx_atomic_wait` function that takes a
predicate.

The key idea here is that this function's signature is changed to look
like this (`std::function` used just for clarity):

```c++
__cxx_atomic_wait_fn(Atp*, std::function<bool(Tp &)> poll, memory_order __order);
```

...where `Tp` is the corresponding `value_type` to the atomic variable
type `Atp`. The function's semantics are similar to `atomic`s `.wait()`,
but instead of having a hardcoded predicate (is the loaded value unequal
to `old`?) the predicate is specified explicitly.

The `poll` function may change its argument, and it is very important
that if it returns `false`, it leaves its current understanding of the
atomic's value in the argument. Internally, `__cxx_atomic_wait_fn`
dispatches to two waiting mechanisms, depending on the type of the
atomic variable:

1. If the atomic variable can be waited on directly (for example,
Linux's futex mechanism only supports waiting on 32 bit long variables),
the value of the atomic variable (which `poll` made its decision on) is
then given to the underlying system wait function (e.g. futex).
2. If the atomic variable can not be waited on directly, there is a
global pool of atomics that are used for this task. The ["eventcount"
pattern](<https://gist.github.com/mratsim/04a29bdd98d6295acda4d0677c4d0041>)
is employed to make this possible.

The eventcount pattern needs a "monitor" variable which is read before
the condition is checked another time. libcxx has the
`__libcpp_atomic_monitor` function for this. However, this function only
has to be called in case "2", i.e. when the eventcount is actually used.
In case "1", the futex is used directly, so the monitor must be the
value of the atomic variable that the `poll` function made its decision
on to continue blocking. Previously, `__libcpp_atomic_monitor` was
_also_ used in case "1". This was the source of the ABA style bug that
PR#79265 fixed.

However, the solution in PR#79265 has some disadvantages:

- It exposes internals such as `cxx_contention_t` or the fact that
`__libcpp_thread_poll_with_backoff` needs two functions to higher level
constructs such as `semaphore`.
- It doesn't prevent consumers calling `__cxx_atomic_wait` in an error
prone way, i.e. by providing to it a predicate that doesn't take an
argument. This makes ABA style issues more likely to appear.

Now, `__cxx_atomic_wait_fn` takes just _one_ function, which is then
transformed into the `poll` and `backoff` callables needed by
`__libcpp_thread_poll_with_backoff`.

Aside from the `__cxx_atomic_wait` changes, the only other change is the
weakening of the initial atomic load of `semaphore`'s `try_acquire` into
`memory_order_relaxed` and the CAS inside the loop is changed from
`strong` to `weak`. Both weakenings should be fine, since the CAS is
called in a loop, and the "acquire" semantics of `try_acquire` come from
the CAS, not from the initial load.
2024-02-19 14:28:51 +00:00
Hristo Hristov
2ea5d167ae
[libc++][complex] P2819R2: Add tuple protocol to complex (#79744)
Implements: P2819R2 <https://wg21.link/P2819R2>
- https://eel.is/c++draft/utilities#concept:tuple-like
- https://eel.is/c++draft/complex.syn
- https://eel.is/c++draft/complex.tuple

---------

Co-authored-by: Zingam <zingam@outlook.com>
2024-02-19 09:56:06 +02:00
Mark de Wever
54daf6af57
[libc++] Fixes istream::sync. (#76467)
This fixes two issues.

The return value
----------------

Based on the wording

[istream.unformatted]/37
Effects: Behaves as an unformatted input function (as described above),
  except that it does not count the number of characters extracted and
  does not affect the value returned by subsequent calls to gcount().
  After constructing a sentry object, if rdbuf() is a null pointer,
  returns -1.

[istream.unformatted]/1
... It then creates an object of class sentry with the default argument
noskipws (second) argument true. If the sentry object returns true, when
converted to a value of type bool, the function endeavors to obtain the
  requested input. ...

It could be argued the current behaviour is correct, however
constructing a istream rdbuf() == nullptr creates a sentry that returns
false; its state is always bad in this case.

As mentioned in the bug report, after this change the 3 major
implementations behave the same.

The setting of the state
------------------------

When pubsync returned -1 it updated the local __state variable and
returned. This early return caused the state up the istream not to be
updated to the new state.

Fixes: https://github.com/llvm/llvm-project/issues/51497
Fixes: https://github.com/llvm/llvm-project/issues/51499

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2024-02-18 17:20:49 +01:00
rilysh
715567d037
[libc++] simplify the midpoint function (#81717)
Right now we've a nested ternary for the midpoint function, but this can
be simplified a bit more, using if statements. This also slightly
increases the readability of that function.
2024-02-17 16:56:02 +01:00
Mark de Wever
8cd5e67260 [NFC][libc++] Requests PR at GitHub instead of Phabricator. 2024-02-17 16:27:43 +01:00
Mark de Wever
d332d88b91
[libc++][chrono] Loads tzdata.zi in tzdb. (#74928)
This implements the loading of the tzdata.zi file and store its contents
in the tzdb struct.

This adds all required members except:
- the leap seconds,
- the locate_zone, and
- current_zone.

The class time_zone is incomplete and only contains the parts needed for
storing the parsed data.

The class time_zone_link is fully implemented including its non-member
functions.

Implements parts of:
- P0355 Extending <chrono> to Calendars and Time Zones
- P1614 The Mothership has Landed

Implements:
- P1982 Rename link to time_zone_link
2024-02-17 14:28:01 +01:00
Louis Dionne
d8278b6823
[libc++] Only include <setjmp.h> from the C library if it exists (#81887)
In 2cea1babefbb, we removed the <setjmp.h> header provided by libc++. However, we did not conditionally include the underlying <setjmp.h>
header only if the C library provides one, which we otherwise do consistently (see e.g. 647ddc08f43c).

rdar://122978778
2024-02-16 14:45:00 -07:00
LRFLEW
fc027e10ba
linear_congruential_engine: Fixes for __lce_alg_picker (#81080)
This fixes two major mistakes in the implementation of
`linear_congruential_engine` that allowed it to produce incorrect
output. Specifically, these mistakes are in `__lce_alg_picker`, which is
used to determine whether Schrage's algorithm is valid and needed.

The first mistake is in the definition of `_OverflowOK`. The code
comment and the description of [D65041](https://reviews.llvm.org/D65041)
both indicate that it's supposed to be true iff `m` is a power of two.
However, the definition used does not work out to that, and instead is
true whenever `m` is even. This could result in
`linear_congruential_engine` using an invalid implementation, as it
would incorrectly assume that any integer overflow can't change the
result. I changed the implementation to one that accurately checks if
`m` is a power of two. Technically, this implementation has an edge case
where it considers `0` to be a power of two, but in this case this is
actually accurate behavior, as `m = 0` indicates a modulus of 2^w where
w is the size of `result_type` in bits, which *is* a power of two.

The second mistake is in the static assert. The original static assert
erroneously included an unnecessary `a != 0 || m != 0`. Combined with
the `|| !_MightOverflow`, this actually resulted in the static assert
being impossible to fail. Applying De Morgan's law and expanding
`_MightOverflow` gives that the only way this static assert can be
triggered is if `a == 0 && m == 0 && a != 0 && m != 0 && ...`, which
clearly cannot be true. I simply removed the explicit checks against `a`
and `m`, as the intended checks are already included in `_MightOverflow`
and `_SchrageOK`, and their inclusion doesn't provide any obvious
semantic benefit.

This should fix all the current instances where
`linear_congruential_engine` uses an invalid implementation. This
technically isn't a complete implementation, though, since the static
assert will cause some instantiations of `linear_congruential_engine`
not disallowed by the standard from compiling. However, this should
still be an improvement, as all compiling instantiations of
`linear_congruential_engine` should use a valid implementation. Fixing
the cases where the static assert triggers will require adding
additional implementations, some of which will be fairly non-trivial, so
I'd rather leave those for another PR so they don't hold up these more
important fixes.

Fixes #33554
2024-02-15 19:46:22 +01:00
Po-yao Chang
08fe7df600
[libc++][format] Don't treat a closing '}' as part of format-spec (#81305)
This allows:
```
std::println("{}>42", std:🧵:id{});
std::println("{}>42", std::span<int>{});
std::println("{}>42", std::pair{42, "Hello"sv});
std::println("{:}>42", std:🧵:id{});
std::println("{:}>42", std::span<int>{});
std::println("{:}>42", std::pair{42, "Hello"sv});
```
to compile and run.
2024-02-16 02:41:07 +08:00
ZijunZhaoCCK
a6b846ae1e
[libc++][ranges] Implement ranges::contains_subrange (#66963) 2024-02-13 15:42:37 -08:00
Mark de Wever
fc0e9c8315
[libc++][modules] Re-add build dir CMakeLists.txt. (#81370)
This CMakeLists.txt is used to build modules without build system
support. This was removed in d06ae33ec32122bb526fb35025c1f0cf979f1090.
This is used in the documentation how to use modules.

Made some minor changes to make it work with the std.compat module using
the std module.

Note the CMakeLists.txt in the build dir should be removed once build
system support is generally available.
2024-02-13 20:04:34 +01:00
Abhina Sree
a70077ed8c
[SystemZ][z/OS][libcxx] mark aligned allocation tests XFAIL on z/OS (#80735)
zOS doesn't support aligned allocation, so mark these testcases as
unsupported.

Continuation of https://reviews.llvm.org/D102798
2024-02-13 08:03:14 -05:00
Hristo Hristov
d2ccf33933
[libc++][sstream] Explicitly delete special member functions (#80254)
The standard declares the copy constructors and copy assign operators as
deleted.

References:
- https://eel.is/c++draft/string.streams
2024-02-13 09:59:31 +02:00
Nikolas Klauser
f9d6d6fbcc
[libc++] Move the contents of __fwd/get.h into the forward declaration headers they actually belong to (#81368)
This brings us closer to one forward declaring header per public header.
2024-02-12 08:32:28 +01:00
Danny Mösch
00e80fbfb9
[NFC] Correct C++ standard names (#81421) 2024-02-11 19:43:34 +01:00
Mark de Wever
fe0d277f31
[libc++][ratio] Avoids accepting unrelated types. (#80491)
The arithmetic and comparison operators are ill-formed when R1 or R2 is
not a std::ratio.

Fixes: https://github.com/llvm/llvm-project/issues/63753
2024-02-11 13:53:59 +01:00
Mark de Wever
4fb7b3301b
[libc++][print] Moves is_terminal to the dylib. (#80464)
Having the test in the header requires including unistd.h on POSIX
platforms. This header has other declarations which may conflict with
code that uses named declarations provided by this header. For example
code using "int pipe;" would conflict with the function pipe in this
header.

Moving the code to the dylib means std::print would not be available on
Apple backdeployment targets. On POSIX platforms there is no transcoding
required so a not Standard conforming implementation is still a useful
and the observable differences are minimal. This behaviour has been done
for print before https://github.com/llvm/llvm-project/pull/76293.

Note questions have been raised in LWG4044 "Confusing requirements for
std::print on POSIX platforms", whether or not the isatty check on POSIX
platforms is required. When this LWG issue is resolved the
backdeployment targets could become Standard compliant.

This patch is intended to be backported to the LLVM-18 branch.

Fixes: https://github.com/llvm/llvm-project/issues/79782
2024-02-10 17:09:53 +01:00