blob: 88a994c5551276a57ab33d1781b88db6ae6773b0 [file] [log] [blame]
Samuel Benzaquen91c92162018-10-12 21:01:15 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10
11#include <string>
12#include <tuple>
13#include <type_traits>
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +000014#include <vector>
Samuel Benzaquen91c92162018-10-12 21:01:15 +000015
16#include "benchmark/benchmark.h"
17#include "test_macros.h"
18
19namespace internal {
20
21template <class D, class E, size_t I>
22struct EnumValue : std::integral_constant<E, static_cast<E>(I)> {
23 static std::string name() { return std::string("_") + D::Names[I]; }
24};
25
26template <class D, class E, size_t ...Idxs>
27constexpr auto makeEnumValueTuple(std::index_sequence<Idxs...>) {
28 return std::make_tuple(EnumValue<D, E, Idxs>{}...);
29}
30
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +000031template <class B>
32static auto skip(const B& Bench, int) -> decltype(Bench.skip()) {
33 return Bench.skip();
Samuel Benzaquen91c92162018-10-12 21:01:15 +000034}
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +000035template <class B>
36static auto skip(const B& Bench, char) {
Samuel Benzaquen91c92162018-10-12 21:01:15 +000037 return false;
38}
39
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +000040template <class B, class Args, size_t... Is>
41void makeBenchmarkFromValuesImpl(const Args& A, std::index_sequence<Is...>) {
42 for (auto& V : A) {
43 B Bench{std::get<Is>(V)...};
44 if (!internal::skip(Bench, 0)) {
45 benchmark::RegisterBenchmark(Bench.name().c_str(),
46 [=](benchmark::State& S) { Bench.run(S); });
47 }
48 }
Samuel Benzaquen91c92162018-10-12 21:01:15 +000049}
50
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +000051template <class B, class... Args>
52void makeBenchmarkFromValues(const std::vector<std::tuple<Args...> >& A) {
53 makeBenchmarkFromValuesImpl<B>(A, std::index_sequence_for<Args...>());
54}
55
56template <template <class...> class B, class Args, class... U>
57void makeBenchmarkImpl(const Args& A, std::tuple<U...> t) {
58 makeBenchmarkFromValues<B<U...> >(A);
59}
60
61template <template <class...> class B, class Args, class... U,
62 class... T, class... Tuples>
63void makeBenchmarkImpl(const Args& A, std::tuple<U...>, std::tuple<T...>,
64 Tuples... rest) {
65 (internal::makeBenchmarkImpl<B>(A, std::tuple<U..., T>(), rest...), ...);
66}
67
68template <class R, class T>
69void allValueCombinations(R& Result, const T& Final) {
70 return Result.push_back(Final);
71}
72
73template <class R, class T, class V, class... Vs>
74void allValueCombinations(R& Result, const T& Prev, const V& Value,
75 const Vs&... Values) {
76 for (const auto& E : Value) {
77 allValueCombinations(Result, std::tuple_cat(Prev, std::make_tuple(E)),
78 Values...);
79 }
Samuel Benzaquen91c92162018-10-12 21:01:15 +000080}
81
82} // namespace internal
83
84// CRTP class that enables using enum types as a dimension for
85// makeCartesianProductBenchmark below.
86// The type passed to `B` will be a std::integral_constant<E, e>, with the
87// additional static function `name()` that returns the stringified name of the
88// label.
89//
90// Eg:
91// enum class MyEnum { A, B };
92// struct AllMyEnum : EnumValuesAsTuple<AllMyEnum, MyEnum, 2> {
93// static constexpr absl::string_view Names[] = {"A", "B"};
94// };
95template <class Derived, class EnumType, size_t NumLabels>
96using EnumValuesAsTuple =
97 decltype(internal::makeEnumValueTuple<Derived, EnumType>(
98 std::make_index_sequence<NumLabels>{}));
99
100// Instantiates B<T0, T1, ..., TN> where <Ti...> are the combinations in the
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +0000101// cartesian product of `Tuples...`, and pass (arg0, ..., argN) as constructor
102// arguments where `(argi...)` are the combination in the cartesian product of
103// the runtime values of `A...`.
Samuel Benzaquen91c92162018-10-12 21:01:15 +0000104// B<T...> requires:
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +0000105// - std::string name(args...): The name of the benchmark.
106// - void run(benchmark::State&, args...): The body of the benchmark.
Samuel Benzaquen91c92162018-10-12 21:01:15 +0000107// It can also optionally provide:
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +0000108// - bool skip(args...): When `true`, skips the combination. Default is false.
Samuel Benzaquen91c92162018-10-12 21:01:15 +0000109//
110// Returns int to facilitate registration. The return value is unspecified.
Samuel Benzaquenca8ba3d2018-10-23 14:49:27 +0000111template <template <class...> class B, class... Tuples, class... Args>
112int makeCartesianProductBenchmark(const Args&... A) {
113 std::vector<std::tuple<typename Args::value_type...> > V;
114 internal::allValueCombinations(V, std::tuple<>(), A...);
115 internal::makeBenchmarkImpl<B>(V, std::tuple<>(), Tuples()...);
116 return 0;
117}
118
119template <class B, class... Args>
120int makeCartesianProductBenchmark(const Args&... A) {
121 std::vector<std::tuple<typename Args::value_type...> > V;
122 internal::allValueCombinations(V, std::tuple<>(), A...);
123 internal::makeBenchmarkFromValues<B>(V);
Samuel Benzaquen91c92162018-10-12 21:01:15 +0000124 return 0;
125}
126
127// When `opaque` is true, this function hides the runtime state of `value` from
128// the optimizer.
129// It returns `value`.
130template <class T>
131TEST_ALWAYS_INLINE inline T maybeOpaque(T value, bool opaque) {
132 if (opaque) benchmark::DoNotOptimize(value);
133 return value;
134}
135