[compiler-rt] Add flags to dump IR to a file before and after LLVM passes (PR #65179)

Nuri Amari via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 3 11:00:14 PDT 2023


================
@@ -830,6 +831,182 @@ void PrintIRInstrumentation::registerCallbacks(
   }
 }
 
+void DumpIRInstrumentation::registerCallbacks(
+    PassInstrumentationCallbacks &PIC) {
+
+  if (!(shouldDumpBeforeSomePass() || shouldDumpAfterSomePass()))
+    return;
+
+  this->PIC = &PIC;
+
+  PIC.registerBeforeNonSkippedPassCallback(
+      [this](StringRef P, Any IR) { this->pushPass(P, IR); });
+
+  if (shouldDumpBeforeSomePass())
+    PIC.registerBeforeNonSkippedPassCallback(
+        [this](StringRef P, Any IR) { this->dumpBeforePass(P, IR); });
+
+  if (shouldDumpAfterSomePass()) {
+    PIC.registerAfterPassCallback(
+        [this](StringRef P, Any IR, const PreservedAnalyses &) {
+          this->dumpAfterPass(P, IR);
+        });
+  }
+
+  // It is important the the "popPass" callback fires after the dumpAfterPass
+  // callback
+  PIC.registerAfterPassCallback(
+      [this](StringRef P, Any IR, const PreservedAnalyses &) {
+        this->popPass(P);
+      });
+}
+
+void DumpIRInstrumentation::dumpBeforePass(StringRef PassID, Any IR) {
+  if (isIgnored(PassID))
----------------
NuriAmari wrote:

What about these lines:

```
SmallString<16> OutputPath =
      fetchCurrentInstrumentationDumpFile("-before.ll");
  std::error_code EC;
  llvm::raw_fd_ostream OS(OutputPath, EC, llvm::sys::fs::CD_CreateAlways);
  if (EC)
    report_fatal_error(Twine("Failed to open ") + OutputPath +
                       " to support dump-before: " + EC.message());

  OS << "*** IR Dump Before " << PassID << " on " << getIRName(IR) << " ***\n";
  unwrapAndPrint(OS, IR);
```

This method is responsible for printing the IR to a file before a pass runs.

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


More information about the llvm-commits mailing list