[NewPM] Add PipelineTuningOption to eagerly invalidate analyses
authorArthur Eubanks <aeubanks@google.com>
Mon, 11 Oct 2021 19:57:30 +0000 (12:57 -0700)
committerArthur Eubanks <aeubanks@google.com>
Mon, 18 Oct 2021 20:20:35 +0000 (13:20 -0700)
This trades off more compile time for less peak memory usage. Right now
it invalidates all function analyses after a module->function or
cgscc->function adaptor.

https://llvm-compile-time-tracker.com/compare.php?from=1fb24fe85a19ae71b00875ff6c96ef1831dcf7e3&to=cb28ddb063c87f0d5df89812ab2de9a69dd276db&stat=instructions
https://llvm-compile-time-tracker.com/compare.php?from=1fb24fe85a19ae71b00875ff6c96ef1831dcf7e3&to=cb28ddb063c87f0d5df89812ab2de9a69dd276db&stat=max-rss

For now this is just experimental.

See comments on why this may affect optimizations.

Reviewed By: asbirlea, nikic

Differential Revision: https://reviews.llvm.org/D111575

llvm/lib/Analysis/CGSCCPassManager.cpp
llvm/lib/IR/PassManager.cpp
llvm/test/Other/new-pm-eager-invalidate.ll [new file with mode: 0644]

index 5573ad1..c2ba3d6 100644 (file)
@@ -38,6 +38,7 @@ using namespace llvm;
 // Explicit template instantiations and specialization definitions for core
 // template typedefs.
 namespace llvm {
+extern cl::opt<bool> EagerlyInvalidateAnalyses;
 
 static cl::opt<bool> AbortOnMaxDevirtIterationsReached(
     "abort-on-max-devirt-iterations-reached",
@@ -556,7 +557,8 @@ PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C,
     // We know that the function pass couldn't have invalidated any other
     // function's analyses (that's the contract of a function pass), so
     // directly handle the function analysis manager's invalidation here.
-    FAM.invalidate(F, PassPA);
+    FAM.invalidate(F, EagerlyInvalidateAnalyses ? PreservedAnalyses::none()
+                                                : PassPA);
 
     // Then intersect the preserved set so that invalidation of module
     // analyses will eventually occur when the module pass completes.
index bb8885a..4965a7d 100644 (file)
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/PassManagerImpl.h"
+#include "llvm/Support/CommandLine.h"
 
 using namespace llvm;
 
+namespace llvm {
+// Experimental option to 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. Currently this invalidates all function
+// analyses after a module->function or cgscc->function adaptor.
+// TODO: make this a PipelineTuningOption.
+cl::opt<bool> EagerlyInvalidateAnalyses(
+    "eagerly-invalidate-analyses", cl::init(false), cl::Hidden,
+    cl::desc("Eagerly invalidate more analyses in default pipelines"));
+
 // Explicit template instantiations and specialization defininitions for core
 // template typedefs.
-namespace llvm {
 template class AllAnalysesOn<Module>;
 template class AllAnalysesOn<Function>;
 template class PassManager<Module>;
@@ -129,7 +141,8 @@ PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M,
     // We know that the function pass couldn't have invalidated any other
     // function's analyses (that's the contract of a function pass), so
     // directly handle the function analysis manager's invalidation here.
-    FAM.invalidate(F, PassPA);
+    FAM.invalidate(F, EagerlyInvalidateAnalyses ? PreservedAnalyses::none()
+                                                : PassPA);
 
     // Then intersect the preserved set so that invalidation of module
     // analyses will eventually occur when the module pass completes.
diff --git a/llvm/test/Other/new-pm-eager-invalidate.ll b/llvm/test/Other/new-pm-eager-invalidate.ll
new file mode 100644 (file)
index 0000000..188ac9b
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: opt -disable-verify -debug-pass-manager -passes='function(require<no-op-function>)' -disable-output -eagerly-invalidate-analyses %s 2>&1 | FileCheck %s
+; RUN: opt -disable-verify -debug-pass-manager -passes='cgscc(function(require<no-op-function>))' -disable-output -eagerly-invalidate-analyses %s 2>&1 | FileCheck %s
+
+; CHECK: Invalidating analysis: NoOpFunctionAnalysis
+
+define void @foo() {
+  unreachable
+}