LLVM  14.0.0git
OptimizationRemarkEmitter.h
Go to the documentation of this file.
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"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Pass.h"
22 
23 namespace llvm {
24 class Function;
25 class 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.
34 public:
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.
50 
52  : F(Arg.F), BFI(Arg.BFI) {}
53 
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,
63 
64  /// Return true iff at least *some* remarks are enabled.
65  bool enabled() const {
66  return F->getContext().getLLVMRemarkStreamer() ||
68  }
69 
70  /// Output the remark via the diagnostic handler and to the
71  /// optimization record file.
72  void emit(DiagnosticInfoOptimizationBase &OptDiag);
73 
74  /// Take a lambda that returns a remark which will be emitted. Second
75  /// argument is only used to restrict this to functions.
76  template <typename T>
77  void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
78  // Avoid building the remark unless we know there are at least *some*
79  // remarks enabled. We can't currently check whether remarks are requested
80  // for the calling pass since that requires actually building the remark.
81 
82  if (enabled()) {
83  auto R = RemarkBuilder();
84  static_assert(
85  std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
86  "the lambda passed to emit() must return a remark");
88  }
89  }
90 
91  /// Whether we allow for extra compile-time budget to perform more
92  /// analysis to produce fewer false positives.
93  ///
94  /// This is useful when reporting missed optimizations. In this case we can
95  /// use the extra analysis (1) to filter trivial false positives or (2) to
96  /// provide more context so that non-trivial false positives can be quickly
97  /// detected by the user.
100  }
102  return allowExtraAnalysis(F.getContext(), PassName);
103  }
105  return Ctx.getLLVMRemarkStreamer() ||
107  }
108 
109 private:
110  const Function *F;
111 
112  BlockFrequencyInfo *BFI;
113 
114  /// If we generate BFI on demand, we need to free it when ORE is freed.
115  std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
116 
117  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
118  /// available.
119  Optional<uint64_t> computeHotness(const Value *V);
120 
121  /// Similar but use value from \p OptDiag and update hotness there.
122  void computeHotness(DiagnosticInfoIROptimization &OptDiag);
123 
124  /// Only allow verbose messages if we know we're filtering by hotness
125  /// (BFI is only set in this case).
126  bool shouldEmitVerbose() { return BFI != nullptr; }
127 
129  void operator=(const OptimizationRemarkEmitter &) = delete;
130 };
131 
132 /// Add a small namespace to avoid name clashes with the classes used in
133 /// the streaming interface. We want these to be short for better
134 /// write/readability.
135 namespace ore {
139 }
140 
141 /// OptimizationRemarkEmitter legacy analysis pass
142 ///
143 /// Note that this pass shouldn't generally be marked as preserved by other
144 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
145 /// could be freed.
147  std::unique_ptr<OptimizationRemarkEmitter> ORE;
148 
149 public:
151 
152  bool runOnFunction(Function &F) override;
153 
154  void getAnalysisUsage(AnalysisUsage &AU) const override;
155 
157  assert(ORE && "pass not run yet");
158  return *ORE;
159  }
160 
161  static char ID;
162 };
163 
165  : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
167  static AnalysisKey Key;
168 
169 public:
170  /// Provide the result typedef for this analysis pass.
172 
173  /// Run the analysis pass over a function and produce BFI.
175 };
176 }
177 #endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Optional.h
llvm::Function
Definition: Function.h:61
Pass.h
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
Definition: OptimizationRemarkEmitter.h:98
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
llvm::OptimizationRemarkEmitterWrapperPass::getORE
OptimizationRemarkEmitter & getORE()
Definition: OptimizationRemarkEmitter.h:156
llvm::Optional< uint64_t >
llvm::DiagnosticInfoIROptimization
Common features for diagnostics dealing with optimization remarks that are used by IR passes.
Definition: DiagnosticInfo.h:617
llvm::OptimizationRemarkEmitter::OptimizationRemarkEmitter
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
Definition: OptimizationRemarkEmitter.h:35
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:408
llvm::OptimizationRemarkEmitter::operator=
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
Definition: OptimizationRemarkEmitter.h:54
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::OptimizationRemarkEmitterWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: OptimizationRemarkEmitter.cpp:117
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::OptimizationRemarkEmitter::emit
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
Definition: OptimizationRemarkEmitter.h:77
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:656
llvm::LLVMContext::getDiagHandlerPtr
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
Definition: LLVMContext.cpp:343
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:421
llvm::OptimizationRemarkEmitterWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: OptimizationRemarkEmitter.cpp:97
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::OptimizationRemarkEmitter::enabled
bool enabled() const
Return true iff at least some remarks are enabled.
Definition: OptimizationRemarkEmitter.h:65
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::OptimizationRemarkEmitter::invalidate
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
Definition: OptimizationRemarkEmitter.cpp:47
llvm::OptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Definition: OptimizationRemarkEmitter.cpp:77
llvm::DiagnosticInfoOptimizationBase::setExtraArgs
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Definition: DiagnosticInfo.h:417
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:391
llvm::OptimizationRemarkEmitterAnalysis::Result
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
Definition: OptimizationRemarkEmitter.h:171
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::OptimizationRemarkEmitterAnalysis::run
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Definition: OptimizationRemarkEmitter.cpp:127
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
BlockFrequencyInfo.h
llvm::OptimizationRemarkEmitter::OptimizationRemarkEmitter
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
Definition: OptimizationRemarkEmitter.h:51
llvm::LLVMContext::getLLVMRemarkStreamer
LLVMRemarkStreamer * getLLVMRemarkStreamer()
The "LLVM remark streamer" used by LLVM to serialize remark diagnostics comming from IR and MIR passe...
Definition: LLVMContext.cpp:158
llvm::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitter legacy analysis pass.
Definition: OptimizationRemarkEmitter.h:146
llvm::DiagnosticHandler::isAnyRemarkEnabled
bool isAnyRemarkEnabled(StringRef PassName) const
Return true if any type of remarks are enabled for this pass.
Definition: DiagnosticHandler.h:63
llvm::ore::setExtraArgs
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
Definition: OptimizationRemarkEmitter.h:138
DiagnosticInfo.h
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
PassManager.h
llvm::OptimizationRemarkEmitterWrapperPass::ID
static char ID
Definition: OptimizationRemarkEmitter.h:161
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName)
Definition: OptimizationRemarkEmitter.h:104
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
static bool allowExtraAnalysis(const Function &F, StringRef PassName)
Definition: OptimizationRemarkEmitter.h:101
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitterWrapperPass()
Definition: OptimizationRemarkEmitter.cpp:91
llvm::OptimizationRemarkEmitterAnalysis
Definition: OptimizationRemarkEmitter.h:164
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
PassName
static const char PassName[]
Definition: X86LowerAMXIntrinsics.cpp:669
llvm::DiagnosticInfoOptimizationBase::setIsVerbose
Used to set IsVerbose via the stream interface.
Definition: DiagnosticInfo.h:411
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::ore::setIsVerbose
DiagnosticInfoOptimizationBase::setIsVerbose setIsVerbose
Definition: OptimizationRemarkEmitter.h:137