LLVM 19.0.0git
Go to the documentation of this file.
1//===- OptimizationRemarkEmitter.cpp - 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.
20#include "llvm/IR/Dominators.h"
21#include "llvm/IR/LLVMContext.h"
23#include <optional>
25using namespace llvm;
28 : F(F), BFI(nullptr) {
29 if (!F->getContext().getDiagnosticsHotnessRequested())
30 return;
32 // First create a dominator tree.
34 DT.recalculate(*const_cast<Function *>(F));
36 // Generate LoopInfo from it.
37 LoopInfo LI;
38 LI.analyze(DT);
40 // Then compute BranchProbabilityInfo.
41 BranchProbabilityInfo BPI(*F, LI, nullptr, &DT, nullptr);
43 // Finally compute BFI.
44 OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
45 BFI = OwnedBFI.get();
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;
60 // Otherwise this analysis result remains valid.
61 return false;
65OptimizationRemarkEmitter::computeHotness(const Value *V) {
66 if (!BFI)
67 return std::nullopt;
69 return BFI->getBlockProfileCount(cast<BasicBlock>(V));
72void OptimizationRemarkEmitter::computeHotness(
74 const Value *V = OptDiag.getCodeRegion();
75 if (V)
76 OptDiag.setHotness(computeHotness(V));
80 DiagnosticInfoOptimizationBase &OptDiagBase) {
81 auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
82 computeHotness(OptDiag);
84 // Only emit it if its hotness meets the threshold.
85 if (OptDiag.getHotness().value_or(0) <
87 return;
88 }
90 F->getContext().diagnose(OptDiag);
94 : FunctionPass(ID) {
102 auto &Context = Fn.getContext();
103 if (Context.getDiagnosticsHotnessRequested()) {
104 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
105 // Get hotness threshold from PSI. This should only happen once.
106 if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
107 if (ProfileSummaryInfo *PSI =
108 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI())
109 Context.setDiagnosticsHotnessThreshold(
110 PSI->getOrCompHotCountThreshold());
111 }
112 } else
113 BFI = nullptr;
115 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
116 return false;
120 AnalysisUsage &AU) const {
123 AU.setPreservesAll();
126AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
132 auto &Context = F.getContext();
134 if (Context.getDiagnosticsHotnessRequested()) {
136 // Get hotness threshold from PSI. This should only happen once.
137 if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
138 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
139 if (ProfileSummaryInfo *PSI =
140 MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()))
141 Context.setDiagnosticsHotnessThreshold(
142 PSI->getOrCompHotCountThreshold());
143 }
144 } else
145 BFI = nullptr;
147 return OptimizationRemarkEmitter(&F, BFI);
151static const char ore_name[] = "Optimization Remark Emitter";
152#define ORE_NAME "opt-remark-emitter"
155 false, true)
basic Basic Alias true
#define F(x, y, z)
Definition: MD5.cpp:55
static const char ore_name[]
#define ORE_NAME
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:358
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