Skip to content

Commit 9fb8718

Browse files
committed
[llvm-exegesis][NFC] Let the pfm::Counter own the PerfHelper.
A perf helper is always only ever cretaed to be checked for validity then passed as Counter ctor argument, never to be touched again. Its lifetime should outlive that of the counter, and there is never any reason to have two different counters of top of the perf helper. Make sure these assumptions always hold by making the Counter consume the PerfHelper.
1 parent 625acd8 commit 9fb8718

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
5555
if (!PerfEvent.valid())
5656
return make_error<Failure>(
5757
Twine("invalid perf event '").concat(CounterName).concat("'"));
58-
pfm::Counter Counter(PerfEvent);
58+
pfm::Counter Counter(std::move(PerfEvent));
5959
Scratch->clear();
6060
{
6161
CrashRecoveryContext CRC;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ StringRef PerfEvent::getPfmEventString() const {
8888
}
8989

9090
#ifdef HAVE_LIBPFM
91-
Counter::Counter(const PerfEvent &Event) {
91+
Counter::Counter(PerfEvent &&E) : Event(std::move(E)){
9292
assert(Event.valid());
9393
const pid_t Pid = 0; // measure current process/thread.
9494
const int Cpu = -1; // measure any processor.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PerfEvent {
6565
// underlying event.
6666
struct Counter {
6767
// event: the PerfEvent to measure.
68-
explicit Counter(const PerfEvent &event);
68+
explicit Counter(PerfEvent &&event);
6969

7070
Counter(const Counter &) = delete;
7171
Counter(Counter &&other) = default;
@@ -77,6 +77,7 @@ struct Counter {
7777
int64_t read() const; // Return the current value of the counter.
7878

7979
private:
80+
PerfEvent Event;
8081
#ifdef HAVE_LIBPFM
8182
int FileDescriptor = -1;
8283
#endif

llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ using ::testing::Not;
2222
TEST(PerfHelperTest, FunctionalTest) {
2323
#ifdef HAVE_LIBPFM
2424
ASSERT_FALSE(pfmInitialize());
25-
const PerfEvent Event("CYCLES:u");
25+
PerfEvent Event("CYCLES:u");
2626
ASSERT_TRUE(Event.valid());
2727
EXPECT_EQ(Event.name(), "CYCLES:u");
2828
EXPECT_THAT(Event.getPfmEventString(), Not(IsEmpty()));
29-
Counter Cnt(Event);
29+
Counter Cnt(std::move(Event));
3030
Cnt.start();
3131
Cnt.stop();
3232
Cnt.read();

0 commit comments

Comments
 (0)