diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -73,6 +73,12 @@ /// Tuning option to enable/disable function merging. Its default value is /// false. bool MergeFunctions; + + /// Eagerly invalidate more analyses. This has the potential to decrease max + /// memory usage in exchange for more compile time. This may affect codegen + /// due to either passes using analyses only when cached, or invalidating and + /// recalculating an analysis that was stale/imprecise but still valid. + bool EagerlyInvalidateAnalyses; }; /// This class provides access to building LLVM's passes. diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -172,6 +172,7 @@ LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; CallGraphProfile = true; MergeFunctions = false; + EagerlyInvalidateAnalyses = false; } namespace llvm { @@ -722,8 +723,10 @@ // Lastly, add the core function simplification pipeline nested inside the // CGSCC walk. - MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( - buildFunctionSimplificationPipeline(Level, Phase))); + FunctionPassManager FPM = buildFunctionSimplificationPipeline(Level, Phase); + if (PTO.EagerlyInvalidateAnalyses) + FPM.addPass(InvalidateAllAnalysesPass()); + MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM))); MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0)); @@ -792,6 +795,8 @@ // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. if (LoadSampleProfile) EarlyFPM.addPass(InstCombinePass()); + if (PTO.EagerlyInvalidateAnalyses) + EarlyFPM.addPass(InvalidateAllAnalysesPass()); MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); if (LoadSampleProfile) { @@ -866,6 +871,8 @@ invokePeepholeEPCallbacks(GlobalCleanupPM, Level); GlobalCleanupPM.addPass(SimplifyCFGPass()); + if (PTO.EagerlyInvalidateAnalyses) + GlobalCleanupPM.addPass(InvalidateAllAnalysesPass()); MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM))); // Add all the requested passes for instrumentation PGO, if requested. @@ -1148,6 +1155,9 @@ OptimizePM.addPass(CoroCleanupPass()); + if (PTO.EagerlyInvalidateAnalyses) + OptimizePM.addPass(InvalidateAllAnalysesPass()); + // Add the core optimizing pipeline. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM))); diff --git a/llvm/test/Other/new-pm-eager-invalidate.ll b/llvm/test/Other/new-pm-eager-invalidate.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Other/new-pm-eager-invalidate.ll @@ -0,0 +1,21 @@ +; RUN: opt -disable-verify -verify-cfg-preserved=0 -debug-pass-manager -passes='default' -S -eagerly-invalidate-analyses %s 2>&1 | FileCheck %s + +; CHECK: Running pass: InvalidateAllAnalysesPass on foo +; CHECK: Running pass: InvalidateAllAnalysesPass on foo +; CHECK: Running pass: InvalidateAllAnalysesPass on foo +; CHECK: Running pass: InvalidateAllAnalysesPass on foo + +declare void @bar() local_unnamed_addr + +define void @foo(i32 %n) local_unnamed_addr { +entry: + br label %loop +loop: + %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ] + %iv.next = add i32 %iv, 1 + tail call void @bar() + %cmp = icmp eq i32 %iv, %n + br i1 %cmp, label %exit, label %loop +exit: + ret void +} 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 @@ -150,6 +150,9 @@ static cl::opt PseudoProbeForProfiling( "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden, cl::desc("Emit pseudo probes to enable PGO profile generation.")); +static cl::opt EagerlyInvalidateAnalyses( + "eagerly-invalidate-analyses", cl::init(false), cl::Hidden, + cl::desc("Eagerly invalidate more analyses in default pipelines")); /// @}} template @@ -309,6 +312,7 @@ // to false above so we shouldn't necessarily need to check whether or not the // option has been enabled. PTO.LoopUnrolling = !DisableLoopUnrolling; + PTO.EagerlyInvalidateAnalyses = EagerlyInvalidateAnalyses; PassBuilder PB(TM, PTO, P, &PIC); registerEPCallbacks(PB);