[llvm] r355984 - Reland "[Remarks] Add -foptimization-record-passes to filter remark emission"

Francis Visoiu Mistrih via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 14:22:28 PDT 2019


Author: thegameg
Date: Tue Mar 12 14:22:27 2019
New Revision: 355984

URL: http://llvm.org/viewvc/llvm-project?rev=355984&view=rev
Log:
Reland "[Remarks] Add -foptimization-record-passes to filter remark emission"

Currently we have -Rpass for filtering the remarks that are displayed as
diagnostics, but when using -fsave-optimization-record, there is no way
to filter the remarks while generating them.

This adds support for filtering remarks by passes using a regex.
Ex: `clang -fsave-optimization-record -foptimization-record-passes=inline`

will only emit the remarks coming from the pass `inline`.

This adds:

* `-fsave-optimization-record` to the driver
* `-opt-record-passes` to cc1
* `-lto-pass-remarks-filter` to the LTOCodeGenerator
* `--opt-remarks-passes` to lld
* `-pass-remarks-filter` to llc, opt, llvm-lto, llvm-lto2
* `-opt-remarks-passes` to gold-plugin

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

Original llvm-svn: 355964

Modified:
    llvm/trunk/include/llvm/IR/RemarkStreamer.h
    llvm/trunk/include/llvm/LTO/Config.h
    llvm/trunk/include/llvm/LTO/LTO.h
    llvm/trunk/lib/IR/RemarkStreamer.cpp
    llvm/trunk/lib/LTO/LTO.cpp
    llvm/trunk/lib/LTO/LTOBackend.cpp
    llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
    llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
    llvm/trunk/test/LTO/Resolution/X86/diagnostic-handler-remarks.ll
    llvm/trunk/test/ThinLTO/X86/diagnostic-handler-remarks.ll
    llvm/trunk/tools/gold/gold-plugin.cpp
    llvm/trunk/tools/llc/llc.cpp
    llvm/trunk/tools/llvm-lto2/llvm-lto2.cpp
    llvm/trunk/tools/opt/opt.cpp

Modified: llvm/trunk/include/llvm/IR/RemarkStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/RemarkStreamer.h?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/RemarkStreamer.h (original)
+++ llvm/trunk/include/llvm/IR/RemarkStreamer.h Tue Mar 12 14:22:27 2019
@@ -14,8 +14,10 @@
 #define LLVM_IR_REMARKSTREAMER_H
 
 #include "llvm/IR/DiagnosticInfo.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/YAMLTraits.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Regex.h"
 #include <string>
 #include <vector>
 
@@ -26,6 +28,8 @@ class RemarkStreamer {
   const std::string Filename;
   /// The open raw_ostream that the remark diagnostics are emitted to.
   raw_ostream &OS;
+  /// The regex used to filter remarks based on the passes that emit them.
+  Optional<Regex> PassFilter;
 
   /// The YAML streamer.
   yaml::Output YAMLOutput;
@@ -36,6 +40,9 @@ public:
   StringRef getFilename() const { return Filename; }
   /// Return stream that the remark diagnostics are emitted to.
   raw_ostream &getStream() { return OS; }
+  /// Set a pass filter based on a regex \p Filter.
+  /// Returns an error if the regex is invalid.
+  Error setFilter(StringRef Filter);
   /// Emit a diagnostic through the streamer.
   void emit(const DiagnosticInfoOptimizationBase &Diag);
 };

Modified: llvm/trunk/include/llvm/LTO/Config.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/Config.h?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/include/llvm/LTO/Config.h (original)
+++ llvm/trunk/include/llvm/LTO/Config.h Tue Mar 12 14:22:27 2019
@@ -96,6 +96,9 @@ struct Config {
   /// Optimization remarks file path.
   std::string RemarksFilename = "";
 
+  /// Optimization remarks pass filter.
+  std::string RemarksPasses = "";
+
   /// Whether to emit optimization remarks with hotness informations.
   bool RemarksWithHotness = false;
 

Modified: llvm/trunk/include/llvm/LTO/LTO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTO.h?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/include/llvm/LTO/LTO.h (original)
+++ llvm/trunk/include/llvm/LTO/LTO.h Tue Mar 12 14:22:27 2019
@@ -84,6 +84,7 @@ std::string getThinLTOOutputFile(const s
 /// Setup optimization remarks.
 Expected<std::unique_ptr<ToolOutputFile>>
 setupOptimizationRemarks(LLVMContext &Context, StringRef LTORemarksFilename,
+                         StringRef LTORemarksPasses,
                          bool LTOPassRemarksWithHotness, int Count = -1);
 
 class LTO;

Modified: llvm/trunk/lib/IR/RemarkStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/RemarkStreamer.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/lib/IR/RemarkStreamer.cpp (original)
+++ llvm/trunk/lib/IR/RemarkStreamer.cpp Tue Mar 12 14:22:27 2019
@@ -21,7 +21,21 @@ RemarkStreamer::RemarkStreamer(StringRef
   assert(!Filename.empty() && "This needs to be a real filename.");
 }
 
+Error RemarkStreamer::setFilter(StringRef Filter) {
+  Regex R = Regex(Filter);
+  std::string RegexError;
+  if (!R.isValid(RegexError))
+    return createStringError(std::make_error_code(std::errc::invalid_argument),
+                             RegexError.data());
+  PassFilter = std::move(R);
+  return Error::success();
+}
+
 void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
+  if (Optional<Regex> &Filter = PassFilter)
+    if (!Filter->match(Diag.getPassName()))
+      return;
+
   DiagnosticInfoOptimizationBase *DiagPtr =
       const_cast<DiagnosticInfoOptimizationBase *>(&Diag);
   YAMLOutput << DiagPtr;

Modified: llvm/trunk/lib/LTO/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTO.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTO.cpp (original)
+++ llvm/trunk/lib/LTO/LTO.cpp Tue Mar 12 14:22:27 2019
@@ -1312,6 +1312,7 @@ Error LTO::runThinLTO(AddStreamFn AddStr
 Expected<std::unique_ptr<ToolOutputFile>>
 lto::setupOptimizationRemarks(LLVMContext &Context,
                               StringRef LTORemarksFilename,
+                              StringRef LTORemarksPasses,
                               bool LTOPassRemarksWithHotness, int Count) {
   if (LTOPassRemarksWithHotness)
     Context.setDiagnosticsHotnessRequested(true);
@@ -1329,6 +1330,11 @@ lto::setupOptimizationRemarks(LLVMContex
     return errorCodeToError(EC);
   Context.setRemarkStreamer(
       llvm::make_unique<RemarkStreamer>(Filename, DiagnosticFile->os()));
+
+  if (!LTORemarksPasses.empty())
+    if (Error E = Context.getRemarkStreamer()->setFilter(LTORemarksPasses))
+      return std::move(E);
+
   DiagnosticFile->keep();
   return std::move(DiagnosticFile);
 }

Modified: llvm/trunk/lib/LTO/LTOBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOBackend.cpp (original)
+++ llvm/trunk/lib/LTO/LTOBackend.cpp Tue Mar 12 14:22:27 2019
@@ -429,8 +429,9 @@ Error lto::backend(Config &C, AddStreamF
   std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
 
   // Setup optimization remarks.
-  auto DiagFileOrErr = lto::setupOptimizationRemarks(
-      Mod->getContext(), C.RemarksFilename, C.RemarksWithHotness);
+  auto DiagFileOrErr =
+      lto::setupOptimizationRemarks(Mod->getContext(), C.RemarksFilename,
+                                    C.RemarksPasses, C.RemarksWithHotness);
   if (!DiagFileOrErr)
     return DiagFileOrErr.takeError();
   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
@@ -484,7 +485,8 @@ Error lto::thinBackend(Config &Conf, uns
 
   // Setup optimization remarks.
   auto DiagFileOrErr = lto::setupOptimizationRemarks(
-      Mod.getContext(), Conf.RemarksFilename, Conf.RemarksWithHotness, Task);
+      Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses,
+      Conf.RemarksWithHotness, Task);
   if (!DiagFileOrErr)
     return DiagFileOrErr.takeError();
   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);

Modified: llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/LTOCodeGenerator.cpp Tue Mar 12 14:22:27 2019
@@ -85,6 +85,12 @@ cl::opt<std::string>
                        cl::desc("Output filename for pass remarks"),
                        cl::value_desc("filename"));
 
+cl::opt<std::string>
+    LTORemarksPasses("lto-pass-remarks-filter",
+                     cl::desc("Only record optimization remarks from passes "
+                              "whose names match the given regular expression"),
+                     cl::value_desc("regex"));
+
 cl::opt<bool> LTOPassRemarksWithHotness(
     "lto-pass-remarks-with-hotness",
     cl::desc("With PGO, include profile count in optimization remarks"),
@@ -505,7 +511,7 @@ bool LTOCodeGenerator::optimize(bool Dis
     return false;
 
   auto DiagFileOrErr = lto::setupOptimizationRemarks(
-      Context, LTORemarksFilename, LTOPassRemarksWithHotness);
+      Context, LTORemarksFilename, LTORemarksPasses, LTOPassRemarksWithHotness);
   if (!DiagFileOrErr) {
     errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
     report_fatal_error("Can't get an output file for the remarks");

Modified: llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp Tue Mar 12 14:22:27 2019
@@ -70,6 +70,7 @@ namespace llvm {
 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
 extern cl::opt<bool> LTODiscardValueNames;
 extern cl::opt<std::string> LTORemarksFilename;
+extern cl::opt<std::string> LTORemarksPasses;
 extern cl::opt<bool> LTOPassRemarksWithHotness;
 }
 
@@ -972,7 +973,8 @@ void ThinLTOCodeGenerator::run() {
         Context.setDiscardValueNames(LTODiscardValueNames);
         Context.enableDebugTypeODRUniquing();
         auto DiagFileOrErr = lto::setupOptimizationRemarks(
-            Context, LTORemarksFilename, LTOPassRemarksWithHotness, count);
+            Context, LTORemarksFilename, LTORemarksPasses,
+            LTOPassRemarksWithHotness, count);
         if (!DiagFileOrErr) {
           errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
           report_fatal_error("ThinLTO: Can't get an output file for the "

Modified: llvm/trunk/test/LTO/Resolution/X86/diagnostic-handler-remarks.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/Resolution/X86/diagnostic-handler-remarks.ll?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/test/LTO/Resolution/X86/diagnostic-handler-remarks.ll (original)
+++ llvm/trunk/test/LTO/Resolution/X86/diagnostic-handler-remarks.ll Tue Mar 12 14:22:27 2019
@@ -4,6 +4,7 @@
 ; RUN: llvm-as < %s >%t.bc
 ; RUN: rm -f %t.yaml
 ; RUN: llvm-lto2 run -pass-remarks-output=%t.yaml \
+; RUN:           -pass-remarks-filter=inline \
 ; RUN:           -r %t.bc,tinkywinky,p \
 ; RUN:           -r %t.bc,patatino,px \
 ; RUN:           -r %t.bc,main,px -o %t.o %t.bc
@@ -13,6 +14,7 @@
 ; RUN: opt -module-summary %s -o %t.bc
 ; RUN: rm -f %t.thin.1.yaml
 ; RUN: llvm-lto2 run -pass-remarks-output=%t \
+; RUN:           -pass-remarks-filter=inline \
 ; RUN:           -r %t.bc,tinkywinky,p \
 ; RUN:           -r %t.bc,patatino,px \
 ; RUN:           -r %t.bc,main,px -o %t.o %t.bc

Modified: llvm/trunk/test/ThinLTO/X86/diagnostic-handler-remarks.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ThinLTO/X86/diagnostic-handler-remarks.ll?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/test/ThinLTO/X86/diagnostic-handler-remarks.ll (original)
+++ llvm/trunk/test/ThinLTO/X86/diagnostic-handler-remarks.ll Tue Mar 12 14:22:27 2019
@@ -5,6 +5,7 @@
 ; RUN: rm -f %t.yaml.thin.0.yaml %t.yaml.thin.1.yaml
 ; RUN: llvm-lto -thinlto-action=run \
 ; RUN:          -lto-pass-remarks-output=%t.yaml \
+; RUN:          -lto-pass-remarks-filter=inline \
 ; RUN:          -exported-symbol _func2 \
 ; RUN:          -exported-symbol _main %t1.bc %t2.bc 2>&1 | \
 ; RUN:     FileCheck %s -allow-empty

Modified: llvm/trunk/tools/gold/gold-plugin.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/gold/gold-plugin.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/tools/gold/gold-plugin.cpp (original)
+++ llvm/trunk/tools/gold/gold-plugin.cpp Tue Mar 12 14:22:27 2019
@@ -205,8 +205,9 @@ namespace options {
   /// Statistics output filename.
   static std::string stats_file;
 
-  // Optimization remarks filename and hotness options
+  // Optimization remarks filename, accepted passes and hotness options
   static std::string OptRemarksFilename;
+  static std::string OptRemarksFilter;
   static bool OptRemarksWithHotness = false;
 
   // Context sensitive PGO options.
@@ -285,6 +286,8 @@ namespace options {
       dwo_dir = opt.substr(strlen("dwo_dir="));
     } else if (opt.startswith("opt-remarks-filename=")) {
       OptRemarksFilename = opt.substr(strlen("opt-remarks-filename="));
+    } else if (opt.startswith("opt-remarks-passes=")) {
+      OptRemarksFilter = opt.substr(strlen("opt-remarks-passes="));
     } else if (opt == "opt-remarks-with-hotness") {
       OptRemarksWithHotness = true;
     } else if (opt.startswith("stats-file=")) {
@@ -908,6 +911,7 @@ static std::unique_ptr<LTO> createLTO(In
 
   // Set up optimization remarks handling.
   Conf.RemarksFilename = options::OptRemarksFilename;
+  Conf.RemarksPasses = options::OptRemarksFilter;
   Conf.RemarksWithHotness = options::OptRemarksWithHotness;
 
   // Use new pass manager if set in driver

Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Tue Mar 12 14:22:27 2019
@@ -148,6 +148,12 @@ static cl::opt<std::string>
                     cl::desc("YAML output filename for pass remarks"),
                     cl::value_desc("filename"));
 
+static cl::opt<std::string>
+    RemarksPasses("pass-remarks-filter",
+                  cl::desc("Only record optimization remarks from passes whose "
+                           "names match the given regular expression"),
+                  cl::value_desc("regex"));
+
 namespace {
 static ManagedStatic<std::vector<std::string>> RunPassNames;
 
@@ -336,6 +342,12 @@ int main(int argc, char **argv) {
     }
     Context.setRemarkStreamer(
         llvm::make_unique<RemarkStreamer>(RemarksFilename, YamlFile->os()));
+
+    if (!RemarksPasses.empty())
+      if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses)) {
+        WithColor::error(errs(), argv[0]) << E << '\n';
+        return 1;
+      }
   }
 
   if (InputLanguage != "" && InputLanguage != "ir" &&

Modified: llvm/trunk/tools/llvm-lto2/llvm-lto2.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lto2/llvm-lto2.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-lto2/llvm-lto2.cpp (original)
+++ llvm/trunk/tools/llvm-lto2/llvm-lto2.cpp Tue Mar 12 14:22:27 2019
@@ -101,6 +101,12 @@ static cl::opt<bool> OptRemarksWithHotne
              "Has effect only if -pass-remarks-output is specified."));
 
 static cl::opt<std::string>
+    OptRemarksPasses("pass-remarks-filter",
+                     cl::desc("Only record optimization remarks from passes "
+                              "whose names match the given regular expression"),
+                     cl::value_desc("regex"));
+
+static cl::opt<std::string>
     SamplePGOFile("lto-sample-profile-file",
                   cl::desc("Specify a SamplePGO profile file"));
 
@@ -220,6 +226,7 @@ static int run(int argc, char **argv) {
 
   // Optimization remarks.
   Conf.RemarksFilename = OptRemarksOutput;
+  Conf.RemarksPasses = OptRemarksPasses;
   Conf.RemarksWithHotness = OptRemarksWithHotness;
 
   Conf.SampleProfile = SamplePGOFile;

Modified: llvm/trunk/tools/opt/opt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=355984&r1=355983&r2=355984&view=diff
==============================================================================
--- llvm/trunk/tools/opt/opt.cpp (original)
+++ llvm/trunk/tools/opt/opt.cpp Tue Mar 12 14:22:27 2019
@@ -275,6 +275,12 @@ static cl::opt<std::string>
                     cl::desc("YAML output filename for pass remarks"),
                     cl::value_desc("filename"));
 
+static cl::opt<std::string>
+    RemarksPasses("pass-remarks-filter",
+                  cl::desc("Only record optimization remarks from passes whose "
+                           "names match the given regular expression"),
+                  cl::value_desc("regex"));
+
 cl::opt<PGOKind>
     PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
                 cl::desc("The kind of profile guided optimization"),
@@ -566,6 +572,12 @@ int main(int argc, char **argv) {
     }
     Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
         RemarksFilename, OptRemarkFile->os()));
+
+    if (!RemarksPasses.empty())
+      if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses)) {
+        errs() << E << '\n';
+        return 1;
+      }
   }
 
   // Load the input module...




More information about the llvm-commits mailing list