357 Commits

Author SHA1 Message Date
Nikolas Klauser
c9863ceacc
[libc++] Specialize allocator_traits for std::allocator (#160278)
This reduces the time to instantiate `allocator_traits<allocator<T>>`.
2026-02-05 11:43:47 +01:00
Nikolas Klauser
dd7348420d
[libc++][NFC] Use forward declarations in {in,}out_ptr.h (#176341)
This doesn't improve compile times currently, but it does make it clear
that we don't need the definition of `shared_ptr` or `unique_ptr` for
`{in,}out_ptr`. It also reduces compile times quite a bit if we ever use
these utilities in another header.
2026-01-19 15:13:47 -05:00
Nikolas Klauser
4304106dc2
Reapply "[libc++] Optimize std::find_if" (#175903) (#175921)
#175913 removed that `__builtin_assume_dereferenceable(ptr, 0)` implies
`ptr != nullptr`, which should allow us to use the builtin with LLVM 23.

This reverts commit 776c09c212e945fdceeae240b42c38df3dd34727.
2026-01-15 10:16:45 +01:00
Nikolas Klauser
776c09c212
Revert "[libc++] Optimize std::find_if" (#175903)
`__builtin_assume_dereferenceable` currently implies that the pointer
given to it is non-null, even if the size is zero. This causes
miscompilations, since we consider [nullptr, nullptr) to be a valid
range.

Reverts llvm/llvm-project#167697
2026-01-14 11:15:21 +01:00
Nikolas Klauser
6189512f73
[libc++] Optimize std::find_if (#167697)
```
Benchmark                       4ecfaa602f56    80d5ac247d34    Difference    % Difference
----------------------------  --------------  --------------  ------------  --------------
bm_find_if_autovectorization         1901.51          306.12      -1595.39         -83.90%
```
2026-01-13 11:13:54 +01:00
Hristo Hristov
e593dbc184
[libc++][memory] Applied [[nodiscard]] to more functions (#172131)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/allocator.traits
- https://wg21.link/specialized.addressof
- https://wg21.link/ptr.align
- https://timsong-cpp.github.io/cppwp/n4659/depr.default.allocator
- https://timsong-cpp.github.io/cppwp/n4659/depr.storage.iterator

Towards #172124
2026-01-08 12:05:43 +02:00
saipubw
1c06165c9b
[libc++] Make std::align an inline function (#167472)
`std::align` is heavily used in memory allocators. When we attempted to
switch from libstdc++ to libc++, we observed a **50%** performance
regression in a database query bench: the issue is that `std::align` in
libc++ is not an inline function, which prevents the compiler from
performing inlining optimizations.

make `std::align` an inline function will run about 2x faster. See
[benchmark
result](https://quick-bench.com/q/wPTnt9JCGn2S-3bu5gY9YrEf6KU).

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2025-12-18 17:55:28 +08:00
Nikolas Klauser
df6c27e752
[libc++] Make std::allocator always trivially default constructible (#169914)
This is technically ABI breaking, since `is_trivial` and
`is_trivially_default_constructible` now return different results.
However, I don't think that's a significant issue, since `allocator` is
almost always used in classes which own memory, making them non-trivial
anyways.
2025-12-11 10:27:43 +01:00
Hristo Hristov
4125e73cdc
[libc++] Applied [[nodiscard]] to hash<shared_ptr>, hash<unique_ptr>, etc. (#170674)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.html


1. `hash<shared_ptr>`, `hash<unique_ptr>`, `std::integer_sequence<>`
etc.
2. Also implements fixes to
https://github.com/llvm/llvm-project/issues/169634 on the go (issues
discovered during current implementation)

---------

Co-authored-by: A. Jiang <de34@live.cn>
Co-authored-by: Hristo Hristov <zingam@outlook.com>
2025-12-08 06:56:33 +02:00
Nikolas Klauser
3dcdb4c765
[libc++][NFC] Move __memory/aligned_alloc.h into src/ (#166172)
This header is only ever used inside `src/`, so we might as well move it
there. As a drive-by this also removes some dead code.
2025-11-24 22:36:13 +01:00
Hristo Hristov
3f151a3fa6
[libc++][memory] Applied [[nodiscard]] to smart pointers (#168483)
Applied `[[nodiscard]]` where relevant to smart pointers and related
functions.

- [x] - `std::unique_ptr`
- [x] - `std::shared_ptr`
- [x] - `std::weak_ptr`

See guidelines:
-
https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- `[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue. For example a locking
constructor in unique_lock.

---------

Co-authored-by: Hristo Hristov <zingam@outlook.com>
2025-11-20 04:19:15 +02:00
Hristo Hristov
9036e2359f
[libc++] Apply [[nodiscard]] to in/out_ptr (#167097)
...according to Coding Guidelines: `[[nodiscard]]` should be applied to
functions where discarding the return value is most likely a correctness
issue.

Changes to:
  - [x] `inout_ptr()` 
  - [x] `out_ptr()` 


At the time of impelentation the `[[nodiscard]]` policy has not been
established yet.

---------

Co-authored-by: Hristo Hristov <zingam@outlook.com>
2025-11-15 03:00:04 +02:00
Nikolas Klauser
f038dfd22d
[libc++] Merge is_{,un}bounded_array.h into is_array.h (#167479)
These headers are incredibly simple and closely related, so this merges
them into a single one.
2025-11-13 09:29:28 +01:00
Nikolas Klauser
2ac9e59d97
[libc++] Simplify the implementation of the unique_ptr -> shared_ptr converting constructor (#165619)
This also backports LWG2415 as a drive-by.
2025-11-13 09:09:44 +01:00
Louis Dionne
17ce48f2b6
[libc++] Remove __is_replaceable emulation (#167355)
The Trivial Relocation feature has been removed from the C++26 working
draft. Based on discussions in Kona, it is unlikely that the
"replaceable" type concept will come back in the C++29 time frame.

Since we don't have a use for the type trait in the library at the
moment, remove the code associated to it. If we end up needing something
like it in the future, we can always add it back.
2025-11-11 07:53:56 -05:00
Nikolas Klauser
c0bca9cb84
[libc++] Remove some of the uses of aligned_storage inside the library (#161635)
`aligned_storage` has been deprecated and will most likely be removed in
a future version of C++. This patch removes some of its uses to avoid
having to work around its removal in the future.
2025-11-11 11:03:02 +01:00
Nikolas Klauser
471dbb90cb
[libc++] Replace __libcpp_is_final with a variable template (#167137) 2025-11-10 11:00:05 +01:00
Nikolas Klauser
9f9b8d56f4
[libc++] Simplify the implementation of destroy_at a bit (#165392) 2025-10-31 12:56:37 +01:00
Louis Dionne
b3a199469c
[libc++] Properly implement array cookies in the ARM ABI (#160182)
When we implemented array cookie support for hardening std::unique_ptr,
the implementation was only done for the Itanium ABI. I did not
initially realize that ARM was using a different ABI for array cookies,
so unique_ptr should not have been hardened on ARM.

However, we were also incorrectly setting the ABI-detection macro: we
were pretending to be using a vanilla Itanium ABI when in reality the
(similar but different) ARM ABI was in use. As a result, unique_ptr was
using the wrong representation for array cookies on ARM, which
fortunately only mattered in the case of overaligned types.

This patch fixes that.

rdar://160852193
2025-10-16 23:40:58 -04:00
Nikolas Klauser
446d950524
[libc++] Use std::__{scope,exception}_guard throughout the code base (#161322)
This simplifies the code quite a bit and seems to improve code size
slightly in some cases.
2025-10-09 09:32:40 +02:00
Nikolas Klauser
cc95c5680e
[libc++] Remove alignment attributes from _LIBCPP_COMPRESSED_PAIR (#158595)
These attributes aren't required anymore, since the potential
overalignemnt of the objects is handled by the surrounding anonymous
struct.
2025-09-25 19:59:08 +02:00
Nikolas Klauser
b5bb50d09a
[libc++] Simplify __memory/shared_count.h a bit (#160048)
This removes a few checks that aren't required anymore and moves some
code around to the places where it's actually used.
2025-09-24 10:40:36 +02:00
Nikolas Klauser
8dae17be29
[libc++] Refactor memory allocation in basic_string (#128423)
This patch introduces a string-internal API to make the allocation and
deallocation the long string simpler. Before this we had a lot of code
duplication, so ensuring that things were actually correct was
non-trivial.
2025-09-18 14:36:46 +02:00
Nikolas Klauser
868aa5f19c
[libc++] Introduce _LIBCPP_COMPRESSED_ELEMENT (#134253)
We have multiple classes with an empty base optimization that contains
just a single type. This patch introduces `_LIBCPP_COMPRESSED_ELEMENT`
to refactor these classes to avoid having them essentially twice,
reducing the amount of code significantly.
2025-09-18 14:31:18 +02:00
Vinay Deshmukh
0b28614f32
[NFC][libc++] Fix typo in libcxx/include/__memory/pointer_traits.h (#157304)
* Apply the typo fix as a separate NFC patch from here:
https://github.com/llvm/llvm-project/pull/134330/files#r2313015079
2025-09-10 11:55:00 +02:00
Nikolas Klauser
34d4f0c136
[libc++][NFC] Use llvm.org/PR to link to bug reports (#156288)
We've built up quite a few links directly to github within the code
base. We should instead use `llvm.org/PR<issue-number>` to link to bugs,
since that is resilient to the bug tracker changing in the future. This
is especially relevant for tests linking to bugs, since they will
probably be there for decades to come. A nice side effect is that these
links are significantly shorter than the GH links, making them much less
of an eyesore.

This patch also replaces a few links that linked to the old bugzilla
instance on llvm.org.
2025-09-04 09:20:02 +02:00
Nikolas Klauser
e8fa13ca4e
[libc++] Split ABI flag for removing iterator bases and removing the second member in reverse_iterator (#143079)
Currently `_LIBCPP_NO_ITERATOR_BASES` controls both whether specific
classes derive from `iterator` and whether `reverse_iterator` has a
second member variable. These two changes are orthogonal though, and one
can be applied in all langauge modes while the other change is only
conforming for C++17 and later.
2025-09-04 09:18:49 +02:00
Nikolas Klauser
f5e687d7bf
[libc++] Fix ABI break introduced by switching to _LIBCPP_COMPRESSED_PAIR (#154686)
LLVM 20 contained an ABI break that can result in the size of
`std::unordered_{map,set,multimap,multiset}` and `std::deque` changing
when used with an allocator type that is empty and contains a base class
that is the same across rebound allocator instantiations (e.g.
``Allocator<int>`` and ``Allocator<char>`` are both empty and contain
the same base class).

In addition, the layout of a user-defined type that:
- contains one of the following containers:
`std::unordered_{map,set,multimap,multiset}`, `std::deque`, `std::map`,
`std::set`, `std::multimap`, `std::multiset`, `std::list` or
`std::vector`, and
- passes an empty allocator, comparator or hasher type to that
container, and
- has a member of that same empty allocator, comparator or hasher type
inside the enclosing struct, and
- that member is either marked with `[[no_unique_address]]` or optimized
out via the EBO (empty base optimization) technique
saw its size increase from LLVM 19 to LLVM 20. This was caused by the
usage of `[[no_unique_address]]` within some of libc++'s containers in a
way that allowed subtle interactions with enclosing objects. This is
fixed in LLVM 21 on Clang (returning to the LLVM 19 ABI), however that
implies an ABI break from LLVM 20 to LLVM 21.
Furthermore, fixing this causes a slight regression to constant
evaluation support in `std::unique_ptr`. Specifically, constant
evaluation will now fail when the deleter relies on being
value-initialized for constant-evaluation admissibility. If a
default-initialized deleter can be used during constant evaluation, or
if the default constructor is non-trivial, the `unique_ptr` is not
affected by this regression. In particular, this regression does not
impact any `unique_ptr` using the default deleter.

Note that there is currently no way to realistically fix this ABI break
on GCC, therefore GCC will remain on the ABI introduced in LLVM 19. That
also means that Clang and GCC will have a slightly different ABI for the
small subset of types listed above until we are able to apply the same
fix we did with Clang on GCC.

We fix this regression by surrounding the members of the
`_LIBCPP_COMPRESSED_PAIR` with an anonymous struct. This restricts the
shifting of empty types to the front of the `_LIBCPP_COMPRESSED_PAIR`
instead of throughout the surrounding object. This "frees up" the zero
offset to contain another object of the same type, restoring the ability
to perform EBO or to elide the storage for a type with
`[[no_unique_address]]` in the enclosing (user-defined) struct.

Fixes #154146

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2025-08-23 07:58:37 +02:00
Nikolas Klauser
091c33b1f5
[libc++] Remove SFINAE on __tuple_impl constructors (#151654)
The SFINAE isn't required, since the primary `tuple` class already does
the SFINAE checks. This removes a bit of code that was only used for
these constraints.

This also moves the `tuple_element` specialization for `tuple` to
`__fwd/tuple.h` to avoid a dependency on `__tuple/sfinae_helpers.h`
(which should be moved in a follow-up).
2025-08-05 09:48:46 +02:00
Nikolas Klauser
b5348e7622
[libc++] Diagnose passing null pointers to a bunch of APIs (#148585) 2025-07-19 11:12:20 +02:00
Nikolas Klauser
30f8c64b1d
[libc++][NFC] Simplify std::__destroy_at a bit (#147025) 2025-07-10 09:22:54 +02:00
Nikolas Klauser
1121d64ba9
[libc++][NFC] Merge add_{r,l}value_reference.h (#147022)
The implementation is now quite simple, so it doesn't make a ton of
sense to have these in separate headers.
2025-07-05 18:11:36 +02:00
Nikolas Klauser
650b451d00
[libc++] Simplify the implementation of pointer_traits a bit (#142260) 2025-06-19 10:06:59 +02:00
Peng Liu
38fb0117ab
[libc++] Make forward_list constexpr as part of P3372R3 (#129435)
Fixes #128658
2025-06-11 12:13:36 -04:00
Nikolas Klauser
040e9e02cc
[libc++] Inline __has_feature and __has_extension uses (#133634)
Since GCC now supports `__has_feature` and `__has_extension` as well,
there isn't much of a reason to define new macros to test for the
features.
2025-06-10 05:54:15 +02:00
A. Jiang
769c42f4a5
[libc++] Fix padding calculation for function reference types (#142125)
#109028 caused `sizeof` to be sometimes applied to function reference
types, which makes a program ill-formed. This PR handles reference types
by specializations to prevent such bogus `sizeof` expression to be
instantiated.

Fixes #142118.
2025-06-03 11:54:49 -04:00
James Y Knight
c3656afdf1
Revert "[libc++] Introduce ABI sensitive areas to avoid requiring _LIBCPP_HIDE_FROM_ABI everywhere (#131156)" (#141756)
This reverts commit c861fe8a71e64f3d2108c58147e7375cd9314521.

Unfortunately, this use of hidden visibility attributes causes
user-defined specializations of standard-library types to also be marked
hidden by default, which is incorrect. See discussion thread on #131156.

...and also reverts the follow-up commits:

Revert "[libc++] Add explicit ABI annotations to functions from the block runtime declared in <__functional/function.h> (#140592)"
This reverts commit 3e4c9dc299c35155934688184319d391b298fff7.

Revert "[libc++] Make ABI annotations explicit for windows-specific code (#140507)"
This reverts commit f73287e623a6c2e4a3485832bc3e10860cd26eb5.

Revert "[libc++][NFC] Replace a few "namespace std" with the correct macro (#140510)"
This reverts commit 1d411f27c769a32cb22ce50b9dc4421e34fd40dd.
2025-05-28 12:04:51 -04:00
Nikolas Klauser
c861fe8a71
[libc++] Introduce ABI sensitive areas to avoid requiring _LIBCPP_HIDE_FROM_ABI everywhere (#131156)
This patch introduces `_LIBCPP_{BEGIN,END}_EXPLICIT_ABI_ANNOTATIONS`,
which allow us to have implicit annotations for most functions, and just
where it's not "hide_from_abi everything" we add explicit annotations.
This allows us to drop the `_LIBCPP_HIDE_FROM_ABI` macro from most
functions in libc++.
2025-05-18 15:47:05 +02:00
Nikolas Klauser
53f11dd4d6
[libc++] Avoid type-punning between __value_type and pair (#134819)
Before this patch, we were dereferencing pointers to objects which were
never constructed. Now we always assume that nodes store `pair<const
KeyT, ValueT>` for maps instead, as they actually do. This patch also
allows for significant follow-up simplifications, since
`__node_value_type` and `__container_value_type` are the same type now.
2025-05-15 09:01:56 +02:00
Louis Dionne
45d493b680
[libc++] Add the __is_replaceable type trait (#132408)
That type trait represents whether move-assigning an object is
equivalent to destroying it and then move-constructing a new one from
the same argument. This will be useful in a few places where we may want
to destroy + construct instead of doing an assignment, in particular
when implementing some container operations in terms of relocation.

This is effectively adding a library emulation of P2786R12's
is_replaceable trait, similarly to what we do for trivial relocation.
Eventually, we can replace this library emulation by the real
compiler-backed trait.

This is building towards #129328.
2025-05-08 16:35:00 -04:00
Damien L-G
557e931d95
[libc++] Implement P2897R7 aligned_accessor: An mdspan accessor expressing pointer over-alignment (#122603)
Closes #118372
2025-04-14 17:33:57 -04:00
Nikolas Klauser
af9c04fa68
[libc++] Remove _LIBCPP_TEMPLATE_VIS (#134885)
The need for `_LIBCPP_TEMPLATE_VIS` has been removed in #133233.
2025-04-09 23:47:57 +02:00
Nikolas Klauser
c13c04fdfe
[libc++] Simplify the implementation of the pointer aliases in allocator_traits (#127079) 2025-03-28 10:27:00 +01:00
Nikolas Klauser
a8e168e0c1
[libc++][NFC] Replace structs with variable templates in <__memory/allocator_traits.h> (#129237)
Variable templates are a bit easier on the compiler and improve the
readability of the code.
2025-03-25 15:58:24 +01:00
Nikolas Klauser
fb44f00641
[libc++] Add [[gnu::nodebug]] on type traits (#128502) 2025-03-23 21:01:25 +01:00
Mark de Wever
9feac2cbd0 [libc++] Improve deprecated diagnostic guards.
Recent Clang-21 builds improved the deprecated diagnotics. This
uncovered missing guards in libc++ internally.

Note: This patch should be a separate commit and not merged.
For testing purposes they are combined.

Reviewed as part of #130497.
2025-03-18 18:30:48 +01:00
Louis Dionne
c53caae1d0 [libc++][NFC] Remove dead link in comment 2025-03-17 13:39:58 -04:00
Mark de Wever
fd973d2512
[libc++] Protect more code against -Wdeprecated. (#130419)
This seems needed when updating the CI Docker image.
2025-03-09 17:33:59 +01:00
Mark de Wever
da618cf0a7
[NFC][libc++] Guard against operator& hijacking. (#128351)
This set usage of operator& instead of std::addressof seems not be easy
to "abuse". Some seem easy to misuse, like basic_ostream::operator<<,
trying to do that results in compilation errors since the `widen`
function is not specialized for the hijacking character type. Hence
there are no tests.
2025-02-27 17:47:34 +01:00
Louis Dionne
5e26fb1699
[libc++] Qualify calls to nullary functions like __throw_foo (#122465)
This is technically not necessary in most cases to prevent issues with ADL,
but let's be consistent. This allows us to remove the libcpp-qualify-declval
clang-tidy check, which is now enforced by the robust-against-adl clang-tidy check.
2025-02-21 07:59:46 -05:00