LegacyDivergenceAnalysis.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_LEGACY_DIVERGENCE_ANALYSIS_H
  21. #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
  22. #include "llvm/ADT/DenseSet.h"
  23. #include "llvm/Pass.h"
  24. #include <memory>
  25. namespace llvm {
  26. class Function;
  27. class GPUDivergenceAnalysis;
  28. class Module;
  29. class raw_ostream;
  30. class TargetTransformInfo;
  31. class Use;
  32. class Value;
  33. class LegacyDivergenceAnalysis : public FunctionPass {
  34. public:
  35. static char ID;
  36. LegacyDivergenceAnalysis();
  37. void getAnalysisUsage(AnalysisUsage &AU) const override;
  38. bool runOnFunction(Function &F) override;
  39. // Print all divergent branches in the function.
  40. void print(raw_ostream &OS, const Module *) const override;
  41. // Returns true if V is divergent at its definition.
  42. bool isDivergent(const Value *V) const;
  43. // Returns true if U is divergent. Uses of a uniform value can be divergent.
  44. bool isDivergentUse(const Use *U) const;
  45. // Returns true if V is uniform/non-divergent.
  46. bool isUniform(const Value *V) const { return !isDivergent(V); }
  47. // Returns true if U is uniform/non-divergent. Uses of a uniform value can be
  48. // divergent.
  49. bool isUniformUse(const Use *U) const { return !isDivergentUse(U); }
  50. // Keep the analysis results uptodate by removing an erased value.
  51. void removeValue(const Value *V) { DivergentValues.erase(V); }
  52. private:
  53. // Whether analysis should be performed by GPUDivergenceAnalysis.
  54. bool shouldUseGPUDivergenceAnalysis(const Function &F,
  55. const TargetTransformInfo &TTI) const;
  56. // (optional) handle to new DivergenceAnalysis
  57. std::unique_ptr<GPUDivergenceAnalysis> gpuDA;
  58. // Stores all divergent values.
  59. DenseSet<const Value *> DivergentValues;
  60. // Stores divergent uses of possibly uniform values.
  61. DenseSet<const Use *> DivergentUses;
  62. };
  63. } // End llvm namespace
  64. #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
  65. #ifdef __GNUC__
  66. #pragma GCC diagnostic pop
  67. #endif