[llvm] 9fb8718 - [llvm-exegesis][NFC] Let the pfm::Counter own the PerfHelper.

Clement Courbet via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 06:46:11 PDT 2020


Author: Clement Courbet
Date: 2020-04-08T15:37:30+02:00
New Revision: 9fb871866e2bb9b1c0968265bc5c1a3aa71d5845

URL: https://github.com/llvm/llvm-project/commit/9fb871866e2bb9b1c0968265bc5c1a3aa71d5845
DIFF: https://github.com/llvm/llvm-project/commit/9fb871866e2bb9b1c0968265bc5c1a3aa71d5845.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
    llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
    llvm/tools/llvm-exegesis/lib/PerfHelper.h
    llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index be778f29f52f..9592fd8faa0c 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -55,7 +55,7 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
       if (!PerfEvent.valid())
         return make_error<Failure>(
             Twine("invalid perf event '").concat(CounterName).concat("'"));
-      pfm::Counter Counter(PerfEvent);
+      pfm::Counter Counter(std::move(PerfEvent));
       Scratch->clear();
       {
         CrashRecoveryContext CRC;

diff  --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
index a4e5c17129ff..59c66ab23743 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
@@ -88,7 +88,7 @@ StringRef PerfEvent::getPfmEventString() const {
 }
 
 #ifdef HAVE_LIBPFM
-Counter::Counter(const PerfEvent &Event) {
+Counter::Counter(PerfEvent &&E) : Event(std::move(E)){
   assert(Event.valid());
   const pid_t Pid = 0;    // measure current process/thread.
   const int Cpu = -1;     // measure any processor.

diff  --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.h b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
index a6a2fc9b9d2d..99c555587c53 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.h
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
@@ -65,7 +65,7 @@ class PerfEvent {
 // underlying event.
 struct Counter {
   // event: the PerfEvent to measure.
-  explicit Counter(const PerfEvent &event);
+  explicit Counter(PerfEvent &&event);
 
   Counter(const Counter &) = delete;
   Counter(Counter &&other) = default;
@@ -77,6 +77,7 @@ struct Counter {
   int64_t read() const; // Return the current value of the counter.
 
 private:
+  PerfEvent Event;
 #ifdef HAVE_LIBPFM
   int FileDescriptor = -1;
 #endif

diff  --git a/llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp b/llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp
index fa6f05c900b4..f4a07e8ebf5c 100644
--- a/llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp
@@ -22,11 +22,11 @@ using ::testing::Not;
 TEST(PerfHelperTest, FunctionalTest) {
 #ifdef HAVE_LIBPFM
   ASSERT_FALSE(pfmInitialize());
-  const PerfEvent Event("CYCLES:u");
+  PerfEvent Event("CYCLES:u");
   ASSERT_TRUE(Event.valid());
   EXPECT_EQ(Event.name(), "CYCLES:u");
   EXPECT_THAT(Event.getPfmEventString(), Not(IsEmpty()));
-  Counter Cnt(Event);
+  Counter Cnt(std::move(Event));
   Cnt.start();
   Cnt.stop();
   Cnt.read();


        


More information about the llvm-commits mailing list