[libcxx-commits] [libcxx] d51a84b - [libc++][PSTL] Implement std::stable_sort
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jun 1 15:07:33 PDT 2023
Author: Nikolas Klauser
Date: 2023-06-01T15:07:28-07:00
New Revision: d51a84b4059c89b5647855781e13f49f47356c17
URL: https://github.com/llvm/llvm-project/commit/d51a84b4059c89b5647855781e13f49f47356c17
DIFF: https://github.com/llvm/llvm-project/commit/d51a84b4059c89b5647855781e13f49f47356c17.diff
LOG: [libc++][PSTL] Implement std::stable_sort
Reviewed By: #libc, ldionne
Spies: ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D151573
Added:
libcxx/include/__algorithm/pstl_backends/cpu_backends/stable_sort.h
libcxx/include/__algorithm/pstl_stable_sort.h
libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp
Modified:
libcxx/include/CMakeLists.txt
libcxx/include/__algorithm/pstl_backend.h
libcxx/include/__algorithm/pstl_backends/cpu_backend.h
libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h
libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h
libcxx/include/algorithm
libcxx/include/module.modulemap.in
libcxx/test/libcxx/private_headers.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index ed94711a2b689..02eb63611cd22 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -80,6 +80,7 @@ set(files
__algorithm/pstl_backends/cpu_backends/for_each.h
__algorithm/pstl_backends/cpu_backends/merge.h
__algorithm/pstl_backends/cpu_backends/serial.h
+ __algorithm/pstl_backends/cpu_backends/stable_sort.h
__algorithm/pstl_backends/cpu_backends/thread.h
__algorithm/pstl_backends/cpu_backends/transform.h
__algorithm/pstl_backends/cpu_backends/transform_reduce.h
@@ -89,6 +90,7 @@ set(files
__algorithm/pstl_for_each.h
__algorithm/pstl_frontend_dispatch.h
__algorithm/pstl_merge.h
+ __algorithm/pstl_stable_sort.h
__algorithm/pstl_transform.h
__algorithm/push_heap.h
__algorithm/ranges_adjacent_find.h
diff --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h
index d21038246b6a0..c25a8b1d0a930 100644
--- a/libcxx/include/__algorithm/pstl_backend.h
+++ b/libcxx/include/__algorithm/pstl_backend.h
@@ -32,6 +32,9 @@ A PSTL parallel backend is a tag type to which the following functions are assoc
template <class _ExecutionPolicy, class _Iterator, class _Predicate>
_Iterator __pstl_find_if(_Backend, _Iterator __first, _Iterator __last, _Predicate __pred);
+ template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
+ void __pstl_stable_sort(_Backend, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp);
+
template <class _ExecutionPolicy, class _InIterator, class _OutIterator, class _UnaryOperation>
_OutIterator __pstl_transform(_InIterator __first, _InIterator __last, _OutIterator __result, _UnaryOperation __op);
diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backend.h b/libcxx/include/__algorithm/pstl_backends/cpu_backend.h
index 7d3d75e6fdeb7..e54f331b9430b 100644
--- a/libcxx/include/__algorithm/pstl_backends/cpu_backend.h
+++ b/libcxx/include/__algorithm/pstl_backends/cpu_backend.h
@@ -37,6 +37,12 @@
_Compare __comp,
_LeafMerge __leaf_merge);
+ template <class _RandomAccessIterator, class _Comp, class _LeafSort>
+ void __parallel_stable_sort(_RandomAccessIterator __first,
+ _RandomAccessIterator __last,
+ _Comp __comp,
+ _LeafSort __leaf_sort);
+
TODO: Document the parallel backend
*/
@@ -46,6 +52,7 @@
#include <__algorithm/pstl_backends/cpu_backends/find_if.h>
#include <__algorithm/pstl_backends/cpu_backends/for_each.h>
#include <__algorithm/pstl_backends/cpu_backends/merge.h>
+#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h>
#include <__algorithm/pstl_backends/cpu_backends/transform.h>
#include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h>
diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h
index fe25e4e5b706a..f151c3b098f67 100644
--- a/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h
+++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h
@@ -12,6 +12,7 @@
#include <__config>
#include <__utility/move.h>
+#include <cstddef>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -35,6 +36,12 @@ __parallel_transform_reduce(_Index __first, _Index __last, _UnaryOp, _Tp __init,
return __reduce(std::move(__first), std::move(__last), std::move(__init));
}
+template <class _RandomAccessIterator, class _Compare, class _LeafSort>
+_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort(
+ _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) {
+ __leaf_sort(__first, __last, __comp);
+}
+
_LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {}
template <class _RandomAccessIterator1,
diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/stable_sort.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/stable_sort.h
new file mode 100644
index 0000000000000..0a701443b3c40
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/stable_sort.h
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
+#define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
+
+#include <__algorithm/pstl_backends/cpu_backends/backend.h>
+#include <__algorithm/stable_sort.h>
+#include <__config>
+#include <__type_traits/is_execution_policy.h>
+#include <__utility/terminate_on_exception.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
+_LIBCPP_HIDE_FROM_ABI void
+__pstl_stable_sort(__cpu_backend_tag, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) {
+ if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy>) {
+ std::__terminate_on_exception([&] {
+ __par_backend::__parallel_stable_sort(
+ __first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
+ std::stable_sort(__g_first, __g_last, __g_comp);
+ });
+ });
+ } else {
+ std::stable_sort(__first, __last, __comp);
+ }
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+#endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h
index eb8e962a8189b..e5f05bc249965 100644
--- a/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h
+++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h
@@ -12,6 +12,7 @@
#include <__assert>
#include <__config>
#include <__utility/move.h>
+#include <cstddef>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -38,6 +39,12 @@ __parallel_transform_reduce(_Index __first, _Index __last, _UnaryOp, _Tp __init,
return __reduce(std::move(__first), std::move(__last), std::move(__init));
}
+template <class _RandomAccessIterator, class _Compare, class _LeafSort>
+_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort(
+ _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) {
+ __leaf_sort(__first, __last, __comp);
+}
+
_LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {}
template <class _RandomAccessIterator1,
diff --git a/libcxx/include/__algorithm/pstl_stable_sort.h b/libcxx/include/__algorithm/pstl_stable_sort.h
new file mode 100644
index 0000000000000..cdb257150112a
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_stable_sort.h
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
+#define _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
+
+#include <__algorithm/pstl_backend.h>
+#include <__config>
+#include <__functional/operations.h>
+#include <__type_traits/is_execution_policy.h>
+#include <__type_traits/remove_cvref.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _ExecutionPolicy,
+ class _RandomAccessIterator,
+ class _Comp = less<>,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI void
+stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp = {}) {
+ using _Backend = typename __select_backend<_RawPolicy>::type;
+ std::__pstl_stable_sort<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__comp));
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+#endif // _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 0f52da9a17d85..24d29fd777f54 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -1806,6 +1806,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/pstl_find.h>
#include <__algorithm/pstl_for_each.h>
#include <__algorithm/pstl_merge.h>
+#include <__algorithm/pstl_stable_sort.h>
#include <__algorithm/pstl_transform.h>
#include <__algorithm/push_heap.h>
#include <__algorithm/ranges_adjacent_find.h>
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index f52fe23960252..46dd028a89858 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -345,6 +345,9 @@ module std [system] {
module pstl_backends_cpu_backends_serial {
private header "__algorithm/pstl_backends/cpu_backends/serial.h"
}
+ module pstl_backends_cpu_backends_stable_sort {
+ private header "__algorithm/pstl_backends/cpu_backends/stable_sort.h"
+ }
module pstl_backends_cpu_backends_thread {
private header "__algorithm/pstl_backends/cpu_backends/thread.h"
}
diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp
index 0910537530093..0aaa71e2dc369 100644
--- a/libcxx/test/libcxx/private_headers.verify.cpp
+++ b/libcxx/test/libcxx/private_headers.verify.cpp
@@ -123,6 +123,7 @@ END-SCRIPT
#include <__algorithm/pstl_backends/cpu_backends/for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/for_each.h'}}
#include <__algorithm/pstl_backends/cpu_backends/merge.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/merge.h'}}
#include <__algorithm/pstl_backends/cpu_backends/serial.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/serial.h'}}
+#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/stable_sort.h'}}
#include <__algorithm/pstl_backends/cpu_backends/thread.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/thread.h'}}
#include <__algorithm/pstl_backends/cpu_backends/transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform.h'}}
#include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform_reduce.h'}}
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp
new file mode 100644
index 0000000000000..7298b367f7f39
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp
@@ -0,0 +1,143 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class RandomAccessIterator>
+// void stable_sort(ExecutionPolicy&& exec,
+// RandomAccessIterator first, RandomAccessIterator last);
+//
+// template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
+// void stable_sort(ExecutionPolicy&& exec,
+// RandomAccessIterator first, RandomAccessIterator last,
+// Compare comp);
+
+#include <algorithm>
+#include <array>
+#include <atomic>
+#include <cassert>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+
+EXECUTION_POLICY_SFINAE_TEST(stable_sort);
+
+static_assert(sfinae_test_stable_sort<int, int*, int*, bool (*)(int)>);
+static_assert(!sfinae_test_stable_sort<std::execution::parallel_policy, int*, int*, bool (*)(int, int)>);
+
+struct OrderedValue {
+ int value;
+ double original_order;
+ bool operator==(const OrderedValue& other) const { return other.value == value; }
+
+ auto operator<(const OrderedValue& rhs) const { return value < rhs.value; }
+ auto operator>(const OrderedValue& rhs) const { return value > rhs.value; }
+};
+
+template <class Iter, std::size_t N>
+void test_one(std::array<int, N> input, std::array<int, N> expected) {
+ std::stable_sort(Iter(input.data()), Iter(input.data() + input.size()));
+ assert(input == expected);
+}
+
+template <class Iter>
+struct Test {
+ template <class Policy>
+ void operator()(Policy&& policy) {
+
+ // Empty sequence.
+ test_one<Iter, 0>({}, {});
+ // 1-element sequence.
+ test_one<Iter, 1>({1}, {1});
+ // 2-element sequence.
+ test_one<Iter, 2>({2, 1}, {1, 2});
+ // 3-element sequence.
+ test_one<Iter, 3>({2, 1, 3}, {1, 2, 3});
+ // Longer sequence.
+ test_one<Iter, 8>({2, 1, 3, 6, 8, 4, 11, 5}, {1, 2, 3, 4, 5, 6, 8, 11});
+ // Longer sequence with duplicates.
+ test_one<Iter, 7>({2, 1, 3, 6, 2, 8, 6}, {1, 2, 2, 3, 6, 6, 8});
+ // All elements are the same.
+ test_one<Iter, 3>({1, 1, 1}, {1, 1, 1});
+ // Already sorted.
+ test_one<Iter, 5>({1, 2, 3, 4, 5}, {1, 2, 3, 4, 5});
+ // Reverse-sorted.
+ test_one<Iter, 5>({5, 4, 3, 2, 1}, {1, 2, 3, 4, 5});
+ // Repeating pattern.
+ test_one<Iter, 6>({1, 2, 1, 2, 1, 2}, {1, 1, 1, 2, 2, 2});
+
+ { // The sort is stable (equivalent elements remain in the same order).
+ using V = OrderedValue;
+ using Array = std::array<V, 20>;
+ Array in = {V{10, 10.1}, {12, 12.1}, {3, 3.1}, {5, 5.1}, {3, 3.2}, {3, 3.3}, {11, 11.1},
+ {12, 12.2}, {4, 4.1}, {4, 4.2}, {4, 4.3}, {1, 1.1}, {6, 6.1}, {3, 3.4},
+ {10, 10.2}, {8, 8.1}, {12, 12.3}, {1, 1.2}, {1, 1.3}, {5, 5.2}};
+ Array expected = {V{1, 1.1}, {1, 1.2}, {1, 1.3}, {3, 3.1}, {3, 3.2}, {3, 3.3}, {3, 3.4},
+ {4, 4.1}, {4, 4.2}, {4, 4.3}, {5, 5.1}, {5, 5.2}, {6, 6.1}, {8, 8.1},
+ {10, 10.1}, {10, 10.2}, {11, 11.1}, {12, 12.1}, {12, 12.2}, {12, 12.3}};
+
+ std::stable_sort(policy, in.begin(), in.end());
+ assert(in == expected);
+ }
+
+ { // A custom comparator works and is stable.
+ using V = OrderedValue;
+ using Array = std::array<V, 11>;
+
+ Array in = {
+ V{1, 1.1},
+ {2, 2.1},
+ {2, 2.2},
+ {3, 3.1},
+ {2, 2.3},
+ {3, 3.2},
+ {4, 4.1},
+ {5, 5.1},
+ {2, 2.4},
+ {5, 5.2},
+ {1, 1.2}};
+ Array expected = {
+ V{5, 5.1},
+ {5, 5.2},
+ {4, 4.1},
+ {3, 3.1},
+ {3, 3.2},
+ {2, 2.1},
+ {2, 2.2},
+ {2, 2.3},
+ {2, 2.4},
+ {1, 1.1},
+ {1, 1.2}};
+
+ std::stable_sort(policy, in.begin(), in.end(), std::greater{});
+ assert(in == expected);
+ }
+ }
+};
+
+int main(int, char**) {
+ types::for_each(types::random_access_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
+
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ std::set_terminate(terminate_successful);
+ int a[] = {1, 2};
+ try {
+ std::stable_sort(std::execution::par, std::begin(a), std::end(a), [](int, int) -> bool { throw int{}; });
+ } catch (int) {
+ assert(false);
+ }
+#endif
+
+ return 0;
+}
More information about the libcxx-commits
mailing list