LLVM  15.0.0git
MachineOptimizationRemarkEmitter.h
Go to the documentation of this file.
1 ///===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- 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 /// \file
9 /// Optimization diagnostic interfaces for machine passes. It's packaged as an
10 /// analysis pass so that by using this service passes become dependent on MBFI
11 /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
12 ///
13 ///===---------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
16 #define LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
17 
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Function.h"
21 
22 namespace llvm {
23 class MachineBasicBlock;
24 class MachineBlockFrequencyInfo;
25 class MachineInstr;
26 
27 /// Common features for diagnostics dealing with optimization remarks
28 /// that are used by machine passes.
30 public:
33  const DiagnosticLocation &Loc,
34  const MachineBasicBlock *MBB)
36  MBB->getParent()->getFunction(), Loc),
37  MBB(MBB) {}
38 
39  /// MI-specific kinds of diagnostic Arguments.
41  /// Print an entire MachineInstr.
43  };
44 
45  static bool classof(const DiagnosticInfo *DI) {
46  return DI->getKind() >= DK_FirstMachineRemark &&
48  }
49 
50  const MachineBasicBlock *getBlock() const { return MBB; }
51 
52 private:
53  const MachineBasicBlock *MBB;
54 };
55 
56 /// Diagnostic information for applied optimization remarks.
58 public:
59  /// \p PassName is the name of the pass emitting this diagnostic. If this name
60  /// matches the regular expression given in -Rpass=, then the diagnostic will
61  /// be emitted. \p RemarkName is a textual identifier for the remark. \p
62  /// Loc is the debug location and \p MBB is the block that the optimization
63  /// operates in.
65  const DiagnosticLocation &Loc,
66  const MachineBasicBlock *MBB)
68  RemarkName, Loc, MBB) {}
69 
70  static bool classof(const DiagnosticInfo *DI) {
71  return DI->getKind() == DK_MachineOptimizationRemark;
72  }
73 
74  /// \see DiagnosticInfoOptimizationBase::isEnabled.
75  bool isEnabled() const override {
76  const Function &Fn = getFunction();
77  LLVMContext &Ctx = Fn.getContext();
79  }
80 };
81 
82 /// Diagnostic information for missed-optimization remarks.
84 public:
85  /// \p PassName is the name of the pass emitting this diagnostic. If this name
86  /// matches the regular expression given in -Rpass-missed=, then the
87  /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
88  /// remark. \p Loc is the debug location and \p MBB is the block that the
89  /// optimization operates in.
91  const DiagnosticLocation &Loc,
92  const MachineBasicBlock *MBB)
94  PassName, RemarkName, Loc, MBB) {}
95 
96  static bool classof(const DiagnosticInfo *DI) {
98  }
99 
100  /// \see DiagnosticInfoOptimizationBase::isEnabled.
101  bool isEnabled() const override {
102  const Function &Fn = getFunction();
103  LLVMContext &Ctx = Fn.getContext();
105  }
106 };
107 
108 /// Diagnostic information for optimization analysis remarks.
110 public:
111  /// \p PassName is the name of the pass emitting this diagnostic. If this name
112  /// matches the regular expression given in -Rpass-analysis=, then the
113  /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
114  /// remark. \p Loc is the debug location and \p MBB is the block that the
115  /// optimization operates in.
117  const DiagnosticLocation &Loc,
118  const MachineBasicBlock *MBB)
120  PassName, RemarkName, Loc, MBB) {}
121 
123  const MachineInstr *MI)
126  MI->getParent()) {}
127 
128  static bool classof(const DiagnosticInfo *DI) {
130  }
131 
132  /// \see DiagnosticInfoOptimizationBase::isEnabled.
133  bool isEnabled() const override {
134  const Function &Fn = getFunction();
135  LLVMContext &Ctx = Fn.getContext();
137  }
138 };
139 
140 /// Extend llvm::ore:: with MI-specific helper names.
141 namespace ore {
143 }
144 
145 /// The optimization diagnostic interface.
146 ///
147 /// It allows reporting when optimizations are performed and when they are not
148 /// along with the reasons for it. Hotness information of the corresponding
149 /// code region can be included in the remark if DiagnosticsHotnessRequested is
150 /// enabled in the LLVM context.
152 public:
155  : MF(MF), MBFI(MBFI) {}
156 
157  /// Emit an optimization remark.
158  void emit(DiagnosticInfoOptimizationBase &OptDiag);
159 
160  /// Whether we allow for extra compile-time budget to perform more
161  /// analysis to be more informative.
162  ///
163  /// This is useful to enable additional missed optimizations to be reported
164  /// that are normally too noisy. In this mode, we can use the extra analysis
165  /// (1) to filter trivial false positives or (2) to provide more context so
166  /// that non-trivial false positives can be quickly detected by the user.
168  return (
171  PassName));
172  }
173 
174  /// Take a lambda that returns a remark which will be emitted. Second
175  /// argument is only used to restrict this to functions.
176  template <typename T>
177  void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
178  // Avoid building the remark unless we know there are at least *some*
179  // remarks enabled. We can't currently check whether remarks are requested
180  // for the calling pass since that requires actually building the remark.
181 
183  MF.getFunction()
184  .getContext()
186  ->isAnyRemarkEnabled()) {
187  auto R = RemarkBuilder();
189  }
190  }
191 
193  return MBFI;
194  }
195 
196 private:
197  MachineFunction &MF;
198 
199  /// MBFI is only set if hotness is requested.
201 
202  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
203  /// available.
204  Optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
205 
206  /// Similar but use value from \p OptDiag and update hotness there.
207  void computeHotness(DiagnosticInfoMIROptimization &Remark);
208 
209  /// Only allow verbose messages if we know we're filtering by hotness
210  /// (BFI is only set in this case).
211  bool shouldEmitVerbose() { return MBFI != nullptr; }
212 };
213 
214 /// The analysis pass
215 ///
216 /// Note that this pass shouldn't generally be marked as preserved by other
217 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
218 /// could be freed.
220  std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
221 
222 public:
224 
225  bool runOnMachineFunction(MachineFunction &MF) override;
226 
227  void getAnalysisUsage(AnalysisUsage &AU) const override;
228 
230  assert(ORE && "pass not run yet");
231  return *ORE;
232  }
233 
234  static char ID;
235 };
236 }
237 
238 #endif
llvm::MachineOptimizationRemarkEmitter::allowExtraAnalysis
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to be more informative.
Definition: MachineOptimizationRemarkEmitter.h:167
llvm::MachineOptimizationRemarkEmitter::emit
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
Definition: MachineOptimizationRemarkEmitter.h:177
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DK_MachineOptimizationRemarkMissed
@ DK_MachineOptimizationRemarkMissed
Definition: DiagnosticInfo.h:79
llvm::DiagnosticHandler::isMissedOptRemarkEnabled
virtual bool isMissedOptRemarkEnabled(StringRef PassName) const
Return true if missed optimization remarks are enabled, override to provide different implementation.
Definition: DiagnosticHandler.cpp:75
llvm::Function
Definition: Function.h:60
llvm::DiagnosticInfoMIROptimization
Common features for diagnostics dealing with optimization remarks that are used by machine passes.
Definition: MachineOptimizationRemarkEmitter.h:29
llvm::DiagnosticInfoOptimizationBase::PassName
const char * PassName
Name of the pass that triggers this report.
Definition: DiagnosticInfo.h:516
llvm::DiagnosticInfoOptimizationBase::getPassName
StringRef getPassName() const
Definition: DiagnosticInfo.h:480
llvm::MachineOptimizationRemarkEmitterPass::getORE
MachineOptimizationRemarkEmitter & getORE()
Definition: MachineOptimizationRemarkEmitter.h:229
llvm::MachineOptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: MachineOptimizationRemarkEmitter.h:151
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::DiagnosticInfo::getKind
int getKind() const
Definition: DiagnosticInfo.h:122
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:320
llvm::DiagnosticInfoOptimizationBase::RemarkName
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
Definition: DiagnosticInfo.h:521
llvm::Optional< uint64_t >
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:414
llvm::DiagnosticKind
DiagnosticKind
Defines the different supported kind of a diagnostic.
Definition: DiagnosticInfo.h:60
llvm::MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass
MachineOptimizationRemarkEmitterPass()
Definition: MachineOptimizationRemarkEmitter.cpp:62
llvm::MachineOptimizationRemark
Diagnostic information for applied optimization remarks.
Definition: MachineOptimizationRemarkEmitter.h:57
llvm::DK_MachineOptimizationRemark
@ DK_MachineOptimizationRemark
Definition: DiagnosticInfo.h:78
llvm::DiagnosticInfoMIROptimization::getBlock
const MachineBasicBlock * getBlock() const
Definition: MachineOptimizationRemarkEmitter.h:50
llvm::MachineBlockFrequencyInfo
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Definition: MachineBlockFrequencyInfo.h:33
llvm::DK_MachineOptimizationRemarkAnalysis
@ DK_MachineOptimizationRemarkAnalysis
Definition: DiagnosticInfo.h:80
llvm::MachineOptimizationRemarkMissed::classof
static bool classof(const DiagnosticInfo *DI)
Definition: MachineOptimizationRemarkEmitter.h:96
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::DiagnosticInfoWithLocationBase::getFunction
const Function & getFunction() const
Definition: DiagnosticInfo.h:401
llvm::MachineOptimizationRemarkAnalysis::MachineOptimizationRemarkAnalysis
MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Definition: MachineOptimizationRemarkEmitter.h:116
llvm::MachineOptimizationRemarkAnalysis
Diagnostic information for optimization analysis remarks.
Definition: MachineOptimizationRemarkEmitter.h:109
llvm::DiagnosticInfo
This is the base abstract class for diagnostic reporting in the backend.
Definition: DiagnosticInfo.h:108
llvm::MachineOptimizationRemarkMissed::MachineOptimizationRemarkMissed
MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Definition: MachineOptimizationRemarkEmitter.h:90
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::LLVMContext::getDiagHandlerPtr
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
Definition: LLVMContext.cpp:358
llvm::MachineOptimizationRemarkMissed::isEnabled
bool isEnabled() const override
Definition: MachineOptimizationRemarkEmitter.h:101
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:427
llvm::MachineOptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
Definition: MachineOptimizationRemarkEmitter.cpp:48
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::MachineOptimizationRemarkMissed
Diagnostic information for missed-optimization remarks.
Definition: MachineOptimizationRemarkEmitter.h:83
llvm::DS_Remark
@ DS_Remark
Definition: DiagnosticInfo.h:52
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::MachineOptimizationRemarkEmitterPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineOptimizationRemarkEmitter.cpp:81
MachineFunctionPass.h
llvm::MachineOptimizationRemarkAnalysis::classof
static bool classof(const DiagnosticInfo *DI)
Definition: MachineOptimizationRemarkEmitter.h:128
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DiagnosticInfoOptimizationBase::Argument::Key
std::string Key
Definition: DiagnosticInfo.h:428
llvm::MachineOptimizationRemarkEmitter::getBFI
MachineBlockFrequencyInfo * getBFI()
Definition: MachineOptimizationRemarkEmitter.h:192
llvm::MachineFunction
Definition: MachineFunction.h:257
getDebugLoc
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
Definition: MachineInstrBundle.cpp:110
llvm::MachineOptimizationRemark::classof
static bool classof(const DiagnosticInfo *DI)
Definition: MachineOptimizationRemarkEmitter.h:70
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MachineOptimizationRemarkEmitterPass
The analysis pass.
Definition: MachineOptimizationRemarkEmitter.h:219
llvm::DiagnosticLocation
Definition: DiagnosticInfo.h:354
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:845
llvm::MachineOptimizationRemarkEmitterPass::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: MachineOptimizationRemarkEmitter.cpp:68
llvm::DiagnosticHandler::isAnalysisRemarkEnabled
virtual bool isAnalysisRemarkEnabled(StringRef PassName) const
Return true if analysis remarks are enabled, override to provide different implementation.
Definition: DiagnosticHandler.cpp:71
llvm::MachineOptimizationRemarkEmitter::MachineOptimizationRemarkEmitter
MachineOptimizationRemarkEmitter(MachineFunction &MF, MachineBlockFrequencyInfo *MBFI)
Definition: MachineOptimizationRemarkEmitter.h:153
llvm::DiagnosticHandler::isPassedOptRemarkEnabled
virtual bool isPassedOptRemarkEnabled(StringRef PassName) const
Return true if passed optimization remarks are enabled, override to provide different implementation.
Definition: DiagnosticHandler.cpp:79
llvm::MachineOptimizationRemarkAnalysis::isEnabled
bool isEnabled() const override
Definition: MachineOptimizationRemarkEmitter.h:133
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:173
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::DK_FirstMachineRemark
@ DK_FirstMachineRemark
Definition: DiagnosticInfo.h:81
llvm::DiagnosticInfoMIROptimization::MachineArgument
MI-specific kinds of diagnostic Arguments.
Definition: MachineOptimizationRemarkEmitter.h:40
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:622
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::DK_LastMachineRemark
@ DK_LastMachineRemark
Definition: DiagnosticInfo.h:82
llvm::MachineOptimizationRemarkAnalysis::MachineOptimizationRemarkAnalysis
MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const MachineInstr *MI)
Definition: MachineOptimizationRemarkEmitter.h:122
DiagnosticInfo.h
Function.h
llvm::MachineOptimizationRemark::MachineOptimizationRemark
MachineOptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Definition: MachineOptimizationRemarkEmitter.h:64
llvm::DiagnosticInfoMIROptimization::DiagnosticInfoMIROptimization
DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
Definition: MachineOptimizationRemarkEmitter.h:31
llvm::DiagnosticInfoMIROptimization::classof
static bool classof(const DiagnosticInfo *DI)
Definition: MachineOptimizationRemarkEmitter.h:45
llvm::MachineOptimizationRemarkEmitterPass::ID
static char ID
Definition: MachineOptimizationRemarkEmitter.h:234
llvm::MachineOptimizationRemark::isEnabled
bool isEnabled() const override
Definition: MachineOptimizationRemarkEmitter.h:75
PassName
static const char PassName[]
Definition: X86LowerAMXIntrinsics.cpp:671
llvm::DiagnosticInfoMIROptimization::MachineArgument::MachineArgument
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
Definition: MachineOptimizationRemarkEmitter.cpp:24