1 //===-- LatencyBenchmarkRunner.cpp ------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "LatencyBenchmarkRunner.h"
11 #include "BenchmarkRunner.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/Error.h"
21 LatencyBenchmarkRunner::LatencyBenchmarkRunner(
22 const LLVMState
&State
, Benchmark::ModeE Mode
,
23 BenchmarkPhaseSelectorE BenchmarkPhaseSelector
,
24 Benchmark::ResultAggregationModeE ResultAgg
, ExecutionModeE ExecutionMode
)
25 : BenchmarkRunner(State
, Mode
, BenchmarkPhaseSelector
, ExecutionMode
) {
26 assert((Mode
== Benchmark::Latency
|| Mode
== Benchmark::InverseThroughput
) &&
28 ResultAggMode
= ResultAgg
;
31 LatencyBenchmarkRunner::~LatencyBenchmarkRunner() = default;
33 static double computeVariance(const llvm::SmallVector
<int64_t, 4> &Values
) {
36 double Sum
= std::accumulate(Values
.begin(), Values
.end(), 0.0);
38 const double Mean
= Sum
/ Values
.size();
40 for (const auto &V
: Values
) {
41 double Delta
= V
- Mean
;
44 return Ret
/ Values
.size();
47 static int64_t findMin(const llvm::SmallVector
<int64_t, 4> &Values
) {
50 return *std::min_element(Values
.begin(), Values
.end());
53 static int64_t findMax(const llvm::SmallVector
<int64_t, 4> &Values
) {
56 return *std::max_element(Values
.begin(), Values
.end());
59 static int64_t findMean(const llvm::SmallVector
<int64_t, 4> &Values
) {
62 return std::accumulate(Values
.begin(), Values
.end(), 0.0) /
63 static_cast<double>(Values
.size());
66 Expected
<std::vector
<BenchmarkMeasure
>> LatencyBenchmarkRunner::runMeasurements(
67 const FunctionExecutor
&Executor
) const {
68 // Cycle measurements include some overhead from the kernel. Repeat the
69 // measure several times and return the aggregated value, as specified by
71 constexpr const int NumMeasurements
= 30;
72 llvm::SmallVector
<int64_t, 4> AccumulatedValues
;
73 double MinVariance
= std::numeric_limits
<double>::infinity();
74 const char *CounterName
= State
.getPfmCounters().CycleCounter
;
75 // Values count for each run.
77 for (size_t I
= 0; I
< NumMeasurements
; ++I
) {
78 auto ExpectedCounterValues
= Executor
.runAndSample(CounterName
);
79 if (!ExpectedCounterValues
)
80 return ExpectedCounterValues
.takeError();
81 ValuesCount
= ExpectedCounterValues
.get().size();
83 AccumulatedValues
.push_back(ExpectedCounterValues
.get()[0]);
85 // We'll keep the reading with lowest variance (ie., most stable)
86 double Variance
= computeVariance(*ExpectedCounterValues
);
87 if (MinVariance
> Variance
) {
88 AccumulatedValues
= std::move(ExpectedCounterValues
.get());
89 MinVariance
= Variance
;
96 case Benchmark::Latency
:
99 case Benchmark::InverseThroughput
:
100 ModeName
= "inverse_throughput";
106 switch (ResultAggMode
) {
107 case Benchmark::MinVariance
: {
108 if (ValuesCount
== 1)
109 llvm::errs() << "Each sample only has one value. result-aggregation-mode "
110 "of min-variance is probably non-sensical\n";
111 std::vector
<BenchmarkMeasure
> Result
;
112 Result
.reserve(AccumulatedValues
.size());
113 for (const int64_t Value
: AccumulatedValues
)
114 Result
.push_back(BenchmarkMeasure::Create(ModeName
, Value
));
115 return std::move(Result
);
117 case Benchmark::Min
: {
118 std::vector
<BenchmarkMeasure
> Result
;
120 BenchmarkMeasure::Create(ModeName
, findMin(AccumulatedValues
)));
121 return std::move(Result
);
123 case Benchmark::Max
: {
124 std::vector
<BenchmarkMeasure
> Result
;
126 BenchmarkMeasure::Create(ModeName
, findMax(AccumulatedValues
)));
127 return std::move(Result
);
129 case Benchmark::Mean
: {
130 std::vector
<BenchmarkMeasure
> Result
;
132 BenchmarkMeasure::Create(ModeName
, findMean(AccumulatedValues
)));
133 return std::move(Result
);
136 return llvm::make_error
<Failure
>(llvm::Twine("Unexpected benchmark mode(")
137 .concat(std::to_string(Mode
))
138 .concat(" and unexpected ResultAggMode ")
139 .concat(std::to_string(ResultAggMode
)));
142 } // namespace exegesis