LLVM 20.0.0git
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
19#include "llvm/IR/Function.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Pass.h"
22#include <optional>
23
24namespace llvm {
25
26/// The optimization diagnostic interface.
27///
28/// It allows reporting when optimizations are performed and when they are not
29/// along with the reasons for it. Hotness information of the corresponding
30/// code region can be included in the remark if DiagnosticsHotnessRequested is
31/// enabled in the LLVM context.
33public:
35 : F(F), BFI(BFI) {}
36
37 /// This variant can be used to generate ORE on demand (without the
38 /// analysis pass).
39 ///
40 /// Note that this ctor has a very different cost depending on whether
41 /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
42 /// the operation is free.
43 ///
44 /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
45 /// operation since BFI and all its required analyses are computed. This is
46 /// for example useful for CGSCC passes that can't use function analyses
47 /// passes in the old PM.
49
51 : F(Arg.F), BFI(Arg.BFI) {}
52
54 F = RHS.F;
55 BFI = RHS.BFI;
56 return *this;
57 }
58
59 /// Handle invalidation events in the new pass manager.
60 bool invalidate(Function &F, const PreservedAnalyses &PA,
62
63 /// Return true iff at least *some* remarks are enabled.
64 bool enabled() const {
65 return F->getContext().getLLVMRemarkStreamer() ||
67 }
68
69 /// Output the remark via the diagnostic handler and to the
70 /// optimization record file.
72
73 /// Take a lambda that returns a remark which will be emitted. Second
74 /// argument is only used to restrict this to functions.
75 template <typename T>
76 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
77 // Avoid building the remark unless we know there are at least *some*
78 // remarks enabled. We can't currently check whether remarks are requested
79 // for the calling pass since that requires actually building the remark.
80
81 if (enabled()) {
82 auto R = RemarkBuilder();
83 static_assert(
84 std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
85 "the lambda passed to emit() must return a remark");
87 }
88 }
89
90 /// Whether we allow for extra compile-time budget to perform more
91 /// analysis to produce fewer false positives.
92 ///
93 /// This is useful when reporting missed optimizations. In this case we can
94 /// use the extra analysis (1) to filter trivial false positives or (2) to
95 /// provide more context so that non-trivial false positives can be quickly
96 /// detected by the user.
99 }
101 return allowExtraAnalysis(F.getContext(), PassName);
102 }
104 return Ctx.getLLVMRemarkStreamer() ||
106 }
107
108private:
109 const Function *F;
110
112
113 /// If we generate BFI on demand, we need to free it when ORE is freed.
114 std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
115
116 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
117 /// available.
118 std::optional<uint64_t> computeHotness(const Value *V);
119
120 /// Similar but use value from \p OptDiag and update hotness there.
121 void computeHotness(DiagnosticInfoIROptimization &OptDiag);
122
123 /// Only allow verbose messages if we know we're filtering by hotness
124 /// (BFI is only set in this case).
125 bool shouldEmitVerbose() { return BFI != nullptr; }
126
127 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
128 void operator=(const OptimizationRemarkEmitter &) = delete;
129};
130
131/// Add a small namespace to avoid name clashes with the classes used in
132/// the streaming interface. We want these to be short for better
133/// write/readability.
134namespace ore {
138}
139
140/// OptimizationRemarkEmitter legacy analysis pass
141///
142/// Note that this pass shouldn't generally be marked as preserved by other
143/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
144/// could be freed.
146 std::unique_ptr<OptimizationRemarkEmitter> ORE;
147
148public:
150
151 bool runOnFunction(Function &F) override;
152
153 void getAnalysisUsage(AnalysisUsage &AU) const override;
154
156 assert(ORE && "pass not run yet");
157 return *ORE;
158 }
159
160 static char ID;
161};
162
164 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
166 static AnalysisKey Key;
167
168public:
169 /// Provide the result typedef for this analysis pass.
171
172 /// Run the analysis pass over a function and produce BFI.
174};
175} // namespace llvm
176#endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
Given that RA is a live value
#define F(x, y, z)
Definition: MD5.cpp:55
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char PassName[]
Value * RHS
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.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Common features for diagnostics dealing with optimization remarks that are used by IR passes.
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:380
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...
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
OptimizationRemarkEmitter legacy analysis pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
The optimization diagnostic interface.
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
static bool allowExtraAnalysis(const Function &F, StringRef PassName)
bool enabled() const
Return true iff at least some remarks are enabled.
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
static bool allowExtraAnalysis(LLVMContext &Ctx, StringRef PassName)
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:50
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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
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.
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Used to set IsVerbose via the stream interface.