LLVM 19.0.0git
OptimizationRemarkEmitter.cpp
Go to the documentation of this file.
1//===- OptimizationRemarkEmitter.cpp - 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
20#include "llvm/IR/Dominators.h"
21#include "llvm/IR/LLVMContext.h"
23#include <optional>
24
25using namespace llvm;
26
28 : F(F), BFI(nullptr) {
29 if (!F->getContext().getDiagnosticsHotnessRequested())
30 return;
31
32 // First create a dominator tree.
34 DT.recalculate(*const_cast<Function *>(F));
35
36 // Generate LoopInfo from it.
37 LoopInfo LI;
38 LI.analyze(DT);
39
40 // Then compute BranchProbabilityInfo.
41 BranchProbabilityInfo BPI(*F, LI, nullptr, &DT, nullptr);
42
43 // Finally compute BFI.
44 OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
45 BFI = OwnedBFI.get();
46}
47
49 Function &F, const PreservedAnalyses &PA,
51 if (OwnedBFI) {
52 OwnedBFI.reset();
53 BFI = nullptr;
54 }
55 // This analysis has no state and so can be trivially preserved but it needs
56 // a fresh view of BFI if it was constructed with one.
57 if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
58 return true;
59
60 // Otherwise this analysis result remains valid.
61 return false;
62}
63
64std::optional<uint64_t>
65OptimizationRemarkEmitter::computeHotness(const Value *V) {
66 if (!BFI)
67 return std::nullopt;
68
69 return BFI->getBlockProfileCount(cast<BasicBlock>(V));
70}
71
72void OptimizationRemarkEmitter::computeHotness(
74 const Value *V = OptDiag.getCodeRegion();
75 if (V)
76 OptDiag.setHotness(computeHotness(V));
77}
78
80 DiagnosticInfoOptimizationBase &OptDiagBase) {
81 auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
82 computeHotness(OptDiag);
83
84 // Only emit it if its hotness meets the threshold.
85 if (OptDiag.getHotness().value_or(0) <
87 return;
88 }
89
90 F->getContext().diagnose(OptDiag);
91}
92
94 : FunctionPass(ID) {
97}
98
101
102 auto &Context = Fn.getContext();
104 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
105 // Get hotness threshold from PSI. This should only happen once.
107 if (ProfileSummaryInfo *PSI =
108 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI())
110 PSI->getOrCompHotCountThreshold());
111 }
112 } else
113 BFI = nullptr;
114
115 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
116 return false;
117}
118
120 AnalysisUsage &AU) const {
123 AU.setPreservesAll();
124}
125
126AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
127
132 auto &Context = F.getContext();
133
136 // Get hotness threshold from PSI. This should only happen once.
138 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
139 if (ProfileSummaryInfo *PSI =
140 MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()))
142 PSI->getOrCompHotCountThreshold());
143 }
144 } else
145 BFI = nullptr;
146
147 return OptimizationRemarkEmitter(&F, BFI);
148}
149
151static const char ore_name[] = "Optimization Remark Emitter";
152#define ORE_NAME "opt-remark-emitter"
153
155 false, true)
basic Basic Alias true
#define F(x, y, z)
Definition: MD5.cpp:55
LLVMContext & Context
static const char ore_name[]
#define ORE_NAME
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:360
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:378
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:473
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
Analysis providing branch probability information.
Common features for diagnostics dealing with optimization remarks that are used by IR passes.
const Value * getCodeRegion() const
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
void setHotness(std::optional< uint64_t > H)
std::optional< uint64_t > getHotness() const
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
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:356
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
void setDiagnosticsHotnessThreshold(std::optional< uint64_t > Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
bool isDiagnosticsHotnessThresholdSetFromPSI() const
Return if hotness threshold is requested from PSI.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
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.
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:756
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeOptimizationRemarkEmitterWrapperPassPass(PassRegistry &)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26