[NewPM] Add option to prevent rerunning function pipeline on functions in CGSCC adaptor

In a CGSCC pass manager, we may visit the same function multiple times
due to SCC mutations. In the inliner pipeline, this results in running
the function simplification pipeline on a function multiple times even
if it hasn't been changed since the last function simplification
pipeline run.

We use a newly introduced analysis to keep track of whether or not a
function has changed since the last time the function simplification
pipeline has run on it. If we see this analysis available for a function
in a CGSCCToFunctionPassAdaptor, we skip running the function passes on
the function. The analysis is queried at the end of the function passes
so that it's available after the first time the function simplification
pipeline runs on a function. This is a per-adaptor option so it doesn't
apply to every adaptor.

The goal of this is to improve compile times. However, currently we
can't turn this on by default at least for the higher optimization
levels since the function simplification pipeline is not robust enough
to be idempotent in many cases, resulting in performance regressions if
we stop running the function simplification pipeline on a function
multiple times. We may be able to turn this on for -O1 in the near
future, but turning this on for higher optimization levels would require
more investment in the function simplification pipeline.

Heavily inspired by D98103.

Example compile time improvements with flag turned on:
https://llvm-compile-time-tracker.com/compare.php?from=998dc4a5d3491d2ae8cbe742d2e13bc1b0cacc5f&to=5c27c913687d3d5559ef3ab42b5a3d513531d61c&stat=instructions

Reviewed By: asbirlea, nikic

Differential Revision: https://reviews.llvm.org/D113947
This commit is contained in:
Arthur Eubanks 2021-11-16 12:58:13 -08:00
parent 2e7f12d5e9
commit e3e25b5112
7 changed files with 88 additions and 7 deletions

View File

@ -478,11 +478,13 @@ public:
using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>;
explicit CGSCCToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass,
bool EagerlyInvalidate)
: Pass(std::move(Pass)), EagerlyInvalidate(EagerlyInvalidate) {}
bool EagerlyInvalidate, bool NoRerun)
: Pass(std::move(Pass)), EagerlyInvalidate(EagerlyInvalidate),
NoRerun(NoRerun) {}
CGSCCToFunctionPassAdaptor(CGSCCToFunctionPassAdaptor &&Arg)
: Pass(std::move(Arg.Pass)), EagerlyInvalidate(Arg.EagerlyInvalidate) {}
: Pass(std::move(Arg.Pass)), EagerlyInvalidate(Arg.EagerlyInvalidate),
NoRerun(Arg.NoRerun) {}
friend void swap(CGSCCToFunctionPassAdaptor &LHS,
CGSCCToFunctionPassAdaptor &RHS) {
@ -513,6 +515,7 @@ public:
private:
std::unique_ptr<PassConceptT> Pass;
bool EagerlyInvalidate;
bool NoRerun;
};
/// A function to deduce a function pass type and wrap it in the
@ -520,7 +523,8 @@ private:
template <typename FunctionPassT>
CGSCCToFunctionPassAdaptor
createCGSCCToFunctionPassAdaptor(FunctionPassT &&Pass,
bool EagerlyInvalidate = false) {
bool EagerlyInvalidate = false,
bool NoRerun = false) {
using PassModelT =
detail::PassModel<Function, FunctionPassT, PreservedAnalyses,
FunctionAnalysisManager>;
@ -529,9 +533,23 @@ createCGSCCToFunctionPassAdaptor(FunctionPassT &&Pass,
return CGSCCToFunctionPassAdaptor(
std::unique_ptr<CGSCCToFunctionPassAdaptor::PassConceptT>(
new PassModelT(std::forward<FunctionPassT>(Pass))),
EagerlyInvalidate);
EagerlyInvalidate, NoRerun);
}
// A marker to determine if function passes should be run on a function within a
// CGSCCToFunctionPassAdaptor. This is used to prevent running an expensive
// function pass (manager) on a function multiple times if SCC mutations cause a
// function to be visited multiple times and the function is not modified by
// other SCC passes.
class ShouldNotRunFunctionPassesAnalysis
: public AnalysisInfoMixin<ShouldNotRunFunctionPassesAnalysis> {
public:
static AnalysisKey Key;
struct Result {};
Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
};
/// A helper that repeats an SCC pass each time an indirect call is refined to
/// a direct call by that pass.
///

View File

@ -132,11 +132,16 @@ public:
/// before run is called, as part of pass pipeline building.
CGSCCPassManager &getPM() { return PM; }
/// Allow adding module-level passes benefiting the contained CGSCC passes.
/// Add a module pass that runs before the CGSCC passes.
template <class T> void addModulePass(T Pass) {
MPM.addPass(std::move(Pass));
}
/// Add a module pass that runs after the CGSCC passes.
template <class T> void addLateModulePass(T Pass) {
AfterCGMPM.addPass(std::move(Pass));
}
void printPipeline(raw_ostream &OS,
function_ref<StringRef(StringRef)> MapClassName2PassName);
@ -144,8 +149,10 @@ private:
const InlineParams Params;
const InliningAdvisorMode Mode;
const unsigned MaxDevirtIterations;
// TODO: Clean this up so we only have one ModulePassManager.
CGSCCPassManager PM;
ModulePassManager MPM;
ModulePassManager AfterCGMPM;
};
} // end namespace llvm

View File

@ -43,6 +43,8 @@ static cl::opt<bool> AbortOnMaxDevirtIterationsReached(
cl::desc("Abort when the max iterations for devirtualization CGSCC repeat "
"pass is reached"));
AnalysisKey ShouldNotRunFunctionPassesAnalysis::Key;
// Explicit instantiations for the core proxy templates.
template class AllAnalysesOn<LazyCallGraph::SCC>;
template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>;
@ -540,6 +542,9 @@ PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C,
Function &F = N->getFunction();
if (NoRerun && FAM.getCachedResult<ShouldNotRunFunctionPassesAnalysis>(F))
continue;
PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F);
if (!PI.runBeforePass<Function>(*Pass, F))
continue;
@ -556,6 +561,8 @@ PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C,
// function's analyses (that's the contract of a function pass), so
// directly handle the function analysis manager's invalidation here.
FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
if (NoRerun)
(void)FAM.getResult<ShouldNotRunFunctionPassesAnalysis>(F);
// Then intersect the preserved set so that invalidation of module
// analyses will eventually occur when the module pass completes.

View File

@ -171,6 +171,13 @@ static cl::opt<bool> EnableEagerlyInvalidateAnalyses(
"eagerly-invalidate-analyses", cl::init(true), cl::Hidden,
cl::desc("Eagerly invalidate more analyses in default pipelines"));
static cl::opt<bool> EnableNoRerunSimplificationPipeline(
"enable-no-rerun-simplification-pipeline", cl::init(false), cl::Hidden,
cl::desc(
"Prevent running the simplification pipeline on a function more "
"than once in the case that SCC mutations cause a function to be "
"visited multiple times as long as the function has not been changed"));
PipelineTuningOptions::PipelineTuningOptions() {
LoopInterleaving = true;
LoopVectorization = true;
@ -736,10 +743,14 @@ PassBuilder::buildInlinerPipeline(OptimizationLevel Level,
// CGSCC walk.
MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
buildFunctionSimplificationPipeline(Level, Phase),
PTO.EagerlyInvalidateAnalyses));
PTO.EagerlyInvalidateAnalyses, EnableNoRerunSimplificationPipeline));
MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0));
if (EnableNoRerunSimplificationPipeline)
MIWP.addLateModulePass(createModuleToFunctionPassAdaptor(
InvalidateAnalysisPass<ShouldNotRunFunctionPassesAnalysis>()));
return MIWP;
}

View File

@ -201,6 +201,7 @@ FUNCTION_ANALYSIS("regions", RegionInfoAnalysis())
FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())
FUNCTION_ANALYSIS("opt-remark-emit", OptimizationRemarkEmitterAnalysis())
FUNCTION_ANALYSIS("scalar-evolution", ScalarEvolutionAnalysis())
FUNCTION_ANALYSIS("should-not-run-function-passes", ShouldNotRunFunctionPassesAnalysis())
FUNCTION_ANALYSIS("stack-safety-local", StackSafetyAnalysis())
FUNCTION_ANALYSIS("targetlibinfo", TargetLibraryAnalysis())
FUNCTION_ANALYSIS("targetir",

View File

@ -1109,6 +1109,8 @@ PreservedAnalyses ModuleInlinerWrapperPass::run(Module &M,
else
MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
createDevirtSCCRepeatedPass(std::move(PM), MaxDevirtIterations)));
MPM.addPass(std::move(AfterCGMPM));
MPM.run(M, MAM);
// Discard the InlineAdvisor, a subsequent inlining session should construct

View File

@ -0,0 +1,35 @@
; RUN: opt < %s -passes='default<O1>' -disable-output -debug-pass-manager=verbose 2>&1 | FileCheck %s --check-prefixes=CHECK,NORMAL
; RUN: opt < %s -passes='default<O2>' -disable-output -debug-pass-manager=verbose 2>&1 | FileCheck %s --check-prefixes=CHECK,NORMAL
; RUN: opt < %s -passes='default<O1>' -disable-output -debug-pass-manager=verbose -enable-no-rerun-simplification-pipeline=1 2>&1 | FileCheck %s --check-prefixes=CHECK,NORERUN
; RUN: opt < %s -passes='default<O2>' -disable-output -debug-pass-manager=verbose -enable-no-rerun-simplification-pipeline=1 2>&1 | FileCheck %s --check-prefixes=CHECK,NORERUN
; BDCE only runs once in the function simplification pipeline and nowhere else so we use that to check for reruns.
; CHECK: PassManager{{.*}}SCC{{.*}} on (f1)
; CHECK: Running pass: BDCEPass on f1
; CHECK: PassManager{{.*}}SCC{{.*}} on (f2, f3)
; CHECK: Running pass: BDCEPass on f2
; CHECK-NOT: BDCEPass
; CHECK: PassManager{{.*}}SCC{{.*}} on (f2)
; NORMAL: Running pass: BDCEPass on f2
; NORERUN-NOT: Running pass: BDCEPass on f2
; CHECK: PassManager{{.*}}SCC{{.*}} on (f3)
; CHECK: Running pass: BDCEPass on f3
define void @f1(void()* %p) alwaysinline {
call void %p()
ret void
}
define void @f2() #0 {
call void @f1(void()* @f2)
call void @f3()
ret void
}
define void @f3() #0 {
call void @f2()
ret void
}
attributes #0 = { nofree noreturn nosync nounwind readnone noinline }