LLVM 20.0.0git
MachineOptimizationRemarkEmitter.cpp
Go to the documentation of this file.
1///===- MachineOptimizationRemarkEmitter.cpp - Opt 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/// \file
9/// Optimization diagnostic interfaces for machine passes. It's packaged as an
10/// analysis pass so that by using this service passes become dependent on MBFI
11/// as well. MBFI is used to compute the "hotness" of the diagnostic message.
12///
13///===---------------------------------------------------------------------===//
14
19#include "llvm/IR/LLVMContext.h"
21#include <optional>
22
23using namespace llvm;
24
26 StringRef MKey, const MachineInstr &MI) {
27 Key = std::string(MKey);
28
30 MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
31 /*SkipDebugLoc=*/true);
32}
33
37 // This analysis has no state and so can be trivially preserved but it needs
38 // a fresh view of BFI if it was constructed with one.
39 return MBFI && Inv.invalidate<MachineBlockFrequencyAnalysis>(MF, PA);
40}
41
42std::optional<uint64_t>
43MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
44 if (!MBFI)
45 return std::nullopt;
46
47 return MBFI->getBlockProfileCount(&MBB);
48}
49
50void MachineOptimizationRemarkEmitter::computeHotness(
52 const MachineBasicBlock *MBB = Remark.getBlock();
53 if (MBB)
54 Remark.setHotness(computeHotness(*MBB));
55}
56
58 DiagnosticInfoOptimizationBase &OptDiagCommon) {
59 auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
60 computeHotness(OptDiag);
61
62 LLVMContext &Ctx = MF.getFunction().getContext();
63
64 // Only emit it if its hotness meets the threshold.
65 if (OptDiag.getHotness().value_or(0) < Ctx.getDiagnosticsHotnessThreshold())
66 return;
67
68 Ctx.diagnose(OptDiag);
69}
70
75}
76
78 MachineFunction &MF) {
80
82 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
83 else
84 MBFI = nullptr;
85
86 ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
87 return false;
88}
89
91 AnalysisUsage &AU) const {
93 AU.setPreservesAll();
95}
96
97AnalysisKey MachineOptimizationRemarkEmitterAnalysis::Key;
98
105 : nullptr;
106 return Result(MF, MBFI);
107}
108
110static const char ore_name[] = "Machine Optimization Remark Emitter";
111#define ORE_NAME "machine-opt-remark-emitter"
112
114 true, true)
MachineBasicBlock & MBB
basic Basic Alias true
IRTranslator LLVM IR MI
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
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:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
raw_pwrite_stream & OS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
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:310
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Common features for diagnostics dealing with optimization remarks that are used by machine passes.
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:380
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
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...
This is an alternative analysis pass to MachineBlockFrequencyInfo.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.