[libcxx-commits] [libcxx] [libc++][pstl] Implementation of parallel std::minmax_element() based on parallel reduce (PR #221572)

Michael G. Kazakov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 6 11:04:43 PDT 2026


https://github.com/mikekazakov updated https://github.com/llvm/llvm-project/pull/221572

>From 7f99257403971a821b9d6296af52f9cf7a959008 Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Sun, 6 Sep 2026 15:11:12 +0100
Subject: [PATCH 1/2] Implementation of parallel minmax_element()

---
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__algorithm/pstl.h             |  25 +++
 libcxx/include/__pstl/backend_fwd.h           |   6 +
 libcxx/include/__pstl/backends/default.h      |   4 +
 libcxx/include/__pstl/backends/libdispatch.h  |   5 +
 libcxx/include/__pstl/backends/serial.h       |  10 +
 libcxx/include/__pstl/backends/std_thread.h   |   5 +
 .../include/__pstl/cpu_algos/minmax_element.h |  92 ++++++++
 libcxx/include/module.modulemap.in            |   3 +
 .../pstl.iterator-requirements.verify.cpp     |   3 +
 .../algorithms/pstl.nodiscard.verify.cpp      |   4 +
 .../alg.min.max/pstl.minmax_element.pass.cpp  | 209 +++++++++++++++++
 .../pstl.minmax_element_comp.pass.cpp         | 211 ++++++++++++++++++
 .../pstl.exception_handling.pass.cpp          |   8 +
 14 files changed, 586 insertions(+)
 create mode 100644 libcxx/include/__pstl/cpu_algos/minmax_element.h
 create mode 100644 libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d8fbc3462b02b..113d4fde7ca59 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -703,6 +703,7 @@ set(files
   __pstl/cpu_algos/is_heap_until.h
   __pstl/cpu_algos/merge.h
   __pstl/cpu_algos/min_element.h
+  __pstl/cpu_algos/minmax_element.h
   __pstl/cpu_algos/mismatch.h
   __pstl/cpu_algos/reverse.h
   __pstl/cpu_algos/search.h
diff --git a/libcxx/include/__algorithm/pstl.h b/libcxx/include/__algorithm/pstl.h
index 14b81ed0ade4f..76bacb87b2dad 100644
--- a/libcxx/include/__algorithm/pstl.h
+++ b/libcxx/include/__algorithm/pstl.h
@@ -189,6 +189,31 @@ min_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardItera
       std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), std::move(__comp));
 }
 
+template <class _ExecutionPolicy,
+          class _ForwardIterator,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>
+minmax_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
+  _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "minmax_element requires ForwardIterators");
+  using _Implementation = __pstl::__dispatch<__pstl::__minmax_element, __pstl::__current_configuration, _RawPolicy>;
+  return __pstl::__handle_exception<_Implementation>(
+      std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), less<>{});
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator,
+          class _Compare,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>
+minmax_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
+  _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "minmax_element requires ForwardIterators");
+  using _Implementation = __pstl::__dispatch<__pstl::__minmax_element, __pstl::__current_configuration, _RawPolicy>;
+  return __pstl::__handle_exception<_Implementation>(
+      std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), std::move(__comp));
+}
+
 template <class _ExecutionPolicy,
           class _ForwardIterator1,
           class _ForwardIterator2,
diff --git a/libcxx/include/__pstl/backend_fwd.h b/libcxx/include/__pstl/backend_fwd.h
index bec595449b40c..361969dfd1ef3 100644
--- a/libcxx/include/__pstl/backend_fwd.h
+++ b/libcxx/include/__pstl/backend_fwd.h
@@ -213,6 +213,12 @@ struct __min_element;
 // optional<_ForwardIterator>
 // operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept;
 
+template <class _Backend, class _ExecutionPolicy>
+struct __minmax_element;
+// template <class _Policy, class _ForwardIterator, class _Compare>
+// optional<pair<_ForwardIterator, _ForwardIterator>>
+// operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept;
+
 template <class _Backend, class _ExecutionPolicy>
 struct __search_n;
 // template <class _Policy, class _ForwardIterator, class _Size, class _T, class _Predicate>
diff --git a/libcxx/include/__pstl/backends/default.h b/libcxx/include/__pstl/backends/default.h
index e63c3802538de..3bb477ce0aa67 100644
--- a/libcxx/include/__pstl/backends/default.h
+++ b/libcxx/include/__pstl/backends/default.h
@@ -82,6 +82,10 @@ namespace __pstl {
 // ---------------
 // - max_element
 //
+// minmax_element family
+// -------------------
+// No other algorithms based on minmax_element
+//
 // mismatch family
 // ---------------
 // - adjacent_find
diff --git a/libcxx/include/__pstl/backends/libdispatch.h b/libcxx/include/__pstl/backends/libdispatch.h
index 5d3fef8a4a176..dbd9fa34e1f87 100644
--- a/libcxx/include/__pstl/backends/libdispatch.h
+++ b/libcxx/include/__pstl/backends/libdispatch.h
@@ -43,6 +43,7 @@
 #include <__pstl/cpu_algos/is_heap_until.h>
 #include <__pstl/cpu_algos/merge.h>
 #include <__pstl/cpu_algos/min_element.h>
+#include <__pstl/cpu_algos/minmax_element.h>
 #include <__pstl/cpu_algos/mismatch.h>
 #include <__pstl/cpu_algos/reverse.h>
 #include <__pstl/cpu_algos/search.h>
@@ -389,6 +390,10 @@ template <class _ExecutionPolicy>
 struct __min_element<__libdispatch_backend_tag, _ExecutionPolicy>
     : __cpu_parallel_min_element<__libdispatch_backend_tag, _ExecutionPolicy> {};
 
+template <class _ExecutionPolicy>
+struct __minmax_element<__libdispatch_backend_tag, _ExecutionPolicy>
+    : __cpu_parallel_minmax_element<__libdispatch_backend_tag, _ExecutionPolicy> {};
+
 template <class _ExecutionPolicy>
 struct __mismatch<__libdispatch_backend_tag, _ExecutionPolicy>
     : __cpu_parallel_mismatch<__libdispatch_backend_tag, _ExecutionPolicy> {};
diff --git a/libcxx/include/__pstl/backends/serial.h b/libcxx/include/__pstl/backends/serial.h
index ae4a21315ea31..17a0878e5e86e 100644
--- a/libcxx/include/__pstl/backends/serial.h
+++ b/libcxx/include/__pstl/backends/serial.h
@@ -16,6 +16,7 @@
 #include <__algorithm/is_heap_until.h>
 #include <__algorithm/merge.h>
 #include <__algorithm/min_element.h>
+#include <__algorithm/minmax_element.h>
 #include <__algorithm/mismatch.h>
 #include <__algorithm/reverse.h>
 #include <__algorithm/search.h>
@@ -145,6 +146,15 @@ struct __min_element<__serial_backend_tag, _ExecutionPolicy> {
   }
 };
 
+template <class _ExecutionPolicy>
+struct __minmax_element<__serial_backend_tag, _ExecutionPolicy> {
+  template <class _Policy, class _ForwardIterator, class _Compare>
+  _LIBCPP_HIDE_FROM_ABI optional<pair<_ForwardIterator, _ForwardIterator>>
+  operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept {
+    return std::minmax_element(std::move(__first), std::move(__last), std::move(__comp));
+  }
+};
+
 template <class _ExecutionPolicy>
 struct __reverse<__serial_backend_tag, _ExecutionPolicy> {
   template <class _Policy, class _BidirectionalIterator>
diff --git a/libcxx/include/__pstl/backends/std_thread.h b/libcxx/include/__pstl/backends/std_thread.h
index 25742c01eb298..bc7fc020867f8 100644
--- a/libcxx/include/__pstl/backends/std_thread.h
+++ b/libcxx/include/__pstl/backends/std_thread.h
@@ -22,6 +22,7 @@
 #include <__pstl/cpu_algos/is_heap_until.h>
 #include <__pstl/cpu_algos/merge.h>
 #include <__pstl/cpu_algos/min_element.h>
+#include <__pstl/cpu_algos/minmax_element.h>
 #include <__pstl/cpu_algos/mismatch.h>
 #include <__pstl/cpu_algos/reverse.h>
 #include <__pstl/cpu_algos/search.h>
@@ -120,6 +121,10 @@ template <class _ExecutionPolicy>
 struct __min_element<__std_thread_backend_tag, _ExecutionPolicy>
     : __cpu_parallel_min_element<__std_thread_backend_tag, _ExecutionPolicy> {};
 
+template <class _ExecutionPolicy>
+struct __minmax_element<__std_thread_backend_tag, _ExecutionPolicy>
+    : __cpu_parallel_minmax_element<__std_thread_backend_tag, _ExecutionPolicy> {};
+
 template <class _ExecutionPolicy>
 struct __mismatch<__std_thread_backend_tag, _ExecutionPolicy>
     : __cpu_parallel_mismatch<__std_thread_backend_tag, _ExecutionPolicy> {};
diff --git a/libcxx/include/__pstl/cpu_algos/minmax_element.h b/libcxx/include/__pstl/cpu_algos/minmax_element.h
new file mode 100644
index 0000000000000..a55257196d7c2
--- /dev/null
+++ b/libcxx/include/__pstl/cpu_algos/minmax_element.h
@@ -0,0 +1,92 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
+#define _LIBCPP___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
+
+#include <__algorithm/minmax_element.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/iterator_traits.h>
+#include <__optional/optional.h>
+#include <__pstl/backend_fwd.h>
+#include <__pstl/cpu_algos/cpu_traits.h>
+#include <__type_traits/is_execution_policy.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 17
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __pstl {
+
+template <class _Backend, class _RawExecutionPolicy>
+struct __cpu_parallel_minmax_element {
+  template <class _Policy, class _ForwardIterator, class _Compare>
+  _LIBCPP_HIDE_FROM_ABI optional<pair<_ForwardIterator, _ForwardIterator>>
+  operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept {
+    if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
+                  __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
+      using _IterPair = pair<_ForwardIterator, _ForwardIterator>;
+
+      if (__first == __last) {
+        return _IterPair{__last, __last}; // Nothing to do
+      }
+
+      _IterPair __init = {__first, __first};
+      ++__first;
+      if (__first == __last) {
+        return __init; // The only element is both the minimum and the maximum
+      }
+
+      // A reduction that returns a pair of iterators pointing to the minimum and maximum elements.
+      // In a case of a tie the minimum iterators are biased left and the maximum iterators are biased right.
+      auto __iter_reduce = [&__comp](_IterPair __lhs, _IterPair __rhs) {
+        return _IterPair{__comp(*__rhs.first, *__lhs.first) ? __rhs.first : __lhs.first,
+                         __comp(*__rhs.second, *__lhs.second) ? __lhs.second : __rhs.second};
+      };
+
+      // Perform a parallel reduction of iterators [first+1, last) with {first, first} as init.
+      return __cpu_traits<_Backend>::__transform_reduce(
+          std::move(__first),
+          std::move(__last),
+          [](auto __it) { return _IterPair{__it, __it}; }, // Transform an iterator into an iterator pair
+          std::move(__init),                               // Use the pair of first iterators as the init element
+          __iter_reduce,                                   // Reduction of 2 minmax pairs
+          [&__iter_reduce, &__comp](auto __brick_first, auto __brick_last, auto __brick_init) {
+            // Reduction of an iterator range + init element: use the serial version to find the minmax among
+            // the iterators and then reduce it with the init element.
+            // Atm __transform_reduce can give empty bricks in edge cases, handle them explicitly until the contract is
+            // tightened.
+            return __brick_first == __brick_last
+                     ? __brick_init
+                     : __iter_reduce(__brick_init, std::minmax_element(__brick_first, __brick_last, __comp));
+          });
+    } else {
+      // Non-random access iterators cannot be processed in parallel, fall back to the sequential implementation.
+      return std::minmax_element(std::move(__first), std::move(__last), std::move(__comp));
+    }
+  }
+};
+
+} // namespace __pstl
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 17
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 45f98a7b6b9e5..d22a9ce36d02c 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -2449,6 +2449,9 @@ module std {
       module min_element {
         header "__pstl/cpu_algos/min_element.h"
       }
+      module minmax_element {
+        header "__pstl/cpu_algos/minmax_element.h"
+      }
       module mismatch {
         header "__pstl/cpu_algos/mismatch.h"
       }
diff --git a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
index d67770cedc944..c7f0b470794b0 100644
--- a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
@@ -191,6 +191,9 @@ void f(non_forward_iterator non_fwd,
     (void)std::max_element(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: max_element}}
     (void)std::min_element(pol, non_fwd, non_fwd);       // expected-error@*:* {{static assertion failed: min_element}}
     (void)std::min_element(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: min_element}}
+    (void)std::minmax_element(pol, non_fwd, non_fwd); // expected-error@*:* {{static assertion failed: minmax_element}}
+    (void)std::minmax_element(
+        pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: minmax_element}}
   }
 
   {
diff --git a/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp b/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
index 1dbb100792d4e..48eea75603476 100644
--- a/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
@@ -84,6 +84,10 @@ void test() {
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::min_element(std::execution::par, std::begin(a), std::end(a), pred2);
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::minmax_element(std::execution::par, std::begin(a), std::end(a));
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::minmax_element(std::execution::par, std::begin(a), std::end(a), pred2);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::mismatch(std::execution::par, std::begin(a), std::end(a), std::begin(b));
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::mismatch(std::execution::par, std::begin(a), std::end(a), std::begin(b), std::end(b));
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
new file mode 100644
index 0000000000000..d4092a90a2919
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
@@ -0,0 +1,209 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator>
+//   pair<ForwardIterator, ForwardIterator>
+//   minmax_element(ExecutionPolicy&& exec,
+//                  ForwardIterator first, ForwardIterator last);
+
+#include <cstddef>
+#include <algorithm>
+#include <cassert>
+#include <iterator>
+#include <numeric>
+#include <random>
+#include <vector>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(minmax_element);
+
+static_assert(sfinae_test_minmax_element<int, int*, int*>);
+static_assert(!sfinae_test_minmax_element<std::execution::parallel_policy, int*, int*>);
+
+template <class Iter>
+struct Test {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    using IterPair = std::pair<Iter, Iter>;
+    { // Check the return type
+      int a[]  = {0};
+      auto res = std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)));
+      static_assert(std::is_same_v<decltype(res), IterPair>);
+    }
+    { // Empty
+      int a[] = {0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::begin(a))) ==
+             IterPair(std::begin(a), std::begin(a)));
+    }
+    { // Single
+      int a[] = {0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a)));
+    }
+    { // Two elements, first min, second max
+      int a[] = {0, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 1));
+    }
+    { // Two elements, second min, first max
+      int a[] = {1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a)));
+    }
+    { // Three elements, first min, third max
+      int a[] = {0, 1, 2};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 2));
+    }
+    { // Three elements, middle min, third max
+      int a[] = {1, 0, 2};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a) + 2));
+    }
+    { // Three elements, middle min and equal to last element, first max
+      int a[] = {1, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a)));
+    }
+    { // Three elements, third min, second max
+      int a[] = {1, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 2, std::begin(a) + 1));
+    }
+    { // Three elements, last min, first max
+      int a[] = {2, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 2, std::begin(a)));
+    }
+    { // Four elements, first min, fourth max
+      int a[] = {0, 1, 2, 3};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Four elements, all equal
+      int a[] = {1, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Four elements, middle min, first max
+      int a[] = {3, 0, 2, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a)));
+    }
+    { // Four elements, middle min and equal to consequent elements, first max
+      int a[] = {3, 0, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a)));
+    }
+    { // Four elements, middle min and equal to consequent elements, second max
+      int a[] = {3, 3, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 2, std::begin(a) + 1));
+    }
+    { // Four elements, last min, first max
+      int a[] = {3, 2, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 3, std::begin(a)));
+    }
+    { // Five elements, all equal
+      int a[] = {1, 1, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, ascending
+      int a[] = {1, 2, 3, 4, 5};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, descending
+      int a[] = {5, 4, 3, 2, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 4, std::begin(a)));
+    }
+    { // Five elements, 2 flat regions, descending
+      int a[] = {1, 1, 1, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 3, std::begin(a) + 2));
+    }
+    { // Five elements, 2 flat regions, ascending
+      int a[] = {0, 0, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, alternating 1/0 pattern
+      int a[] = {1, 0, 1, 0, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 1, std::begin(a) + 4));
+    }
+    { // Check for minimum and maximum among iotaed and shuffled elements
+      std::mt19937 randomness;
+      auto verify_min = [&](Iter first, Iter last) {
+        IterPair i = std::minmax_element(policy, first, last);
+        if (first != last) {
+          for (Iter j = first; j != last; ++j) {
+            assert(!(*j < *i.first));
+            assert(!(*i.second < *j));
+          }
+        } else {
+          assert(i == IterPair(last, last));
+        }
+      };
+      auto test_n_elem = [&](int N) {
+        std::vector<int> a(N);
+        std::iota(a.begin(), a.end(), 0);
+        std::shuffle(a.begin(), a.end(), randomness);
+        verify_min(Iter(a.data()), Iter(a.data() + N));
+      };
+      test_n_elem(3);
+      test_n_elem(4);
+      test_n_elem(5);
+      test_n_elem(10);
+      test_n_elem(100);
+      test_n_elem(1000);
+    }
+    { // Check that the first lowest is returned as the minimum and the last greatest is returned as the maximum
+      int a[1073];
+      // Fill with 2 2 2 2 .... 0 1 0 1 ...
+      for (std::size_t i = 0; i < std::size(a); ++i)
+        a[i] = (i < 473) ? 2 : (i % 2 ? 0 : 1);
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+             IterPair(std::begin(a) + 473, std::begin(a) + 472));
+    }
+    { // Check that the minimum/maximum at sampled locations is found
+      int a[1073];
+      std::fill(std::begin(a), std::end(a), 1);
+      runway_sample(std::size(a) - 1, [&](size_t i) {
+        if (i == 0)
+          return;
+        a[i] = 0;
+        assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+               IterPair(std::begin(a) + i, std::begin(a) + std::size(a) - 1));
+        a[i] = 2;
+        assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+               IterPair(std::begin(a), std::begin(a) + i));
+        a[i] = 1;
+      });
+    }
+  }
+};
+
+int main(int, char**) {
+  types::for_each(types::forward_iterator_list<const int*>{}, TestIteratorWithPolicies<Test>{});
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp
new file mode 100644
index 0000000000000..f815027d4886d
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp
@@ -0,0 +1,211 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator, class Compare>
+//   pair<ForwardIterator, ForwardIterator>
+//   minmax_element(ExecutionPolicy&& exec,
+//                  ForwardIterator first, ForwardIterator last,
+//                  Compare comp);
+
+#include <cstddef>
+#include <algorithm>
+#include <cassert>
+#include <iterator>
+#include <numeric>
+#include <random>
+#include <vector>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(minmax_element);
+
+static_assert(sfinae_test_minmax_element<int, int*, int*, bool (*)(int, int)>);
+static_assert(!sfinae_test_minmax_element<std::execution::parallel_policy, int*, int*, bool (*)(int, int)>);
+
+template <class Iter>
+struct Test {
+  template <class ExecutionPolicy>
+  void operator()(ExecutionPolicy&& policy) {
+    using IterPair = std::pair<Iter, Iter>;
+    std::greater<int> comp;
+    { // Check the return type
+      int a[]  = {0};
+      auto res = std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp);
+      static_assert(std::is_same_v<decltype(res), IterPair>);
+    }
+    { // Empty
+      int a[] = {0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::begin(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a)));
+    }
+    { // Single
+      int a[] = {0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a)));
+    }
+    { // Two elements, second min, first max
+      int a[] = {0, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 1, std::begin(a)));
+    }
+    { // Two elements, first min, second max
+      int a[] = {1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 1));
+    }
+    { // Three elements, third min, first max
+      int a[] = {0, 1, 2};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 2, std::begin(a)));
+    }
+    { // Three elements, third min, middle max
+      int a[] = {1, 0, 2};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 2, std::begin(a) + 1));
+    }
+    { // Three elements, first min, third max
+      int a[] = {1, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 2));
+    }
+    { // Three elements, first min, third max
+      int a[] = {1, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 2));
+    }
+    { // Three elements, first min, third max
+      int a[] = {2, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 2));
+    }
+    { // Four elements, fourth min, first max
+      int a[] = {0, 1, 2, 3};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 3, std::begin(a)));
+    }
+    { // Four elements, all equal
+      int a[] = {1, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Four elements, first min, middle max
+      int a[] = {3, 0, 2, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 1));
+    }
+    { // Four elements, first min, fourth max
+      int a[] = {3, 0, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Four elements, first min, fourth max
+      int a[] = {3, 3, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Four elements, first min, fourth max
+      int a[] = {3, 2, 1, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Five elements, all equal
+      int a[] = {1, 1, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, ascending
+      int a[] = {1, 2, 3, 4, 5};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 4, std::begin(a)));
+    }
+    { // Five elements, descending
+      int a[] = {5, 4, 3, 2, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, 2 flat regions, descending
+      int a[] = {1, 1, 1, 0, 0};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 4));
+    }
+    { // Five elements, 2 flat regions, ascending
+      int a[] = {0, 0, 1, 1, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a) + 2, std::begin(a) + 1));
+    }
+    { // Five elements, alternating 1/0 pattern
+      int a[] = {1, 0, 1, 0, 1};
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 3));
+    }
+    { // Check for minimum and maximum among iotaed and shuffled elements
+      std::mt19937 randomness;
+      auto verify_min = [&](Iter first, Iter last) {
+        IterPair i = std::minmax_element(policy, first, last, comp);
+        if (first != last) {
+          for (Iter j = first; j != last; ++j) {
+            assert(!(*i.first < *j));
+            assert(!(*j < *i.second));
+          }
+        } else {
+          assert(i == IterPair(last, last));
+        }
+      };
+      auto test_n_elem = [&](int N) {
+        std::vector<int> a(N);
+        std::iota(a.begin(), a.end(), 0);
+        std::shuffle(a.begin(), a.end(), randomness);
+        verify_min(Iter(a.data()), Iter(a.data() + N));
+      };
+      test_n_elem(3);
+      test_n_elem(4);
+      test_n_elem(5);
+      test_n_elem(10);
+      test_n_elem(100);
+      test_n_elem(1000);
+    }
+    { // Check that the first greatest is returned as the minimum and the last smallest is returned as the maximum
+      int a[1073];
+      // Fill with 2 2 2 2 .... 0 1 0 1 ...
+      for (std::size_t i = 0; i < std::size(a); ++i)
+        a[i] = (i < 473) ? 2 : (i % 2 ? 0 : 1);
+      assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+             IterPair(std::begin(a), std::begin(a) + 1071));
+    }
+    { // Check that the minimum/maximum at sampled locations is found
+      int a[1073];
+      std::fill(std::begin(a), std::end(a), 1);
+      runway_sample(std::size(a) - 1, [&](size_t i) {
+        if (i == 0)
+          return;
+        a[i] = 0;
+        assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+               IterPair(std::begin(a), std::begin(a) + i));
+        a[i] = 2;
+        assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+               IterPair(std::begin(a) + i, std::begin(a) + std::size(a) - 1));
+        a[i] = 1;
+      });
+    }
+  }
+};
+
+int main(int, char**) {
+  types::for_each(types::forward_iterator_list<const int*>{}, TestIteratorWithPolicies<Test>{});
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
index 6c652c8918f63..9c0ff9252745d 100644
--- a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
+++ b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
@@ -300,6 +300,14 @@ int main(int, char**) {
         assert_non_throwing([=, &policy] {
           (void)std::min_element(policy, std::move(first1), std::move(last1), compare);
         });
+
+        // minmax_element(first, last)
+        assert_non_throwing([=, &policy] { (void)std::minmax_element(policy, std::move(first1), std::move(last1)); });
+
+        // minmax_element(first, last, comp)
+        assert_non_throwing([=, &policy] {
+          (void)std::minmax_element(policy, std::move(first1), std::move(last1), compare);
+        });
       }
 
       {

>From b371f0ea5d4818ca140acaac811d02c2463f8530 Mon Sep 17 00:00:00 2001
From: Michael Kazakov <mike.kazakov at gmail.com>
Date: Sun, 6 Sep 2026 19:04:28 +0100
Subject: [PATCH 2/2] missing include

---
 libcxx/include/__pstl/backends/serial.h          | 1 +
 libcxx/include/__pstl/cpu_algos/minmax_element.h | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libcxx/include/__pstl/backends/serial.h b/libcxx/include/__pstl/backends/serial.h
index 17a0878e5e86e..85c73b0f15db5 100644
--- a/libcxx/include/__pstl/backends/serial.h
+++ b/libcxx/include/__pstl/backends/serial.h
@@ -30,6 +30,7 @@
 #include <__utility/empty.h>
 #include <__utility/forward.h>
 #include <__utility/move.h>
+#include <__utility/pair.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
diff --git a/libcxx/include/__pstl/cpu_algos/minmax_element.h b/libcxx/include/__pstl/cpu_algos/minmax_element.h
index a55257196d7c2..de737ed9f3317 100644
--- a/libcxx/include/__pstl/cpu_algos/minmax_element.h
+++ b/libcxx/include/__pstl/cpu_algos/minmax_element.h
@@ -20,6 +20,7 @@
 #include <__pstl/cpu_algos/cpu_traits.h>
 #include <__type_traits/is_execution_policy.h>
 #include <__utility/move.h>
+#include <__utility/pair.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header



More information about the libcxx-commits mailing list