LLVM 19.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 {
25class Function;
26class Value;
27
28/// The optimization diagnostic interface.
29///
30/// It allows reporting when optimizations are performed and when they are not
31/// along with the reasons for it. Hotness information of the corresponding
32/// code region can be included in the remark if DiagnosticsHotnessRequested is
33/// enabled in the LLVM context.
35public:
37 : F(F), BFI(BFI) {}
38
39 /// This variant can be used to generate ORE on demand (without the
40 /// analysis pass).
41 ///
42 /// Note that this ctor has a very different cost depending on whether
43 /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
44 /// the operation is free.
45 ///
46 /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
47 /// operation since BFI and all its required analyses are computed. This is
48 /// for example useful for CGSCC passes that can't use function analyses
49 /// passes in the old PM.
51
53 : F(Arg.F), BFI(Arg.BFI) {}
54
56 F = RHS.F;
57 BFI = RHS.BFI;
58 return *this;
59 }
60
61 /// Handle invalidation events in the new pass manager.
62 bool invalidate(Function &F, const PreservedAnalyses &PA,
64
65 /// Return true iff at least *some* remarks are enabled.
66 bool enabled() const {
67 return F->getContext().getLLVMRemarkStreamer() ||
69 }
70
71 /// Output the remark via the diagnostic handler and to the
72 /// optimization record file.
74
75 /// Take a lambda that returns a remark which will be emitted. Second
76 /// argument is only used to restrict this to functions.
77 template <typename T>
78 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
79 // Avoid building the remark unless we know there are at least *some*
80 // remarks enabled. We can't currently check whether remarks are requested
81 // for the calling pass since that requires actually building the remark.
82
83 if (enabled()) {
84 auto R = RemarkBuilder();
85 static_assert(
86 std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
87 "the lambda passed to emit() must return a remark");
89 }
90 }
91
92 /// Whether we allow for extra compile-time budget to perform more
93 /// analysis to produce fewer false positives.
94 ///
95 /// This is useful when reporting missed optimizations. In this case we can
96 /// use the extra analysis (1) to filter trivial false positives or (2) to
97 /// provide more context so that non-trivial false positives can be quickly
98 /// detected by the user.
101 }
103 return allowExtraAnalysis(F.getContext(), PassName);
104 }
106 return Ctx.getLLVMRemarkStreamer() ||
108 }
109
110private:
111 const Function *F;
112
114
115 /// If we generate BFI on demand, we need to free it when ORE is freed.
116 std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
117
118 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
119 /// available.
120 std::optional<uint64_t> computeHotness(const Value *V);
121
122 /// Similar but use value from \p OptDiag and update hotness there.
123 void computeHotness(DiagnosticInfoIROptimization &OptDiag);
124
125 /// Only allow verbose messages if we know we're filtering by hotness
126 /// (BFI is only set in this case).
127 bool shouldEmitVerbose() { return BFI != nullptr; }
128
129 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
130 void operator=(const OptimizationRemarkEmitter &) = delete;
131};
132
133/// Add a small namespace to avoid name clashes with the classes used in
134/// the streaming interface. We want these to be short for better
135/// write/readability.
136namespace ore {
140}
141
142/// OptimizationRemarkEmitter legacy analysis pass
143///
144/// Note that this pass shouldn't generally be marked as preserved by other
145/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
146/// could be freed.
148 std::unique_ptr<OptimizationRemarkEmitter> ORE;
149
150public:
152
153 bool runOnFunction(Function &F) override;
154
155 void getAnalysisUsage(AnalysisUsage &AU) const override;
156
158 assert(ORE && "pass not run yet");
159 return *ORE;
160 }
161
162 static char ID;
163};
164
166 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
168 static AnalysisKey Key;
169
170public:
171 /// Provide the result typedef for this analysis pass.
173
174 /// Run the analysis pass over a function and produce BFI.
176};
177}
178#endif // LLVM_ANALYSIS_OPTIMIZATIONREMARKEMITTER_H
dxil metadata emit
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:311
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:358
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)
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.