DivergenceAnalysis.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //===- llvm/Analysis/DivergenceAnalysis.h - Divergence Analysis -*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // \file
  10. // The divergence analysis determines which instructions and branches are
  11. // divergent given a set of divergent source instructions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ANALYSIS_DIVERGENCEANALYSIS_H
  15. #define LLVM_ANALYSIS_DIVERGENCEANALYSIS_H
  16. #include "llvm/ADT/DenseSet.h"
  17. #include "llvm/Analysis/SyncDependenceAnalysis.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/Pass.h"
  20. #include <vector>
  21. namespace llvm {
  22. class Module;
  23. class Value;
  24. class Instruction;
  25. class Loop;
  26. class raw_ostream;
  27. class TargetTransformInfo;
  28. /// \brief Generic divergence analysis for reducible CFGs.
  29. ///
  30. /// This analysis propagates divergence in a data-parallel context from sources
  31. /// of divergence to all users. It requires reducible CFGs. All assignments
  32. /// should be in SSA form.
  33. class DivergenceAnalysisImpl {
  34. public:
  35. /// \brief This instance will analyze the whole function \p F or the loop \p
  36. /// RegionLoop.
  37. ///
  38. /// \param RegionLoop if non-null the analysis is restricted to \p RegionLoop.
  39. /// Otherwise the whole function is analyzed.
  40. /// \param IsLCSSAForm whether the analysis may assume that the IR in the
  41. /// region in in LCSSA form.
  42. DivergenceAnalysisImpl(const Function &F, const Loop *RegionLoop,
  43. const DominatorTree &DT, const LoopInfo &LI,
  44. SyncDependenceAnalysis &SDA, bool IsLCSSAForm);
  45. /// \brief The loop that defines the analyzed region (if any).
  46. const Loop *getRegionLoop() const { return RegionLoop; }
  47. const Function &getFunction() const { return F; }
  48. /// \brief Whether \p BB is part of the region.
  49. bool inRegion(const BasicBlock &BB) const;
  50. /// \brief Whether \p I is part of the region.
  51. bool inRegion(const Instruction &I) const;
  52. /// \brief Mark \p UniVal as a value that is always uniform.
  53. void addUniformOverride(const Value &UniVal);
  54. /// \brief Mark \p DivVal as a value that is always divergent. Will not do so
  55. /// if `isAlwaysUniform(DivVal)`.
  56. /// \returns Whether the tracked divergence state of \p DivVal changed.
  57. bool markDivergent(const Value &DivVal);
  58. /// \brief Propagate divergence to all instructions in the region.
  59. /// Divergence is seeded by calls to \p markDivergent.
  60. void compute();
  61. /// \brief Whether any value was marked or analyzed to be divergent.
  62. bool hasDetectedDivergence() const { return !DivergentValues.empty(); }
  63. /// \brief Whether \p Val will always return a uniform value regardless of its
  64. /// operands
  65. bool isAlwaysUniform(const Value &Val) const;
  66. /// \brief Whether \p Val is divergent at its definition.
  67. bool isDivergent(const Value &Val) const;
  68. /// \brief Whether \p U is divergent. Uses of a uniform value can be
  69. /// divergent.
  70. bool isDivergentUse(const Use &U) const;
  71. private:
  72. /// \brief Mark \p Term as divergent and push all Instructions that become
  73. /// divergent as a result on the worklist.
  74. void analyzeControlDivergence(const Instruction &Term);
  75. /// \brief Mark all phi nodes in \p JoinBlock as divergent and push them on
  76. /// the worklist.
  77. void taintAndPushPhiNodes(const BasicBlock &JoinBlock);
  78. /// \brief Identify all Instructions that become divergent because \p DivExit
  79. /// is a divergent loop exit of \p DivLoop. Mark those instructions as
  80. /// divergent and push them on the worklist.
  81. void propagateLoopExitDivergence(const BasicBlock &DivExit,
  82. const Loop &DivLoop);
  83. /// \brief Internal implementation function for propagateLoopExitDivergence.
  84. void analyzeLoopExitDivergence(const BasicBlock &DivExit,
  85. const Loop &OuterDivLoop);
  86. /// \brief Mark all instruction as divergent that use a value defined in \p
  87. /// OuterDivLoop. Push their users on the worklist.
  88. void analyzeTemporalDivergence(const Instruction &I,
  89. const Loop &OuterDivLoop);
  90. /// \brief Push all users of \p Val (in the region) to the worklist.
  91. void pushUsers(const Value &I);
  92. /// \brief Whether \p Val is divergent when read in \p ObservingBlock.
  93. bool isTemporalDivergent(const BasicBlock &ObservingBlock,
  94. const Value &Val) const;
  95. /// \brief Whether \p Block is join divergent
  96. ///
  97. /// (see markBlockJoinDivergent).
  98. bool isJoinDivergent(const BasicBlock &Block) const {
  99. return DivergentJoinBlocks.contains(&Block);
  100. }
  101. private:
  102. const Function &F;
  103. // If regionLoop != nullptr, analysis is only performed within \p RegionLoop.
  104. // Otherwise, analyze the whole function
  105. const Loop *RegionLoop;
  106. const DominatorTree &DT;
  107. const LoopInfo &LI;
  108. // Recognized divergent loops
  109. DenseSet<const Loop *> DivergentLoops;
  110. // The SDA links divergent branches to divergent control-flow joins.
  111. SyncDependenceAnalysis &SDA;
  112. // Use simplified code path for LCSSA form.
  113. bool IsLCSSAForm;
  114. // Set of known-uniform values.
  115. DenseSet<const Value *> UniformOverrides;
  116. // Blocks with joining divergent control from different predecessors.
  117. DenseSet<const BasicBlock *> DivergentJoinBlocks; // FIXME Deprecated
  118. // Detected/marked divergent values.
  119. DenseSet<const Value *> DivergentValues;
  120. // Internal worklist for divergence propagation.
  121. std::vector<const Instruction *> Worklist;
  122. };
  123. class DivergenceInfo {
  124. Function &F;
  125. // If the function contains an irreducible region the divergence
  126. // analysis can run indefinitely. We set ContainsIrreducible and no
  127. // analysis is actually performed on the function. All values in
  128. // this function are conservatively reported as divergent instead.
  129. bool ContainsIrreducible;
  130. std::unique_ptr<SyncDependenceAnalysis> SDA;
  131. std::unique_ptr<DivergenceAnalysisImpl> DA;
  132. public:
  133. DivergenceInfo(Function &F, const DominatorTree &DT,
  134. const PostDominatorTree &PDT, const LoopInfo &LI,
  135. const TargetTransformInfo &TTI, bool KnownReducible);
  136. /// Whether any divergence was detected.
  137. bool hasDivergence() const {
  138. return ContainsIrreducible || DA->hasDetectedDivergence();
  139. }
  140. /// The GPU kernel this analysis result is for
  141. const Function &getFunction() const { return F; }
  142. /// Whether \p V is divergent at its definition.
  143. bool isDivergent(const Value &V) const {
  144. return ContainsIrreducible || DA->isDivergent(V);
  145. }
  146. /// Whether \p U is divergent. Uses of a uniform value can be divergent.
  147. bool isDivergentUse(const Use &U) const {
  148. return ContainsIrreducible || DA->isDivergentUse(U);
  149. }
  150. /// Whether \p V is uniform/non-divergent.
  151. bool isUniform(const Value &V) const { return !isDivergent(V); }
  152. /// Whether \p U is uniform/non-divergent. Uses of a uniform value can be
  153. /// divergent.
  154. bool isUniformUse(const Use &U) const { return !isDivergentUse(U); }
  155. };
  156. /// \brief Divergence analysis frontend for GPU kernels.
  157. class DivergenceAnalysis : public AnalysisInfoMixin<DivergenceAnalysis> {
  158. friend AnalysisInfoMixin<DivergenceAnalysis>;
  159. static AnalysisKey Key;
  160. public:
  161. using Result = DivergenceInfo;
  162. /// Runs the divergence analysis on @F, a GPU kernel
  163. Result run(Function &F, FunctionAnalysisManager &AM);
  164. };
  165. /// Printer pass to dump divergence analysis results.
  166. struct DivergenceAnalysisPrinterPass
  167. : public PassInfoMixin<DivergenceAnalysisPrinterPass> {
  168. DivergenceAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
  169. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
  170. private:
  171. raw_ostream &OS;
  172. }; // class DivergenceAnalysisPrinterPass
  173. } // namespace llvm
  174. #endif // LLVM_ANALYSIS_DIVERGENCEANALYSIS_H