[libcxx-commits] [libcxx] [libc++] Add assumption for align of begin and end pointers of vector. (PR #108961)
Florian Hahn via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 17 04:20:22 PDT 2024
https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/108961
>From a347676935f0c2162f84b68b6946ff16e98d8914 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/2] [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 | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/vector b/libcxx/include/vector
index 7d3aac5989a48c..720b46d573c954 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -1027,6 +1027,10 @@ private:
}
_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) _NOEXCEPT {
+ return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
+ }
};
#if _LIBCPP_STD_VER >= 17
@@ -1418,25 +1422,25 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(size_type __n
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
vector<_Tp, _Allocator>::begin() _NOEXCEPT {
- return __make_iter(this->__begin_);
+ return __make_iter(__add_alignment_assumption(this->__begin_));
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
vector<_Tp, _Allocator>::begin() const _NOEXCEPT {
- return __make_iter(this->__begin_);
+ return __make_iter(__add_alignment_assumption(this->__begin_));
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
vector<_Tp, _Allocator>::end() _NOEXCEPT {
- return __make_iter(this->__end_);
+ return __make_iter(__add_alignment_assumption(this->__end_));
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
vector<_Tp, _Allocator>::end() const _NOEXCEPT {
- return __make_iter(this->__end_);
+ return __make_iter(__add_alignment_assumption(this->__end_));
}
template <class _Tp, class _Allocator>
>From 83f60da52c8d08b6ca0168faaba3cfc81f40b5df Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 17 Sep 2024 12:20:01 +0100
Subject: [PATCH 2/2] !fixup add missing const
---
libcxx/include/vector | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/vector b/libcxx/include/vector
index 720b46d573c954..a8507d042f1efb 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -1028,7 +1028,7 @@ private:
_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) _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))));
}
};
More information about the libcxx-commits
mailing list