Skip to content

[CodeGen][NewPM] Port RegUsageInfoPropagation pass to NPM #114010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

optimisan
Copy link
Contributor

No description provided.

@optimisan
Copy link
Contributor Author

optimisan commented Oct 29, 2024

@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from f84d99b to 5f42368 Compare October 29, 2024 09:14
@optimisan optimisan marked this pull request as ready for review October 29, 2024 09:51
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2024

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-regalloc

Author: Akshat Oke (optimisan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/114010.diff

8 Files Affected:

  • (added) llvm/include/llvm/CodeGen/RegUsageInfoPropagate.h (+25)
  • (modified) llvm/include/llvm/InitializePasses.h (+1-1)
  • (modified) llvm/include/llvm/Passes/CodeGenPassBuilder.h (+1)
  • (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+1-1)
  • (modified) llvm/lib/CodeGen/CodeGen.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/RegUsageInfoPropagate.cpp (+52-23)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/test/CodeGen/AArch64/preserve.ll (+4)
diff --git a/llvm/include/llvm/CodeGen/RegUsageInfoPropagate.h b/llvm/include/llvm/CodeGen/RegUsageInfoPropagate.h
new file mode 100644
index 00000000000000..73624015e37d9d
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/RegUsageInfoPropagate.h
@@ -0,0 +1,25 @@
+//===- llvm/CodeGen/RegUsageInfoPropagate.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_REGUSAGEINFOPROPAGATE_H
+#define LLVM_CODEGEN_REGUSAGEINFOPROPAGATE_H
+
+#include "llvm/CodeGen/MachinePassManager.h"
+
+namespace llvm {
+
+class RegUsageInfoPropagationPass
+    : public PassInfoMixin<RegUsageInfoPropagationPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_REGUSAGEINFOPROPAGATE_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index c881dcd57006db..a9ab739af33ad8 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -258,7 +258,7 @@ void initializeRegAllocScoringPass(PassRegistry &);
 void initializeRegBankSelectPass(PassRegistry &);
 void initializeRegToMemWrapperPassPass(PassRegistry &);
 void initializeRegUsageInfoCollectorLegacyPass(PassRegistry &);
-void initializeRegUsageInfoPropagationPass(PassRegistry &);
+void initializeRegUsageInfoPropagationLegacyPass(PassRegistry &);
 void initializeRegionInfoPassPass(PassRegistry &);
 void initializeRegionOnlyPrinterPass(PassRegistry &);
 void initializeRegionOnlyViewerPass(PassRegistry &);
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index 14fcf9d79fbc23..a64ecd69e55913 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -54,6 +54,7 @@
 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
 #include "llvm/CodeGen/RegAllocFast.h"
 #include "llvm/CodeGen/RegUsageInfoCollector.h"
+#include "llvm/CodeGen/RegUsageInfoPropagate.h"
 #include "llvm/CodeGen/RegisterUsageInfo.h"
 #include "llvm/CodeGen/ReplaceWithVeclib.h"
 #include "llvm/CodeGen/SafeStack.h"
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 36d17b713639c1..099b009a2b3fee 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -156,6 +156,7 @@ MACHINE_FUNCTION_PASS("print<machine-post-dom-tree>",
 MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(dbgs()))
 MACHINE_FUNCTION_PASS("print<virtregmap>", VirtRegMapPrinterPass(dbgs()))
 MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass())
+MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass())
 MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
                       RequireAllMachineFunctionPropertiesPass())
 MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
@@ -250,7 +251,6 @@ DUMMY_MACHINE_FUNCTION_PASS("prologepilog-code", PrologEpilogCodeInserterPass)
 DUMMY_MACHINE_FUNCTION_PASS("ra-basic", RABasicPass)
 DUMMY_MACHINE_FUNCTION_PASS("ra-greedy", RAGreedyPass)
 DUMMY_MACHINE_FUNCTION_PASS("ra-pbqp", RAPBQPPass)
-DUMMY_MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass)
 DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass)
 DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass)
 DUMMY_MACHINE_FUNCTION_PASS("regbankselect", RegBankSelectPass)
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 76b74ea4e6fe0b..20d1417193a864 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -114,7 +114,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeRAGreedyPass(Registry);
   initializeRegAllocFastPass(Registry);
   initializeRegUsageInfoCollectorLegacyPass(Registry);
-  initializeRegUsageInfoPropagationPass(Registry);
+  initializeRegUsageInfoPropagationLegacyPass(Registry);
   initializeRegisterCoalescerPass(Registry);
   initializeRemoveLoadsIntoFakeUsesPass(Registry);
   initializeRemoveRedundantDebugValuesPass(Registry);
diff --git a/llvm/lib/CodeGen/RegUsageInfoPropagate.cpp b/llvm/lib/CodeGen/RegUsageInfoPropagate.cpp
index 5ffe6acc83d601..3877d1c0d62488 100644
--- a/llvm/lib/CodeGen/RegUsageInfoPropagate.cpp
+++ b/llvm/lib/CodeGen/RegUsageInfoPropagate.cpp
@@ -18,6 +18,7 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/RegUsageInfoPropagate.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
@@ -25,8 +26,10 @@
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/RegisterUsageInfo.h"
+#include "llvm/IR/Analysis.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Pass.h"
+#include "llvm/Passes/CodeGenPassBuilder.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -38,26 +41,16 @@ using namespace llvm;
 
 namespace {
 
-class RegUsageInfoPropagation : public MachineFunctionPass {
+class RegUsageInfoPropagation {
 public:
-  RegUsageInfoPropagation() : MachineFunctionPass(ID) {
-    PassRegistry &Registry = *PassRegistry::getPassRegistry();
-    initializeRegUsageInfoPropagationPass(Registry);
-  }
+  explicit RegUsageInfoPropagation(PhysicalRegisterUsageInfo *PRUI)
+      : PRUI(PRUI) {}
 
-  StringRef getPassName() const override { return RUIP_NAME; }
-
-  bool runOnMachineFunction(MachineFunction &MF) override;
-
-  void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.addRequired<PhysicalRegisterUsageInfoWrapperLegacy>();
-    AU.setPreservesAll();
-    MachineFunctionPass::getAnalysisUsage(AU);
-  }
-
-  static char ID;
+  bool run(MachineFunction &MF);
 
 private:
+  PhysicalRegisterUsageInfo *PRUI;
+
   static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
     assert(RegMask.size() ==
            MachineOperand::getRegMaskSize(MI.getParent()->getParent()
@@ -71,15 +64,34 @@ class RegUsageInfoPropagation : public MachineFunctionPass {
   }
 };
 
+class RegUsageInfoPropagationLegacy : public MachineFunctionPass {
+public:
+  static char ID;
+  RegUsageInfoPropagationLegacy() : MachineFunctionPass(ID) {
+    PassRegistry &Registry = *PassRegistry::getPassRegistry();
+    initializeRegUsageInfoPropagationLegacyPass(Registry);
+  }
+
+  StringRef getPassName() const override { return RUIP_NAME; }
+
+  bool runOnMachineFunction(MachineFunction &MF) override;
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.addRequired<PhysicalRegisterUsageInfoWrapperLegacy>();
+    AU.setPreservesAll();
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
+};
+
 } // end of anonymous namespace
 
-INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
+INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
                       RUIP_NAME, false, false)
 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfoWrapperLegacy)
-INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
+INITIALIZE_PASS_END(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
                     RUIP_NAME, false, false)
 
-char RegUsageInfoPropagation::ID = 0;
+char RegUsageInfoPropagationLegacy::ID = 0;
 
 // Assumes call instructions have a single reference to a function.
 static const Function *findCalledFunction(const Module &M,
@@ -95,12 +107,29 @@ static const Function *findCalledFunction(const Module &M,
   return nullptr;
 }
 
-bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
-  const Module &M = *MF.getFunction().getParent();
+bool RegUsageInfoPropagationLegacy::runOnMachineFunction(MachineFunction &MF) {
   PhysicalRegisterUsageInfo *PRUI =
       &getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
 
-  LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
+  RegUsageInfoPropagation RUIP(PRUI);
+  return RUIP.run(MF);
+}
+
+PreservedAnalyses
+RegUsageInfoPropagationPass::run(MachineFunction &MF,
+                                 MachineFunctionAnalysisManager &MFAM) {
+  Module &MFA = *MF.getFunction().getParent();
+  auto *PRUI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
+                   .getCachedResult<PhysicalRegisterUsageInfoAnalysis>(MFA);
+  assert(PRUI && "PhysicalRegisterUsageInfoAnalysis not available");
+  RegUsageInfoPropagation(PRUI).run(MF);
+  return PreservedAnalyses::all();
+}
+
+bool RegUsageInfoPropagation::run(MachineFunction &MF) {
+  const Module &M = *MF.getFunction().getParent();
+
+  LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << RUIP_NAME
                     << " ++++++++++++++++++++  \n");
   LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
 
@@ -151,5 +180,5 @@ bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
 }
 
 FunctionPass *llvm::createRegUsageInfoPropPass() {
-  return new RegUsageInfoPropagation();
+  return new RegUsageInfoPropagationLegacy();
 }
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index a76ebb01251b72..5e129419c9f63d 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -119,6 +119,7 @@
 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
 #include "llvm/CodeGen/RegAllocFast.h"
 #include "llvm/CodeGen/RegUsageInfoCollector.h"
+#include "llvm/CodeGen/RegUsageInfoPropagate.h"
 #include "llvm/CodeGen/RegisterUsageInfo.h"
 #include "llvm/CodeGen/SafeStack.h"
 #include "llvm/CodeGen/SelectOptimize.h"
diff --git a/llvm/test/CodeGen/AArch64/preserve.ll b/llvm/test/CodeGen/AArch64/preserve.ll
index 5839bb5440a9b0..0e342b97a39dee 100644
--- a/llvm/test/CodeGen/AArch64/preserve.ll
+++ b/llvm/test/CodeGen/AArch64/preserve.ll
@@ -1,6 +1,10 @@
 
 ; RUN: llc -enable-ipra -print-regusage -o /dev/null 2>&1 < %s | FileCheck %s
 
+; RUN: llc -stop-after=irtranslator -o - %s \
+; RUN: | llc -x=mir -enable-ipra -passes="require<reg-usage-info>,function(machine-function(reg-usage-propagation,reg-usage-collector)),print<regusage>" -o /dev/null 2>&1 \
+; RUN: | FileCheck %s
+
 target triple = "aarch64-unknown-unknown"
 declare void @bar1()
 define preserve_mostcc void @baz() #0 {

@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from 7656d20 to 3628633 Compare October 30, 2024 06:59
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 5f42368 to 5f01016 Compare October 30, 2024 06:59
@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from 3628633 to 5a318a8 Compare October 30, 2024 07:32
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 5f01016 to b4d5e1b Compare October 30, 2024 07:32
Comment on lines +122 to +124
auto *PRUI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
.getCachedResult<PhysicalRegisterUsageAnalysis>(MFA);
assert(PRUI && "PhysicalRegisterUsageAnalysis not available");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use getResult and delete the assert?

Copy link
Contributor Author

@optimisan optimisan Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an outer analysis so cannot run it from an inner IR

@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from b4d5e1b to 670c20c Compare November 4, 2024 11:10
@arsenm arsenm requested a review from aeubanks November 5, 2024 19:56
@optimisan
Copy link
Contributor Author

optimisan commented Nov 14, 2024

Merge activity

  • Nov 14, 8:37 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Nov 14, 8:40 AM EST: Graphite couldn't merge this pull request because a downstack PR [CodeGen][NewPM] Port RegisterUsageInfo to NPM #113873 failed to merge.
  • Nov 15, 1:25 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Nov 15, 1:31 AM EST: Graphite rebased this pull request as part of a merge.
  • Nov 15, 1:34 AM EST: Graphite rebased this pull request as part of a merge.
  • Nov 15, 1:36 AM EST: A user merged this pull request with Graphite.

@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from 97eb13a to 50d950c Compare November 14, 2024 13:48
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 670c20c to 931fab8 Compare November 14, 2024 13:49
@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from 50d950c to e6bad87 Compare November 15, 2024 05:15
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 931fab8 to f69e911 Compare November 15, 2024 05:15
@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from e6bad87 to 4903ec8 Compare November 15, 2024 05:20
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from f69e911 to 9b98195 Compare November 15, 2024 05:21
@optimisan optimisan force-pushed the users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm branch from 4903ec8 to cd123fe Compare November 15, 2024 06:26
Base automatically changed from users/Akshat-Oke/10-28-_codegen_newpm_port_regusageinfocollector_pass_to_npm to main November 15, 2024 06:30
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 9b98195 to 612b09c Compare November 15, 2024 06:30
@optimisan optimisan force-pushed the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch from 612b09c to 00737da Compare November 15, 2024 06:33
@optimisan optimisan merged commit 47928ab into main Nov 15, 2024
5 of 8 checks passed
@optimisan optimisan deleted the users/Akshat-Oke/10-29-_codegen_newpm_port_regusageinfopropagation_pass_to_npm branch November 15, 2024 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants