Recommit r256952 "Filtering IR printing for print-after-all/print-before-all"
authorWeiming Zhao <weimingz@codeaurora.org>
Wed, 6 Jan 2016 22:55:03 +0000 (22:55 +0000)
committerWeiming Zhao <weimingz@codeaurora.org>
Wed, 6 Jan 2016 22:55:03 +0000 (22:55 +0000)
Fix lit test fail due to outputting an extra line.

Differential Revision: http://reviews.llvm.org/D15776

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256987 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Pass.h
lib/Analysis/CallGraphSCCPass.cpp
lib/Analysis/LoopPass.cpp
lib/CodeGen/MachineFunctionPrinterPass.cpp
lib/IR/IRPrintingPasses.cpp
lib/IR/LegacyPassManager.cpp
test/Other/2010-05-06-Printer.ll

index 3c4d838a465268f42e82a989e84d29bee35140e5..99604cdbc9caa012e9317d29906933c68330bf1f 100644 (file)
@@ -369,6 +369,10 @@ protected:
 /// @brief This is the storage for the -time-passes option.
 extern bool TimePassesIsEnabled;
 
+/// isFunctionInPrintList - returns true if a function should be printed via
+//  debugging options like -print-after-all/-print-before-all.
+//  @brief Tells if the function IR should be printed by PrinterPass.
+extern bool isFunctionInPrintList(StringRef FunctionName);
 } // End llvm namespace
 
 // Include support files that contain important APIs commonly used by Passes,
index 07b389a2a1393913d275ca93cc25dce8b042adf0..6dd1d0a066b6bef30de55134dbc5650d50e2432f 100644 (file)
@@ -612,9 +612,10 @@ namespace {
     bool runOnSCC(CallGraphSCC &SCC) override {
       Out << Banner;
       for (CallGraphNode *CGN : SCC) {
-        if (CGN->getFunction())
-          CGN->getFunction()->print(Out);
-        else
+        if (CGN->getFunction()) {
+          if (isFunctionInPrintList(CGN->getFunction()->getName()))
+            CGN->getFunction()->print(Out);
+        } else
           Out << "\nPrinting <null> Function\n";
       }
       return false;
index dc424734dd5661dcc8f1fccd3c88afb7017198a8..e24a9e46fc1577a3a6104ff2818dbcc226937c4c 100644 (file)
@@ -42,7 +42,11 @@ public:
   }
 
   bool runOnLoop(Loop *L, LPPassManager &) override {
-    P.run(*L);
+    auto BBI = find_if(L->blocks().begin(), L->blocks().end(),
+                       [](BasicBlock *BB) { return BB; });
+    if (BBI != L->blocks().end() &&
+        isFunctionInPrintList((*BBI)->getParent()->getName()))
+      P.run(*L);
     return false;
   }
 };
index 790f5accdb26fa289ab6e441099c2ebff0fb6475..4f424ff292cc24a2b31b5b571359562c07bc996b 100644 (file)
@@ -31,7 +31,7 @@ struct MachineFunctionPrinterPass : public MachineFunctionPass {
   const std::string Banner;
 
   MachineFunctionPrinterPass() : MachineFunctionPass(ID), OS(dbgs()) { }
-  MachineFunctionPrinterPass(raw_ostream &os, const std::string &banner) 
+  MachineFunctionPrinterPass(raw_ostream &os, const std::string &banner)
       : MachineFunctionPass(ID), OS(os), Banner(banner) {}
 
   const char *getPassName() const override { return "MachineFunction Printer"; }
@@ -42,6 +42,8 @@ struct MachineFunctionPrinterPass : public MachineFunctionPass {
   }
 
   bool runOnMachineFunction(MachineFunction &MF) override {
+    if (!llvm::isFunctionInPrintList(MF.getName()))
+      return false;
     OS << "# " << Banner << ":\n";
     MF.print(OS, getAnalysisIfAvailable<SlotIndexes>());
     return false;
index c1ac336c1fbfafa287f5cfd3568dee72d8f3ae13..822dbeb08b3305d146d182a182ff4565a12dce12 100644 (file)
@@ -28,7 +28,13 @@ PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
 
 PreservedAnalyses PrintModulePass::run(Module &M) {
   OS << Banner;
-  M.print(OS, nullptr, ShouldPreserveUseListOrder);
+  if (llvm::isFunctionInPrintList("*"))
+    M.print(OS, nullptr, ShouldPreserveUseListOrder);
+  else {
+    for(const auto &F : M.functions())
+      if (llvm::isFunctionInPrintList(F.getName()))
+        F.print(OS);
+  }
   return PreservedAnalyses::all();
 }
 
@@ -37,7 +43,8 @@ PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
     : OS(OS), Banner(Banner) {}
 
 PreservedAnalyses PrintFunctionPass::run(Function &F) {
-  OS << Banner << static_cast<Value &>(F);
+  if (isFunctionInPrintList(F.getName()))
+    OS << Banner << static_cast<Value &>(F);
   return PreservedAnalyses::all();
 }
 
index f2e0c7d32c0209ea8ca666b15a120adb12291344..63d89f21b350ebdcf3cbfe72711d71ab416be425 100644 (file)
@@ -28,6 +28,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <map>
+#include <unordered_set>
 using namespace llvm;
 using namespace llvm::legacy;
 
@@ -83,6 +84,13 @@ PrintAfterAll("print-after-all",
               llvm::cl::desc("Print IR after each pass"),
               cl::init(false));
 
+static cl::list<std::string>
+    PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
+                   cl::desc("Only print IR for functions whose name "
+                            "match this for all print-[before|after][-all] "
+                            "options"),
+                   cl::CommaSeparated);
+
 /// This is a helper to determine whether to print IR before or
 /// after a pass.
 
@@ -109,6 +117,11 @@ static bool ShouldPrintAfterPass(const PassInfo *PI) {
   return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
 }
 
+bool llvm::isFunctionInPrintList(StringRef FunctionName) {
+  static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
+                                                        PrintFuncsList.end());
+  return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
+}
 /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
 /// or higher is specified.
 bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
index e57b9825b3347868ee5798d5cc4211cab8dcbc2d..dcc0e752bb5adbeecbc9e29deab85846a609ed9f 100644 (file)
@@ -1,7 +1,19 @@
 ; RUN: llc -O2 -print-after-all < %s 2>/dev/null
+; RUN: llc -O2 -print-after-all < %s 2>&1 | FileCheck %s --check-prefix=ALL
+; RUN: llc -O2 -print-after-all -filter-print-funcs=foo < %s 2>&1 | FileCheck %s --check-prefix=FOO
 ; REQUIRES: default_triple
-
 define void @tester(){
   ret void
 }
 
+define void @foo(){
+  ret void
+}
+
+;ALL: define void @tester()
+;ALL: define void @foo()
+;ALL: ModuleID =
+
+;FOO: IR Dump After
+;FOO-NEXT: define void @foo()
+;FOO-NOT: define void @tester