[llvm-exegesis] Improve error reporting in BenchmarkRunner.cpp
authorMiloš Stojanović <Milos.Stojanovic@rt-rk.com>
Fri, 7 Feb 2020 12:45:10 +0000 (13:45 +0100)
committerMiloš Stojanović <Milos.Stojanovic@rt-rk.com>
Fri, 7 Feb 2020 15:29:52 +0000 (16:29 +0100)
Followup to D74085.
Replace the use of `report_fatal_error()` with returning the error to
`llvm-exegesis.cpp` and handling it there.
To facilitate this, a new `Error` type has been added which is only used
to log errors to the yaml output.

Differential Revision: https://reviews.llvm.org/D74215

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

index b4a1c62..6981aaa 100644 (file)
@@ -52,7 +52,7 @@ private:
       CounterName = CounterName.trim();
       pfm::PerfEvent PerfEvent(CounterName);
       if (!PerfEvent.valid())
-        report_fatal_error(
+        return make_error<Failure>(
             Twine("invalid perf event '").concat(CounterName).concat("'"));
       pfm::Counter Counter(PerfEvent);
       Scratch->clear();
@@ -67,7 +67,7 @@ private:
         CrashRecoveryContext::Disable();
         // FIXME: Better diagnosis.
         if (Crashed)
-          return make_error<Failure>("snippet crashed while running");
+          return make_error<SnippetCrash>("snippet crashed while running");
       }
       CounterValue += Counter.read();
     }
@@ -79,7 +79,7 @@ private:
 };
 } // namespace
 
-InstructionBenchmark BenchmarkRunner::runConfiguration(
+Expected<InstructionBenchmark> BenchmarkRunner::runConfiguration(
     const BenchmarkCode &BC, unsigned NumRepetitions,
     const SnippetRepetitor &Repetitor, bool DumpObjectToDisk) const {
   InstructionBenchmark InstrBenchmark;
@@ -138,6 +138,8 @@ InstructionBenchmark BenchmarkRunner::runConfiguration(
                                       Scratch.get());
   auto Measurements = runMeasurements(Executor);
   if (Error E = Measurements.takeError()) {
+    if (!E.isA<SnippetCrash>())
+      return std::move(E);
     InstrBenchmark.Error = toString(std::move(E));
     return InstrBenchmark;
   }
index d8dcc79..1a88667 100644 (file)
@@ -38,10 +38,10 @@ public:
 
   virtual ~BenchmarkRunner();
 
-  InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
-                                        unsigned NumRepetitions,
-                                        const SnippetRepetitor &Repetitor,
-                                        bool DumpObjectToDisk) const;
+  Expected<InstructionBenchmark>
+  runConfiguration(const BenchmarkCode &Configuration, unsigned NumRepetitions,
+                   const SnippetRepetitor &Repetitor,
+                   bool DumpObjectToDisk) const;
 
   // Scratch space to run instructions that touch memory.
   struct ScratchSpace {
index d8aaf78..51ce41b 100644 (file)
@@ -13,13 +13,19 @@ namespace exegesis {
 
 char ClusteringError::ID;
 
-void ClusteringError::log(raw_ostream &OS) const {
-  OS << Msg;
-}
+void ClusteringError::log(raw_ostream &OS) const { OS << Msg; }
 
 std::error_code ClusteringError::convertToErrorCode() const {
   return inconvertibleErrorCode();
 }
 
+char SnippetCrash::ID;
+
+void SnippetCrash::log(raw_ostream &OS) const { OS << Msg; }
+
+std::error_code SnippetCrash::convertToErrorCode() const {
+  return inconvertibleErrorCode();
+}
+
 } // namespace exegesis
 } // namespace llvm
index dc203c8..e5fa093 100644 (file)
@@ -26,13 +26,29 @@ public:
 class ClusteringError : public ErrorInfo<ClusteringError> {
 public:
   static char ID;
-  ClusteringError(const Twine&S) : Msg(S.str()) {}
+  ClusteringError(const Twine &S) : Msg(S.str()) {}
 
   void log(raw_ostream &OS) const override;
 
   std::error_code convertToErrorCode() const override;
+
+private:
+  std::string Msg;
+};
+
+// A class representing failures that happened during snippet execution.
+// Instead of terminating the program crashes are logged into the output.
+class SnippetCrash : public ErrorInfo<SnippetCrash> {
+public:
+  static char ID;
+  SnippetCrash(const Twine &S) : Msg(S.str()) {}
+
+  void log(raw_ostream &OS) const override;
+
+  std::error_code convertToErrorCode() const override;
+
 private:
-    std::string Msg;
+  std::string Msg;
 };
 
 } // namespace exegesis
index d886d18..49b01f3 100644 (file)
@@ -309,8 +309,8 @@ void benchmarkMain() {
     BenchmarkFile = "-";
 
   for (const BenchmarkCode &Conf : Configurations) {
-    InstructionBenchmark Result = Runner->runConfiguration(
-        Conf, NumRepetitions, *Repetitor, DumpObjectToDisk);
+    InstructionBenchmark Result = ExitOnErr(Runner->runConfiguration(
+        Conf, NumRepetitions, *Repetitor, DumpObjectToDisk));
     ExitOnFileError(BenchmarkFile, Result.writeYaml(State, BenchmarkFile));
   }
   exegesis::pfm::pfmTerminate();