blob: 2eea156819330575d0f3311e067e5881966f25c5 [file] [log] [blame]
Samuel Benzaquen0e6a8332018-10-12 21:01:151//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:403// 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
Samuel Benzaquen0e6a8332018-10-12 21:01:156//
7//===----------------------------------------------------------------------===//
8
9
10#include <string>
11#include <tuple>
12#include <type_traits>
Samuel Benzaquen37632992018-10-23 14:49:2713#include <vector>
Samuel Benzaquen0e6a8332018-10-12 21:01:1514
15#include "benchmark/benchmark.h"
16#include "test_macros.h"
17
18namespace internal {
19
20template <class D, class E, size_t I>
21struct EnumValue : std::integral_constant<E, static_cast<E>(I)> {
22 static std::string name() { return std::string("_") + D::Names[I]; }
23};
24
25template <class D, class E, size_t ...Idxs>
26constexpr auto makeEnumValueTuple(std::index_sequence<Idxs...>) {
27 return std::make_tuple(EnumValue<D, E, Idxs>{}...);
28}
29
Samuel Benzaquen37632992018-10-23 14:49:2730template <class B>
31static auto skip(const B& Bench, int) -> decltype(Bench.skip()) {
32 return Bench.skip();
Samuel Benzaquen0e6a8332018-10-12 21:01:1533}
Samuel Benzaquen37632992018-10-23 14:49:2734template <class B>
35static auto skip(const B& Bench, char) {
Samuel Benzaquen0e6a8332018-10-12 21:01:1536 return false;
37}
38
Samuel Benzaquen37632992018-10-23 14:49:2739template <class B, class Args, size_t... Is>
40void makeBenchmarkFromValuesImpl(const Args& A, std::index_sequence<Is...>) {
41 for (auto& V : A) {
42 B Bench{std::get<Is>(V)...};
43 if (!internal::skip(Bench, 0)) {
44 benchmark::RegisterBenchmark(Bench.name().c_str(),
45 [=](benchmark::State& S) { Bench.run(S); });
46 }
47 }
Samuel Benzaquen0e6a8332018-10-12 21:01:1548}
49
Samuel Benzaquen37632992018-10-23 14:49:2750template <class B, class... Args>
51void makeBenchmarkFromValues(const std::vector<std::tuple<Args...> >& A) {
52 makeBenchmarkFromValuesImpl<B>(A, std::index_sequence_for<Args...>());
53}
54
55template <template <class...> class B, class Args, class... U>
56void makeBenchmarkImpl(const Args& A, std::tuple<U...> t) {
57 makeBenchmarkFromValues<B<U...> >(A);
58}
59
60template <template <class...> class B, class Args, class... U,
61 class... T, class... Tuples>
62void makeBenchmarkImpl(const Args& A, std::tuple<U...>, std::tuple<T...>,
63 Tuples... rest) {
64 (internal::makeBenchmarkImpl<B>(A, std::tuple<U..., T>(), rest...), ...);
65}
66
67template <class R, class T>
68void allValueCombinations(R& Result, const T& Final) {
69 return Result.push_back(Final);
70}
71
72template <class R, class T, class V, class... Vs>
73void allValueCombinations(R& Result, const T& Prev, const V& Value,
74 const Vs&... Values) {
75 for (const auto& E : Value) {
76 allValueCombinations(Result, std::tuple_cat(Prev, std::make_tuple(E)),
77 Values...);
78 }
Samuel Benzaquen0e6a8332018-10-12 21:01:1579}
80
81} // namespace internal
82
83// CRTP class that enables using enum types as a dimension for
84// makeCartesianProductBenchmark below.
85// The type passed to `B` will be a std::integral_constant<E, e>, with the
86// additional static function `name()` that returns the stringified name of the
87// label.
88//
89// Eg:
90// enum class MyEnum { A, B };
91// struct AllMyEnum : EnumValuesAsTuple<AllMyEnum, MyEnum, 2> {
92// static constexpr absl::string_view Names[] = {"A", "B"};
93// };
94template <class Derived, class EnumType, size_t NumLabels>
95using EnumValuesAsTuple =
96 decltype(internal::makeEnumValueTuple<Derived, EnumType>(
97 std::make_index_sequence<NumLabels>{}));
98
99// Instantiates B<T0, T1, ..., TN> where <Ti...> are the combinations in the
Samuel Benzaquen37632992018-10-23 14:49:27100// cartesian product of `Tuples...`, and pass (arg0, ..., argN) as constructor
101// arguments where `(argi...)` are the combination in the cartesian product of
102// the runtime values of `A...`.
Samuel Benzaquen0e6a8332018-10-12 21:01:15103// B<T...> requires:
Samuel Benzaquen37632992018-10-23 14:49:27104// - std::string name(args...): The name of the benchmark.
105// - void run(benchmark::State&, args...): The body of the benchmark.
Samuel Benzaquen0e6a8332018-10-12 21:01:15106// It can also optionally provide:
Samuel Benzaquen37632992018-10-23 14:49:27107// - bool skip(args...): When `true`, skips the combination. Default is false.
Samuel Benzaquen0e6a8332018-10-12 21:01:15108//
109// Returns int to facilitate registration. The return value is unspecified.
Samuel Benzaquen37632992018-10-23 14:49:27110template <template <class...> class B, class... Tuples, class... Args>
111int makeCartesianProductBenchmark(const Args&... A) {
112 std::vector<std::tuple<typename Args::value_type...> > V;
113 internal::allValueCombinations(V, std::tuple<>(), A...);
114 internal::makeBenchmarkImpl<B>(V, std::tuple<>(), Tuples()...);
115 return 0;
116}
117
118template <class B, class... Args>
119int makeCartesianProductBenchmark(const Args&... A) {
120 std::vector<std::tuple<typename Args::value_type...> > V;
121 internal::allValueCombinations(V, std::tuple<>(), A...);
122 internal::makeBenchmarkFromValues<B>(V);
Samuel Benzaquen0e6a8332018-10-12 21:01:15123 return 0;
124}
125
126// When `opaque` is true, this function hides the runtime state of `value` from
127// the optimizer.
128// It returns `value`.
129template <class T>
130TEST_ALWAYS_INLINE inline T maybeOpaque(T value, bool opaque) {
131 if (opaque) benchmark::DoNotOptimize(value);
132 return value;
133}