[llvm] 645c685 - [NFC] Add raw_ostream parameter to printIR routines

Anh Tuyen Tran via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 18 09:05:41 PDT 2020


Author: Jamie Schmeiser
Date: 2020-08-18T16:05:27Z
New Revision: 645c6856a68af9b9dd7d918f630560cf07462ed7

URL: https://github.com/llvm/llvm-project/commit/645c6856a68af9b9dd7d918f630560cf07462ed7
DIFF: https://github.com/llvm/llvm-project/commit/645c6856a68af9b9dd7d918f630560cf07462ed7.diff

LOG: [NFC] Add raw_ostream parameter to printIR routines

This is a non-functional-change to generalize the printIR routines so that
the output can be saved and manipulated rather than being directly output
to dbgs(). This is a prerequisite change for many upcoming changes that
allow new ways of examining changes made to the IR in the new pass manager.

Reviewed By: aeubanks (Arthur Eubanks)

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

Added: 
    

Modified: 
    llvm/lib/Passes/StandardInstrumentations.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 9e9caa1557b4..55dbca714371 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -86,39 +86,39 @@ Optional<std::pair<const Module *, std::string>> unwrapModule(Any IR) {
   llvm_unreachable("Unknown IR unit");
 }
 
-void printIR(const Function *F, StringRef Banner, StringRef Extra = StringRef(),
-             bool Brief = false) {
+void printIR(raw_ostream &OS, const Function *F, StringRef Banner,
+             StringRef Extra = StringRef(), bool Brief = false) {
   if (Brief) {
-    dbgs() << F->getName() << '\n';
+    OS << F->getName() << '\n';
     return;
   }
 
   if (!llvm::isFunctionInPrintList(F->getName()))
     return;
-  dbgs() << Banner << Extra << "\n" << static_cast<const Value &>(*F);
+  OS << Banner << Extra << "\n" << static_cast<const Value &>(*F);
 }
 
-void printIR(const Module *M, StringRef Banner, StringRef Extra = StringRef(),
-             bool Brief = false) {
+void printIR(raw_ostream &OS, const Module *M, StringRef Banner,
+             StringRef Extra = StringRef(), bool Brief = false) {
   if (Brief) {
-    dbgs() << M->getName() << '\n';
+    OS << M->getName() << '\n';
     return;
   }
 
   if (llvm::isFunctionInPrintList("*") || llvm::forcePrintModuleIR()) {
-    dbgs() << Banner << Extra << "\n";
-    M->print(dbgs(), nullptr, false);
+    OS << Banner << Extra << "\n";
+    M->print(OS, nullptr, false);
   } else {
     for (const auto &F : M->functions()) {
-      printIR(&F, Banner, Extra);
+      printIR(OS, &F, Banner, Extra);
     }
   }
 }
 
-void printIR(const LazyCallGraph::SCC *C, StringRef Banner,
+void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C, StringRef Banner,
              StringRef Extra = StringRef(), bool Brief = false) {
   if (Brief) {
-    dbgs() << *C << '\n';
+    OS << *C << '\n';
     return;
   }
 
@@ -127,47 +127,48 @@ void printIR(const LazyCallGraph::SCC *C, StringRef Banner,
     const Function &F = N.getFunction();
     if (!F.isDeclaration() && llvm::isFunctionInPrintList(F.getName())) {
       if (!BannerPrinted) {
-        dbgs() << Banner << Extra << "\n";
+        OS << Banner << Extra << "\n";
         BannerPrinted = true;
       }
-      F.print(dbgs());
+      F.print(OS);
     }
   }
 }
 
-void printIR(const Loop *L, StringRef Banner, bool Brief = false) {
+void printIR(raw_ostream &OS, const Loop *L, StringRef Banner,
+             bool Brief = false) {
   if (Brief) {
-    dbgs() << *L;
+    OS << *L;
     return;
   }
 
   const Function *F = L->getHeader()->getParent();
   if (!llvm::isFunctionInPrintList(F->getName()))
     return;
-  llvm::printLoop(const_cast<Loop &>(*L), dbgs(), std::string(Banner));
+  llvm::printLoop(const_cast<Loop &>(*L), OS, std::string(Banner));
 }
 
 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
 /// llvm::Any and does actual print job.
-void unwrapAndPrint(Any IR, StringRef Banner, bool ForceModule = false,
-                    bool Brief = false) {
+void unwrapAndPrint(raw_ostream &OS, Any IR, StringRef Banner,
+                    bool ForceModule = false, bool Brief = false) {
   if (ForceModule) {
     if (auto UnwrappedModule = unwrapModule(IR))
-      printIR(UnwrappedModule->first, Banner, UnwrappedModule->second);
+      printIR(OS, UnwrappedModule->first, Banner, UnwrappedModule->second);
     return;
   }
 
   if (any_isa<const Module *>(IR)) {
     const Module *M = any_cast<const Module *>(IR);
     assert(M && "module should be valid for printing");
-    printIR(M, Banner, "", Brief);
+    printIR(OS, M, Banner, "", Brief);
     return;
   }
 
   if (any_isa<const Function *>(IR)) {
     const Function *F = any_cast<const Function *>(IR);
     assert(F && "function should be valid for printing");
-    printIR(F, Banner, "", Brief);
+    printIR(OS, F, Banner, "", Brief);
     return;
   }
 
@@ -175,14 +176,14 @@ void unwrapAndPrint(Any IR, StringRef Banner, bool ForceModule = false,
     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
     assert(C && "scc should be valid for printing");
     std::string Extra = std::string(formatv(" (scc: {0})", C->getName()));
-    printIR(C, Banner, Extra, Brief);
+    printIR(OS, C, Banner, Extra, Brief);
     return;
   }
 
   if (any_isa<const Loop *>(IR)) {
     const Loop *L = any_cast<const Loop *>(IR);
     assert(L && "Loop should be valid for printing");
-    printIR(L, Banner, Brief);
+    printIR(OS, L, Banner, Brief);
     return;
   }
   llvm_unreachable("Unknown wrapped IR type");
@@ -226,7 +227,7 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
     return;
 
   SmallString<20> Banner = formatv("*** IR Dump Before {0} ***", PassID);
-  unwrapAndPrint(IR, Banner, llvm::forcePrintModuleIR());
+  unwrapAndPrint(dbgs(), IR, Banner, llvm::forcePrintModuleIR());
   return;
 }
 
@@ -241,7 +242,7 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
     popModuleDesc(PassID);
 
   SmallString<20> Banner = formatv("*** IR Dump After {0} ***", PassID);
-  unwrapAndPrint(IR, Banner, llvm::forcePrintModuleIR());
+  unwrapAndPrint(dbgs(), IR, Banner, llvm::forcePrintModuleIR());
 }
 
 void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
@@ -262,7 +263,7 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
 
   SmallString<20> Banner =
       formatv("*** IR Dump After {0} *** invalidated: ", PassID);
-  printIR(M, Banner, Extra);
+  printIR(dbgs(), M, Banner, Extra);
 }
 
 void PrintIRInstrumentation::registerCallbacks(
@@ -315,7 +316,7 @@ void PrintPassInstrumentation::registerCallbacks(
                "Unexpectedly skipping special pass");
 
         dbgs() << "Skipping pass: " << PassID << " on ";
-        unwrapAndPrint(IR, "", false, true);
+        unwrapAndPrint(dbgs(), IR, "", false, true);
       });
 
   PIC.registerBeforeNonSkippedPassCallback(
@@ -324,12 +325,12 @@ void PrintPassInstrumentation::registerCallbacks(
           return;
 
         dbgs() << "Running pass: " << PassID << " on ";
-        unwrapAndPrint(IR, "", false, true);
+        unwrapAndPrint(dbgs(), IR, "", false, true);
       });
 
   PIC.registerBeforeAnalysisCallback([](StringRef PassID, Any IR) {
     dbgs() << "Running analysis: " << PassID << " on ";
-    unwrapAndPrint(IR, "", false, true);
+    unwrapAndPrint(dbgs(), IR, "", false, true);
   });
 }
 


        


More information about the llvm-commits mailing list