LegacyDivergenceAnalysis.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // The kernel divergence analysis is an LLVM pass which can be used to find out
  15. // if a branch instruction in a GPU program (kernel) is divergent or not. It can help
  16. // branch optimizations such as jump threading and loop unswitching to make
  17. // better decisions.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H
  21. #define LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H
  22. #include "llvm/ADT/DenseSet.h"
  23. #include "llvm/Analysis/LoopInfo.h"
  24. #include "llvm/Analysis/PostDominators.h"
  25. #include "llvm/IR/PassManager.h"
  26. #include "llvm/Pass.h"
  27. #include <memory>
  28. namespace llvm {
  29. class DivergenceInfo;
  30. class Function;
  31. class Module;
  32. class raw_ostream;
  33. class TargetTransformInfo;
  34. class Use;
  35. class Value;
  36. class LegacyDivergenceAnalysisImpl {
  37. public:
  38. // Returns true if V is divergent at its definition.
  39. bool isDivergent(const Value *V) const;
  40. // Returns true if U is divergent. Uses of a uniform value can be divergent.
  41. bool isDivergentUse(const Use *U) const;
  42. // Returns true if V is uniform/non-divergent.
  43. bool isUniform(const Value *V) const { return !isDivergent(V); }
  44. // Returns true if U is uniform/non-divergent. Uses of a uniform value can be
  45. // divergent.
  46. bool isUniformUse(const Use *U) const { return !isDivergentUse(U); }
  47. // Keep the analysis results uptodate by removing an erased value.
  48. void removeValue(const Value *V) { DivergentValues.erase(V); }
  49. // Print all divergent branches in the function.
  50. void print(raw_ostream &OS, const Module *) const;
  51. // Whether analysis should be performed by GPUDivergenceAnalysis.
  52. bool shouldUseGPUDivergenceAnalysis(const Function &F,
  53. const TargetTransformInfo &TTI,
  54. const LoopInfo &LI);
  55. void run(Function &F, TargetTransformInfo &TTI, DominatorTree &DT,
  56. PostDominatorTree &PDT, const LoopInfo &LI);
  57. protected:
  58. // (optional) handle to new DivergenceAnalysis
  59. std::unique_ptr<DivergenceInfo> gpuDA;
  60. // Stores all divergent values.
  61. DenseSet<const Value *> DivergentValues;
  62. // Stores divergent uses of possibly uniform values.
  63. DenseSet<const Use *> DivergentUses;
  64. };
  65. class LegacyDivergenceAnalysis : public FunctionPass,
  66. public LegacyDivergenceAnalysisImpl {
  67. public:
  68. static char ID;
  69. LegacyDivergenceAnalysis();
  70. void getAnalysisUsage(AnalysisUsage &AU) const override;
  71. bool runOnFunction(Function &F) override;
  72. };
  73. class LegacyDivergenceAnalysisPass
  74. : public PassInfoMixin<LegacyDivergenceAnalysisPass>,
  75. public LegacyDivergenceAnalysisImpl {
  76. public:
  77. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  78. private:
  79. // (optional) handle to new DivergenceAnalysis
  80. std::unique_ptr<DivergenceInfo> gpuDA;
  81. // Stores all divergent values.
  82. DenseSet<const Value *> DivergentValues;
  83. // Stores divergent uses of possibly uniform values.
  84. DenseSet<const Use *> DivergentUses;
  85. };
  86. } // end namespace llvm
  87. #endif // LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif