[Polly] Port ForwardOpTree to the NewPM.

This commit is contained in:
Michael Kruse 2021-02-09 21:19:47 -06:00
parent e89fcbfad6
commit 4c64d8ee3a
6 changed files with 123 additions and 44 deletions

View File

@ -13,18 +13,35 @@
#ifndef POLLY_FORWARDOPTREE_H
#define POLLY_FORWARDOPTREE_H
namespace llvm {
#include "polly/ScopPass.h"
namespace llvm {
class PassRegistry;
void initializeForwardOpTreePass(PassRegistry &);
void initializeForwardOpTreeWrapperPassPass(PassRegistry &);
} // namespace llvm
namespace polly {
Pass *createForwardOpTreeWrapperPass();
class ScopPass;
struct ForwardOpTreePass : llvm::PassInfoMixin<ForwardOpTreePass> {
ForwardOpTreePass() {}
llvm::PreservedAnalyses run(Scop &S, ScopAnalysisManager &SAM,
ScopStandardAnalysisResults &SAR, SPMUpdater &U);
};
struct ForwardOpTreePrinterPass
: llvm::PassInfoMixin<ForwardOpTreePrinterPass> {
ForwardOpTreePrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(Scop &S, ScopAnalysisManager &,
ScopStandardAnalysisResults &SAR, SPMUpdater &);
private:
llvm::raw_ostream &OS;
};
ScopPass *createForwardOpTreePass();
} // namespace polly
#endif // POLLY_FORWARDOPTREE_H

View File

@ -57,6 +57,7 @@ createManagedMemoryRewritePassPass(GPUArch Arch = GPUArch::NVPTX64,
#endif
llvm::Pass *createIslScheduleOptimizerPass();
llvm::Pass *createFlattenSchedulePass();
llvm::Pass *createForwardOpTreeWrapperPass();
llvm::Pass *createDeLICMPass();
llvm::Pass *createMaximalStaticExpansionPass();
@ -95,6 +96,7 @@ struct PollyForcePassLinking {
polly::createIslScheduleOptimizerPass();
polly::createMaximalStaticExpansionPass();
polly::createFlattenSchedulePass();
polly::createForwardOpTreeWrapperPass();
polly::createDeLICMPass();
polly::createDumpModulePass("", true);
polly::createSimplifyPass();
@ -121,6 +123,7 @@ void initializeIslScheduleOptimizerPass(llvm::PassRegistry &);
void initializeMaximalStaticExpanderPass(llvm::PassRegistry &);
void initializePollyCanonicalizePass(llvm::PassRegistry &);
void initializeFlattenSchedulePass(llvm::PassRegistry &);
void initializeForwardOpTreeWrapperPassPass(llvm::PassRegistry &);
void initializeDeLICMPass(llvm::PassRegistry &);
} // namespace llvm

View File

@ -30,4 +30,6 @@ SCOP_PASS("print<polly-dependences>", DependenceInfoPrinterPass(outs()))
SCOP_PASS("polly-codegen", CodeGenerationPass())
SCOP_PASS("polly-simplify", SimplifyPass())
SCOP_PASS("print<polly-simplify>", SimplifyPrinterPass(outs()))
SCOP_PASS("polly-optree", ForwardOpTreePass())
SCOP_PASS("print<polly-optree>", ForwardOpTreePrinterPass(outs()))
#undef SCOP_PASS

View File

@ -261,7 +261,7 @@ void initializePollyPasses(PassRegistry &Registry) {
initializeRewriteByrefParamsPass(Registry);
initializeCodegenCleanupPass(Registry);
initializeFlattenSchedulePass(Registry);
initializeForwardOpTreePass(Registry);
initializeForwardOpTreeWrapperPassPass(Registry);
initializeDeLICMPass(Registry);
initializeSimplifyLegacyPassPass(Registry);
initializeDumpModulePass(Registry);
@ -321,7 +321,7 @@ void registerPollyPasses(llvm::legacy::PassManagerBase &PM) {
if (EnableSimplify)
PM.add(polly::createSimplifyPass(0));
if (EnableForwardOpTree)
PM.add(polly::createForwardOpTreePass());
PM.add(polly::createForwardOpTreeWrapperPass());
if (EnableDeLICM)
PM.add(polly::createDeLICMPass());
if (EnableSimplify)
@ -468,6 +468,8 @@ static void buildDefaultPollyPipeline(FunctionPassManager &PM,
assert(!PollyPrinter && "This option is not implemented");
assert(!PollyOnlyPrinter && "This option is not implemented");
assert(!EnablePolyhedralInfo && "This option is not implemented");
if (EnableForwardOpTree)
SPM.addPass(ForwardOpTreePass());
assert(!EnableDeLICM && "This option is not implemented");
assert(!EnableSimplify && "This option is not implemented");
if (ImportJScop)

View File

@ -1024,8 +1024,75 @@ public:
printStatements(OS, Indent);
}
bool isModified() const { return Modified; }
};
static std::unique_ptr<ForwardOpTreeImpl> runForwardOpTree(Scop &S,
LoopInfo &LI) {
std::unique_ptr<ForwardOpTreeImpl> Impl;
{
IslMaxOperationsGuard MaxOpGuard(S.getIslCtx().get(), MaxOps, false);
Impl = std::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
if (AnalyzeKnown) {
LLVM_DEBUG(dbgs() << "Prepare forwarders...\n");
Impl->computeKnownValues();
}
LLVM_DEBUG(dbgs() << "Forwarding operand trees...\n");
Impl->forwardOperandTrees();
if (MaxOpGuard.hasQuotaExceeded()) {
LLVM_DEBUG(dbgs() << "Not all operations completed because of "
"max_operations exceeded\n");
KnownOutOfQuota++;
}
}
LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
LLVM_DEBUG(dbgs() << S);
// Update statistics
Scop::ScopStatistics ScopStats = S.getStatistics();
NumValueWrites += ScopStats.NumValueWrites;
NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
NumPHIWrites += ScopStats.NumPHIWrites;
NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
NumSingletonWrites += ScopStats.NumSingletonWrites;
NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
return Impl;
}
static PreservedAnalyses
runForwardOpTreeUsingNPM(Scop &S, ScopAnalysisManager &SAM,
ScopStandardAnalysisResults &SAR, SPMUpdater &U,
raw_ostream *OS) {
LoopInfo &LI = SAR.LI;
std::unique_ptr<ForwardOpTreeImpl> Impl = runForwardOpTree(S, LI);
if (OS) {
*OS << "Printing analysis 'Polly - Forward operand tree' for region: '"
<< S.getName() << "' in function '" << S.getFunction().getName()
<< "':\n";
if (Impl) {
assert(Impl->getScop() == &S);
Impl->print(*OS);
}
}
if (!Impl->isModified())
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserveSet<AllAnalysesOn<Module>>();
PA.preserveSet<AllAnalysesOn<Function>>();
PA.preserveSet<AllAnalysesOn<Loop>>();
return PA;
}
/// Pass that redirects scalar reads to array elements that are known to contain
/// the same value.
///
@ -1034,7 +1101,7 @@ public:
/// scalar definition are redirected (We currently do not care about removing
/// the write in this case). This is also useful for the main DeLICM pass as
/// there are less scalars to be mapped.
class ForwardOpTree : public ScopPass {
class ForwardOpTreeWrapperPass : public ScopPass {
private:
/// The pass implementation, also holding per-scop data.
std::unique_ptr<ForwardOpTreeImpl> Impl;
@ -1042,9 +1109,10 @@ private:
public:
static char ID;
explicit ForwardOpTree() : ScopPass(ID) {}
ForwardOpTree(const ForwardOpTree &) = delete;
ForwardOpTree &operator=(const ForwardOpTree &) = delete;
explicit ForwardOpTreeWrapperPass() : ScopPass(ID) {}
ForwardOpTreeWrapperPass(const ForwardOpTreeWrapperPass &) = delete;
ForwardOpTreeWrapperPass &
operator=(const ForwardOpTreeWrapperPass &) = delete;
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequiredTransitive<ScopInfoRegionPass>();
@ -1058,36 +1126,7 @@ public:
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
{
IslMaxOperationsGuard MaxOpGuard(S.getIslCtx().get(), MaxOps, false);
Impl = std::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
if (AnalyzeKnown) {
LLVM_DEBUG(dbgs() << "Prepare forwarders...\n");
Impl->computeKnownValues();
}
LLVM_DEBUG(dbgs() << "Forwarding operand trees...\n");
Impl->forwardOperandTrees();
if (MaxOpGuard.hasQuotaExceeded()) {
LLVM_DEBUG(dbgs() << "Not all operations completed because of "
"max_operations exceeded\n");
KnownOutOfQuota++;
}
}
LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
LLVM_DEBUG(dbgs() << S);
// Update statistics
auto ScopStats = S.getStatistics();
NumValueWrites += ScopStats.NumValueWrites;
NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
NumPHIWrites += ScopStats.NumPHIWrites;
NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
NumSingletonWrites += ScopStats.NumSingletonWrites;
NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
Impl = runForwardOpTree(S, LI);
return false;
}
@ -1103,13 +1142,28 @@ public:
void releaseMemory() override { Impl.reset(); }
}; // class ForwardOpTree
char ForwardOpTree::ID;
char ForwardOpTreeWrapperPass::ID;
} // namespace
ScopPass *polly::createForwardOpTreePass() { return new ForwardOpTree(); }
Pass *polly::createForwardOpTreeWrapperPass() {
return new ForwardOpTreeWrapperPass();
}
INITIALIZE_PASS_BEGIN(ForwardOpTree, "polly-optree",
INITIALIZE_PASS_BEGIN(ForwardOpTreeWrapperPass, "polly-optree",
"Polly - Forward operand tree", false, false)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_END(ForwardOpTree, "polly-optree",
INITIALIZE_PASS_END(ForwardOpTreeWrapperPass, "polly-optree",
"Polly - Forward operand tree", false, false)
llvm::PreservedAnalyses ForwardOpTreePass::run(Scop &S,
ScopAnalysisManager &SAM,
ScopStandardAnalysisResults &SAR,
SPMUpdater &U) {
return runForwardOpTreeUsingNPM(S, SAM, SAR, U, nullptr);
}
llvm::PreservedAnalyses
ForwardOpTreePrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
ScopStandardAnalysisResults &SAR, SPMUpdater &U) {
return runForwardOpTreeUsingNPM(S, SAM, SAR, U, &OS);
}

View File

@ -1,4 +1,5 @@
; RUN: opt %loadPolly -polly-optree -analyze < %s | FileCheck %s -match-full-lines
; RUN: opt %loadPolly "-passes=scop(print<polly-optree>)" -disable-output < %s | FileCheck %s -match-full-lines
;
; Rematerialize a load.
;