diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1272,10 +1272,10 @@ PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS; PTO.Coroutines = LangOpts.Coroutines; - LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager); - FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager); - CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager); - ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager); + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; PassInstrumentationCallbacks PIC; StandardInstrumentations SI(CodeGenOpts.DebugPassManager); diff --git a/llvm/include/llvm/CodeGen/MachinePassManager.h b/llvm/include/llvm/CodeGen/MachinePassManager.h --- a/llvm/include/llvm/CodeGen/MachinePassManager.h +++ b/llvm/include/llvm/CodeGen/MachinePassManager.h @@ -40,11 +40,10 @@ public: using Base = AnalysisManager; - MachineFunctionAnalysisManager() : Base(false), FAM(nullptr), MAM(nullptr) {} + MachineFunctionAnalysisManager() : Base(), FAM(nullptr), MAM(nullptr) {} MachineFunctionAnalysisManager(FunctionAnalysisManager &FAM, - ModuleAnalysisManager &MAM, - bool DebugLogging = false) - : Base(DebugLogging), FAM(&FAM), MAM(&MAM) {} + ModuleAnalysisManager &MAM) + : Base(), FAM(&FAM), MAM(&MAM) {} MachineFunctionAnalysisManager(MachineFunctionAnalysisManager &&) = default; MachineFunctionAnalysisManager & operator=(MachineFunctionAnalysisManager &&) = default; diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h --- a/llvm/include/llvm/IR/PassInstrumentation.h +++ b/llvm/include/llvm/IR/PassInstrumentation.h @@ -81,6 +81,8 @@ using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &); using BeforeAnalysisFunc = void(StringRef, Any); using AfterAnalysisFunc = void(StringRef, Any); + using AnalysisInvalidatedFunc = void(StringRef, Any); + using AnalysesClearedFunc = void(StringRef); public: PassInstrumentationCallbacks() {} @@ -123,6 +125,16 @@ AfterAnalysisCallbacks.emplace_back(std::move(C)); } + template + void registerAnalysisInvalidatedCallback(CallableT C) { + AnalysisInvalidatedCallbacks.emplace_back(std::move(C)); + } + + template + void registerAnalysesClearedCallback(CallableT C) { + AnalysesClearedCallbacks.emplace_back(std::move(C)); + } + /// Add a class name to pass name mapping for use by pass instrumentation. void addClassToPassName(StringRef ClassName, StringRef PassName); /// Get the pass name for a given pass class name. @@ -152,6 +164,12 @@ /// These are run on analyses that have been run. SmallVector, 4> AfterAnalysisCallbacks; + /// These are run on analyses that have been invalidated. + SmallVector, 4> + AnalysisInvalidatedCallbacks; + /// These are run on analyses that have been cleared. + SmallVector, 4> + AnalysesClearedCallbacks; StringMap ClassToPassName; }; @@ -256,6 +274,24 @@ C(Analysis.name(), llvm::Any(&IR)); } + /// AnalysisInvalidated instrumentation point - takes \p Analysis instance + /// that has just been invalidated and constant reference to IR it operated + /// on. + template + void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const { + if (Callbacks) + for (auto &C : Callbacks->AnalysisInvalidatedCallbacks) + C(Analysis.name(), llvm::Any(&IR)); + } + + /// AnalysesCleared instrumentation point - takes name of IR that analyses + /// operated on. + void runAnalysesCleared(StringRef Name) const { + if (Callbacks) + for (auto &C : Callbacks->AnalysesClearedCallbacks) + C(Name); + } + /// Handle invalidation from the pass manager when PassInstrumentation /// is used as the result of PassInstrumentationAnalysis. /// diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h --- a/llvm/include/llvm/IR/PassManager.h +++ b/llvm/include/llvm/IR/PassManager.h @@ -746,9 +746,7 @@ }; /// Construct an empty analysis manager. - /// - /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs(). - AnalysisManager(bool DebugLogging = false); + AnalysisManager(); AnalysisManager(AnalysisManager &&); AnalysisManager &operator=(AnalysisManager &&); @@ -910,9 +908,6 @@ /// Map from an analysis ID and IR unit to a particular cached /// analysis result. AnalysisResultMapT AnalysisResults; - - /// Indicates whether we log to \c llvm::dbgs(). - bool DebugLogging; }; extern template class AnalysisManager; diff --git a/llvm/include/llvm/IR/PassManagerImpl.h b/llvm/include/llvm/IR/PassManagerImpl.h --- a/llvm/include/llvm/IR/PassManagerImpl.h +++ b/llvm/include/llvm/IR/PassManagerImpl.h @@ -20,9 +20,7 @@ namespace llvm { template -inline AnalysisManager::AnalysisManager( - bool DebugLogging) - : DebugLogging(DebugLogging) {} +inline AnalysisManager::AnalysisManager() {} template inline AnalysisManager::AnalysisManager( @@ -37,8 +35,8 @@ inline void AnalysisManager::clear(IRUnitT &IR, llvm::StringRef Name) { - if (DebugLogging) - dbgs() << "Clearing all analysis results for: " << Name << "\n"; + if (auto *PI = getCachedResult(IR)) + PI->runAnalysesCleared(Name); auto ResultsListI = AnalysisResultLists.find(&IR); if (ResultsListI == AnalysisResultLists.end()) @@ -133,9 +131,8 @@ continue; } - if (DebugLogging) - dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name() - << " on " << IR.getName() << "\n"; + if (auto *PI = getCachedResult(IR)) + PI->runAnalysisInvalidated(this->lookUpPass(ID), IR); I = ResultsList.erase(I); AnalysisResults.erase({ID, &IR}); diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -221,10 +221,10 @@ PGOOptions::IRUse, PGOOptions::CSIRUse); } - LoopAnalysisManager LAM(Conf.DebugPassManager); - FunctionAnalysisManager FAM(Conf.DebugPassManager); - CGSCCAnalysisManager CGAM(Conf.DebugPassManager); - ModuleAnalysisManager MAM(Conf.DebugPassManager); + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; PassInstrumentationCallbacks PIC; StandardInstrumentations SI(Conf.DebugPassManager); diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp --- a/llvm/lib/Passes/StandardInstrumentations.cpp +++ b/llvm/lib/Passes/StandardInstrumentations.cpp @@ -900,6 +900,14 @@ PIC.registerBeforeAnalysisCallback([](StringRef PassID, Any IR) { dbgs() << "Running analysis: " << PassID << " on " << getIRName(IR) << "\n"; }); + + PIC.registerAnalysisInvalidatedCallback([](StringRef PassID, Any IR) { + dbgs() << "Invalidating analysis: " << PassID << " on " << getIRName(IR) + << "\n"; + }); + PIC.registerAnalysesClearedCallback([](StringRef IRName) { + dbgs() << "Clearing all analysis results for: " << IRName << "\n"; + }); } void PassStructurePrinter::printWithIdent(bool Expand, const Twine &Msg) { diff --git a/llvm/test/Transforms/LoopUnroll/unroll-loop-invalidation.ll b/llvm/test/Transforms/LoopUnroll/unroll-loop-invalidation.ll --- a/llvm/test/Transforms/LoopUnroll/unroll-loop-invalidation.ll +++ b/llvm/test/Transforms/LoopUnroll/unroll-loop-invalidation.ll @@ -23,8 +23,8 @@ ; CHECK: Running pass: LoopUnrollPass ; CHECK: Clearing all analysis results for: inner2.header ; CHECK: Clearing all analysis results for: outer.header -; CHECK: Invalidating analysis: LoopAccessAnalysis on inner1.header -; CHECK-NOT: Invalidating analysis: LoopAccessAnalysis on inner1.header.1 +; CHECK: Invalidating analysis: LoopAccessAnalysis on {{.*}}inner1.header +; CHECK-NOT: Invalidating analysis: LoopAccessAnalysis on {{.*}}inner1.header.1 ; CHECK: Starting Loop pass manager run. ; CHECK: Running pass: LoopAccessInfoPrinterPass ; CHECK: Running analysis: LoopAccessAnalysis on Loop at depth 1 containing: %inner1.header diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -275,10 +275,10 @@ P->CSAction = PGOOptions::CSIRUse; } } - LoopAnalysisManager LAM(DebugPM); - FunctionAnalysisManager FAM(DebugPM); - CGSCCAnalysisManager CGAM(DebugPM); - ModuleAnalysisManager MAM(DebugPM); + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; PassInstrumentationCallbacks PIC; StandardInstrumentations SI(DebugPM, VerifyEachPass); diff --git a/llvm/unittests/Analysis/CGSCCPassManagerTest.cpp b/llvm/unittests/Analysis/CGSCCPassManagerTest.cpp --- a/llvm/unittests/Analysis/CGSCCPassManagerTest.cpp +++ b/llvm/unittests/Analysis/CGSCCPassManagerTest.cpp @@ -202,8 +202,7 @@ public: CGSCCPassManagerTest() - : FAM(/*DebugLogging*/ true), CGAM(/*DebugLogging*/ true), - MAM(/*DebugLogging*/ true), + : FAM(), CGAM(), MAM(), M(parseIR( // Define a module with the following call graph, where calls go // out the bottom of nodes and enter the top: diff --git a/llvm/unittests/CodeGen/PassManagerTest.cpp b/llvm/unittests/CodeGen/PassManagerTest.cpp --- a/llvm/unittests/CodeGen/PassManagerTest.cpp +++ b/llvm/unittests/CodeGen/PassManagerTest.cpp @@ -208,10 +208,10 @@ LLVMTargetMachine *LLVMTM = static_cast(TM.get()); M->setDataLayout(TM->createDataLayout()); - LoopAnalysisManager LAM(/*DebugLogging=*/true); - FunctionAnalysisManager FAM(/*DebugLogging=*/true); - CGSCCAnalysisManager CGAM(/*DebugLogging=*/true); - ModuleAnalysisManager MAM(/*DebugLogging=*/true); + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; PassBuilder PB(TM.get()); PB.registerModuleAnalyses(MAM); PB.registerFunctionAnalyses(FAM); @@ -225,8 +225,7 @@ MachineFunctionAnalysisManager MFAM; { // Test move assignment. - MachineFunctionAnalysisManager NestedMFAM(FAM, MAM, - /*DebugLogging*/ true); + MachineFunctionAnalysisManager NestedMFAM(FAM, MAM); NestedMFAM.registerPass([&] { return PassInstrumentationAnalysis(); }); NestedMFAM.registerPass([&] { return TestMachineFunctionAnalysis(); }); MFAM = std::move(NestedMFAM); @@ -241,7 +240,7 @@ MachineFunctionPassManager MFPM; { // Test move assignment. - MachineFunctionPassManager NestedMFPM(/*DebugLogging*/ true); + MachineFunctionPassManager NestedMFPM; NestedMFPM.addPass(TestMachineModulePass(Count, TestMachineModuleCount[0])); NestedMFPM.addPass(TestMachineFunctionPass(Count, BeforeInitialization[0], BeforeFinalization[0], diff --git a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp --- a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp +++ b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp @@ -467,7 +467,7 @@ "}\n")), CallbacksHandle(), PB(false, nullptr, PipelineTuningOptions(), None, &CallbacksHandle.Callbacks), - PM(true), LAM(true), FAM(true), CGAM(true), AM(true) { + PM(true), LAM(), FAM(), CGAM(), AM() { EXPECT_TRUE(&CallbacksHandle.Callbacks == PB.getPassInstrumentationCallbacks()); diff --git a/llvm/unittests/IR/PassManagerTest.cpp b/llvm/unittests/IR/PassManagerTest.cpp --- a/llvm/unittests/IR/PassManagerTest.cpp +++ b/llvm/unittests/IR/PassManagerTest.cpp @@ -419,11 +419,11 @@ } TEST_F(PassManagerTest, Basic) { - FunctionAnalysisManager FAM(/*DebugLogging*/ true); + FunctionAnalysisManager FAM; int FunctionAnalysisRuns = 0; FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); - ModuleAnalysisManager MAM(/*DebugLogging*/ true); + ModuleAnalysisManager MAM; int ModuleAnalysisRuns = 0; MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); }); MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); @@ -704,8 +704,8 @@ }; TEST_F(PassManagerTest, IndirectAnalysisInvalidation) { - FunctionAnalysisManager FAM(/*DebugLogging*/ true); - ModuleAnalysisManager MAM(/*DebugLogging*/ true); + FunctionAnalysisManager FAM; + ModuleAnalysisManager MAM; int FunctionAnalysisRuns = 0, ModuleAnalysisRuns = 0, IndirectAnalysisRuns = 0, DoublyIndirectAnalysisRuns = 0; FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); @@ -823,7 +823,7 @@ "}\n"); auto *F = M->getFunction("foo"); - FunctionAnalysisManager FAM(/*DebugLogging*/ true); + FunctionAnalysisManager FAM; FunctionPassManager FPM(/*DebugLogging*/ true); PassInstrumentationCallbacks PIC; StandardInstrumentations SI(/*DebugLogging*/ true); @@ -869,7 +869,7 @@ "}\n"); auto *F = M->getFunction("foo"); - FunctionAnalysisManager FAM(/*DebugLogging*/ true); + FunctionAnalysisManager FAM; FunctionPassManager FPM(/*DebugLogging*/ true); PassInstrumentationCallbacks PIC; StandardInstrumentations SI(/*DebugLogging*/ true); @@ -934,7 +934,7 @@ "}\n"); auto *F = M->getFunction("foo"); - FunctionAnalysisManager FAM(/*DebugLogging*/ true); + FunctionAnalysisManager FAM; FunctionPassManager FPM(/*DebugLogging*/ true); PassInstrumentationCallbacks PIC; StandardInstrumentations SI(/*DebugLogging*/ true); diff --git a/llvm/unittests/Transforms/Scalar/LoopPassManagerTest.cpp b/llvm/unittests/Transforms/Scalar/LoopPassManagerTest.cpp --- a/llvm/unittests/Transforms/Scalar/LoopPassManagerTest.cpp +++ b/llvm/unittests/Transforms/Scalar/LoopPassManagerTest.cpp @@ -297,7 +297,7 @@ "end:\n" " ret void\n" "}\n")), - LAM(true), FAM(true), MAM(true) { + LAM(), FAM(), MAM() { // Register our mock analysis. LAM.registerPass([&] { return MLAHandle.getAnalysis(); });