CartesianBenchmarks.h 4.5 KB

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