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