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