LLVM 19.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
34std::optional<uint64_t>
35MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
36 if (!MBFI)
37 return std::nullopt;
38
39 return MBFI->getBlockProfileCount(&MBB);
40}
41
42void MachineOptimizationRemarkEmitter::computeHotness(
44 const MachineBasicBlock *MBB = Remark.getBlock();
45 if (MBB)
46 Remark.setHotness(computeHotness(*MBB));
47}
48
50 DiagnosticInfoOptimizationBase &OptDiagCommon) {
51 auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
52 computeHotness(OptDiag);
53
54 LLVMContext &Ctx = MF.getFunction().getContext();
55
56 // Only emit it if its hotness meets the threshold.
57 if (OptDiag.getHotness().value_or(0) < Ctx.getDiagnosticsHotnessThreshold())
58 return;
59
60 Ctx.diagnose(OptDiag);
61}
62
67}
68
70 MachineFunction &MF) {
72
74 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
75 else
76 MBFI = nullptr;
77
78 ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
79 return false;
80}
81
83 AnalysisUsage &AU) const {
85 AU.setPreservesAll();
87}
88
90static const char ore_name[] = "Machine Optimization Remark Emitter";
91#define ORE_NAME "machine-opt-remark-emitter"
92
94 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:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
raw_pwrite_stream & OS
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:356
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
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.
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
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:660
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.