Samuel Benzaquen | 91c9216 | 2018-10-12 21:01:15 +0000 | [diff] [blame^] | 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 | |
| 15 | #include "benchmark/benchmark.h" |
| 16 | #include "test_macros.h" |
| 17 | |
| 18 | namespace internal { |
| 19 | |
| 20 | template <class D, class E, size_t I> |
| 21 | struct EnumValue : std::integral_constant<E, static_cast<E>(I)> { |
| 22 | static std::string name() { return std::string("_") + D::Names[I]; } |
| 23 | }; |
| 24 | |
| 25 | template <class D, class E, size_t ...Idxs> |
| 26 | constexpr auto makeEnumValueTuple(std::index_sequence<Idxs...>) { |
| 27 | return std::make_tuple(EnumValue<D, E, Idxs>{}...); |
| 28 | } |
| 29 | |
| 30 | template <class T> |
| 31 | static auto skip(int) -> decltype(T::skip()) { |
| 32 | return T::skip(); |
| 33 | } |
| 34 | template <class T> |
| 35 | static bool skip(char) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | template <template <class...> class B, class... U> |
| 40 | void 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 | |
| 46 | template <template <class...> class B, class... U, class... T, class... Tuples> |
| 47 | void 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 | // }; |
| 64 | template <class Derived, class EnumType, size_t NumLabels> |
| 65 | using 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. |
| 78 | template <template <class...> class B, class... Tuples> |
| 79 | int 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`. |
| 87 | template <class T> |
| 88 | TEST_ALWAYS_INLINE inline T maybeOpaque(T value, bool opaque) { |
| 89 | if (opaque) benchmark::DoNotOptimize(value); |
| 90 | return value; |
| 91 | } |
| 92 | |