OptimizationRemarkEmitter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- 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. // Optimization diagnostic interfaces. It's packaged as an analysis pass so
  10. // that by using this service passes become dependent on BFI as well. BFI is
  11. // used to compute the "hotness" of the diagnostic message.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
  14. #define LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/Analysis/BlockFrequencyInfo.h"
  17. #include "llvm/IR/DiagnosticInfo.h"
  18. #include "llvm/IR/PassManager.h"
  19. #include "llvm/Pass.h"
  20. namespace llvm {
  21. class Function;
  22. class Value;
  23. /// The optimization diagnostic interface.
  24. ///
  25. /// It allows reporting when optimizations are performed and when they are not
  26. /// along with the reasons for it. Hotness information of the corresponding
  27. /// code region can be included in the remark if DiagnosticsHotnessRequested is
  28. /// enabled in the LLVM context.
  29. class OptimizationRemarkEmitter {
  30. public:
  31. OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
  32. : F(F), BFI(BFI) {}
  33. /// This variant can be used to generate ORE on demand (without the
  34. /// analysis pass).
  35. ///
  36. /// Note that this ctor has a very different cost depending on whether
  37. /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
  38. /// the operation is free.
  39. ///
  40. /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
  41. /// operation since BFI and all its required analyses are computed. This is
  42. /// for example useful for CGSCC passes that can't use function analyses
  43. /// passes in the old PM.
  44. OptimizationRemarkEmitter(const Function *F);
  45. OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
  46. : F(Arg.F), BFI(Arg.BFI) {}
  47. OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) {
  48. F = RHS.F;
  49. BFI = RHS.BFI;
  50. return *this;
  51. }
  52. /// Handle invalidation events in the new pass manager.
  53. bool invalidate(Function &F, const PreservedAnalyses &PA,
  54. FunctionAnalysisManager::Invalidator &Inv);
  55. /// Return true iff at least *some* remarks are enabled.
  56. bool enabled() const {
  57. return F->getContext().getLLVMRemarkStreamer() ||
  58. F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled();
  59. }
  60. /// Output the remark via the diagnostic handler and to the
  61. /// optimization record file.
  62. void emit(DiagnosticInfoOptimizationBase &OptDiag);
  63. /// Take a lambda that returns a remark which will be emitted. Second
  64. /// argument is only used to restrict this to functions.
  65. template <typename T>
  66. void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
  67. // Avoid building the remark unless we know there are at least *some*
  68. // remarks enabled. We can't currently check whether remarks are requested
  69. // for the calling pass since that requires actually building the remark.
  70. if (enabled()) {
  71. auto R = RemarkBuilder();
  72. static_assert(
  73. std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
  74. "the lambda passed to emit() must return a remark");
  75. emit((DiagnosticInfoOptimizationBase &)R);
  76. }
  77. }
  78. /// Whether we allow for extra compile-time budget to perform more
  79. /// analysis to produce fewer false positives.
  80. ///
  81. /// This is useful when reporting missed optimizations. In this case we can
  82. /// use the extra analysis (1) to filter trivial false positives or (2) to
  83. /// provide more context so that non-trivial false positives can be quickly
  84. /// detected by the user.
  85. bool allowExtraAnalysis(StringRef PassName) const {
  86. return OptimizationRemarkEmitter::allowExtraAnalysis(*F, PassName);
  87. }
  88. static bool allowExtraAnalysis(const Function &F, StringRef PassName) {
  89. return allowExtraAnalysis(F.getContext(), PassName);
  90. }
  91. static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName) {
  92. return Ctx.getLLVMRemarkStreamer() ||
  93. Ctx.getDiagHandlerPtr()->isAnyRemarkEnabled(PassName);
  94. }
  95. private:
  96. const Function *F;
  97. BlockFrequencyInfo *BFI;
  98. /// If we generate BFI on demand, we need to free it when ORE is freed.
  99. std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
  100. /// Compute hotness from IR value (currently assumed to be a block) if PGO is
  101. /// available.
  102. Optional<uint64_t> computeHotness(const Value *V);
  103. /// Similar but use value from \p OptDiag and update hotness there.
  104. void computeHotness(DiagnosticInfoIROptimization &OptDiag);
  105. /// Only allow verbose messages if we know we're filtering by hotness
  106. /// (BFI is only set in this case).
  107. bool shouldEmitVerbose() { return BFI != nullptr; }
  108. OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
  109. void operator=(const OptimizationRemarkEmitter &) = delete;
  110. };
  111. /// Add a small namespace to avoid name clashes with the classes used in
  112. /// the streaming interface. We want these to be short for better
  113. /// write/readability.
  114. namespace ore {
  115. using NV = DiagnosticInfoOptimizationBase::Argument;
  116. using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
  117. using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
  118. }
  119. /// OptimizationRemarkEmitter legacy analysis pass
  120. ///
  121. /// Note that this pass shouldn't generally be marked as preserved by other
  122. /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
  123. /// could be freed.
  124. class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
  125. std::unique_ptr<OptimizationRemarkEmitter> ORE;
  126. public:
  127. OptimizationRemarkEmitterWrapperPass();
  128. bool runOnFunction(Function &F) override;
  129. void getAnalysisUsage(AnalysisUsage &AU) const override;
  130. OptimizationRemarkEmitter &getORE() {
  131. assert(ORE && "pass not run yet");
  132. return *ORE;
  133. }
  134. static char ID;
  135. };
  136. class OptimizationRemarkEmitterAnalysis
  137. : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
  138. friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
  139. static AnalysisKey Key;
  140. public:
  141. /// Provide the result typedef for this analysis pass.
  142. typedef OptimizationRemarkEmitter Result;
  143. /// Run the analysis pass over a function and produce BFI.
  144. Result run(Function &F, FunctionAnalysisManager &AM);
  145. };
  146. }
  147. #endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H