• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
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>
14 #include <vector>
15 
16 #include "benchmark/benchmark.h"
17 #include "test_macros.h"
18 
19 namespace internal {
20 
21 template <class D, class E, size_t I>
22 struct EnumValue : std::integral_constant<E, static_cast<E>(I)> {
nameinternal::EnumValue23   static std::string name() { return std::string("_") + D::Names[I]; }
24 };
25 
26 template <class D, class E, size_t ...Idxs>
makeEnumValueTuple(std::index_sequence<Idxs...>)27 constexpr auto makeEnumValueTuple(std::index_sequence<Idxs...>) {
28   return std::make_tuple(EnumValue<D, E, Idxs>{}...);
29 }
30 
31 template <class B>
skip(const B & Bench,int)32 static auto skip(const B& Bench, int) -> decltype(Bench.skip()) {
33   return Bench.skip();
34 }
35 template <class B>
skip(const B & Bench,char)36 static auto skip(const B& Bench, char) {
37   return false;
38 }
39 
40 template <class B, class Args, size_t... Is>
makeBenchmarkFromValuesImpl(const Args & A,std::index_sequence<Is...>)41 void 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   }
49 }
50 
51 template <class B, class... Args>
makeBenchmarkFromValues(const std::vector<std::tuple<Args...>> & A)52 void makeBenchmarkFromValues(const std::vector<std::tuple<Args...> >& A) {
53   makeBenchmarkFromValuesImpl<B>(A, std::index_sequence_for<Args...>());
54 }
55 
56 template <template <class...> class B, class Args, class... U>
makeBenchmarkImpl(const Args & A,std::tuple<U...> t)57 void makeBenchmarkImpl(const Args& A, std::tuple<U...> t) {
58   makeBenchmarkFromValues<B<U...> >(A);
59 }
60 
61 template <template <class...> class B, class Args, class... U,
62           class... T, class... Tuples>
makeBenchmarkImpl(const Args & A,std::tuple<U...>,std::tuple<T...>,Tuples...rest)63 void 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 
68 template <class R, class T>
allValueCombinations(R & Result,const T & Final)69 void allValueCombinations(R& Result, const T& Final) {
70   return Result.push_back(Final);
71 }
72 
73 template <class R, class T, class V, class... Vs>
allValueCombinations(R & Result,const T & Prev,const V & Value,const Vs &...Values)74 void 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   }
80 }
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 // };
95 template <class Derived, class EnumType, size_t NumLabels>
96 using 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
101 // 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...`.
104 // B<T...> requires:
105 //  - std::string name(args...): The name of the benchmark.
106 //  - void run(benchmark::State&, args...): The body of the benchmark.
107 // It can also optionally provide:
108 //  - bool skip(args...): When `true`, skips the combination. Default is false.
109 //
110 // Returns int to facilitate registration. The return value is unspecified.
111 template <template <class...> class B, class... Tuples, class... Args>
makeCartesianProductBenchmark(const Args &...A)112 int 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 
119 template <class B, class... Args>
makeCartesianProductBenchmark(const Args &...A)120 int 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);
124   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`.
130 template <class T>
maybeOpaque(T value,bool opaque)131 TEST_ALWAYS_INLINE inline T maybeOpaque(T value, bool opaque) {
132   if (opaque) benchmark::DoNotOptimize(value);
133   return value;
134 }
135 
136