LLVM 22.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
36 MachineFunctionAnalysisManager::Invalidator &Inv) {
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
76
78 MachineFunction &MF) {
80
83 else
84 MBFI = nullptr;
85
86 ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
87 return false;
88}
89
96
97AnalysisKey MachineOptimizationRemarkEmitterAnalysis::Key;
98
108
110static const char ore_name[] = "Machine Optimization Remark Emitter";
111#define ORE_NAME "machine-opt-remark-emitter"
112
114 true, true)
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
static const char 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
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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:359
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
This is an alternative analysis pass to MachineBlockFrequencyInfo.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
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.
LLVM_ABI 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.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
LLVM_ABI bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.