[libcxx-commits] [libcxx] [libc++] Add assumption for align of begin and end pointers of vector. (PR #108961)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jan 13 12:19:59 PST 2025
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/108961
>From 94c2ede1d6289e973497d44db7f31d1fff14b0cb Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 17 Sep 2024 12:07:29 +0100
Subject: [PATCH 1/5] [libc++] Add assumption for align of begin and end
pointers of vector.
Missing information about begin and end pointers of std::vector can lead
to missed optimizations in LLVM.
See https://github.com/llvm/llvm-project/issues/101372 for a discussion
of missed range check optimizations in hardened mode.
Once https://github.com/llvm/llvm-project/pull/108958 lands, the created
`llvm.assume` calls for the alignment should be folded into the `load`
instructions, resulting in no extra instructions after InstCombine.
---
libcxx/include/__vector/vector.h | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index ddbf1235b90691..eaccb1710c766d 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -341,13 +341,17 @@ class _LIBCPP_TEMPLATE_VIS vector {
//
// Iterators
//
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __make_iter(this->__begin_); }
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
+ return __make_iter(__add_alignment_assumption(this->__begin_));
+ }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
- return __make_iter(this->__begin_);
+ return __make_iter(__add_alignment_assumption(this->__begin_));
+ }
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
+ return __make_iter(__add_alignment_assumption(this->__end_));
}
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __make_iter(this->__end_); }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
- return __make_iter(this->__end_);
+ return __make_iter(__add_alignment_assumption(this->__end_));
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
@@ -775,6 +779,10 @@ class _LIBCPP_TEMPLATE_VIS vector {
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
+
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) const _NOEXCEPT {
+ return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
+ }
};
#if _LIBCPP_STD_VER >= 17
>From 3daa6c5661ab4794c4ae5f8a97615c704d92e6cf Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 13 Jan 2025 13:18:45 -0500
Subject: [PATCH 2/5] Address review comments
---
libcxx/include/__vector/vector.h | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index eaccb1710c766d..9e8e2ee1a03cd9 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -780,8 +780,13 @@ class _LIBCPP_TEMPLATE_VIS vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) const _NOEXCEPT {
- return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
+ static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) _NOEXCEPT {
+ if constexpr (is_pointer<pointer>::value) {
+ if (!__libcpp_is_constant_evaluated()) {
+ return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
+ }
+ }
+ return __p;
}
};
>From ddb387b9bc40f5a240cd932c0d26d8749f54ce80 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 13 Jan 2025 14:14:58 -0500
Subject: [PATCH 3/5] Handle C++03
---
libcxx/include/__vector/vector.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 9e8e2ee1a03cd9..f30815f8a4b230 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -781,11 +781,13 @@ class _LIBCPP_TEMPLATE_VIS vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) _NOEXCEPT {
+#ifndef _LIBCPP_CXX03_LANG
if constexpr (is_pointer<pointer>::value) {
if (!__libcpp_is_constant_evaluated()) {
return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
}
}
+#endif
return __p;
}
};
>From a2cd12c1728fede8a7577a2a0bcbb342c7997ae9 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 13 Jan 2025 14:49:43 -0500
Subject: [PATCH 4/5] Add missing include
---
libcxx/include/__vector/vector.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index f30815f8a4b230..2c2d737bee4335 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -51,6 +51,7 @@
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_nothrow_assignable.h>
#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/is_pointer.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_relocatable.h>
#include <__type_traits/type_identity.h>
>From a9fb6aa244b4e60d6e9e76db7b57f3d59c9ef2ac Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 13 Jan 2025 15:18:45 -0500
Subject: [PATCH 5/5] Avoid instantiating vector::begin() when declaring
flat_map
Otherwise, this prevents us from being able to declare a flat_map containing
an incomplete type.
---
libcxx/include/__flat_map/key_value_iterator.h | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/__flat_map/key_value_iterator.h b/libcxx/include/__flat_map/key_value_iterator.h
index 06a23f34299745..3ebb653deb1973 100644
--- a/libcxx/include/__flat_map/key_value_iterator.h
+++ b/libcxx/include/__flat_map/key_value_iterator.h
@@ -15,9 +15,7 @@
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__memory/addressof.h>
-#include <__ranges/access.h>
#include <__type_traits/conditional.h>
-#include <__type_traits/maybe_const.h>
#include <__utility/move.h>
#include <__utility/pair.h>
@@ -41,9 +39,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Owner, class _KeyContainer, class _MappedContainer, bool _Const>
struct __key_value_iterator {
private:
- using __key_iterator _LIBCPP_NODEBUG = ranges::iterator_t<const _KeyContainer>;
- using __mapped_iterator _LIBCPP_NODEBUG = ranges::iterator_t<__maybe_const<_Const, _MappedContainer>>;
- using __reference _LIBCPP_NODEBUG = _If<_Const, typename _Owner::const_reference, typename _Owner::reference>;
+ using __key_iterator _LIBCPP_NODEBUG = typename _KeyContainer::const_iterator;
+ using __mapped_iterator _LIBCPP_NODEBUG =
+ _If<_Const, typename _MappedContainer::const_iterator, typename _MappedContainer::iterator>;
+ using __reference _LIBCPP_NODEBUG = _If<_Const, typename _Owner::const_reference, typename _Owner::reference>;
struct __arrow_proxy {
__reference __ref_;
@@ -71,8 +70,8 @@ struct __key_value_iterator {
_LIBCPP_HIDE_FROM_ABI __key_value_iterator() = default;
_LIBCPP_HIDE_FROM_ABI __key_value_iterator(__key_value_iterator<_Owner, _KeyContainer, _MappedContainer, !_Const> __i)
- requires _Const && convertible_to<ranges::iterator_t<_KeyContainer>, __key_iterator> &&
- convertible_to<ranges::iterator_t<_MappedContainer>, __mapped_iterator>
+ requires _Const && convertible_to<typename _KeyContainer::iterator, __key_iterator> &&
+ convertible_to<typename _MappedContainer::iterator, __mapped_iterator>
: __key_iter_(std::move(__i.__key_iter_)), __mapped_iter_(std::move(__i.__mapped_iter_)) {}
_LIBCPP_HIDE_FROM_ABI __key_value_iterator(__key_iterator __key_iter, __mapped_iterator __mapped_iter)
More information about the libcxx-commits
mailing list