[PM] Add names and debug logging for analysis passes to the new pass
authorChandler Carruth <chandlerc@gmail.com>
Mon, 5 Jan 2015 12:21:44 +0000 (12:21 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Mon, 5 Jan 2015 12:21:44 +0000 (12:21 +0000)
manager.

This starts to allow us to test analyses more easily, but it's really
only the beginning. Some of the code here is still untestable without
manual changes to create analysis passes, but I wanted to factor it into
a small of chunks as possible.

Next up in order to be able to test things are, in no particular order:
- No-op analyses passes so we don't have to use real ones to exercise
  the pass maneger itself.
- Automatic way of generating dummy passes that require an analysis be
  run, including a variant that calls a 'print' method on a pass to make
  it even easier to print out the results of an analysis.
- Dummy passes that invalidate all analyses for their IR unit so we can
  test invalidation and re-runs.
- Automatic way to print each analysis pass as it is re-run.
- Automatic but optional verification of analysis passes everywhere
  possible.

I'm not claiming I'll get to all of these immediately, but that's what
is in the pipeline at some stage. I'm fleshing out exactly what I need
and what to prioritize by working on converting analyses and then trying
to test the conversion. =]

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

include/llvm/Analysis/CGSCCPassManager.h
include/llvm/Analysis/LazyCallGraph.h
include/llvm/IR/PassManager.h
include/llvm/IR/PassManagerInternal.h
lib/Analysis/CGSCCPassManager.cpp
lib/IR/PassManager.cpp
test/Other/new-pass-manager.ll
unittests/IR/PassManagerTest.cpp

index 7ddaf05604b85dd1cadffd0ecc8428bb54c172d1..70b3ea9bad49fab0f8884db3527aa0b08250cace 100644 (file)
@@ -195,6 +195,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "CGSCCAnalysisManagerModuleProxy"; }
+
   explicit CGSCCAnalysisManagerModuleProxy(CGSCCAnalysisManager &CGAM)
       : CGAM(&CGAM) {}
   // We have to explicitly define all the special member functions because MSVC
@@ -265,6 +267,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "ModuleAnalysisManagerCGSCCProxy"; }
+
   ModuleAnalysisManagerCGSCCProxy(const ModuleAnalysisManager &MAM)
       : MAM(&MAM) {}
   // We have to explicitly define all the special member functions because MSVC
@@ -417,6 +421,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "FunctionAnalysisManagerCGSCCProxy"; }
+
   explicit FunctionAnalysisManagerCGSCCProxy(FunctionAnalysisManager &FAM)
       : FAM(&FAM) {}
   // We have to explicitly define all the special member functions because MSVC
@@ -487,6 +493,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "CGSCCAnalysisManagerFunctionProxy"; }
+
   CGSCCAnalysisManagerFunctionProxy(const CGSCCAnalysisManager &CGAM)
       : CGAM(&CGAM) {}
   // We have to explicitly define all the special member functions because MSVC
index 4008bb71643864e2c0b5d629e8151c18862059fd..f5697dfe054add2ae7bbc4c483c2bceafb993ab4 100644 (file)
@@ -252,6 +252,12 @@ public:
     /// \brief Test if this SCC is a descendant of \a C.
     bool isDescendantOf(const SCC &C) const;
 
+    /// \brief Short name useful for debugging or logging.
+    ///
+    /// We use the name of the first function in the SCC to name the SCC for
+    /// the purposes of debugging and logging.
+    StringRef getName() const { return (*begin())->getFunction().getName(); }
+
     ///@{
     /// \name Mutation API
     ///
@@ -537,6 +543,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "Lazy CallGraph Analysis"; }
+
   /// \brief Compute the \c LazyCallGraph for the module \c M.
   ///
   /// This just builds the set of entry points to the call graph. The rest is
index db792efca362479a876b7fa171d91ac69f3e5b9f..b2bf73ffe8b4b5d5af93bd41ad31fefe3a82cf60 100644 (file)
@@ -557,6 +557,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "FunctionAnalysisManagerModuleProxy"; }
+
   explicit FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM)
       : FAM(&FAM) {}
   // We have to explicitly define all the special member functions because MSVC
@@ -663,6 +665,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "ModuleAnalysisManagerFunctionProxy"; }
+
   ModuleAnalysisManagerFunctionProxy(const ModuleAnalysisManager &MAM)
       : MAM(&MAM) {}
   // We have to explicitly define all the special member functions because MSVC
index fb9c210dba29f386a623d05cbee8f5135edf3271..1b43a72c408cfece3df22a18df906d36abebb857 100644 (file)
@@ -256,6 +256,9 @@ struct AnalysisPassConcept {
   /// users.
   virtual std::unique_ptr<AnalysisResultConcept<IRUnitT>>
   run(IRUnitT IR, AnalysisManagerT *AM) = 0;
+
+  /// \brief Polymorphic method to access the name of a pass.
+  virtual StringRef name() = 0;
 };
 
 /// \brief Wrapper to model the analysis pass concept.
@@ -299,6 +302,11 @@ struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, true>
     return make_unique<ResultModelT>(Pass.run(IR, AM));
   }
 
+  /// \brief The model delegates to a static \c PassT::name method.
+  ///
+  /// The returned string ref must point to constant immutable data!
+  StringRef name() override { return PassT::name(); }
+
   PassT Pass;
 };
 
@@ -333,6 +341,11 @@ struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, false>
     return make_unique<ResultModelT>(Pass.run(IR));
   }
 
+  /// \brief The model delegates to a static \c PassT::name method.
+  ///
+  /// The returned string ref must point to constant immutable data!
+  StringRef name() override { return PassT::name(); }
+
   PassT Pass;
 };
 
index 7be0afb70b4a546ce93c7408ce7db5578bfdc33e..4d569a972fe999ab5e3dc786f033297590c6fd26 100644 (file)
@@ -84,11 +84,18 @@ void CGSCCAnalysisManager::invalidateImpl(void *PassID, LazyCallGraph::SCC &C) {
   if (RI == CGSCCAnalysisResults.end())
     return;
 
+  if (DebugPM)
+    dbgs() << "Invalidating CGSCC analysis: " << lookupPass(PassID).name()
+           << "\n";
   CGSCCAnalysisResultLists[&C].erase(RI->second);
 }
 
 void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC &C,
                                           const PreservedAnalyses &PA) {
+  if (DebugPM)
+    dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName()
+           << "\n";
+
   // Clear all the invalidated results associated specifically with this
   // function.
   SmallVector<void *, 8> InvalidatedPassIDs;
@@ -97,6 +104,10 @@ void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC &C,
                                           E = ResultsList.end();
        I != E;)
     if (I->second->invalidate(C, PA)) {
+      if (DebugPM)
+        dbgs() << "Invalidating CGSCC analysis: " << lookupPass(I->first).name()
+               << "\n";
+
       InvalidatedPassIDs.push_back(I->first);
       I = ResultsList.erase(I);
     } else {
index 121ec001fd1b96ceebeeefdfad8a9ee7265bde82..c7638bb7fce3f7da5944debf6d655ea1c7331a11 100644 (file)
@@ -52,8 +52,12 @@ ModuleAnalysisManager::getResultImpl(void *PassID, Module &M) {
 
   // If we don't have a cached result for this module, look up the pass and run
   // it to produce a result, which we then add to the cache.
-  if (Inserted)
-    RI->second = lookupPass(PassID).run(M, this);
+  if (Inserted) {
+    auto &P = lookupPass(PassID);
+    if (DebugPM)
+      dbgs() << "Running module analysis: " << P.name() << "\n";
+    RI->second = P.run(M, this);
+  }
 
   return *RI->second;
 }
@@ -66,18 +70,30 @@ ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module &M) const {
 }
 
 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module &M) {
+  if (DebugPM)
+    dbgs() << "Invalidating module analysis: " << lookupPass(PassID).name()
+           << "\n";
   ModuleAnalysisResults.erase(PassID);
 }
 
 void ModuleAnalysisManager::invalidateImpl(Module &M,
                                            const PreservedAnalyses &PA) {
+  if (DebugPM)
+    dbgs() << "Invalidating all non-preserved analyses for module: "
+           << M.getModuleIdentifier() << "\n";
+
   // FIXME: This is a total hack based on the fact that erasure doesn't
   // invalidate iteration for DenseMap.
   for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
                                           E = ModuleAnalysisResults.end();
        I != E; ++I)
-    if (I->second->invalidate(M, PA))
+    if (I->second->invalidate(M, PA)) {
+      if (DebugPM)
+        dbgs() << "Invalidating module analysis: "
+               << lookupPass(I->first).name() << "\n";
+
       ModuleAnalysisResults.erase(I);
+    }
 }
 
 PreservedAnalyses FunctionPassManager::run(Function &F,
@@ -128,8 +144,11 @@ FunctionAnalysisManager::getResultImpl(void *PassID, Function &F) {
   // If we don't have a cached result for this function, look up the pass and
   // run it to produce a result, which we then add to the cache.
   if (Inserted) {
+    auto &P = lookupPass(PassID);
+    if (DebugPM)
+      dbgs() << "Running function analysis: " << P.name() << "\n";
     FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[&F];
-    ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
+    ResultList.emplace_back(PassID, P.run(F, this));
     RI->second = std::prev(ResultList.end());
   }
 
@@ -149,11 +168,18 @@ void FunctionAnalysisManager::invalidateImpl(void *PassID, Function &F) {
   if (RI == FunctionAnalysisResults.end())
     return;
 
+  if (DebugPM)
+    dbgs() << "Invalidating function analysis: " << lookupPass(PassID).name()
+           << "\n";
   FunctionAnalysisResultLists[&F].erase(RI->second);
 }
 
 void FunctionAnalysisManager::invalidateImpl(Function &F,
                                              const PreservedAnalyses &PA) {
+  if (DebugPM)
+    dbgs() << "Invalidating all non-preserved analyses for function: "
+           << F.getName() << "\n";
+
   // Clear all the invalidated results associated specifically with this
   // function.
   SmallVector<void *, 8> InvalidatedPassIDs;
@@ -162,6 +188,10 @@ void FunctionAnalysisManager::invalidateImpl(Function &F,
                                              E = ResultsList.end();
        I != E;)
     if (I->second->invalidate(F, PA)) {
+      if (DebugPM)
+        dbgs() << "Invalidating function analysis: "
+               << lookupPass(I->first).name() << "\n";
+
       InvalidatedPassIDs.push_back(I->first);
       I = ResultsList.erase(I);
     } else {
index 2c565e6944cdbf159e9c18e60e4a3c7ba660a091..f9fc444dbf10f76376975c373013f560c0e96bd7 100644 (file)
@@ -28,6 +28,8 @@
 ; RUN:     | FileCheck %s --check-prefix=CHECK-FUNCTION-PRINT
 ; CHECK-FUNCTION-PRINT: Starting module pass manager
 ; CHECK-FUNCTION-PRINT: Running module pass: VerifierPass
+; CHECK-FUNCTION-PRINT: Running module pass: ModuleToFunctionPassAdaptor
+; CHECK-FUNCTION-PRINT: Running module analysis: FunctionAnalysisManagerModuleProxy
 ; CHECK-FUNCTION-PRINT: Starting function pass manager
 ; CHECK-FUNCTION-PRINT: Running function pass: PrintFunctionPass
 ; CHECK-FUNCTION-PRINT-NOT: ModuleID
 ; CHECK-NO-VERIFY-NOT: VerifierPass
 ; CHECK-NO-VERIFY: Finished module pass manager
 
+; RUN: opt -disable-output -debug-pass-manager -debug-cgscc-pass-manager -passes='cgscc(no-op-cgscc)' %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=CHECK-LCG-ANALYSIS
+; CHECK-LCG-ANALYSIS: Starting module pass manager
+; CHECK-LCG-ANALYSIS: Running module pass: ModuleToPostOrderCGSCCPassAdaptor
+; CHECK-LCG-ANALYSIS: Running module analysis: CGSCCAnalysisManagerModuleProxy
+; CHECK-LCG-ANALYSIS: Running module analysis: Lazy CallGraph Analysis
+; CHECK-LCG-ANALYSIS: Starting CGSCC pass manager run.
+
 define void @foo() {
   ret void
 }
index d641e36ac6ad7722c632468ba595898e3c3fb488..41af0b0bd25c4165b98b34b4e4a3679619a8a765 100644 (file)
@@ -29,6 +29,9 @@ public:
   /// \brief Returns an opaque, unique ID for this pass type.
   static void *ID() { return (void *)&PassID; }
 
+  /// \brief Returns the name of the analysis.
+  static StringRef name() { return "TestFunctionAnalysis"; }
+
   TestFunctionAnalysis(int &Runs) : Runs(Runs) {}
 
   /// \brief Run the analysis pass over the function and return a result.
@@ -60,6 +63,8 @@ public:
 
   static void *ID() { return (void *)&PassID; }
 
+  static StringRef name() { return "TestModuleAnalysis"; }
+
   TestModuleAnalysis(int &Runs) : Runs(Runs) {}
 
   Result run(Module &M, ModuleAnalysisManager *AM) {