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/ADT/Optional.h" 18#include "llvm/Analysis/BlockFrequencyInfo.h" 19#include "llvm/IR/DiagnosticInfo.h" 20#include "llvm/IR/PassManager.h" 21#include "llvm/Pass.h" 22 23namespace llvm { 24class Function; 25class Value; 26 27/// The optimization diagnostic interface. 28/// 29/// It allows reporting when optimizations are performed and when they are not 30/// along with the reasons for it. Hotness information of the corresponding 31/// code region can be included in the remark if DiagnosticsHotnessRequested is 32/// enabled in the LLVM context. 33class OptimizationRemarkEmitter { 34public: 35 OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI) 36 : F(F), BFI(BFI) {} 37 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 51 OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg) 52 : F(Arg.F), BFI(Arg.BFI) {} 53 54 OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) { 55 F = RHS.F; 56 BFI = RHS.BFI; 57 return *this; 58 } 59 60 /// Handle invalidation events in the new pass manager. 61 bool invalidate(Function &F, const PreservedAnalyses &PA, 62 FunctionAnalysisManager::Invalidator &Inv); 63 64 /// Output the remark via the diagnostic handler and to the 65 /// optimization record file. 66 void emit(DiagnosticInfoOptimizationBase &OptDiag); 67 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 76 if (F->getContext().getLLVMRemarkStreamer() || 77 F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) { 78 auto R = RemarkBuilder(); 79 emit((DiagnosticInfoOptimizationBase &)R); 80 } 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 101private: 102 const Function *F; 103 104 BlockFrequencyInfo *BFI; 105 106 /// If we generate BFI on demand, we need to free it when ORE is freed. 107 std::unique_ptr<BlockFrequencyInfo> OwnedBFI; 108 109 /// Compute hotness from IR value (currently assumed to be a block) if PGO is 110 /// available. 111 Optional<uint64_t> computeHotness(const Value *V); 112 113 /// Similar but use value from \p OptDiag and update hotness there. 114 void computeHotness(DiagnosticInfoIROptimization &OptDiag); 115 116 /// Only allow verbose messages if we know we're filtering by hotness 117 /// (BFI is only set in this case). 118 bool shouldEmitVerbose() { return BFI != nullptr; } 119 120 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete; 121 void operator=(const OptimizationRemarkEmitter &) = delete; 122}; 123 124/// Add a small namespace to avoid name clashes with the classes used in 125/// the streaming interface. We want these to be short for better 126/// write/readability. 127namespace ore { 128using NV = DiagnosticInfoOptimizationBase::Argument; 129using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose; 130using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs; 131} 132 133/// OptimizationRemarkEmitter legacy analysis pass 134/// 135/// Note that this pass shouldn't generally be marked as preserved by other 136/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI 137/// could be freed. 138class OptimizationRemarkEmitterWrapperPass : public FunctionPass { 139 std::unique_ptr<OptimizationRemarkEmitter> ORE; 140 141public: 142 OptimizationRemarkEmitterWrapperPass(); 143 144 bool runOnFunction(Function &F) override; 145 146 void getAnalysisUsage(AnalysisUsage &AU) const override; 147 148 OptimizationRemarkEmitter &getORE() { 149 assert(ORE && "pass not run yet"); 150 return *ORE; 151 } 152 153 static char ID; 154}; 155 156class OptimizationRemarkEmitterAnalysis 157 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> { 158 friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>; 159 static AnalysisKey Key; 160 161public: 162 /// Provide the result typedef for this analysis pass. 163 typedef OptimizationRemarkEmitter Result; 164 165 /// Run the analysis pass over a function and produce BFI. 166 Result run(Function &F, FunctionAnalysisManager &AM); 167}; 168} 169#endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H 170