[llvm] [CodeGen][NPM] Port MachineLateInstrsCleanup to NPM (PR #128160)

Akshat Oke via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 02:28:44 PST 2025


https://github.com/optimisan created https://github.com/llvm/llvm-project/pull/128160

None

>From 350731a74b172ee18d7f452090674f481d971089 Mon Sep 17 00:00:00 2001
From: Akshat Oke <Akshat.Oke at amd.com>
Date: Fri, 21 Feb 2025 10:23:41 +0000
Subject: [PATCH] [CodeGen][NPM] Port MachineLateInstrsCleanup to NPM

---
 .../llvm/CodeGen/MachineLateInstrsCleanup.h   | 29 +++++++++++++++
 llvm/include/llvm/InitializePasses.h          |  2 +-
 llvm/include/llvm/Passes/CodeGenPassBuilder.h |  1 +
 .../llvm/Passes/MachinePassRegistry.def       |  2 +-
 llvm/lib/CodeGen/CodeGen.cpp                  |  2 +-
 llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp | 36 +++++++++++++++----
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 7 files changed, 63 insertions(+), 10 deletions(-)
 create mode 100644 llvm/include/llvm/CodeGen/MachineLateInstrsCleanup.h

diff --git a/llvm/include/llvm/CodeGen/MachineLateInstrsCleanup.h b/llvm/include/llvm/CodeGen/MachineLateInstrsCleanup.h
new file mode 100644
index 0000000000000..8a539eb1bb271
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineLateInstrsCleanup.h
@@ -0,0 +1,29 @@
+//===- llvm/CodeGen/MachineLateInstrsCleanup.h ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CODEGEN_MACHINELATEINSTRSCLEANUP_H
+#define LLVM_CODEGEN_MACHINELATEINSTRSCLEANUP_H
+
+#include "llvm/CodeGen/MachinePassManager.h"
+
+namespace llvm {
+
+class MachineLateInstrsCleanupPass
+    : public PassInfoMixin<MachineLateInstrsCleanupPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MachineFunction,
+                        MachineFunctionAnalysisManager &MachineFunctionAM);
+
+  MachineFunctionProperties getRequiredProperties() const {
+    return MachineFunctionProperties().set(
+        MachineFunctionProperties::Property::NoVRegs);
+  }
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_MACHINELATEINSTRSCLEANUP_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 5b30eb53208a8..0dfb46a210c7e 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -197,7 +197,7 @@ void initializeMachineDominanceFrontierPass(PassRegistry &);
 void initializeMachineDominatorTreeWrapperPassPass(PassRegistry &);
 void initializeMachineFunctionPrinterPassPass(PassRegistry &);
 void initializeMachineFunctionSplitterPass(PassRegistry &);
-void initializeMachineLateInstrsCleanupPass(PassRegistry &);
+void initializeMachineLateInstrsCleanupLegacyPass(PassRegistry &);
 void initializeMachineLICMPass(PassRegistry &);
 void initializeMachineLoopInfoWrapperPassPass(PassRegistry &);
 void initializeMachineModuleInfoWrapperPassPass(PassRegistry &);
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index 12781e2b84623..30f0742fd2c26 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -48,6 +48,7 @@
 #include "llvm/CodeGen/MachineCopyPropagation.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachineLICM.h"
+#include "llvm/CodeGen/MachineLateInstrsCleanup.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/CodeGen/MachineScheduler.h"
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 373bd047e2395..e1caf81f73578 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -144,6 +144,7 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
 MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
 MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass())
 MACHINE_FUNCTION_PASS("machine-cse", MachineCSEPass())
+MACHINE_FUNCTION_PASS("machine-latecleanup", MachineLateInstrsCleanupPass())
 MACHINE_FUNCTION_PASS("machine-scheduler", MachineSchedulerPass(TM))
 MACHINE_FUNCTION_PASS("machinelicm", MachineLICMPass())
 MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
@@ -245,7 +246,6 @@ DUMMY_MACHINE_FUNCTION_PASS("lrshrink", LiveRangeShrinkPass)
 DUMMY_MACHINE_FUNCTION_PASS("machine-combiner", MachineCombinerPass)
 DUMMY_MACHINE_FUNCTION_PASS("static-data-splitter", StaticDataSplitter)
 DUMMY_MACHINE_FUNCTION_PASS("machine-function-splitter", MachineFunctionSplitterPass)
-DUMMY_MACHINE_FUNCTION_PASS("machine-latecleanup", MachineLateInstrsCleanupPass)
 DUMMY_MACHINE_FUNCTION_PASS("machine-sanmd", MachineSanitizerBinaryMetadata)
 DUMMY_MACHINE_FUNCTION_PASS("machine-sink", MachineSinkingPass)
 DUMMY_MACHINE_FUNCTION_PASS("machine-uniformity", MachineUniformityInfoWrapperPass)
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 35df2a479a545..046a3ee42dd6b 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -83,7 +83,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeMachineDominatorTreeWrapperPassPass(Registry);
   initializeMachineFunctionPrinterPassPass(Registry);
   initializeMachineFunctionSplitterPass(Registry);
-  initializeMachineLateInstrsCleanupPass(Registry);
+  initializeMachineLateInstrsCleanupLegacyPass(Registry);
   initializeMachineLICMPass(Registry);
   initializeMachineLoopInfoWrapperPassPass(Registry);
   initializeMachineModuleInfoWrapperPassPass(Registry);
diff --git a/llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp b/llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp
index 8827a8327670a..503e577712a7f 100644
--- a/llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp
+++ b/llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/MachineLateInstrsCleanup.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/Statistic.h"
@@ -36,7 +37,7 @@ STATISTIC(NumRemoved, "Number of redundant instructions removed.");
 
 namespace {
 
-class MachineLateInstrsCleanup : public MachineFunctionPass {
+class MachineLateInstrsCleanup {
   const TargetRegisterInfo *TRI = nullptr;
   const TargetInstrInfo *TII = nullptr;
 
@@ -59,11 +60,17 @@ class MachineLateInstrsCleanup : public MachineFunctionPass {
   void clearKillsForDef(Register Reg, MachineBasicBlock *MBB,
                         BitVector &VisitedPreds);
 
+public:
+  bool run(MachineFunction &MF);
+};
+
+class MachineLateInstrsCleanupLegacy : public MachineFunctionPass {
 public:
   static char ID; // Pass identification, replacement for typeid
 
-  MachineLateInstrsCleanup() : MachineFunctionPass(ID) {
-    initializeMachineLateInstrsCleanupPass(*PassRegistry::getPassRegistry());
+  MachineLateInstrsCleanupLegacy() : MachineFunctionPass(ID) {
+    initializeMachineLateInstrsCleanupLegacyPass(
+        *PassRegistry::getPassRegistry());
   }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -81,17 +88,32 @@ class MachineLateInstrsCleanup : public MachineFunctionPass {
 
 } // end anonymous namespace
 
-char MachineLateInstrsCleanup::ID = 0;
+char MachineLateInstrsCleanupLegacy::ID = 0;
 
-char &llvm::MachineLateInstrsCleanupID = MachineLateInstrsCleanup::ID;
+char &llvm::MachineLateInstrsCleanupID = MachineLateInstrsCleanupLegacy::ID;
 
-INITIALIZE_PASS(MachineLateInstrsCleanup, DEBUG_TYPE,
+INITIALIZE_PASS(MachineLateInstrsCleanupLegacy, DEBUG_TYPE,
                 "Machine Late Instructions Cleanup Pass", false, false)
 
-bool MachineLateInstrsCleanup::runOnMachineFunction(MachineFunction &MF) {
+bool MachineLateInstrsCleanupLegacy::runOnMachineFunction(MachineFunction &MF) {
   if (skipFunction(MF.getFunction()))
     return false;
 
+  return MachineLateInstrsCleanup().run(MF);
+}
+
+PreservedAnalyses
+MachineLateInstrsCleanupPass::run(MachineFunction &MF,
+                                  MachineFunctionAnalysisManager &MFAM) {
+  MFPropsModifier _(*this, MF);
+  if (!MachineLateInstrsCleanup().run(MF))
+    return PreservedAnalyses::all();
+  auto PA = getMachineFunctionPassPreservedAnalyses();
+  PA.preserveSet<CFGAnalyses>();
+  return PA;
+}
+
+bool MachineLateInstrsCleanup::run(MachineFunction &MF) {
   TRI = MF.getSubtarget().getRegisterInfo();
   TII = MF.getSubtarget().getInstrInfo();
 
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5bb2e7d0abdd9..9174a3dc7072f 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -114,6 +114,7 @@
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachineLICM.h"
+#include "llvm/CodeGen/MachineLateInstrsCleanup.h"
 #include "llvm/CodeGen/MachineLoopInfo.h"
 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
 #include "llvm/CodeGen/MachinePassManager.h"



More information about the llvm-commits mailing list