85 #define DEBUG_TYPE "divergence" 90 cl::desc(
"turn the LegacyDivergenceAnalysis into " 91 "a wrapper for GPUDivergenceAnalysis"));
100 : F(F), TTI(TTI), DT(DT), PDT(PDT), DV(DV), DU(DU) {}
101 void populateWithSourcesOfDivergence();
106 void exploreDataDependency(
Value *V);
115 void findUsersOutsideInfluenceRegion(
122 std::vector<Value *> Worklist;
128 void DivergencePropagator::populateWithSourcesOfDivergence() {
133 if (TTI.isSourceOfDivergence(&
I)) {
134 Worklist.push_back(&
I);
138 for (
auto &
Arg :
F.args()) {
139 if (TTI.isSourceOfDivergence(&
Arg)) {
140 Worklist.push_back(&
Arg);
146 void DivergencePropagator::exploreSyncDependency(
Instruction *TI) {
169 if (IPostDom ==
nullptr)
172 for (
auto I = IPostDom->
begin(); isa<PHINode>(
I); ++
I) {
175 if (!cast<PHINode>(
I)->hasConstantOrUndefValue() && DV.insert(&*
I).second)
176 Worklist.push_back(&*
I);
199 computeInfluenceRegion(ThisBB, IPostDom, InfluenceRegion);
205 while (InfluenceRegion.
count(InfluencedBB)) {
206 for (
auto &
I : *InfluencedBB) {
208 findUsersOutsideInfluenceRegion(
I, InfluenceRegion);
211 if (IDomNode ==
nullptr)
213 InfluencedBB = IDomNode->
getBlock();
217 void DivergencePropagator::findUsersOutsideInfluenceRegion(
223 if (DV.insert(UserInst).second)
224 Worklist.push_back(UserInst);
234 std::vector<BasicBlock *> &InfluenceStack) {
236 if (Succ != End && InfluenceRegion.
insert(Succ).second)
237 InfluenceStack.push_back(Succ);
241 void DivergencePropagator::computeInfluenceRegion(
245 "End does not properly dominate Start");
250 std::vector<BasicBlock *> InfluenceStack;
251 addSuccessorsToInfluenceRegion(Start, End, InfluenceRegion, InfluenceStack);
252 while (!InfluenceStack.empty()) {
254 InfluenceStack.pop_back();
255 addSuccessorsToInfluenceRegion(BB, End, InfluenceRegion, InfluenceStack);
259 void DivergencePropagator::exploreDataDependency(
Value *V) {
262 if (!TTI.isAlwaysUniform(U) && DV.insert(U).second)
263 Worklist.push_back(U);
269 while (!Worklist.empty()) {
270 Value *V = Worklist.back();
276 exploreSyncDependency(I);
278 exploreDataDependency(V);
290 "Legacy Divergence Analysis",
false,
true)
295 "Legacy Divergence Analysis",
false,
true)
309 bool LegacyDivergenceAnalysis::shouldUseGPUDivergenceAnalysis(
315 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
317 RPOTraversal FuncRPOT(&F);
323 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
324 if (TTIWP ==
nullptr)
333 DivergentValues.
clear();
334 DivergentUses.clear();
337 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
338 auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
340 if (shouldUseGPUDivergenceAnalysis(F)) {
342 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
343 gpuDA = std::make_unique<GPUDivergenceAnalysis>(
F, DT, PDT, LI, TTI);
348 DP.populateWithSourcesOfDivergence();
361 return gpuDA->isDivergent(*V);
363 return DivergentValues.
count(V);
368 return gpuDA->isDivergentUse(*U);
370 return DivergentValues.
count(U->get()) || DivergentUses.count(U);
374 if ((!gpuDA || !gpuDA->hasDivergence()) && DivergentValues.
empty())
378 if (!DivergentValues.
empty()) {
379 const Value *FirstDivergentValue = *DivergentValues.
begin();
380 if (
const Argument *
Arg = dyn_cast<Argument>(FirstDivergentValue)) {
381 F =
Arg->getParent();
383 dyn_cast<Instruction>(FirstDivergentValue)) {
389 F = &gpuDA->getFunction();
400 for (
auto BI = F->
begin(), BE = F->
end(); BI != BE; ++BI) {
402 OS <<
"\n " << BB.
getName() <<
":\n";
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isDivergent(const Value *V) const
This class represents an incoming formal argument to a Function.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
LegacyDivergenceAnalysis()
static cl::opt< bool > UseGPUDA("use-gpu-divergence-analysis", cl::init(false), cl::Hidden, cl::desc("turn the LegacyDivergenceAnalysis into " "a wrapper for GPUDivergenceAnalysis"))
bool isTerminator() const
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
const PostDominatorTree & PDT
block Block Frequency true
INITIALIZE_PASS_BEGIN(LegacyDivergenceAnalysis, "divergence", "Legacy Divergence Analysis", false, true) INITIALIZE_PASS_END(LegacyDivergenceAnalysis
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
A Use represents the edge between a Value definition and its users.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI)
Return true if the control flow in RPOTraversal is irreducible.
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug() const
Return a const iterator range over the instructions in the block, skipping any debug instructions...
DomTreeNodeBase * getIDom() const
std::pair< iterator, bool > insert(const ValueT &V)
bool isDivergentUse(const Use *U) const
DivergencePropagator(const FunctionRPOT &FuncRPOT, const DominatorTree &DT, const PostDominatorTree &PDT, const LoopInfo &LI)
Represent the analysis usage information of a pass.
const Instruction & back() const
FunctionPass class - This class is used to implement most global optimizations.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
static void propagate(InstantiatedValue From, InstantiatedValue To, MatchState State, ReachabilitySet &ReachSet, std::vector< WorkListItem > &WorkList)
FunctionPass * createLegacyDivergenceAnalysisPass()
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
succ_range successors(Instruction *I)
This class implements an extremely fast bulk output stream that can only output to a stream...
The legacy pass manager's analysis pass to compute loop information.
inst_range instructions(Function *F)
Legacy analysis pass which computes a DominatorTree.
void initializeLegacyDivergenceAnalysisPass(PassRegistry &)
iterator_range< arg_iterator > args()
const BasicBlock * getParent() const