LLVM 22.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"
23#include <optional>
24
25namespace llvm {
26class MachineBasicBlock;
27class MachineBlockFrequencyInfo;
28class MachineInstr;
29
30/// Common features for diagnostics dealing with optimization remarks
31/// that are used by machine passes.
33public:
36 const DiagnosticLocation &Loc,
37 const MachineBasicBlock *MBB)
39 MBB->getParent()->getFunction(), Loc),
40 MBB(MBB) {}
41
42 /// MI-specific kinds of diagnostic Arguments.
44 /// Print an entire MachineInstr.
46 };
47
48 static bool classof(const DiagnosticInfo *DI) {
49 return DI->getKind() >= DK_FirstMachineRemark &&
51 }
52
53 const MachineBasicBlock *getBlock() const { return MBB; }
54
55private:
56 const MachineBasicBlock *MBB;
57};
58
59/// Diagnostic information for applied optimization remarks.
61public:
62 /// \p PassName is the name of the pass emitting this diagnostic. If this name
63 /// matches the regular expression given in -Rpass=, then the diagnostic will
64 /// be emitted. \p RemarkName is a textual identifier for the remark. \p
65 /// Loc is the debug location and \p MBB is the block that the optimization
66 /// operates in.
68 const DiagnosticLocation &Loc,
69 const MachineBasicBlock *MBB)
71 RemarkName, Loc, MBB) {}
72
73 static bool classof(const DiagnosticInfo *DI) {
75 }
76
77 /// \see DiagnosticInfoOptimizationBase::isEnabled.
78 bool isEnabled() const override {
79 const Function &Fn = getFunction();
80 LLVMContext &Ctx = Fn.getContext();
82 }
83};
84
85/// Diagnostic information for missed-optimization remarks.
87public:
88 /// \p PassName is the name of the pass emitting this diagnostic. If this name
89 /// matches the regular expression given in -Rpass-missed=, then the
90 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
91 /// remark. \p Loc is the debug location and \p MBB is the block that the
92 /// optimization operates in.
94 const DiagnosticLocation &Loc,
95 const MachineBasicBlock *MBB)
97 PassName, RemarkName, Loc, MBB) {}
98
99 static bool classof(const DiagnosticInfo *DI) {
101 }
102
103 /// \see DiagnosticInfoOptimizationBase::isEnabled.
104 bool isEnabled() const override {
105 const Function &Fn = getFunction();
106 LLVMContext &Ctx = Fn.getContext();
108 }
109};
110
111/// Diagnostic information for optimization analysis remarks.
113public:
114 /// \p PassName is the name of the pass emitting this diagnostic. If this name
115 /// matches the regular expression given in -Rpass-analysis=, then the
116 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
117 /// remark. \p Loc is the debug location and \p MBB is the block that the
118 /// optimization operates in.
120 const DiagnosticLocation &Loc,
121 const MachineBasicBlock *MBB)
123 PassName, RemarkName, Loc, MBB) {}
124
126 const MachineInstr *MI)
129 MI->getParent()) {}
130
131 static bool classof(const DiagnosticInfo *DI) {
133 }
134
135 /// \see DiagnosticInfoOptimizationBase::isEnabled.
136 bool isEnabled() const override {
137 const Function &Fn = getFunction();
138 LLVMContext &Ctx = Fn.getContext();
140 }
141};
142
143/// Extend llvm::ore:: with MI-specific helper names.
144namespace ore {
146}
147
148/// The optimization diagnostic interface.
149///
150/// It allows reporting when optimizations are performed and when they are not
151/// along with the reasons for it. Hotness information of the corresponding
152/// code region can be included in the remark if DiagnosticsHotnessRequested is
153/// enabled in the LLVM context.
155public:
158 : MF(MF), MBFI(MBFI) {}
159
161 default;
162
163 /// Handle invalidation events in the new pass manager.
166
167 /// Emit an optimization remark.
169
170 /// Whether we allow for extra compile-time budget to perform more
171 /// analysis to be more informative.
172 ///
173 /// This is useful to enable additional missed optimizations to be reported
174 /// that are normally too noisy. In this mode, we can use the extra analysis
175 /// (1) to filter trivial false positives or (2) to provide more context so
176 /// that non-trivial false positives can be quickly detected by the user.
178 return (
181 PassName));
182 }
183
184 /// Take a lambda that returns a remark which will be emitted. Second
185 /// argument is only used to restrict this to functions.
186 template <typename T>
187 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
188 // Avoid building the remark unless we know there are at least *some*
189 // remarks enabled. We can't currently check whether remarks are requested
190 // for the calling pass since that requires actually building the remark.
191
193 MF.getFunction()
194 .getContext()
196 ->isAnyRemarkEnabled()) {
197 auto R = RemarkBuilder();
199 }
200 }
201
203 return MBFI;
204 }
205
206private:
207 MachineFunction &MF;
208
209 /// MBFI is only set if hotness is requested.
211
212 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
213 /// available.
214 std::optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
215
216 /// Similar but use value from \p OptDiag and update hotness there.
217 void computeHotness(DiagnosticInfoMIROptimization &Remark);
218
219 /// Only allow verbose messages if we know we're filtering by hotness
220 /// (BFI is only set in this case).
221 bool shouldEmitVerbose() { return MBFI != nullptr; }
222};
223
224/// The analysis pass
226 : public AnalysisInfoMixin<MachineOptimizationRemarkEmitterAnalysis> {
228 LLVM_ABI static AnalysisKey Key;
229
230public:
234};
235
236/// The analysis pass
237///
238/// Note that this pass shouldn't generally be marked as preserved by other
239/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
240/// could be freed.
242 : public MachineFunctionPass {
243 std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
244
245public:
247
248 bool runOnMachineFunction(MachineFunction &MF) override;
249
250 void getAnalysisUsage(AnalysisUsage &AU) const override;
251
253 assert(ORE && "pass not run yet");
254 return *ORE;
255 }
256
257 static char ID;
258};
259}
260
261#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static const Function * getParent(const Value *V)
#define LLVM_ABI
Definition: Compiler.h:213
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.
static const char PassName[]
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
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, CamelCase).
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:359
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
LLVM_ABI const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
LLVM_ABI 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:72
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.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI 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)
LLVM_ABI 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:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
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.