@@ -37,6 +37,34 @@ BenchmarkRunner::BenchmarkRunner(const LLVMState &State,
37
37
38
38
BenchmarkRunner::~BenchmarkRunner () = default ;
39
39
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
+
40
68
namespace {
41
69
class FunctionExecutorImpl : public BenchmarkRunner ::FunctionExecutor {
42
70
public:
@@ -58,67 +86,43 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
58
86
}
59
87
60
88
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 {
69
90
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" ;
103
112
#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;
113
119
}
120
+ #endif
121
+ return make_error<SnippetCrash>(std::move (Msg));
114
122
}
115
-
116
- auto ValueOrError = Counter->readOrError (Function.getFunctionBytes ());
117
- if (!ValueOrError)
118
- return ValueOrError.takeError ();
119
- accumulateCounterValues (ValueOrError.get (), &CounterValues);
120
123
}
121
- return CounterValues;
124
+
125
+ return Counter->readOrError (Function.getFunctionBytes ());
122
126
}
123
127
124
128
const LLVMState &State;
0 commit comments