LLVM 22.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) {}
95
98
99 auto &Context = Fn.getContext();
100 if (Context.getDiagnosticsHotnessRequested()) {
101 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
102 // Get hotness threshold from PSI. This should only happen once.
103 if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
104 if (ProfileSummaryInfo *PSI =
105 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI())
106 Context.setDiagnosticsHotnessThreshold(
107 PSI->getOrCompHotCountThreshold());
108 }
109 } else
110 BFI = nullptr;
111
112 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
113 return false;
114}
115
117 AnalysisUsage &AU) const {
120 AU.setPreservesAll();
121}
122
123AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
124
129 auto &Context = F.getContext();
130
131 if (Context.getDiagnosticsHotnessRequested()) {
133 // Get hotness threshold from PSI. This should only happen once.
134 if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
135 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
136 if (ProfileSummaryInfo *PSI =
137 MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()))
138 Context.setDiagnosticsHotnessThreshold(
139 PSI->getOrCompHotCountThreshold());
140 }
141 } else
142 BFI = nullptr;
143
144 return OptimizationRemarkEmitter(&F, BFI);
145}
146
148static const char ore_name[] = "Optimization Remark Emitter";
149#define ORE_NAME "opt-remark-emitter"
150
152 false, true)
#define F(x, y, z)
Definition: MD5.cpp:55
static const char ore_name[]
#define ORE_NAME
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:39
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
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:312
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
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...
LLVM_ABI 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 BasicBlock * 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:165
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
LLVM_ABI 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:716
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
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:75
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29