LLVM
22.0.0git
include
llvm
Analysis
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/Analysis/BlockFrequencyInfo.h
"
18
#include "
llvm/IR/DiagnosticInfo.h
"
19
#include "
llvm/IR/Function.h
"
20
#include "
llvm/IR/PassManager.h
"
21
#include "
llvm/Pass.h
"
22
#include "
llvm/Support/Compiler.h
"
23
#include <optional>
24
25
namespace
llvm
{
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.
33
class
OptimizationRemarkEmitter
{
34
public
:
35
OptimizationRemarkEmitter
(
const
Function
*F,
BlockFrequencyInfo
*BFI)
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.
49
LLVM_ABI
OptimizationRemarkEmitter
(
const
Function
*
F
);
50
51
OptimizationRemarkEmitter
(
OptimizationRemarkEmitter
&&Arg)
52
: F(Arg.F), BFI(Arg.BFI) {}
53
54
OptimizationRemarkEmitter
&
operator=
(
OptimizationRemarkEmitter
&&
RHS
) {
55
F =
RHS
.F;
56
BFI =
RHS
.BFI;
57
return
*
this
;
58
}
59
60
/// Handle invalidation events in the new pass manager.
61
LLVM_ABI
bool
invalidate
(
Function
&
F
,
const
PreservedAnalyses
&PA,
62
FunctionAnalysisManager::Invalidator &Inv);
63
64
/// Return true iff at least *some* remarks are enabled.
65
bool
enabled
()
const
{
66
return
F->getContext().getLLVMRemarkStreamer() ||
67
F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled();
68
}
69
70
/// Output the remark via the diagnostic handler and to the
71
/// optimization record file.
72
LLVM_ABI
void
emit
(
DiagnosticInfoOptimizationBase
&OptDiag);
73
/// Also allow r-value for OptDiag to allow emitting a temporarily-constructed
74
/// diagnostic.
75
void
emit
(
DiagnosticInfoOptimizationBase
&&OptDiag) {
emit
(OptDiag); }
76
77
/// Take a lambda that returns a remark which will be emitted. Second
78
/// argument is only used to restrict this to functions.
79
template
<
typename
T>
80
void
emit
(
T
RemarkBuilder,
decltype
(RemarkBuilder()) * =
nullptr
) {
81
// Avoid building the remark unless we know there are at least *some*
82
// remarks enabled. We can't currently check whether remarks are requested
83
// for the calling pass since that requires actually building the remark.
84
85
if
(
enabled
()) {
86
auto
R = RemarkBuilder();
87
static_assert
(
88
std::is_base_of<
DiagnosticInfoOptimizationBase
,
decltype
(R)>::value,
89
"the lambda passed to emit() must return a remark"
);
90
emit
((
DiagnosticInfoOptimizationBase
&)R);
91
}
92
}
93
94
/// Whether we allow for extra compile-time budget to perform more
95
/// analysis to produce fewer false positives.
96
///
97
/// This is useful when reporting missed optimizations. In this case we can
98
/// use the extra analysis (1) to filter trivial false positives or (2) to
99
/// provide more context so that non-trivial false positives can be quickly
100
/// detected by the user.
101
bool
allowExtraAnalysis
(
StringRef
PassName
)
const
{
102
return
OptimizationRemarkEmitter::allowExtraAnalysis
(*F,
PassName
);
103
}
104
static
bool
allowExtraAnalysis
(
const
Function
&F,
StringRef
PassName
) {
105
return
allowExtraAnalysis
(F.getContext(),
PassName
);
106
}
107
static
bool
allowExtraAnalysis
(
LLVMContext
&Ctx,
StringRef
PassName
) {
108
return
Ctx.getLLVMRemarkStreamer() ||
109
Ctx.getDiagHandlerPtr()->isAnyRemarkEnabled(
PassName
);
110
}
111
112
private
:
113
const
Function
*
F
;
114
115
BlockFrequencyInfo
*BFI;
116
117
/// If we generate BFI on demand, we need to free it when ORE is freed.
118
std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
119
120
/// Compute hotness from IR value (currently assumed to be a block) if PGO is
121
/// available.
122
std::optional<uint64_t> computeHotness(
const
Value
*V);
123
124
/// Similar but use value from \p OptDiag and update hotness there.
125
void
computeHotness(
DiagnosticInfoIROptimization
&OptDiag);
126
127
/// Only allow verbose messages if we know we're filtering by hotness
128
/// (BFI is only set in this case).
129
bool
shouldEmitVerbose() {
return
BFI !=
nullptr
; }
130
131
OptimizationRemarkEmitter
(
const
OptimizationRemarkEmitter
&) =
delete
;
132
void
operator=
(
const
OptimizationRemarkEmitter
&) =
delete
;
133
};
134
135
/// Add a small namespace to avoid name clashes with the classes used in
136
/// the streaming interface. We want these to be short for better
137
/// write/readability.
138
namespace
ore
{
139
using
NV
=
DiagnosticInfoOptimizationBase::Argument
;
140
using
setIsVerbose
=
DiagnosticInfoOptimizationBase::setIsVerbose
;
141
using
setExtraArgs
=
DiagnosticInfoOptimizationBase::setExtraArgs
;
142
}
143
144
/// OptimizationRemarkEmitter legacy analysis pass
145
///
146
/// Note that this pass shouldn't generally be marked as preserved by other
147
/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
148
/// could be freed.
149
class
LLVM_ABI
OptimizationRemarkEmitterWrapperPass
:
public
FunctionPass
{
150
std::unique_ptr<OptimizationRemarkEmitter> ORE;
151
152
public
:
153
OptimizationRemarkEmitterWrapperPass
();
154
155
bool
runOnFunction
(
Function
&
F
)
override
;
156
157
void
getAnalysisUsage
(
AnalysisUsage
&AU)
const override
;
158
159
OptimizationRemarkEmitter
&
getORE
() {
160
assert
(ORE &&
"pass not run yet"
);
161
return
*ORE;
162
}
163
164
static
char
ID
;
165
};
166
167
class
OptimizationRemarkEmitterAnalysis
168
:
public
AnalysisInfoMixin
<OptimizationRemarkEmitterAnalysis> {
169
friend
AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>
;
170
LLVM_ABI
static
AnalysisKey
Key;
171
172
public
:
173
/// Provide the result typedef for this analysis pass.
174
typedef
OptimizationRemarkEmitter
Result
;
175
176
/// Run the analysis pass over a function and produce BFI.
177
LLVM_ABI
Result
run
(
Function
&
F
,
FunctionAnalysisManager
&AM);
178
};
179
}
// namespace llvm
180
#endif
// LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
assert
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
BlockFrequencyInfo.h
Compiler.h
LLVM_ABI
#define LLVM_ABI
Definition
Compiler.h:213
DiagnosticInfo.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition
EntryExitInstrumenter.cpp:103
Function.h
PassManager.h
This header defines various interfaces for pass management in LLVM.
F
#define F(x, y, z)
Definition
MD5.cpp:55
T
#define T
Definition
Mips16ISelLowering.cpp:353
Pass.h
PassName
static const char PassName[]
Definition
X86LowerAMXIntrinsics.cpp:663
RHS
Value * RHS
Definition
X86PartialReduction.cpp:74
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition
PassAnalysisSupport.h:48
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition
BlockFrequencyInfo.h:38
llvm::DiagnosticInfoIROptimization
Common features for diagnostics dealing with optimization remarks that are used by IR passes.
Definition
DiagnosticInfo.h:698
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition
DiagnosticInfo.h:523
llvm::FunctionPass::FunctionPass
FunctionPass(char &pid)
Definition
Pass.h:316
llvm::Function
Definition
Function.h:64
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition
LLVMContext.h:68
llvm::OptimizationRemarkEmitterAnalysis
Definition
OptimizationRemarkEmitter.h:168
llvm::OptimizationRemarkEmitterAnalysis::Result
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
Definition
OptimizationRemarkEmitter.h:174
llvm::OptimizationRemarkEmitterAnalysis::run
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Definition
OptimizationRemarkEmitter.cpp:126
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:116
llvm::OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitterWrapperPass()
Definition
OptimizationRemarkEmitter.cpp:93
llvm::OptimizationRemarkEmitterWrapperPass::getORE
OptimizationRemarkEmitter & getORE()
Definition
OptimizationRemarkEmitter.h:159
llvm::OptimizationRemarkEmitterWrapperPass::ID
static char ID
Definition
OptimizationRemarkEmitter.h:164
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition
OptimizationRemarkEmitter.h:33
llvm::OptimizationRemarkEmitter::invalidate
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
Definition
OptimizationRemarkEmitter.cpp:48
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
static bool allowExtraAnalysis(const Function &F, StringRef PassName)
Definition
OptimizationRemarkEmitter.h:104
llvm::OptimizationRemarkEmitter::enabled
bool enabled() const
Return true iff at least some remarks are enabled.
Definition
OptimizationRemarkEmitter.h:65
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:101
llvm::OptimizationRemarkEmitter::OptimizationRemarkEmitter
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
Definition
OptimizationRemarkEmitter.h:35
llvm::OptimizationRemarkEmitter::emit
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Definition
OptimizationRemarkEmitter.cpp:79
llvm::OptimizationRemarkEmitter::operator=
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
Definition
OptimizationRemarkEmitter.h:54
llvm::OptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &&OptDiag)
Also allow r-value for OptDiag to allow emitting a temporarily-constructed diagnostic.
Definition
OptimizationRemarkEmitter.h:75
llvm::OptimizationRemarkEmitter::emit
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
Definition
OptimizationRemarkEmitter.h:80
llvm::OptimizationRemarkEmitter::OptimizationRemarkEmitter
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
Definition
OptimizationRemarkEmitter.h:51
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName)
Definition
OptimizationRemarkEmitter.h:107
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition
Analysis.h:112
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition
StringRef.h:55
llvm::Value
LLVM Value Representation.
Definition
Value.h:75
llvm::ore
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
Definition
OptimizationRemarkEmitter.h:138
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition
OptimizationRemarkEmitter.h:139
llvm::ore::setIsVerbose
DiagnosticInfoOptimizationBase::setIsVerbose setIsVerbose
Definition
OptimizationRemarkEmitter.h:140
llvm::ore::setExtraArgs
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
Definition
OptimizationRemarkEmitter.h:141
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition
AddressRanges.h:18
llvm::FunctionAnalysisManager
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
Definition
PassManager.h:564
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition
PassManager.h:93
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition
Analysis.h:29
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition
DiagnosticInfo.h:536
llvm::DiagnosticInfoOptimizationBase::setExtraArgs
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Definition
DiagnosticInfo.h:532
llvm::DiagnosticInfoOptimizationBase::setIsVerbose
Used to set IsVerbose via the stream interface.
Definition
DiagnosticInfo.h:526
Generated on
for LLVM by
1.14.0