Skip to content

Commit d228059

Browse files
[llvm-exegesis] Refactor common parts out of FunctionExecutorImpl
This patch refactors some code out of FunctionExecutorImpl into the base class that should be common across all implementations of FunctionExecutor. Particularly, this patch factors out accumulateCounterValues, and also factors out runAndSample, moving implementation specific code into a new runWithCounter function. This makes adding new implementations of FunctinExecutor easier. Reviewed By: gchatelet Differential Revision: https://reviews.llvm.org/D148079
1 parent 28575f4 commit d228059

File tree

2 files changed

+68
-57
lines changed

2 files changed

+68
-57
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ BenchmarkRunner::BenchmarkRunner(const LLVMState &State,
3737

3838
BenchmarkRunner::~BenchmarkRunner() = default;
3939

40+
void BenchmarkRunner::FunctionExecutor::accumulateCounterValues(
41+
const llvm::SmallVectorImpl<int64_t> &NewValues,
42+
llvm::SmallVectorImpl<int64_t> *Result) {
43+
const size_t NumValues = std::max(NewValues.size(), Result->size());
44+
if (NumValues > Result->size())
45+
Result->resize(NumValues, 0);
46+
for (size_t I = 0, End = NewValues.size(); I < End; ++I)
47+
(*Result)[I] += NewValues[I];
48+
}
49+
50+
Expected<llvm::SmallVector<int64_t, 4>>
51+
BenchmarkRunner::FunctionExecutor::runAndSample(const char *Counters) const {
52+
// We sum counts when there are several counters for a single ProcRes
53+
// (e.g. P23 on SandyBridge).
54+
llvm::SmallVector<int64_t, 4> CounterValues;
55+
SmallVector<StringRef, 2> CounterNames;
56+
StringRef(Counters).split(CounterNames, '+');
57+
for (auto &CounterName : CounterNames) {
58+
CounterName = CounterName.trim();
59+
Expected<SmallVector<int64_t, 4>> ValueOrError =
60+
runWithCounter(CounterName);
61+
if (!ValueOrError)
62+
return ValueOrError.takeError();
63+
accumulateCounterValues(ValueOrError.get(), &CounterValues);
64+
}
65+
return CounterValues;
66+
};
67+
4068
namespace {
4169
class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
4270
public:
@@ -58,67 +86,43 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
5886
}
5987

6088
Expected<llvm::SmallVector<int64_t, 4>>
61-
runAndSample(const char *Counters) const override {
62-
// We sum counts when there are several counters for a single ProcRes
63-
// (e.g. P23 on SandyBridge).
64-
llvm::SmallVector<int64_t, 4> CounterValues;
65-
int Reserved = 0;
66-
SmallVector<StringRef, 2> CounterNames;
67-
StringRef(Counters).split(CounterNames, '+');
68-
char *const ScratchPtr = Scratch->ptr();
89+
runWithCounter(StringRef CounterName) const override {
6990
const ExegesisTarget &ET = State.getExegesisTarget();
70-
for (auto &CounterName : CounterNames) {
71-
CounterName = CounterName.trim();
72-
auto CounterOrError = ET.createCounter(CounterName, State);
73-
74-
if (!CounterOrError)
75-
return CounterOrError.takeError();
76-
77-
pfm::Counter *Counter = CounterOrError.get().get();
78-
if (Reserved == 0) {
79-
Reserved = Counter->numValues();
80-
CounterValues.reserve(Reserved);
81-
} else if (Reserved != Counter->numValues())
82-
// It'd be wrong to accumulate vectors of different sizes.
83-
return make_error<Failure>(
84-
llvm::Twine("Inconsistent number of values for counter ")
85-
.concat(CounterName)
86-
.concat(std::to_string(Counter->numValues()))
87-
.concat(" vs expected of ")
88-
.concat(std::to_string(Reserved)));
89-
Scratch->clear();
90-
{
91-
auto PS = ET.withSavedState();
92-
CrashRecoveryContext CRC;
93-
CrashRecoveryContext::Enable();
94-
const bool Crashed = !CRC.RunSafely([this, Counter, ScratchPtr]() {
95-
Counter->start();
96-
this->Function(ScratchPtr);
97-
Counter->stop();
98-
});
99-
CrashRecoveryContext::Disable();
100-
PS.reset();
101-
if (Crashed) {
102-
std::string Msg = "snippet crashed while running";
91+
char *const ScratchPtr = Scratch->ptr();
92+
auto CounterOrError = ET.createCounter(CounterName, State);
93+
94+
if (!CounterOrError)
95+
return CounterOrError.takeError();
96+
97+
pfm::Counter *Counter = CounterOrError.get().get();
98+
Scratch->clear();
99+
{
100+
auto PS = ET.withSavedState();
101+
CrashRecoveryContext CRC;
102+
CrashRecoveryContext::Enable();
103+
const bool Crashed = !CRC.RunSafely([this, Counter, ScratchPtr]() {
104+
Counter->start();
105+
this->Function(ScratchPtr);
106+
Counter->stop();
107+
});
108+
CrashRecoveryContext::Disable();
109+
PS.reset();
110+
if (Crashed) {
111+
std::string Msg = "snippet crashed while running";
103112
#ifdef LLVM_ON_UNIX
104-
// See "Exit Status for Commands":
105-
// https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
106-
constexpr const int kSigOffset = 128;
107-
if (const char *const SigName = strsignal(CRC.RetCode - kSigOffset)) {
108-
Msg += ": ";
109-
Msg += SigName;
110-
}
111-
#endif
112-
return make_error<SnippetCrash>(std::move(Msg));
113+
// See "Exit Status for Commands":
114+
// https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
115+
constexpr const int kSigOffset = 128;
116+
if (const char *const SigName = strsignal(CRC.RetCode - kSigOffset)) {
117+
Msg += ": ";
118+
Msg += SigName;
113119
}
120+
#endif
121+
return make_error<SnippetCrash>(std::move(Msg));
114122
}
115-
116-
auto ValueOrError = Counter->readOrError(Function.getFunctionBytes());
117-
if (!ValueOrError)
118-
return ValueOrError.takeError();
119-
accumulateCounterValues(ValueOrError.get(), &CounterValues);
120123
}
121-
return CounterValues;
124+
125+
return Counter->readOrError(Function.getFunctionBytes());
122126
}
123127

124128
const LLVMState &State;

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ class BenchmarkRunner {
8989
public:
9090
virtual ~FunctionExecutor();
9191

92+
Expected<llvm::SmallVector<int64_t, 4>>
93+
runAndSample(const char *Counters) const;
94+
95+
protected:
96+
static void
97+
accumulateCounterValues(const llvm::SmallVectorImpl<int64_t> &NewValues,
98+
llvm::SmallVectorImpl<int64_t> *Result);
9299
virtual Expected<llvm::SmallVector<int64_t, 4>>
93-
runAndSample(const char *Counters) const = 0;
100+
runWithCounter(StringRef CounterName) const = 0;
94101
};
95102

96103
protected:

0 commit comments

Comments
 (0)