LLVM 19.0.0git
Go to the documentation of this file.
1//===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- C++ -*-===//
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
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.
19#include "llvm/IR/PassManager.h"
20#include "llvm/Pass.h"
21#include <optional>
23namespace llvm {
24class Function;
25class Value;
27/// The optimization diagnostic interface.
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.
36 : F(F), BFI(BFI) {}
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.
52 : F(Arg.F), BFI(Arg.BFI) {}
55 F = RHS.F;
57 return *this;
58 }
60 /// Handle invalidation events in the new pass manager.
61 bool invalidate(Function &F, const PreservedAnalyses &PA,
64 /// Return true iff at least *some* remarks are enabled.
65 bool enabled() const {
66 return F->getContext().getLLVMRemarkStreamer() ||
68 }
70 /// Output the remark via the diagnostic handler and to the
71 /// optimization record file.
74 /// Take a lambda that returns a remark which will be emitted. Second
75 /// argument is only used to restrict this to functions.
76 template <typename T>
77 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
78 // Avoid building the remark unless we know there are at least *some*
79 // remarks enabled. We can't currently check whether remarks are requested
80 // for the calling pass since that requires actually building the remark.
82 if (enabled()) {
83 auto R = RemarkBuilder();
84 static_assert(
85 std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
86 "the lambda passed to emit() must return a remark");
88 }
89 }
91 /// Whether we allow for extra compile-time budget to perform more
92 /// analysis to produce fewer false positives.
93 ///
94 /// This is useful when reporting missed optimizations. In this case we can
95 /// use the extra analysis (1) to filter trivial false positives or (2) to
96 /// provide more context so that non-trivial false positives can be quickly
97 /// detected by the user.
100 }
102 return allowExtraAnalysis(F.getContext(), PassName);
103 }
105 return Ctx.getLLVMRemarkStreamer() ||
107 }
110 const Function *F;
114 /// If we generate BFI on demand, we need to free it when ORE is freed.
115 std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
117 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
118 /// available.
119 std::optional<uint64_t> computeHotness(const Value *V);
121 /// Similar but use value from \p OptDiag and update hotness there.
122 void computeHotness(DiagnosticInfoIROptimization &OptDiag);
124 /// Only allow verbose messages if we know we're filtering by hotness
125 /// (BFI is only set in this case).
126 bool shouldEmitVerbose() { return BFI != nullptr; }
128 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
129 void operator=(const OptimizationRemarkEmitter &) = delete;
132/// Add a small namespace to avoid name clashes with the classes used in
133/// the streaming interface. We want these to be short for better
134/// write/readability.
135namespace ore {
141/// OptimizationRemarkEmitter legacy analysis pass
143/// Note that this pass shouldn't generally be marked as preserved by other
144/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
145/// could be freed.
147 std::unique_ptr<OptimizationRemarkEmitter> ORE;
152 bool runOnFunction(Function &F) override;
154 void getAnalysisUsage(AnalysisUsage &AU) const override;
157 assert(ORE && "pass not run yet");
158 return *ORE;
159 }
161 static char ID;
165 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
167 static AnalysisKey Key;
170 /// Provide the result typedef for this analysis pass.
173 /// Run the analysis pass over a function and produce BFI.
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:360
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
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:109
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:97
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
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.