[llvm] Add option to dump IR to files intstead of stderr (PR #66412)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 11:40:48 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir
            
<details>
<summary>Changes</summary>
This patch adds a flag to LLVM such that the output generated by the `-print-(before|after|all)`
family of flags is written to files in a directory rather than to stderr.

This new flag is `-ir-dump-directory` and is used to specify where to write the files. No other flags are added, it just modifies the behavior of the print flags.

This is a second simplified version of the changes proposed in https://github.com/llvm/llvm-project/pull/65179.

This patch only adds support for the new pass manager. If this patch is accepted, similar support can be added to the legacy pass manager.
--
Full diff: https://github.com/llvm/llvm-project/pull/66412.diff

5 Files Affected:

- (modified) llvm/include/llvm/IR/PrintPasses.h (+4) 
- (modified) llvm/include/llvm/Passes/StandardInstrumentations.h (+17-3) 
- (modified) llvm/lib/IR/PrintPasses.cpp (+9) 
- (modified) llvm/lib/Passes/StandardInstrumentations.cpp (+144-23) 
- (added) llvm/test/Other/dump-before-after.ll (+57) 


<pre>
diff --git a/llvm/include/llvm/IR/PrintPasses.h b/llvm/include/llvm/IR/PrintPasses.h
index 95b97e76c867cb2..c4baadfa3975531 100644
--- a/llvm/include/llvm/IR/PrintPasses.h
+++ b/llvm/include/llvm/IR/PrintPasses.h
@@ -55,6 +55,10 @@ bool forcePrintModuleIR();
 bool isPassInPrintList(StringRef PassName);
 bool isFilterPassesEmpty();
 
+// Returns a non-empty string if printing before/after passes is to be
+// dumped into files in the returned directory instead of written to stderr.
+std::string irDumpDirectory();
+
 // Returns true if we should print the function.
 bool isFunctionInPrintList(StringRef FunctionName);
 
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 331130c6b22d990..fd512635356e5dd 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -46,6 +46,16 @@ class PrintIRInstrumentation {
   void registerCallbacks(PassInstrumentationCallbacks &amp;PIC);
 
 private:
+  enum SuffixType {
+    before,
+    after,
+    invalidated,
+  };
+
+  using PrintModuleDesc = std::tuple&lt;const Module *, std::string /* IRName */,
+                                     StringRef /* StoredPassID */,
+                                     SmallString&lt;128&gt; /* DumpFilename */&gt;;
+
   void printBeforePass(StringRef PassID, Any IR);
   void printAfterPass(StringRef PassID, Any IR);
   void printAfterPassInvalidated(StringRef PassID);
@@ -55,11 +65,15 @@ class PrintIRInstrumentation {
   bool shouldPrintPassNumbers();
   bool shouldPrintAtPassNumber();
 
-  using PrintModuleDesc = std::tuple&lt;const Module *, std::string, StringRef&gt;;
-
-  void pushModuleDesc(StringRef PassID, Any IR);
+  void pushModuleDesc(StringRef PassID, Any IR,
+                      SmallString&lt;128&gt; DumpIRFilename);
   PrintModuleDesc popModuleDesc(StringRef PassID);
 
+  SmallString&lt;128&gt; fetchDumpFilename(StringRef PassId, Any IR);
+  StringRef getFileSuffix(SuffixType);
+
+  static constexpr std::array FileSuffixes = {&quot;-before.ll&quot;, &quot;-after.ll&quot;,
+                                              &quot;-invalidated.ll&quot;};
   PassInstrumentationCallbacks *PIC;
   /// Stack of Module description, enough to print the module after a given
   /// pass.
diff --git a/llvm/lib/IR/PrintPasses.cpp b/llvm/lib/IR/PrintPasses.cpp
index e2ef20bb81ba7d7..406af4a0a5e004e 100644
--- a/llvm/lib/IR/PrintPasses.cpp
+++ b/llvm/lib/IR/PrintPasses.cpp
@@ -103,6 +103,13 @@ static cl::list&lt;std::string&gt;
                             &quot;options&quot;),
                    cl::CommaSeparated, cl::Hidden);
 
+static cl::opt&lt;std::string&gt; IRDumpDirectory(
+    &quot;ir-dump-directory&quot;,
+    llvm::cl::desc(&quot;If specified, IR printed using the &quot;
+                   &quot;-print-[before|after]{-all} options will be dumped into &quot;
+                   &quot;files in this directory rather than written to stderr&quot;),
+    cl::init(&quot;&quot;), cl::Hidden, cl::value_desc(&quot;filename&quot;));
+
 /// This is a helper to determine whether to print IR before or
 /// after a pass.
 
@@ -139,6 +146,8 @@ std::vector&lt;std::string&gt; llvm::printAfterPasses() {
   return std::vector&lt;std::string&gt;(PrintAfter);
 }
 
+std::string llvm::irDumpDirectory() { return IRDumpDirectory; }
+
 bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
 
 bool llvm::isPassInPrintList(StringRef PassName) {
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 6244c0a5a949ba1..64bf306438d99d2 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -14,6 +14,7 @@
 
 #include &quot;llvm/Passes/StandardInstrumentations.h&quot;
 #include &quot;llvm/ADT/Any.h&quot;
+#include &quot;llvm/ADT/Hashing.h&quot;
 #include &quot;llvm/ADT/StringRef.h&quot;
 #include &quot;llvm/Analysis/CallGraphSCCPass.h&quot;
 #include &quot;llvm/Analysis/LazyCallGraph.h&quot;
@@ -33,6 +34,7 @@
 #include &quot;llvm/Support/FormatVariadic.h&quot;
 #include &quot;llvm/Support/GraphWriter.h&quot;
 #include &quot;llvm/Support/MemoryBuffer.h&quot;
+#include &quot;llvm/Support/Path.h&quot;
 #include &quot;llvm/Support/Program.h&quot;
 #include &quot;llvm/Support/Regex.h&quot;
 #include &quot;llvm/Support/Signals.h&quot;
@@ -684,9 +686,64 @@ PrintIRInstrumentation::~PrintIRInstrumentation() {
   assert(ModuleDescStack.empty() &amp;&amp; &quot;ModuleDescStack is not empty at exit&quot;);
 }
 
-void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
+static SmallString&lt;32&gt; getIRDisplayName(Any IR) {
+
+  auto hashName = [](StringRef name) {
+    const size_t hashValue = hash_value(name);
+    return std::to_string(hashValue);
+  };
+
+  SmallString&lt;32&gt; Result;
+  const Module *M = unwrapModule(IR);
+  std::string ModuleName = hashName(M-&gt;getName());
+  SmallString&lt;32&gt; IRName;
+  if (any_cast&lt;const Module *&gt;(&amp;IR)) {
+    IRName += &quot;-module&quot;;
+  } else if (const Function **F = any_cast&lt;const Function *&gt;(&amp;IR)) {
+    IRName += &quot;-function-&quot;;
+    IRName += hashName((*F)-&gt;getName());
+  } else if (const LazyCallGraph::SCC **C =
+                 any_cast&lt;const LazyCallGraph::SCC *&gt;(&amp;IR)) {
+    IRName += &quot;-scc-&quot;;
+    IRName += hashName((*C)-&gt;getName());
+  } else if (const Loop **L = any_cast&lt;const Loop *&gt;(&amp;IR)) {
+    IRName += &quot;-loop-&quot;;
+    IRName += hashName((*L)-&gt;getName());
+  } else {
+    llvm_unreachable(&quot;Unknown wrapped IR type&quot;);
+  }
+
+  Result += ModuleName;
+  Result += IRName;
+  return Result;
+}
+
+SmallString&lt;128&gt; PrintIRInstrumentation::fetchDumpFilename(StringRef PassID,
+                                                           Any IR) {
+  const std::string &amp;RootDirectory = irDumpDirectory();
+  assert(!RootDirectory.empty() &amp;&amp;
+         &quot;The flag -ir-dump-directory must be passed to dump IR to files&quot;);
+  SmallString&lt;128&gt; ResultPath;
+  ResultPath += RootDirectory;
+  SmallString&lt;16&gt; Filename;
+  Filename += std::to_string(CurrentPassNumber);
+  Filename += &quot;-&quot;;
+  Filename += getIRDisplayName(IR);
+  Filename += &quot;-&quot;;
+  Filename += PassID;
+  sys::path::append(ResultPath, Filename);
+  return ResultPath;
+}
+
+StringRef
+PrintIRInstrumentation::getFileSuffix(PrintIRInstrumentation::SuffixType Type) {
+  return FileSuffixes[Type];
+}
+
+void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR,
+                                            SmallString&lt;128&gt; DumpIRFilename) {
   const Module *M = unwrapModule(IR);
-  ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
+  ModuleDescStack.emplace_back(M, getIRName(IR), PassID, DumpIRFilename);
 }
 
 PrintIRInstrumentation::PrintModuleDesc
@@ -697,17 +754,42 @@ PrintIRInstrumentation::popModuleDesc(StringRef PassID) {
   return ModuleDesc;
 }
 
+// Callers are responsible for closing the returned file descriptor
+static int prepareDumpIRFileDescriptor(StringRef DumpIRFilename) {
+  std::error_code EC;
+  auto ParentPath = llvm::sys::path::parent_path(DumpIRFilename);
+  if (!ParentPath.empty()) {
+    std::error_code EC = llvm::sys::fs::create_directories(ParentPath);
+    if (EC)
+      report_fatal_error(Twine(&quot;Failed to create directory &quot;) + ParentPath +
+                         &quot; to support -ir-dump-directory: &quot; + EC.message());
+  }
+  int Result = 0;
+  EC =
+      sys::fs::openFile(DumpIRFilename, Result, sys::fs::CD_OpenAlways,
+                        sys::fs::FA_Write | sys::fs::FA_Read, sys::fs::OF_None);
+  if (EC)
+    report_fatal_error(Twine(&quot;Failed to open &quot;) + DumpIRFilename +
+                       &quot; to support -ir-dump-directory: &quot; + EC.message());
+  return Result;
+}
+
 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
   if (isIgnored(PassID))
     return;
 
+  SmallString&lt;128&gt; DumpIRFilename;
+  if (!irDumpDirectory().empty() &amp;&amp;
+      (shouldPrintBeforePass(PassID) || shouldPrintAfterPass(PassID)))
+    DumpIRFilename = fetchDumpFilename(PassID, IR);
+
   // Saving Module for AfterPassInvalidated operations.
   // Note: here we rely on a fact that we do not change modules while
   // traversing the pipeline, so the latest captured module is good
   // for all print operations that has not happen yet.
   if (shouldPrintPassNumbers() || shouldPrintAtPassNumber() ||
       shouldPrintAfterPass(PassID))
-    pushModuleDesc(PassID, IR);
+    pushModuleDesc(PassID, IR, DumpIRFilename);
 
   if (!shouldPrintIR(IR))
     return;
@@ -720,9 +802,20 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
   if (!shouldPrintBeforePass(PassID))
     return;
 
-  dbgs() &lt;&lt; &quot;*** IR Dump Before &quot; &lt;&lt; PassID &lt;&lt; &quot; on &quot; &lt;&lt; getIRName(IR)
-         &lt;&lt; &quot; ***\n&quot;;
-  unwrapAndPrint(dbgs(), IR);
+  auto WriteIRToStream = [&amp;](raw_ostream &amp;Stream) {
+    Stream &lt;&lt; &quot;*** IR Dump Before &quot; &lt;&lt; PassID &lt;&lt; &quot; on &quot; &lt;&lt; getIRName(IR)
+           &lt;&lt; &quot; ***\n&quot;;
+    unwrapAndPrint(Stream, IR);
+  };
+
+  if (!DumpIRFilename.empty()) {
+    DumpIRFilename += getFileSuffix(SuffixType::before);
+    llvm::raw_fd_ostream DumpIRFileStream{
+        prepareDumpIRFileDescriptor(DumpIRFilename), /* shouldClose */ true};
+    WriteIRToStream(DumpIRFileStream);
+  } else {
+    WriteIRToStream(dbgs());
+  }
 }
 
 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
@@ -736,18 +829,32 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
   const Module *M;
   std::string IRName;
   StringRef StoredPassID;
-  std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
+  SmallString&lt;128&gt; DumpIRFilename;
+  std::tie(M, IRName, StoredPassID, DumpIRFilename) = popModuleDesc(PassID);
   assert(StoredPassID == PassID &amp;&amp; &quot;mismatched PassID&quot;);
 
   if (!shouldPrintIR(IR) || !shouldPrintAfterPass(PassID))
     return;
 
-  dbgs() &lt;&lt; &quot;*** IR Dump &quot;
-         &lt;&lt; (shouldPrintAtPassNumber()
-                 ? StringRef(formatv(&quot;At {0}-{1}&quot;, CurrentPassNumber, PassID))
-                 : StringRef(formatv(&quot;After {0}&quot;, PassID)))
-         &lt;&lt; &quot; on &quot; &lt;&lt; IRName &lt;&lt; &quot; ***\n&quot;;
-  unwrapAndPrint(dbgs(), IR);
+  auto WriteIRToStream = [&amp;](raw_ostream &amp;Stream) {
+    Stream &lt;&lt; &quot;*** IR Dump &quot;
+           &lt;&lt; (shouldPrintAtPassNumber()
+                   ? StringRef(formatv(&quot;At {0}-{1}&quot;, CurrentPassNumber, PassID))
+                   : StringRef(formatv(&quot;After {0}&quot;, PassID)))
+           &lt;&lt; &quot; on &quot; &lt;&lt; IRName &lt;&lt; &quot; ***\n&quot;;
+    unwrapAndPrint(Stream, IR);
+  };
+
+  if (!irDumpDirectory().empty()) {
+    assert(!DumpIRFilename.empty() &amp;&amp; &quot;DumpIRFilename must not be empty and &quot;
+                                      &quot;should be set in printBeforePass&quot;);
+    DumpIRFilename += getFileSuffix(SuffixType::after);
+    llvm::raw_fd_ostream DumpIRFileStream{
+        prepareDumpIRFileDescriptor(DumpIRFilename), /* shouldClose */ true};
+    WriteIRToStream(DumpIRFileStream);
+  } else {
+    WriteIRToStream(dbgs());
+  }
 }
 
 void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
@@ -761,22 +868,36 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
   const Module *M;
   std::string IRName;
   StringRef StoredPassID;
-  std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
+  SmallString&lt;128&gt; DumpIRFilename;
+  std::tie(M, IRName, StoredPassID, DumpIRFilename) = popModuleDesc(PassID);
   assert(StoredPassID == PassID &amp;&amp; &quot;mismatched PassID&quot;);
   // Additional filtering (e.g. -filter-print-func) can lead to module
   // printing being skipped.
   if (!M || !shouldPrintAfterPass(PassID))
     return;
 
-  SmallString&lt;20&gt; Banner;
-  if (shouldPrintAtPassNumber())
-    Banner = formatv(&quot;*** IR Dump At {0}-{1} on {2} (invalidated) ***&quot;,
-                     CurrentPassNumber, PassID, IRName);
-  else 
-    Banner = formatv(&quot;*** IR Dump After {0} on {1} (invalidated) ***&quot;, 
-                     PassID, IRName);
-  dbgs() &lt;&lt; Banner &lt;&lt; &quot;\n&quot;;
-  printIR(dbgs(), M);
+  auto WriteIRToStream = [&amp;](raw_ostream &amp;Stream) {
+    SmallString&lt;20&gt; Banner;
+    if (shouldPrintAtPassNumber())
+      Banner = formatv(&quot;*** IR Dump At {0}-{1} on {2} (invalidated) ***&quot;,
+                       CurrentPassNumber, PassID, IRName);
+    else
+      Banner = formatv(&quot;*** IR Dump After {0} on {1} (invalidated) ***&quot;, PassID,
+                       IRName);
+    Stream &lt;&lt; Banner &lt;&lt; &quot;\n&quot;;
+    printIR(Stream, M);
+  };
+
+  if (!irDumpDirectory().empty()) {
+    assert(!DumpIRFilename.empty() &amp;&amp; &quot;DumpIRFilename must not be empty and &quot;
+                                      &quot;should be set in printBeforePass&quot;);
+    DumpIRFilename += getFileSuffix(SuffixType::invalidated);
+    llvm::raw_fd_ostream DumpIRFileStream{
+        prepareDumpIRFileDescriptor(DumpIRFilename), /* shouldClose */ true};
+    WriteIRToStream(DumpIRFileStream);
+  } else {
+    WriteIRToStream(dbgs());
+  }
 }
 
 bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
diff --git a/llvm/test/Other/dump-before-after.ll b/llvm/test/Other/dump-before-after.ll
new file mode 100644
index 000000000000000..1f0d01022551123
--- /dev/null
+++ b/llvm/test/Other/dump-before-after.ll
@@ -0,0 +1,57 @@
+; RUN: mkdir -p %t/logs
+; RUN: rm -rf %t/logs
+
+; Basic dump before and after a single module pass
+
+; RUN: opt %s -disable-output -passes=&#x27;no-op-module&#x27; -ir-dump-directory %t/logs -print-after=no-op-module -print-before=no-op-module
+; RUN: find %t/logs -type f -print | sort | FileCheck %s --check-prefix=SINGLE-PASS
+; SINGLE-PASS: {{[0-9]+}}-[[MODULE_NAME_HASH:[0-9]+]]-module-NoOpModulePass-after.ll
+; SINGLE-PASS: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-before.ll
+; RUN: rm -rf %t/logs
+
+
+; Dump before and after multiple runs of the same module pass
+; The integers preceeding log files represent relative pass execution order,
+; but they are not necessarily continuous. That is passes which are run
+; but not printed, still increment the count -- leading to gaps in the printed
+; integers.
+
+; RUN: opt %s -disable-output -passes=&#x27;no-op-module,no-op-module,no-op-module&#x27; -ir-dump-directory %t/logs -print-after=no-op-module -print-before=no-op-module
+; RUN: find %t/logs -type f -print | sort | FileCheck %s --check-prefix=MULTIPLE-PASSES
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH:[0-9]+]]-module-NoOpModulePass-after.ll
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-before.ll
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-after.ll
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-before.ll
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-after.ll
+; MULTIPLE-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-before.ll
+; RUN: rm -rf %t/logs
+
+; Dump before and after multiple passes, of various levels of granularity
+
+; RUN: opt %s -disable-output -passes=&#x27;no-op-module,cgscc(no-op-cgscc),function(no-op-function),function(loop(no-op-loop))&#x27; -ir-dump-directory %t/logs -print-after=no-op-module,no-op-cgscc,no-op-function,no-op-loop -print-before=no-op-module,no-op-cgscc,no-op-function,no-op-loop
+; RUN: find %t/logs -type f -print | sort | FileCheck %s --check-prefix=MULTIPLE-GRANULAR-PASSES
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH:[0-9]+]]-module-NoOpModulePass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-module-NoOpModulePass-before.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-scc-[[SCC_FOO_HASH:[0-9]+]]-NoOpCGSCCPass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-scc-[[SCC_FOO_HASH]]-NoOpCGSCCPass-before.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-scc-[[SCC_BAR_HASH:[0-9]+]]-NoOpCGSCCPass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-scc-[[SCC_BAR_HASH]]-NoOpCGSCCPass-before.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-function-[[FUNCTION_FOO_HASH:[0-9]+]]-NoOpFunctionPass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-function-[[FUNCTION_FOO_HASH]]-NoOpFunctionPass-before.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-function-[[FUNCTION_BAR_HASH:[0-9]+]]-NoOpFunctionPass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-function-[[FUNCTION_BAR_HASH]]-NoOpFunctionPass-before.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-loop-[[LOOP_NAME_HASH:[0-9]+]]-NoOpLoopPass-after.ll
+; MULTIPLE-GRANULAR-PASSES: {{[0-9]+}}-[[MODULE_NAME_HASH]]-loop-[[LOOP_NAME_HASH]]-NoOpLoopPass-before.ll
+
+; RUN: rm -rf %t/logs
+
+define void @foo() {
+    ret void
+}
+
+define void @bar() {
+entry:
+    br label %my-loop
+my-loop:
+    br label %my-loop
+}
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66412


More information about the llvm-commits mailing list