Skip to content

Commit 05cc2d5

Browse files
authored
[libc++] Vectorize std::mismatch with trivially equality comparable types (#87716)
1 parent 2b38688 commit 05cc2d5

File tree

8 files changed

+285
-25
lines changed

8 files changed

+285
-25
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ set(files
446446
__ios/fpos.h
447447
__iterator/access.h
448448
__iterator/advance.h
449+
__iterator/aliasing_iterator.h
449450
__iterator/back_insert_iterator.h
450451
__iterator/bounded_iter.h
451452
__iterator/common_iterator.h

libcxx/include/__algorithm/mismatch.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <__algorithm/unwrap_iter.h>
1717
#include <__config>
1818
#include <__functional/identity.h>
19+
#include <__iterator/aliasing_iterator.h>
1920
#include <__type_traits/desugars_to.h>
2021
#include <__type_traits/invoke.h>
2122
#include <__type_traits/is_constant_evaluated.h>
@@ -55,18 +56,13 @@ __mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Pred& __pred, _Pro
5556

5657
#if _LIBCPP_VECTORIZE_ALGORITHMS
5758

58-
template <class _Tp,
59-
class _Pred,
60-
class _Proj1,
61-
class _Proj2,
62-
__enable_if_t<is_integral<_Tp>::value && __desugars_to_v<__equal_tag, _Pred, _Tp, _Tp> &&
63-
__is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
64-
int> = 0>
65-
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
66-
__mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
59+
template <class _Iter>
60+
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
61+
__mismatch_vectorized(_Iter __first1, _Iter __last1, _Iter __first2) {
62+
using __value_type = __iter_value_type<_Iter>;
6763
constexpr size_t __unroll_count = 4;
68-
constexpr size_t __vec_size = __native_vector_size<_Tp>;
69-
using __vec = __simd_vector<_Tp, __vec_size>;
64+
constexpr size_t __vec_size = __native_vector_size<__value_type>;
65+
using __vec = __simd_vector<__value_type, __vec_size>;
7066

7167
if (!__libcpp_is_constant_evaluated()) {
7268
auto __orig_first1 = __first1;
@@ -116,9 +112,41 @@ __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred& __pred, _Proj1& __
116112
} // else loop over the elements individually
117113
}
118114

119-
return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
115+
__equal_to __pred;
116+
__identity __proj;
117+
return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj, __proj);
118+
}
119+
120+
template <class _Tp,
121+
class _Pred,
122+
class _Proj1,
123+
class _Proj2,
124+
__enable_if_t<is_integral<_Tp>::value && __desugars_to_v<__equal_tag, _Pred, _Tp, _Tp> &&
125+
__is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
126+
int> = 0>
127+
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
128+
__mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred&, _Proj1&, _Proj2&) {
129+
return std::__mismatch_vectorized(__first1, __last1, __first2);
120130
}
121131

132+
template <class _Tp,
133+
class _Pred,
134+
class _Proj1,
135+
class _Proj2,
136+
__enable_if_t<!is_integral<_Tp>::value && __desugars_to_v<__equal_tag, _Pred, _Tp, _Tp> &&
137+
__is_identity<_Proj1>::value && __is_identity<_Proj2>::value &&
138+
__can_map_to_integer_v<_Tp> && __libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value,
139+
int> = 0>
140+
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
141+
__mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
142+
if (__libcpp_is_constant_evaluated()) {
143+
return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
144+
} else {
145+
using _Iter = __aliasing_iterator<_Tp*, __get_as_integer_type_t<_Tp>>;
146+
auto __ret = std::__mismatch_vectorized(_Iter(__first1), _Iter(__last1), _Iter(__first2));
147+
return {__ret.first.__base(), __ret.second.__base()};
148+
}
149+
}
122150
#endif // _LIBCPP_VECTORIZE_ALGORITHMS
123151

124152
template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>

libcxx/include/__algorithm/simd_utils.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ _LIBCPP_PUSH_MACROS
4343

4444
_LIBCPP_BEGIN_NAMESPACE_STD
4545

46+
template <class _Tp>
47+
inline constexpr bool __can_map_to_integer_v =
48+
sizeof(_Tp) == alignof(_Tp) && (sizeof(_Tp) == 1 || sizeof(_Tp) == 2 || sizeof(_Tp) == 4 || sizeof(_Tp) == 8);
49+
50+
template <size_t _TypeSize>
51+
struct __get_as_integer_type_impl;
52+
53+
template <>
54+
struct __get_as_integer_type_impl<1> {
55+
using type = uint8_t;
56+
};
57+
58+
template <>
59+
struct __get_as_integer_type_impl<2> {
60+
using type = uint16_t;
61+
};
62+
template <>
63+
struct __get_as_integer_type_impl<4> {
64+
using type = uint32_t;
65+
};
66+
template <>
67+
struct __get_as_integer_type_impl<8> {
68+
using type = uint64_t;
69+
};
70+
71+
template <class _Tp>
72+
using __get_as_integer_type_t = typename __get_as_integer_type_impl<sizeof(_Tp)>::type;
73+
4674
// This isn't specialized for 64 byte vectors on purpose. They have the potential to significantly reduce performance
4775
// in mixed simd/non-simd workloads and don't provide any performance improvement for currently vectorized algorithms
4876
// as far as benchmarks are concerned.
@@ -80,10 +108,10 @@ template <class _VecT>
80108
using __simd_vector_underlying_type_t = decltype(std::__simd_vector_underlying_type_impl(_VecT{}));
81109

82110
// This isn't inlined without always_inline when loading chars.
83-
template <class _VecT, class _Tp>
84-
_LIBCPP_NODISCARD _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _VecT __load_vector(const _Tp* __ptr) noexcept {
111+
template <class _VecT, class _Iter>
112+
_LIBCPP_NODISCARD _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _VecT __load_vector(_Iter __iter) noexcept {
85113
return [=]<size_t... _Indices>(index_sequence<_Indices...>) _LIBCPP_ALWAYS_INLINE noexcept {
86-
return _VecT{__ptr[_Indices]...};
114+
return _VecT{__iter[_Indices]...};
87115
}(make_index_sequence<__simd_vector_size_v<_VecT>>{});
88116
}
89117

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___ITERATOR_ALIASING_ITERATOR_H
10+
#define _LIBCPP___ITERATOR_ALIASING_ITERATOR_H
11+
12+
#include <__config>
13+
#include <__iterator/iterator_traits.h>
14+
#include <__memory/pointer_traits.h>
15+
#include <__type_traits/is_trivial.h>
16+
#include <cstddef>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
// This iterator wrapper is used to type-pun an iterator to return a different type. This is done without UB by not
23+
// actually punning the type, but instead inspecting the object representation of the base type and copying that into
24+
// an instance of the alias type. For that reason the alias type has to be trivial. The alias is returned as a prvalue
25+
// when derferencing the iterator, since it is temporary storage. This wrapper is used to vectorize some algorithms.
26+
27+
_LIBCPP_BEGIN_NAMESPACE_STD
28+
29+
template <class _BaseIter, class _Alias>
30+
struct __aliasing_iterator_wrapper {
31+
class __iterator {
32+
_BaseIter __base_ = nullptr;
33+
34+
using __iter_traits = iterator_traits<_BaseIter>;
35+
using __base_value_type = typename __iter_traits::value_type;
36+
37+
static_assert(__has_random_access_iterator_category<_BaseIter>::value,
38+
"The base iterator has to be a random access iterator!");
39+
40+
public:
41+
using iterator_category = random_access_iterator_tag;
42+
using value_type = _Alias;
43+
using difference_type = ptrdiff_t;
44+
using reference = value_type&;
45+
using pointer = value_type*;
46+
47+
static_assert(is_trivial<value_type>::value);
48+
static_assert(sizeof(__base_value_type) == sizeof(value_type));
49+
50+
_LIBCPP_HIDE_FROM_ABI __iterator() = default;
51+
_LIBCPP_HIDE_FROM_ABI __iterator(_BaseIter __base) _NOEXCEPT : __base_(__base) {}
52+
53+
_LIBCPP_HIDE_FROM_ABI __iterator& operator++() _NOEXCEPT {
54+
++__base_;
55+
return *this;
56+
}
57+
58+
_LIBCPP_HIDE_FROM_ABI __iterator operator++(int) _NOEXCEPT {
59+
__iterator __tmp(*this);
60+
++__base_;
61+
return __tmp;
62+
}
63+
64+
_LIBCPP_HIDE_FROM_ABI __iterator& operator--() _NOEXCEPT {
65+
--__base_;
66+
return *this;
67+
}
68+
69+
_LIBCPP_HIDE_FROM_ABI __iterator operator--(int) _NOEXCEPT {
70+
__iterator __tmp(*this);
71+
--__base_;
72+
return __tmp;
73+
}
74+
75+
_LIBCPP_HIDE_FROM_ABI friend __iterator operator+(__iterator __iter, difference_type __n) _NOEXCEPT {
76+
return __iterator(__iter.__base_ + __n);
77+
}
78+
79+
_LIBCPP_HIDE_FROM_ABI friend __iterator operator+(difference_type __n, __iterator __iter) _NOEXCEPT {
80+
return __iterator(__n + __iter.__base_);
81+
}
82+
83+
_LIBCPP_HIDE_FROM_ABI __iterator& operator+=(difference_type __n) _NOEXCEPT {
84+
__base_ += __n;
85+
return *this;
86+
}
87+
88+
_LIBCPP_HIDE_FROM_ABI friend __iterator operator-(__iterator __iter, difference_type __n) _NOEXCEPT {
89+
return __iterator(__iter.__base_ - __n);
90+
}
91+
92+
_LIBCPP_HIDE_FROM_ABI friend difference_type operator-(__iterator __lhs, __iterator __rhs) _NOEXCEPT {
93+
return __lhs.__base_ - __rhs.__base_;
94+
}
95+
96+
_LIBCPP_HIDE_FROM_ABI __iterator& operator-=(difference_type __n) _NOEXCEPT {
97+
__base_ -= __n;
98+
return *this;
99+
}
100+
101+
_LIBCPP_HIDE_FROM_ABI _BaseIter __base() const _NOEXCEPT { return __base_; }
102+
103+
_LIBCPP_HIDE_FROM_ABI _Alias operator*() const _NOEXCEPT {
104+
_Alias __val;
105+
__builtin_memcpy(&__val, std::__to_address(__base_), sizeof(value_type));
106+
return __val;
107+
}
108+
109+
_LIBCPP_HIDE_FROM_ABI value_type operator[](difference_type __n) const _NOEXCEPT { return *(*this + __n); }
110+
111+
_LIBCPP_HIDE_FROM_ABI friend bool operator==(const __iterator& __lhs, const __iterator& __rhs) _NOEXCEPT {
112+
return __lhs.__base_ == __rhs.__base_;
113+
}
114+
115+
_LIBCPP_HIDE_FROM_ABI friend bool operator!=(const __iterator& __lhs, const __iterator& __rhs) _NOEXCEPT {
116+
return __lhs.__base_ != __rhs.__base_;
117+
}
118+
};
119+
};
120+
121+
// This is required to avoid ADL instantiations on _BaseT
122+
template <class _BaseT, class _Alias>
123+
using __aliasing_iterator = typename __aliasing_iterator_wrapper<_BaseT, _Alias>::__iterator;
124+
125+
_LIBCPP_END_NAMESPACE_STD
126+
127+
#endif // _LIBCPP___ITERATOR_ALIASING_ITERATOR_H

libcxx/include/__type_traits/is_equality_comparable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ struct __is_equality_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>()
4444
// pointers that don't have the same type (ignoring cv-qualifiers): pointers to virtual bases are equality comparable,
4545
// but don't have the same bit-pattern. An exception to this is comparing to a void-pointer. There the bit-pattern is
4646
// always compared.
47+
// objects with padding bytes: since objects with padding bytes may compare equal, even though their object
48+
// representation may not be equivalent.
4749

4850
template <class _Tp, class _Up, class = void>
4951
struct __libcpp_is_trivially_equality_comparable_impl : false_type {};

libcxx/include/module.modulemap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ module std_private_algorithm_minmax_element [system
700700
module std_private_algorithm_mismatch [system] {
701701
header "__algorithm/mismatch.h"
702702
export std_private_algorithm_simd_utils
703+
export std_private_iterator_aliasing_iterator
703704
}
704705
module std_private_algorithm_move [system] { header "__algorithm/move.h" }
705706
module std_private_algorithm_move_backward [system] { header "__algorithm/move_backward.h" }
@@ -1390,6 +1391,7 @@ module std_private_iosfwd_streambuf_fwd [system] { header "__fwd/streambuf.h" }
13901391

13911392
module std_private_iterator_access [system] { header "__iterator/access.h" }
13921393
module std_private_iterator_advance [system] { header "__iterator/advance.h" }
1394+
module std_private_iterator_aliasing_iterator [system] { header "__iterator/aliasing_iterator.h" }
13931395
module std_private_iterator_back_insert_iterator [system] { header "__iterator/back_insert_iterator.h" }
13941396
module std_private_iterator_bounded_iter [system] { header "__iterator/bounded_iter.h" }
13951397
module std_private_iterator_common_iterator [system] { header "__iterator/common_iterator.h" }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// ADDITIONAL_COMPILE_FLAGS(clang): -Wprivate-header
10+
11+
#include <__iterator/aliasing_iterator.h>
12+
#include <cassert>
13+
14+
struct NonTrivial {
15+
int i_;
16+
17+
NonTrivial(int i) : i_(i) {}
18+
NonTrivial(const NonTrivial& other) : i_(other.i_) {}
19+
20+
NonTrivial& operator=(const NonTrivial& other) {
21+
i_ = other.i_;
22+
return *this;
23+
}
24+
25+
~NonTrivial() {}
26+
};
27+
28+
int main(int, char**) {
29+
{
30+
NonTrivial arr[] = {1, 2, 3, 4};
31+
std::__aliasing_iterator<NonTrivial*, int> iter(arr);
32+
33+
assert(*iter == 1);
34+
assert(iter[0] == 1);
35+
assert(iter[1] == 2);
36+
++iter;
37+
assert(*iter == 2);
38+
assert(iter[-1] == 1);
39+
assert(iter.__base() == arr + 1);
40+
assert(iter == iter);
41+
assert(iter != (iter + 1));
42+
}
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)