1 //===- llvm/Analysis/DivergenceAnalysis.h - Divergence Analysis -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // The divergence analysis determines which instructions and branches are
11 // divergent given a set of divergent source instructions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_DIVERGENCE_ANALYSIS_H
16 #define LLVM_ANALYSIS_DIVERGENCE_ANALYSIS_H
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/Analysis/SyncDependenceAnalysis.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Pass.h"
30 class TargetTransformInfo
;
32 /// \brief Generic divergence analysis for reducible CFGs.
34 /// This analysis propagates divergence in a data-parallel context from sources
35 /// of divergence to all users. It requires reducible CFGs. All assignments
36 /// should be in SSA form.
37 class DivergenceAnalysis
{
39 /// \brief This instance will analyze the whole function \p F or the loop \p
42 /// \param RegionLoop if non-null the analysis is restricted to \p RegionLoop.
43 /// Otherwise the whole function is analyzed.
44 /// \param IsLCSSAForm whether the analysis may assume that the IR in the
45 /// region in in LCSSA form.
46 DivergenceAnalysis(const Function
&F
, const Loop
*RegionLoop
,
47 const DominatorTree
&DT
, const LoopInfo
&LI
,
48 SyncDependenceAnalysis
&SDA
, bool IsLCSSAForm
);
50 /// \brief The loop that defines the analyzed region (if any).
51 const Loop
*getRegionLoop() const { return RegionLoop
; }
52 const Function
&getFunction() const { return F
; }
54 /// \brief Whether \p BB is part of the region.
55 bool inRegion(const BasicBlock
&BB
) const;
56 /// \brief Whether \p I is part of the region.
57 bool inRegion(const Instruction
&I
) const;
59 /// \brief Mark \p UniVal as a value that is always uniform.
60 void addUniformOverride(const Value
&UniVal
);
62 /// \brief Mark \p DivVal as a value that is always divergent.
63 void markDivergent(const Value
&DivVal
);
65 /// \brief Propagate divergence to all instructions in the region.
66 /// Divergence is seeded by calls to \p markDivergent.
69 /// \brief Whether any value was marked or analyzed to be divergent.
70 bool hasDetectedDivergence() const { return !DivergentValues
.empty(); }
72 /// \brief Whether \p Val will always return a uniform value regardless of its
74 bool isAlwaysUniform(const Value
&Val
) const;
76 /// \brief Whether \p Val is a divergent value
77 bool isDivergent(const Value
&Val
) const;
79 void print(raw_ostream
&OS
, const Module
*) const;
82 bool updateTerminator(const Instruction
&Term
) const;
83 bool updatePHINode(const PHINode
&Phi
) const;
85 /// \brief Computes whether \p Inst is divergent based on the
86 /// divergence of its operands.
88 /// \returns Whether \p Inst is divergent.
90 /// This should only be called for non-phi, non-terminator instructions.
91 bool updateNormalInstruction(const Instruction
&Inst
) const;
93 /// \brief Mark users of live-out users as divergent.
95 /// \param LoopHeader the header of the divergent loop.
97 /// Marks all users of live-out values of the loop headed by \p LoopHeader
98 /// as divergent and puts them on the worklist.
99 void taintLoopLiveOuts(const BasicBlock
&LoopHeader
);
101 /// \brief Push all users of \p Val (in the region) to the worklist
102 void pushUsers(const Value
&I
);
104 /// \brief Push all phi nodes in @block to the worklist
105 void pushPHINodes(const BasicBlock
&Block
);
107 /// \brief Mark \p Block as join divergent
109 /// A block is join divergent if two threads may reach it from different
110 /// incoming blocks at the same time.
111 void markBlockJoinDivergent(const BasicBlock
&Block
) {
112 DivergentJoinBlocks
.insert(&Block
);
115 /// \brief Whether \p Val is divergent when read in \p ObservingBlock.
116 bool isTemporalDivergent(const BasicBlock
&ObservingBlock
,
117 const Value
&Val
) const;
119 /// \brief Whether \p Block is join divergent
121 /// (see markBlockJoinDivergent).
122 bool isJoinDivergent(const BasicBlock
&Block
) const {
123 return DivergentJoinBlocks
.find(&Block
) != DivergentJoinBlocks
.end();
126 /// \brief Propagate control-induced divergence to users (phi nodes and
129 // \param JoinBlock is a divergent loop exit or join point of two disjoint
131 // \returns Whether \p JoinBlock is a divergent loop exit of \p TermLoop.
132 bool propagateJoinDivergence(const BasicBlock
&JoinBlock
,
133 const Loop
*TermLoop
);
135 /// \brief Propagate induced value divergence due to control divergence in \p
137 void propagateBranchDivergence(const Instruction
&Term
);
139 /// \brief Propagate divergent caused by a divergent loop exit.
141 /// \param ExitingLoop is a divergent loop.
142 void propagateLoopDivergence(const Loop
&ExitingLoop
);
146 // If regionLoop != nullptr, analysis is only performed within \p RegionLoop.
147 // Otw, analyze the whole function
148 const Loop
*RegionLoop
;
150 const DominatorTree
&DT
;
153 // Recognized divergent loops
154 DenseSet
<const Loop
*> DivergentLoops
;
156 // The SDA links divergent branches to divergent control-flow joins.
157 SyncDependenceAnalysis
&SDA
;
159 // Use simplified code path for LCSSA form.
162 // Set of known-uniform values.
163 DenseSet
<const Value
*> UniformOverrides
;
165 // Blocks with joining divergent control from different predecessors.
166 DenseSet
<const BasicBlock
*> DivergentJoinBlocks
;
168 // Detected/marked divergent values.
169 DenseSet
<const Value
*> DivergentValues
;
171 // Internal worklist for divergence propagation.
172 std::vector
<const Instruction
*> Worklist
;
175 /// \brief Divergence analysis frontend for GPU kernels.
176 class GPUDivergenceAnalysis
{
177 SyncDependenceAnalysis SDA
;
178 DivergenceAnalysis DA
;
181 /// Runs the divergence analysis on @F, a GPU kernel
182 GPUDivergenceAnalysis(Function
&F
, const DominatorTree
&DT
,
183 const PostDominatorTree
&PDT
, const LoopInfo
&LI
,
184 const TargetTransformInfo
&TTI
);
186 /// Whether any divergence was detected.
187 bool hasDivergence() const { return DA
.hasDetectedDivergence(); }
189 /// The GPU kernel this analysis result is for
190 const Function
&getFunction() const { return DA
.getFunction(); }
192 /// Whether \p V is divergent.
193 bool isDivergent(const Value
&V
) const;
195 /// Whether \p V is uniform/non-divergent
196 bool isUniform(const Value
&V
) const { return !isDivergent(V
); }
198 /// Print all divergent values in the kernel.
199 void print(raw_ostream
&OS
, const Module
*) const;
204 #endif // LLVM_ANALYSIS_DIVERGENCE_ANALYSIS_H