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