[PATCH] D120993: [PassManager] Add pretty stack entries before P->run() call.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 4 06:07:02 PST 2022


fhahn created this revision.
fhahn added reviewers: asbirlea, aeubanks.
Herald added subscribers: dexonsmith, hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

This patch adds PrettyStackEntries before running passes. The entries
include the pass name and the IR unit the pass runs on.

The information is used the print additional information when a pass
crashes, including the name and a reference to the IR unit on which it
crashed. This is similar to the behavior of the legacy pass manager.

The improved stack trace now includes:

Stack dump:
0.	Program arguments: bin/opt -loop-vectorize -force-vector-width=4 crash.ll

1. Running pass 'ModuleToFunctionPassAdaptor' on module 'crash.ll'
2. Running pass 'LoopVectorizePass' on function '@a'


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120993

Files:
  llvm/include/llvm/IR/PassManager.h
  llvm/lib/IR/PassManager.cpp


Index: llvm/lib/IR/PassManager.cpp
===================================================================
--- llvm/lib/IR/PassManager.cpp
+++ llvm/lib/IR/PassManager.cpp
@@ -124,6 +124,7 @@
     PreservedAnalyses PassPA;
     {
       TimeTraceScope TimeScope(Pass->name(), F.getName());
+      NewPassManagerPrettyStackEntry StackEntry(Pass->name(), F);
       PassPA = Pass->run(F, FAM);
     }
 
@@ -152,3 +153,31 @@
 AnalysisSetKey CFGAnalyses::SetKey;
 
 AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
+
+void NewPassManagerPrettyStackEntry::print(raw_ostream &OS) const {
+  OS << "Running pass '" << PassName << "'";
+
+  if (M) {
+    OS << " on module '" << M->getModuleIdentifier() << "'.\n";
+    return;
+  }
+
+  if (MF) {
+    OS << " on machine function \n"; //'" << MF->getName() << "'.\n";
+    return;
+  }
+
+  assert(V && "Either M, MF or V must be set");
+
+  OS << " on ";
+  if (isa<Function>(V))
+    OS << "function";
+  else if (isa<BasicBlock>(V))
+    OS << "basic block";
+  else
+    OS << "value";
+
+  OS << " '";
+  V->printAsOperand(OS, /*PrintType=*/false);
+  OS << "'\n";
+}
Index: llvm/include/llvm/IR/PassManager.h
===================================================================
--- llvm/include/llvm/IR/PassManager.h
+++ llvm/include/llvm/IR/PassManager.h
@@ -37,6 +37,7 @@
 #ifndef LLVM_IR_PASSMANAGER_H
 #define LLVM_IR_PASSMANAGER_H
 
+#include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
@@ -451,6 +452,29 @@
 // header.
 class PassInstrumentationAnalysis;
 
+class MachineFunction;
+
+/// Add stack entry with the pass name and the IR unit it runs on.
+class NewPassManagerPrettyStackEntry : public PrettyStackTraceEntry {
+  StringRef PassName;
+  Module *M = nullptr;
+  MachineFunction *MF = nullptr;
+  Value *V = nullptr;
+
+public:
+  NewPassManagerPrettyStackEntry(StringRef PassName, Module &M)
+    : PassName(PassName), M(&M) {}
+
+  NewPassManagerPrettyStackEntry(StringRef PassName, MachineFunction &MF)
+    : PassName(PassName), MF(&MF) {}
+
+  NewPassManagerPrettyStackEntry(StringRef PassName, Value &V)
+    : PassName(PassName), V(&V) {}
+
+  /// print - Emit information about this stack frame to OS.
+  void print(raw_ostream &OS) const override;
+};
+
 /// Manages a sequence of passes over a particular unit of IR.
 ///
 /// A pass manager contains a sequence of passes to run over a particular unit
@@ -519,6 +543,7 @@
       PreservedAnalyses PassPA;
       {
         TimeTraceScope TimeScope(P->name(), IR.getName());
+        NewPassManagerPrettyStackEntry StackEntry(P->name(), IR);
         PassPA = P->run(IR, AM, ExtraArgs...);
       }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120993.412991.patch
Type: text/x-patch
Size: 2709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220304/94168341/attachment.bin>


More information about the llvm-commits mailing list