LLVM 20.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
21#include "llvm/IR/Function.h"
22#include <optional>
23
24namespace llvm {
25class MachineBasicBlock;
26class MachineBlockFrequencyInfo;
27class MachineInstr;
28
29/// Common features for diagnostics dealing with optimization remarks
30/// that are used by machine passes.
32public:
35 const DiagnosticLocation &Loc,
36 const MachineBasicBlock *MBB)
38 MBB->getParent()->getFunction(), Loc),
39 MBB(MBB) {}
40
41 /// MI-specific kinds of diagnostic Arguments.
43 /// Print an entire MachineInstr.
45 };
46
47 static bool classof(const DiagnosticInfo *DI) {
48 return DI->getKind() >= DK_FirstMachineRemark &&
50 }
51
52 const MachineBasicBlock *getBlock() const { return MBB; }
53
54private:
55 const MachineBasicBlock *MBB;
56};
57
58/// Diagnostic information for applied optimization remarks.
60public:
61 /// \p PassName is the name of the pass emitting this diagnostic. If this name
62 /// matches the regular expression given in -Rpass=, then the diagnostic will
63 /// be emitted. \p RemarkName is a textual identifier for the remark. \p
64 /// Loc is the debug location and \p MBB is the block that the optimization
65 /// operates in.
67 const DiagnosticLocation &Loc,
68 const MachineBasicBlock *MBB)
70 RemarkName, Loc, MBB) {}
71
72 static bool classof(const DiagnosticInfo *DI) {
74 }
75
76 /// \see DiagnosticInfoOptimizationBase::isEnabled.
77 bool isEnabled() const override {
78 const Function &Fn = getFunction();
79 LLVMContext &Ctx = Fn.getContext();
81 }
82};
83
84/// Diagnostic information for missed-optimization remarks.
86public:
87 /// \p PassName is the name of the pass emitting this diagnostic. If this name
88 /// matches the regular expression given in -Rpass-missed=, then the
89 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
90 /// remark. \p Loc is the debug location and \p MBB is the block that the
91 /// optimization operates in.
93 const DiagnosticLocation &Loc,
94 const MachineBasicBlock *MBB)
96 PassName, RemarkName, Loc, MBB) {}
97
98 static bool classof(const DiagnosticInfo *DI) {
100 }
101
102 /// \see DiagnosticInfoOptimizationBase::isEnabled.
103 bool isEnabled() const override {
104 const Function &Fn = getFunction();
105 LLVMContext &Ctx = Fn.getContext();
107 }
108};
109
110/// Diagnostic information for optimization analysis remarks.
112public:
113 /// \p PassName is the name of the pass emitting this diagnostic. If this name
114 /// matches the regular expression given in -Rpass-analysis=, then the
115 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
116 /// remark. \p Loc is the debug location and \p MBB is the block that the
117 /// optimization operates in.
119 const DiagnosticLocation &Loc,
120 const MachineBasicBlock *MBB)
122 PassName, RemarkName, Loc, MBB) {}
123
125 const MachineInstr *MI)
128 MI->getParent()) {}
129
130 static bool classof(const DiagnosticInfo *DI) {
132 }
133
134 /// \see DiagnosticInfoOptimizationBase::isEnabled.
135 bool isEnabled() const override {
136 const Function &Fn = getFunction();
137 LLVMContext &Ctx = Fn.getContext();
139 }
140};
141
142/// Extend llvm::ore:: with MI-specific helper names.
143namespace ore {
145}
146
147/// The optimization diagnostic interface.
148///
149/// It allows reporting when optimizations are performed and when they are not
150/// along with the reasons for it. Hotness information of the corresponding
151/// code region can be included in the remark if DiagnosticsHotnessRequested is
152/// enabled in the LLVM context.
154public:
157 : MF(MF), MBFI(MBFI) {}
158
160 default;
161
162 /// Handle invalidation events in the new pass manager.
163 bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
165
166 /// Emit an optimization remark.
168
169 /// Whether we allow for extra compile-time budget to perform more
170 /// analysis to be more informative.
171 ///
172 /// This is useful to enable additional missed optimizations to be reported
173 /// that are normally too noisy. In this mode, we can use the extra analysis
174 /// (1) to filter trivial false positives or (2) to provide more context so
175 /// that non-trivial false positives can be quickly detected by the user.
177 return (
180 PassName));
181 }
182
183 /// Take a lambda that returns a remark which will be emitted. Second
184 /// argument is only used to restrict this to functions.
185 template <typename T>
186 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
187 // Avoid building the remark unless we know there are at least *some*
188 // remarks enabled. We can't currently check whether remarks are requested
189 // for the calling pass since that requires actually building the remark.
190
192 MF.getFunction()
193 .getContext()
195 ->isAnyRemarkEnabled()) {
196 auto R = RemarkBuilder();
198 }
199 }
200
202 return MBFI;
203 }
204
205private:
206 MachineFunction &MF;
207
208 /// MBFI is only set if hotness is requested.
210
211 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
212 /// available.
213 std::optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
214
215 /// Similar but use value from \p OptDiag and update hotness there.
216 void computeHotness(DiagnosticInfoMIROptimization &Remark);
217
218 /// Only allow verbose messages if we know we're filtering by hotness
219 /// (BFI is only set in this case).
220 bool shouldEmitVerbose() { return MBFI != nullptr; }
221};
222
223/// The analysis pass
225 : public AnalysisInfoMixin<MachineOptimizationRemarkEmitterAnalysis> {
227 static AnalysisKey Key;
228
229public:
232};
233
234/// The analysis pass
235///
236/// Note that this pass shouldn't generally be marked as preserved by other
237/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
238/// could be freed.
240 std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
241
242public:
244
245 bool runOnMachineFunction(MachineFunction &MF) override;
246
247 void getAnalysisUsage(AnalysisUsage &AU) const override;
248
250 assert(ORE && "pass not run yet");
251 return *ORE;
252 }
253
254 static char ID;
255};
256}
257
258#endif
MachineBasicBlock & MBB
static const Function * getParent(const Value *V)
IRTranslator LLVM IR MI
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char PassName[]
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
Common features for diagnostics dealing with optimization remarks that are used by machine passes.
static bool classof(const DiagnosticInfo *DI)
DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
const char * PassName
Name of the pass that triggers this report.
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
const Function & getFunction() const
This is the base abstract class for diagnostic reporting in the backend.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
LLVMRemarkStreamer * getLLVMRemarkStreamer()
The "LLVM remark streamer" used by LLVM to serialize remark diagnostics comming from IR and MIR passe...
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
Diagnostic information for optimization analysis remarks.
MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const MachineInstr *MI)
MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to be more informative.
MachineOptimizationRemarkEmitter(MachineFunction &MF, MachineBlockFrequencyInfo *MBFI)
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
MachineOptimizationRemarkEmitter(MachineOptimizationRemarkEmitter &&)=default
Diagnostic information for missed-optimization remarks.
static bool classof(const DiagnosticInfo *DI)
MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Diagnostic information for applied optimization remarks.
static bool classof(const DiagnosticInfo *DI)
MachineOptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DiagnosticKind
Defines the different supported kind of a diagnostic.
@ DK_LastMachineRemark
@ DK_MachineOptimizationRemark
@ DK_MachineOptimizationRemarkAnalysis
@ DK_FirstMachineRemark
@ DK_MachineOptimizationRemarkMissed
@ DS_Remark
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
virtual bool isPassedOptRemarkEnabled(StringRef PassName) const
Return true if passed optimization remarks are enabled, override to provide different implementation.
virtual bool isAnalysisRemarkEnabled(StringRef PassName) const
Return true if analysis remarks are enabled, override to provide different implementation.
virtual bool isMissedOptRemarkEnabled(StringRef PassName) const
Return true if missed optimization remarks are enabled, override to provide different implementation.
bool isAnyRemarkEnabled(StringRef PassName) const
Return true if any type of remarks are enabled for this pass.
Used in the streaming interface as the general argument type.