[llvm] [CodeGen][NewPM] Add `MachineOptimizationRemarkEmitterAnalysis` (PR #98601)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 12 01:18:31 PDT 2024
https://github.com/paperchalice created https://github.com/llvm/llvm-project/pull/98601
Add `MachineOptimizationRemarkEmitterAnalysis` the legacy version `MachineOptimizationRemarkEmitterPass` is already a wrapper.
>From d501d90795bd443d47accaba491780ad86f301be Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 11 Jul 2024 17:31:08 +0800
Subject: [PATCH] [CodeGen][NewPM] Add
`MachineOptimizationRemarkEmitterAnalysis`
---
.../MachineOptimizationRemarkEmitter.h | 19 ++++++++++++++++++
.../llvm/Passes/MachinePassRegistry.def | 4 ++--
.../MachineOptimizationRemarkEmitter.cpp | 20 +++++++++++++++++++
llvm/lib/Passes/PassBuilder.cpp | 1 +
4 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h b/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
index 2b177e6763d39..d4edacedb88e6 100644
--- a/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
+++ b/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
@@ -16,6 +16,7 @@
#define LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include <optional>
@@ -155,6 +156,13 @@ class MachineOptimizationRemarkEmitter {
MachineBlockFrequencyInfo *MBFI)
: MF(MF), MBFI(MBFI) {}
+ MachineOptimizationRemarkEmitter(MachineOptimizationRemarkEmitter &&) =
+ default;
+
+ /// Handle invalidation events in the new pass manager.
+ bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
+ MachineFunctionAnalysisManager::Invalidator &Inv);
+
/// Emit an optimization remark.
void emit(DiagnosticInfoOptimizationBase &OptDiag);
@@ -212,6 +220,17 @@ class MachineOptimizationRemarkEmitter {
bool shouldEmitVerbose() { return MBFI != nullptr; }
};
+/// The analysis pass
+class MachineOptimizationRemarkEmitterAnalysis
+ : public AnalysisInfoMixin<MachineOptimizationRemarkEmitterAnalysis> {
+ friend AnalysisInfoMixin<MachineOptimizationRemarkEmitterAnalysis>;
+ static AnalysisKey Key;
+
+public:
+ using Result = MachineOptimizationRemarkEmitter;
+ Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
+};
+
/// The analysis pass
///
/// Note that this pass shouldn't generally be marked as preserved by other
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index d5cd8d4a132fc..70c4f3484af1a 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -101,6 +101,8 @@ MACHINE_FUNCTION_ANALYSIS("machine-branch-prob",
MachineBranchProbabilityAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopAnalysis())
+MACHINE_FUNCTION_ANALYSIS("machine-opt-remark-emitter",
+ MachineOptimizationRemarkEmitterAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
MachinePostDominatorTreeAnalysis())
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
@@ -112,8 +114,6 @@ MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-dom-frontier",
// MachineDominanceFrontierAnalysis())
-// MACHINE_FUNCTION_ANALYSIS("machine-ore",
-// MachineOptimizationRemarkEmitterPassAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
// MachinePostDominatorTreeAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-region-info",
diff --git a/llvm/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp b/llvm/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp
index 1c31eba909e78..039f07f2e5e3f 100644
--- a/llvm/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp
+++ b/llvm/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp
@@ -31,6 +31,14 @@ DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
/*SkipDebugLoc=*/true);
}
+bool MachineOptimizationRemarkEmitter::invalidate(
+ MachineFunction &MF, const PreservedAnalyses &PA,
+ MachineFunctionAnalysisManager::Invalidator &Inv) {
+ // This analysis has no state and so can be trivially preserved but it needs
+ // a fresh view of BFI if it was constructed with one.
+ return MBFI && Inv.invalidate<MachineBlockFrequencyAnalysis>(MF, PA);
+}
+
std::optional<uint64_t>
MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
if (!MBFI)
@@ -86,6 +94,18 @@ void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
MachineFunctionPass::getAnalysisUsage(AU);
}
+AnalysisKey MachineOptimizationRemarkEmitterAnalysis::Key;
+
+MachineOptimizationRemarkEmitterAnalysis::Result
+MachineOptimizationRemarkEmitterAnalysis::run(
+ MachineFunction &MF, MachineFunctionAnalysisManager &MFAM) {
+ MachineBlockFrequencyInfo *MBFI =
+ MF.getFunction().getContext().getDiagnosticsHotnessRequested()
+ ? &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF)
+ : nullptr;
+ return Result(MF, MBFI);
+}
+
char MachineOptimizationRemarkEmitterPass::ID = 0;
static const char ore_name[] = "Machine Optimization Remark Emitter";
#define ORE_NAME "machine-opt-remark-emitter"
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index d4eca4a48e55d..96fd446551975 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -100,6 +100,7 @@
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
+#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachinePostDominators.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
More information about the llvm-commits
mailing list