LLVM  13.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 
17 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/InitializePasses.h"
23 
24 using namespace llvm;
25 
27  : F(F), BFI(nullptr) {
28  if (!F->getContext().getDiagnosticsHotnessRequested())
29  return;
30 
31  // First create a dominator tree.
32  DominatorTree DT;
33  DT.recalculate(*const_cast<Function *>(F));
34 
35  // Generate LoopInfo from it.
36  LoopInfo LI;
37  LI.analyze(DT);
38 
39  // Then compute BranchProbabilityInfo.
40  BranchProbabilityInfo BPI(*F, LI, nullptr, &DT, nullptr);
41 
42  // Finally compute BFI.
43  OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
44  BFI = OwnedBFI.get();
45 }
46 
48  Function &F, const PreservedAnalyses &PA,
50  if (OwnedBFI.get()) {
51  OwnedBFI.reset();
52  BFI = nullptr;
53  }
54  // This analysis has no state and so can be trivially preserved but it needs
55  // a fresh view of BFI if it was constructed with one.
56  if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
57  return true;
58 
59  // Otherwise this analysis result remains valid.
60  return false;
61 }
62 
63 Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
64  if (!BFI)
65  return None;
66 
67  return BFI->getBlockProfileCount(cast<BasicBlock>(V));
68 }
69 
70 void OptimizationRemarkEmitter::computeHotness(
72  const Value *V = OptDiag.getCodeRegion();
73  if (V)
74  OptDiag.setHotness(computeHotness(V));
75 }
76 
78  DiagnosticInfoOptimizationBase &OptDiagBase) {
79  auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
80  computeHotness(OptDiag);
81 
82  // Only emit it if its hotness meets the threshold.
83  if (OptDiag.getHotness().getValueOr(0) <
85  return;
86  }
87 
88  F->getContext().diagnose(OptDiag);
89 }
90 
92  : FunctionPass(ID) {
95 }
96 
99 
100  auto &Context = Fn.getContext();
102  BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
103  // Get hotness threshold from PSI. This should only happen once.
105  if (ProfileSummaryInfo *PSI =
106  &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI())
108  PSI->getOrCompHotCountThreshold());
109  }
110  } else
111  BFI = nullptr;
112 
113  ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
114  return false;
115 }
116 
118  AnalysisUsage &AU) const {
121  AU.setPreservesAll();
122 }
123 
124 AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
125 
130  auto &Context = F.getContext();
131 
134  // Get hotness threshold from PSI. This should only happen once.
136  auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
137  if (ProfileSummaryInfo *PSI =
138  MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()))
140  PSI->getOrCompHotCountThreshold());
141  }
142  } else
143  BFI = nullptr;
144 
145  return OptimizationRemarkEmitter(&F, BFI);
146 }
147 
149 static const char ore_name[] = "Optimization Remark Emitter";
150 #define ORE_NAME "opt-remark-emitter"
151 
153  false, true)
154 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
llvm::LLVMContext::setDiagnosticsHotnessThreshold
void setDiagnosticsHotnessThreshold(Optional< uint64_t > Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition: LLVMContext.cpp:135
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::OuterAnalysisManagerProxy
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1051
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
llvm::DiagnosticInfoOptimizationBase::setHotness
void setHotness(Optional< uint64_t > H)
Definition: DiagnosticInfo.h:478
llvm::Function
Definition: Function.h:61
llvm::AnalysisManager::Invalidator::invalidate
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:674
LazyBlockFrequencyInfo.h
OptimizationRemarkEmitter.h
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
Definition: LazyBlockFrequencyInfo.cpp:62
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
llvm::Optional< uint64_t >
llvm::DiagnosticInfoIROptimization
Common features for diagnostics dealing with optimization remarks that are used by IR passes.
Definition: DiagnosticInfo.h:617
llvm::OptimizationRemarkEmitter::OptimizationRemarkEmitter
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
Definition: OptimizationRemarkEmitter.h:35
llvm::LLVMContext::getDiagnosticsHotnessThreshold
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
Definition: LLVMContext.cpp:139
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:408
llvm::DiagnosticInfoIROptimization::getCodeRegion
const Value * getCodeRegion() const
Definition: DiagnosticInfo.h:671
ORE_NAME
#define ORE_NAME
Definition: OptimizationRemarkEmitter.cpp:150
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::OptimizationRemarkEmitterWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: OptimizationRemarkEmitter.cpp:117
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::LoopInfoBase::analyze
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
Definition: LoopInfoImpl.h:548
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::initializeOptimizationRemarkEmitterWrapperPassPass
void initializeOptimizationRemarkEmitterWrapperPassPass(PassRegistry &)
llvm::DiagnosticInfoOptimizationBase::getHotness
Optional< uint64_t > getHotness() const
Definition: DiagnosticInfo.h:477
llvm::BranchProbabilityInfo
Analysis providing branch probability information.
Definition: BranchProbabilityInfo.h:115
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
ore_name
static const char ore_name[]
Definition: OptimizationRemarkEmitter.cpp:149
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, false, true) INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass
false
Definition: StackSlotColoring.cpp:142
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:656
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:112
llvm::None
const NoneType None
Definition: None.h:23
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
LoopInfo.h
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
llvm::Optional::getValueOr
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
Definition: Optional.h:297
BranchProbabilityInfo.h
ProfileSummaryInfo.h
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::OptimizationRemarkEmitterWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: OptimizationRemarkEmitter.cpp:97
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::ProfileSummaryInfoWrapperPass
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Definition: ProfileSummaryInfo.h:185
llvm::OptimizationRemarkEmitter::invalidate
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
Definition: OptimizationRemarkEmitter.cpp:47
llvm::OptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Definition: OptimizationRemarkEmitter.cpp:77
llvm::ProfileSummaryAnalysis
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Definition: ProfileSummaryInfo.h:203
llvm::DominatorTreeBase::recalculate
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
Definition: GenericDomTree.h:778
llvm::LoopInfo
Definition: LoopInfo.h:1083
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::OptimizationRemarkEmitterAnalysis::run
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Definition: OptimizationRemarkEmitter.cpp:127
llvm::LLVMContext::isDiagnosticsHotnessThresholdSetFromPSI
bool isDiagnosticsHotnessThresholdSetFromPSI() const
Return if hotness threshold is requested from PSI.
Definition: LLVMContext.cpp:143
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:421
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition: LLVMContext.cpp:228
llvm::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitter legacy analysis pass.
Definition: OptimizationRemarkEmitter.h:146
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
DiagnosticInfo.h
llvm::OptimizationRemarkEmitterWrapperPass::ID
static char ID
Definition: OptimizationRemarkEmitter.h:161
llvm::LLVMContext::getDiagnosticsHotnessRequested
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
Definition: LLVMContext.cpp:131
llvm::BlockFrequencyInfo::getBlockProfileCount
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
Definition: BlockFrequencyInfo.cpp:209
Dominators.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
LLVMContext.h
llvm::OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitterWrapperPass()
Definition: OptimizationRemarkEmitter.cpp:91
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38