Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:16

0001 //===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Optimization diagnostic interfaces.  It's packaged as an analysis pass so
0010 // that by using this service passes become dependent on BFI as well.  BFI is
0011 // used to compute the "hotness" of the diagnostic message.
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
0015 #define LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
0016 
0017 #include "llvm/Analysis/BlockFrequencyInfo.h"
0018 #include "llvm/IR/DiagnosticInfo.h"
0019 #include "llvm/IR/Function.h"
0020 #include "llvm/IR/PassManager.h"
0021 #include "llvm/Pass.h"
0022 #include <optional>
0023 
0024 namespace llvm {
0025 
0026 /// The optimization diagnostic interface.
0027 ///
0028 /// It allows reporting when optimizations are performed and when they are not
0029 /// along with the reasons for it.  Hotness information of the corresponding
0030 /// code region can be included in the remark if DiagnosticsHotnessRequested is
0031 /// enabled in the LLVM context.
0032 class OptimizationRemarkEmitter {
0033 public:
0034   OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
0035       : F(F), BFI(BFI) {}
0036 
0037   /// This variant can be used to generate ORE on demand (without the
0038   /// analysis pass).
0039   ///
0040   /// Note that this ctor has a very different cost depending on whether
0041   /// F->getContext().getDiagnosticsHotnessRequested() is on or not.  If it's off
0042   /// the operation is free.
0043   ///
0044   /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
0045   /// operation since BFI and all its required analyses are computed.  This is
0046   /// for example useful for CGSCC passes that can't use function analyses
0047   /// passes in the old PM.
0048   OptimizationRemarkEmitter(const Function *F);
0049 
0050   OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
0051       : F(Arg.F), BFI(Arg.BFI) {}
0052 
0053   OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) {
0054     F = RHS.F;
0055     BFI = RHS.BFI;
0056     return *this;
0057   }
0058 
0059   /// Handle invalidation events in the new pass manager.
0060   bool invalidate(Function &F, const PreservedAnalyses &PA,
0061                   FunctionAnalysisManager::Invalidator &Inv);
0062 
0063   /// Return true iff at least *some* remarks are enabled.
0064   bool enabled() const {
0065     return F->getContext().getLLVMRemarkStreamer() ||
0066            F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled();
0067   }
0068 
0069   /// Output the remark via the diagnostic handler and to the
0070   /// optimization record file.
0071   void emit(DiagnosticInfoOptimizationBase &OptDiag);
0072 
0073   /// Take a lambda that returns a remark which will be emitted.  Second
0074   /// argument is only used to restrict this to functions.
0075   template <typename T>
0076   void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
0077     // Avoid building the remark unless we know there are at least *some*
0078     // remarks enabled. We can't currently check whether remarks are requested
0079     // for the calling pass since that requires actually building the remark.
0080 
0081     if (enabled()) {
0082       auto R = RemarkBuilder();
0083       static_assert(
0084           std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
0085           "the lambda passed to emit() must return a remark");
0086       emit((DiagnosticInfoOptimizationBase &)R);
0087     }
0088   }
0089 
0090   /// Whether we allow for extra compile-time budget to perform more
0091   /// analysis to produce fewer false positives.
0092   ///
0093   /// This is useful when reporting missed optimizations.  In this case we can
0094   /// use the extra analysis (1) to filter trivial false positives or (2) to
0095   /// provide more context so that non-trivial false positives can be quickly
0096   /// detected by the user.
0097   bool allowExtraAnalysis(StringRef PassName) const {
0098     return OptimizationRemarkEmitter::allowExtraAnalysis(*F, PassName);
0099   }
0100   static bool allowExtraAnalysis(const Function &F, StringRef PassName) {
0101     return allowExtraAnalysis(F.getContext(), PassName);
0102   }
0103   static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName) {
0104     return Ctx.getLLVMRemarkStreamer() ||
0105            Ctx.getDiagHandlerPtr()->isAnyRemarkEnabled(PassName);
0106   }
0107 
0108 private:
0109   const Function *F;
0110 
0111   BlockFrequencyInfo *BFI;
0112 
0113   /// If we generate BFI on demand, we need to free it when ORE is freed.
0114   std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
0115 
0116   /// Compute hotness from IR value (currently assumed to be a block) if PGO is
0117   /// available.
0118   std::optional<uint64_t> computeHotness(const Value *V);
0119 
0120   /// Similar but use value from \p OptDiag and update hotness there.
0121   void computeHotness(DiagnosticInfoIROptimization &OptDiag);
0122 
0123   /// Only allow verbose messages if we know we're filtering by hotness
0124   /// (BFI is only set in this case).
0125   bool shouldEmitVerbose() { return BFI != nullptr; }
0126 
0127   OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
0128   void operator=(const OptimizationRemarkEmitter &) = delete;
0129 };
0130 
0131 /// Add a small namespace to avoid name clashes with the classes used in
0132 /// the streaming interface.  We want these to be short for better
0133 /// write/readability.
0134 namespace ore {
0135 using NV = DiagnosticInfoOptimizationBase::Argument;
0136 using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
0137 using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
0138 }
0139 
0140 /// OptimizationRemarkEmitter legacy analysis pass
0141 ///
0142 /// Note that this pass shouldn't generally be marked as preserved by other
0143 /// passes.  It's holding onto BFI, so if the pass does not preserve BFI, BFI
0144 /// could be freed.
0145 class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
0146   std::unique_ptr<OptimizationRemarkEmitter> ORE;
0147 
0148 public:
0149   OptimizationRemarkEmitterWrapperPass();
0150 
0151   bool runOnFunction(Function &F) override;
0152 
0153   void getAnalysisUsage(AnalysisUsage &AU) const override;
0154 
0155   OptimizationRemarkEmitter &getORE() {
0156     assert(ORE && "pass not run yet");
0157     return *ORE;
0158   }
0159 
0160   static char ID;
0161 };
0162 
0163 class OptimizationRemarkEmitterAnalysis
0164     : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
0165   friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
0166   static AnalysisKey Key;
0167 
0168 public:
0169   /// Provide the result typedef for this analysis pass.
0170   typedef OptimizationRemarkEmitter Result;
0171 
0172   /// Run the analysis pass over a function and produce BFI.
0173   Result run(Function &F, FunctionAnalysisManager &AM);
0174 };
0175 } // namespace llvm
0176 #endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H