OptimizationRemarkEmitter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_IR_OPTIMIZATIONDIAGNOSTICINFO_H
  19. #define LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_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. /// 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 (F->getContext().getLLVMRemarkStreamer() ||
  71. F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) {
  72. auto R = RemarkBuilder();
  73. emit((DiagnosticInfoOptimizationBase &)R);
  74. }
  75. }
  76. /// Whether we allow for extra compile-time budget to perform more
  77. /// analysis to produce fewer false positives.
  78. ///
  79. /// This is useful when reporting missed optimizations. In this case we can
  80. /// use the extra analysis (1) to filter trivial false positives or (2) to
  81. /// provide more context so that non-trivial false positives can be quickly
  82. /// detected by the user.
  83. bool allowExtraAnalysis(StringRef PassName) const {
  84. return OptimizationRemarkEmitter::allowExtraAnalysis(*F, PassName);
  85. }
  86. static bool allowExtraAnalysis(const Function &F, StringRef PassName) {
  87. return allowExtraAnalysis(F.getContext(), PassName);
  88. }
  89. static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName) {
  90. return Ctx.getLLVMRemarkStreamer() ||
  91. Ctx.getDiagHandlerPtr()->isAnyRemarkEnabled(PassName);
  92. }
  93. private:
  94. const Function *F;
  95. BlockFrequencyInfo *BFI;
  96. /// If we generate BFI on demand, we need to free it when ORE is freed.
  97. std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
  98. /// Compute hotness from IR value (currently assumed to be a block) if PGO is
  99. /// available.
  100. Optional<uint64_t> computeHotness(const Value *V);
  101. /// Similar but use value from \p OptDiag and update hotness there.
  102. void computeHotness(DiagnosticInfoIROptimization &OptDiag);
  103. /// Only allow verbose messages if we know we're filtering by hotness
  104. /// (BFI is only set in this case).
  105. bool shouldEmitVerbose() { return BFI != nullptr; }
  106. OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
  107. void operator=(const OptimizationRemarkEmitter &) = delete;
  108. };
  109. /// Add a small namespace to avoid name clashes with the classes used in
  110. /// the streaming interface. We want these to be short for better
  111. /// write/readability.
  112. namespace ore {
  113. using NV = DiagnosticInfoOptimizationBase::Argument;
  114. using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
  115. using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
  116. }
  117. /// OptimizationRemarkEmitter legacy analysis pass
  118. ///
  119. /// Note that this pass shouldn't generally be marked as preserved by other
  120. /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
  121. /// could be freed.
  122. class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
  123. std::unique_ptr<OptimizationRemarkEmitter> ORE;
  124. public:
  125. OptimizationRemarkEmitterWrapperPass();
  126. bool runOnFunction(Function &F) override;
  127. void getAnalysisUsage(AnalysisUsage &AU) const override;
  128. OptimizationRemarkEmitter &getORE() {
  129. assert(ORE && "pass not run yet");
  130. return *ORE;
  131. }
  132. static char ID;
  133. };
  134. class OptimizationRemarkEmitterAnalysis
  135. : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
  136. friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
  137. static AnalysisKey Key;
  138. public:
  139. /// Provide the result typedef for this analysis pass.
  140. typedef OptimizationRemarkEmitter Result;
  141. /// Run the analysis pass over a function and produce BFI.
  142. Result run(Function &F, FunctionAnalysisManager &AM);
  143. };
  144. }
  145. #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
  146. #ifdef __GNUC__
  147. #pragma GCC diagnostic pop
  148. #endif