blob: 7cd13a052cf8f99d19f6cc342d18af8db26abe00 [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>
14
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
30template <class T>
31static auto skip(int) -> decltype(T::skip()) {
32 return T::skip();
33}
34template <class T>
35static bool skip(char) {
36 return false;
37}
38
39template <template <class...> class B, class... U>
40void makeBenchmarkImpl(std::tuple<U...> t) {
41 using T = B<U...>;
42 if (!internal::skip<T>(0))
43 benchmark::RegisterBenchmark(T::name().c_str(), T::run);
44}
45
46template <template <class...> class B, class... U, class... T, class... Tuples>
47void makeBenchmarkImpl(std::tuple<U...>, std::tuple<T...>, Tuples... rest) {
48 (internal::makeBenchmarkImpl<B>(std::tuple<U..., T>(), rest...), ...);
49}
50
51} // namespace internal
52
53// CRTP class that enables using enum types as a dimension for
54// makeCartesianProductBenchmark below.
55// The type passed to `B` will be a std::integral_constant<E, e>, with the
56// additional static function `name()` that returns the stringified name of the
57// label.
58//
59// Eg:
60// enum class MyEnum { A, B };
61// struct AllMyEnum : EnumValuesAsTuple<AllMyEnum, MyEnum, 2> {
62// static constexpr absl::string_view Names[] = {"A", "B"};
63// };
64template <class Derived, class EnumType, size_t NumLabels>
65using EnumValuesAsTuple =
66 decltype(internal::makeEnumValueTuple<Derived, EnumType>(
67 std::make_index_sequence<NumLabels>{}));
68
69// Instantiates B<T0, T1, ..., TN> where <Ti...> are the combinations in the
70// cartesian product of `Tuples...`
71// B<T...> requires:
72// - static std::string name(): The name of the benchmark.
73// - static void run(benchmark::State&): The body of the benchmark.
74// It can also optionally provide:
75// - static bool skip(): When `true`, skips the combination. Default is false.
76//
77// Returns int to facilitate registration. The return value is unspecified.
78template <template <class...> class B, class... Tuples>
79int makeCartesianProductBenchmark() {
80 internal::makeBenchmarkImpl<B>(std::tuple<>(), Tuples()...);
81 return 0;
82}
83
84// When `opaque` is true, this function hides the runtime state of `value` from
85// the optimizer.
86// It returns `value`.
87template <class T>
88TEST_ALWAYS_INLINE inline T maybeOpaque(T value, bool opaque) {
89 if (opaque) benchmark::DoNotOptimize(value);
90 return value;
91}
92