[libcxx-commits] [libcxx] 8445ad1 - [libc++] Added a benchmark for std::reduce with a trivial reduction operator (std::plus) (#236853)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Oct 30 12:52:40 PDT 2026
Author: Michael G. Kazakov
Date: 2026-10-30T12:52:35-07:00
New Revision: 8445ad1d24a66725308b1d4b7e9e625d9b2e3c79
URL: https://github.com/llvm/llvm-project/commit/8445ad1d24a66725308b1d4b7e9e625d9b2e3c79
DIFF: https://github.com/llvm/llvm-project/commit/8445ad1d24a66725308b1d4b7e9e625d9b2e3c79.diff
LOG: [libc++] Added a benchmark for std::reduce with a trivial reduction operator (std::plus) (#236853)
This PR adds benchmarks for serial reductions with a trivial operator,
to specifically capture the cases where the compiler can do a good job
optimising the loop. This includes cases where the compiler can already
perform reordering without an explicit permission (e.g. with `int`s) and
where it requires an opt-int like `clang loop vectorize(enable)
interleave(enable)` (e.g. with `float`s, which libc++ currently doesn't
do).
Part of #234887.
Added:
Modified:
libcxx/test/benchmarks/numeric/reduce.bench.cpp
Removed:
################################################################################
diff --git a/libcxx/test/benchmarks/numeric/reduce.bench.cpp b/libcxx/test/benchmarks/numeric/reduce.bench.cpp
index ece7578d18ff32..8360d13a8a6877 100644
--- a/libcxx/test/benchmarks/numeric/reduce.bench.cpp
+++ b/libcxx/test/benchmarks/numeric/reduce.bench.cpp
@@ -9,6 +9,7 @@
// REQUIRES: std-at-least-c++20
#include <algorithm>
+#include <complex>
#include <cstddef>
#include <iterator>
#include <list>
@@ -26,8 +27,10 @@ template <class T>
T random_value() {
if constexpr (std::is_integral_v<T>)
return getRandomInteger<T>(1, 1000);
- else
+ else if constexpr (std::is_floating_point_v<T>)
return getRandomFloat<T>(1, 1000);
+ else // std::complex
+ return T(getRandomFloat<typename T::value_type>(1, 1000), getRandomFloat<typename T::value_type>(1, 1000));
}
int main(int argc, char** argv) {
@@ -73,6 +76,37 @@ int main(int argc, char** argv) {
bm.operator()<std::list<double>>("std::reduce(list<double>)");
}
+ // std::reduce(first, last, init), trivial reduction operator (std::plus<>).
+ {
+ auto bm = []<class Container>(std::string name) {
+ benchmark::RegisterBenchmark(
+ name,
+ [](auto& st) TEST_ALIGN_BENCHMARK {
+ std::size_t const size = st.range(0);
+ using ValueType = typename Container::value_type;
+
+ Container c;
+ std::generate_n(std::back_inserter(c), size, [] { return random_value<ValueType>(); });
+ ValueType init = static_cast<ValueType>(1);
+
+ for (auto _ : st) {
+ benchmark::ClobberMemory();
+ auto result = std::reduce(c.begin(), c.end(), init);
+ benchmark::DoNotOptimize(result);
+ }
+ })
+ ->Arg(32)
+ ->Arg(50) // non power-of-two
+ ->Arg(1024)
+ ->Arg(8192);
+ };
+ bm.operator()<std::vector<int>>("std::reduce(vector<int>, plus)");
+ bm.operator()<std::vector<float>>("std::reduce(vector<float>, plus)");
+ bm.operator()<std::vector<std::complex<float>>>("std::reduce(vector<complex<float>>, plus)");
+ bm.operator()<std::vector<double>>("std::reduce(vector<double>, plus)");
+ bm.operator()<std::vector<std::complex<double>>>("std::reduce(vector<complex<double>>, plus)");
+ }
+
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
More information about the libcxx-commits
mailing list