OptimizationRemarkEmitter.h 6.6 KB

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