Skip to content

Commit 1eca6bc

Browse files
committedAug 13, 2016
[PM] Port LoopDataPrefetch to new pass manager
Summary: Refactor the existing support into a LoopDataPrefetch implementation class and a LoopDataPrefetchLegacyPass class that invokes it. Add a new LoopDataPrefetchPass for the new pass manager that utilizes the LoopDataPrefetch implementation class. Reviewers: mehdi_amini Subscribers: sanjoy, mzolotukhin, nemanjai, llvm-commits Differential Revision: https://reviews.llvm.org/D23483 llvm-svn: 278591
1 parent c1ebd82 commit 1eca6bc

File tree

7 files changed

+136
-62
lines changed

7 files changed

+136
-62
lines changed
 

‎llvm/include/llvm/InitializePasses.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void initializeLoaderPassPass(PassRegistry&);
180180
void initializeLoadStoreVectorizerPass(PassRegistry&);
181181
void initializeLocalStackSlotPassPass(PassRegistry&);
182182
void initializeLoopAccessLegacyAnalysisPass(PassRegistry&);
183-
void initializeLoopDataPrefetchPass(PassRegistry&);
183+
void initializeLoopDataPrefetchLegacyPassPass(PassRegistry &);
184184
void initializeLoopDeletionLegacyPassPass(PassRegistry&);
185185
void initializeLoopDistributeLegacyPass(PassRegistry&);
186186
void initializeLoopExtractorPass(PassRegistry&);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-------- LoopDataPrefetch.h - Loop Data Prefetching Pass ---*- C++ -*-===//
2+
//
3+
//
4+
// The LLVM Compiler Infrastructure
5+
//
6+
// This file is distributed under the University of Illinois Open Source
7+
// License. See LICENSE.TXT for details.
8+
//
9+
//===----------------------------------------------------------------------===//
10+
/// \file
11+
/// This file provides the interface for LLVM's Loop Data Prefetching Pass.
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TRANSFORMS_SCALAR_LOOPDATAPREFETCH_H
15+
#define LLVM_TRANSFORMS_SCALAR_LOOPDATAPREFETCH_H
16+
17+
#include "llvm/IR/Function.h"
18+
#include "llvm/IR/PassManager.h"
19+
20+
namespace llvm {
21+
22+
/// An optimization pass inserting data prefetches in loops.
23+
class LoopDataPrefetchPass : public PassInfoMixin<LoopDataPrefetchPass> {
24+
public:
25+
LoopDataPrefetchPass() {}
26+
/// \brief Run the pass over the function.
27+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
28+
};
29+
}
30+
31+
#endif

‎llvm/lib/Passes/PassBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
9494
#include "llvm/Transforms/Scalar/JumpThreading.h"
9595
#include "llvm/Transforms/Scalar/LICM.h"
96+
#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
9697
#include "llvm/Transforms/Scalar/LoopDeletion.h"
9798
#include "llvm/Transforms/Scalar/LoopDistribute.h"
9899
#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"

‎llvm/lib/Passes/PassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ FUNCTION_PASS("nary-reassociate", NaryReassociatePass())
159159
FUNCTION_PASS("jump-threading", JumpThreadingPass())
160160
FUNCTION_PASS("partially-inline-libcalls", PartiallyInlineLibCallsPass())
161161
FUNCTION_PASS("lcssa", LCSSAPass())
162+
FUNCTION_PASS("loop-data-prefetch", LoopDataPrefetchPass())
162163
FUNCTION_PASS("loop-distribute", LoopDistributePass())
163164
FUNCTION_PASS("loop-vectorize", LoopVectorizePass())
164165
FUNCTION_PASS("print", PrintFunctionPass(dbgs()))

‎llvm/lib/Transforms/Scalar/LoopDataPrefetch.cpp

+100-60
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
15+
1416
#define DEBUG_TYPE "loop-data-prefetch"
1517
#include "llvm/ADT/DepthFirstIterator.h"
1618
#include "llvm/ADT/Statistic.h"
@@ -59,80 +61,89 @@ static cl::opt<unsigned> MaxPrefetchIterationsAhead(
5961

6062
STATISTIC(NumPrefetches, "Number of prefetches inserted");
6163

62-
namespace llvm {
63-
void initializeLoopDataPrefetchPass(PassRegistry&);
64-
}
65-
6664
namespace {
6765

68-
class LoopDataPrefetch : public FunctionPass {
69-
public:
70-
static char ID; // Pass ID, replacement for typeid
71-
LoopDataPrefetch() : FunctionPass(ID) {
72-
initializeLoopDataPrefetchPass(*PassRegistry::getPassRegistry());
73-
}
66+
/// Loop prefetch implementation class.
67+
class LoopDataPrefetch {
68+
public:
69+
LoopDataPrefetch(AssumptionCache *AC, LoopInfo *LI, ScalarEvolution *SE,
70+
const TargetTransformInfo *TTI,
71+
OptimizationRemarkEmitter *ORE)
72+
: AC(AC), LI(LI), SE(SE), TTI(TTI), ORE(ORE) {}
7473

75-
void getAnalysisUsage(AnalysisUsage &AU) const override {
76-
AU.addRequired<AssumptionCacheTracker>();
77-
AU.addPreserved<DominatorTreeWrapperPass>();
78-
AU.addRequired<LoopInfoWrapperPass>();
79-
AU.addPreserved<LoopInfoWrapperPass>();
80-
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
81-
AU.addRequired<ScalarEvolutionWrapperPass>();
82-
// FIXME: For some reason, preserving SE here breaks LSR (even if
83-
// this pass changes nothing).
84-
// AU.addPreserved<ScalarEvolutionWrapperPass>();
85-
AU.addRequired<TargetTransformInfoWrapperPass>();
86-
}
74+
bool run();
8775

88-
bool runOnFunction(Function &F) override;
76+
private:
77+
bool runOnLoop(Loop *L);
8978

90-
private:
91-
bool runOnLoop(Loop *L);
79+
/// \brief Check if the the stride of the accesses is large enough to
80+
/// warrant a prefetch.
81+
bool isStrideLargeEnough(const SCEVAddRecExpr *AR);
9282

93-
/// \brief Check if the the stride of the accesses is large enough to
94-
/// warrant a prefetch.
95-
bool isStrideLargeEnough(const SCEVAddRecExpr *AR);
83+
unsigned getMinPrefetchStride() {
84+
if (MinPrefetchStride.getNumOccurrences() > 0)
85+
return MinPrefetchStride;
86+
return TTI->getMinPrefetchStride();
87+
}
9688

97-
unsigned getMinPrefetchStride() {
98-
if (MinPrefetchStride.getNumOccurrences() > 0)
99-
return MinPrefetchStride;
100-
return TTI->getMinPrefetchStride();
101-
}
89+
unsigned getPrefetchDistance() {
90+
if (PrefetchDistance.getNumOccurrences() > 0)
91+
return PrefetchDistance;
92+
return TTI->getPrefetchDistance();
93+
}
10294

103-
unsigned getPrefetchDistance() {
104-
if (PrefetchDistance.getNumOccurrences() > 0)
105-
return PrefetchDistance;
106-
return TTI->getPrefetchDistance();
107-
}
95+
unsigned getMaxPrefetchIterationsAhead() {
96+
if (MaxPrefetchIterationsAhead.getNumOccurrences() > 0)
97+
return MaxPrefetchIterationsAhead;
98+
return TTI->getMaxPrefetchIterationsAhead();
99+
}
108100

109-
unsigned getMaxPrefetchIterationsAhead() {
110-
if (MaxPrefetchIterationsAhead.getNumOccurrences() > 0)
111-
return MaxPrefetchIterationsAhead;
112-
return TTI->getMaxPrefetchIterationsAhead();
113-
}
101+
AssumptionCache *AC;
102+
LoopInfo *LI;
103+
ScalarEvolution *SE;
104+
const TargetTransformInfo *TTI;
105+
OptimizationRemarkEmitter *ORE;
106+
};
107+
108+
/// Legacy class for inserting loop data prefetches.
109+
class LoopDataPrefetchLegacyPass : public FunctionPass {
110+
public:
111+
static char ID; // Pass ID, replacement for typeid
112+
LoopDataPrefetchLegacyPass() : FunctionPass(ID) {
113+
initializeLoopDataPrefetchLegacyPassPass(*PassRegistry::getPassRegistry());
114+
}
114115

115-
AssumptionCache *AC;
116-
LoopInfo *LI;
117-
ScalarEvolution *SE;
118-
const TargetTransformInfo *TTI;
119-
const DataLayout *DL;
120-
OptimizationRemarkEmitter *ORE;
116+
void getAnalysisUsage(AnalysisUsage &AU) const override {
117+
AU.addRequired<AssumptionCacheTracker>();
118+
AU.addPreserved<DominatorTreeWrapperPass>();
119+
AU.addRequired<LoopInfoWrapperPass>();
120+
AU.addPreserved<LoopInfoWrapperPass>();
121+
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
122+
AU.addRequired<ScalarEvolutionWrapperPass>();
123+
// FIXME: For some reason, preserving SE here breaks LSR (even if
124+
// this pass changes nothing).
125+
// AU.addPreserved<ScalarEvolutionWrapperPass>();
126+
AU.addRequired<TargetTransformInfoWrapperPass>();
127+
}
128+
129+
bool runOnFunction(Function &F) override;
121130
};
122131
}
123132

124-
char LoopDataPrefetch::ID = 0;
125-
INITIALIZE_PASS_BEGIN(LoopDataPrefetch, "loop-data-prefetch",
133+
char LoopDataPrefetchLegacyPass::ID = 0;
134+
INITIALIZE_PASS_BEGIN(LoopDataPrefetchLegacyPass, "loop-data-prefetch",
126135
"Loop Data Prefetch", false, false)
127136
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
128137
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
129138
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
130139
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
131140
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
132-
INITIALIZE_PASS_END(LoopDataPrefetch, "loop-data-prefetch",
141+
INITIALIZE_PASS_END(LoopDataPrefetchLegacyPass, "loop-data-prefetch",
133142
"Loop Data Prefetch", false, false)
134143

135-
FunctionPass *llvm::createLoopDataPrefetchPass() { return new LoopDataPrefetch(); }
144+
FunctionPass *llvm::createLoopDataPrefetchPass() {
145+
return new LoopDataPrefetchLegacyPass();
146+
}
136147

137148
bool LoopDataPrefetch::isStrideLargeEnough(const SCEVAddRecExpr *AR) {
138149
unsigned TargetMinStride = getMinPrefetchStride();
@@ -150,17 +161,46 @@ bool LoopDataPrefetch::isStrideLargeEnough(const SCEVAddRecExpr *AR) {
150161
return TargetMinStride <= AbsStride;
151162
}
152163

153-
bool LoopDataPrefetch::runOnFunction(Function &F) {
164+
PreservedAnalyses LoopDataPrefetchPass::run(Function &F,
165+
FunctionAnalysisManager &AM) {
166+
LoopInfo *LI = &AM.getResult<LoopAnalysis>(F);
167+
ScalarEvolution *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
168+
AssumptionCache *AC = &AM.getResult<AssumptionAnalysis>(F);
169+
OptimizationRemarkEmitter *ORE =
170+
&AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
171+
const TargetTransformInfo *TTI = &AM.getResult<TargetIRAnalysis>(F);
172+
173+
LoopDataPrefetch LDP(AC, LI, SE, TTI, ORE);
174+
bool Changed = LDP.run();
175+
176+
if (Changed) {
177+
PreservedAnalyses PA;
178+
PA.preserve<DominatorTreeAnalysis>();
179+
PA.preserve<LoopAnalysis>();
180+
return PA;
181+
}
182+
183+
return PreservedAnalyses::all();
184+
}
185+
186+
bool LoopDataPrefetchLegacyPass::runOnFunction(Function &F) {
154187
if (skipFunction(F))
155188
return false;
156189

157-
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
158-
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
159-
DL = &F.getParent()->getDataLayout();
160-
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
161-
ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
162-
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
190+
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
191+
ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
192+
AssumptionCache *AC =
193+
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
194+
OptimizationRemarkEmitter *ORE =
195+
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
196+
const TargetTransformInfo *TTI =
197+
&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
198+
199+
LoopDataPrefetch LDP(AC, LI, SE, TTI, ORE);
200+
return LDP.run();
201+
}
163202

203+
bool LoopDataPrefetch::run() {
164204
// If PrefetchDistance is not set, don't run the pass. This gives an
165205
// opportunity for targets to run this pass for selected subtargets only
166206
// (whose TTI sets PrefetchDistance).

‎llvm/lib/Transforms/Scalar/Scalar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
5050
initializeIndVarSimplifyLegacyPassPass(Registry);
5151
initializeJumpThreadingPass(Registry);
5252
initializeLegacyLICMPassPass(Registry);
53-
initializeLoopDataPrefetchPass(Registry);
53+
initializeLoopDataPrefetchLegacyPassPass(Registry);
5454
initializeLoopDeletionLegacyPassPass(Registry);
5555
initializeLoopAccessLegacyAnalysisPass(Registry);
5656
initializeLoopInstSimplifyLegacyPassPass(Registry);

‎llvm/test/Transforms/LoopDataPrefetch/PowerPC/basic.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -mcpu=a2 -loop-data-prefetch -S < %s | FileCheck %s
2+
; RUN: opt -mcpu=a2 -passes=loop-data-prefetch -S < %s | FileCheck %s
23
target datalayout = "E-m:e-i64:64-n32:64"
34
target triple = "powerpc64-bgq-linux"
45

0 commit comments

Comments
 (0)
Please sign in to comment.