[llvm] [CodeGen][NPM] Avoid MachineModuleInfo in MachineModuleSlotTracker (PR #171257)

Teja Alaghari via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 8 22:21:29 PST 2025


https://github.com/TejaX-Alaghari updated https://github.com/llvm/llvm-project/pull/171257

>From 5900283e4c5884001a9ec1adcb9e7895e0c48c8d Mon Sep 17 00:00:00 2001
From: vikhegde <vikram.hegde at amd.com>
Date: Thu, 11 Sep 2025 14:50:27 +0530
Subject: [PATCH 1/3] Fixes to be upstreamed - 12

see "[CodeGen] Avoid MachineModuleInfo in MachineModuleSlotTracker (PR 140530)"
---
 llvm/include/llvm/CodeGen/MIRPrinter.h        |  4 +--
 .../llvm/CodeGen/MachineModuleSlotTracker.h   |  7 +++--
 llvm/lib/CodeGen/MIRPrinter.cpp               | 29 ++++++++++++++-----
 llvm/lib/CodeGen/MIRPrintingPass.cpp          | 14 ++++-----
 llvm/lib/CodeGen/MachineModuleSlotTracker.cpp |  9 +++---
 llvm/tools/llvm-reduce/ReducerWorkItem.cpp    |  2 +-
 llvm/unittests/MIR/MachineMetadata.cpp        | 12 ++++----
 7 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MIRPrinter.h b/llvm/include/llvm/CodeGen/MIRPrinter.h
index d8bcc6c4bdbc4..f90d23c2d993b 100644
--- a/llvm/include/llvm/CodeGen/MIRPrinter.h
+++ b/llvm/include/llvm/CodeGen/MIRPrinter.h
@@ -50,8 +50,8 @@ LLVM_ABI void printMIR(raw_ostream &OS, const Module &M);
 
 /// Print a machine function using the MIR serialization format to the given
 /// output stream.
-LLVM_ABI void printMIR(raw_ostream &OS, const MachineModuleInfo &MMI,
-                       const MachineFunction &MF);
+LLVM_ABI void printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
+                       FunctionAnalysisManager *MFA, const MachineFunction &MF);
 
 /// Determine a possible list of successors of a basic block based on the
 /// basic block machine operand being used inside the block. This should give
diff --git a/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h b/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
index 55048960fd4d5..0f99dac3a759c 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
@@ -20,9 +20,11 @@ class MachineModuleInfo;
 class MachineFunction;
 class Module;
 
+using MFGetterFnT = std::function<MachineFunction *(const Function &)>;
+
 class LLVM_ABI MachineModuleSlotTracker : public ModuleSlotTracker {
   const Function &TheFunction;
-  const MachineModuleInfo &TheMMI;
+  MFGetterFnT MachineFunctionGetterFn;
   unsigned MDNStartSlot = 0, MDNEndSlot = 0;
 
   void processMachineFunctionMetadata(AbstractSlotTrackerStorage *AST,
@@ -34,8 +36,7 @@ class LLVM_ABI MachineModuleSlotTracker : public ModuleSlotTracker {
                               bool ShouldInitializeAllMetadata);
 
 public:
-  MachineModuleSlotTracker(const MachineModuleInfo &MMI,
-                           const MachineFunction *MF,
+  MachineModuleSlotTracker(MFGetterFnT Fn, const MachineFunction *MF,
                            bool ShouldInitializeAllMetadata = true);
   ~MachineModuleSlotTracker() override;
 
diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp
index 02f07b474c048..b1f188b3782bc 100644
--- a/llvm/lib/CodeGen/MIRPrinter.cpp
+++ b/llvm/lib/CodeGen/MIRPrinter.cpp
@@ -25,9 +25,11 @@
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/CodeGen/MachineMemOperand.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachineModuleSlotTracker.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -102,8 +104,8 @@ struct MFPrintState {
   /// Synchronization scope names registered with LLVMContext.
   SmallVector<StringRef, 8> SSNs;
 
-  MFPrintState(const MachineModuleInfo &MMI, const MachineFunction &MF)
-      : MST(MMI, &MF) {}
+  MFPrintState(MFGetterFnT Fn, const MachineFunction &MF)
+      : MST(std::move(Fn), &MF) {}
 };
 
 } // end anonymous namespace
@@ -169,9 +171,10 @@ static void convertCalledGlobals(yaml::MachineFunction &YMF,
                                  const MachineFunction &MF,
                                  MachineModuleSlotTracker &MST);
 
-static void printMF(raw_ostream &OS, const MachineModuleInfo &MMI,
+static void printMF(raw_ostream &OS, MFGetterFnT Fn,
                     const MachineFunction &MF) {
-  MFPrintState State(MMI, MF);
+  MFPrintState State(std::move(Fn), MF);
+
   State.RegisterMaskIds = initRegisterMaskIds(MF);
 
   yaml::MachineFunction YamlMF;
@@ -1018,7 +1021,19 @@ void llvm::printMIR(raw_ostream &OS, const Module &M) {
   Out << const_cast<Module &>(M);
 }
 
-void llvm::printMIR(raw_ostream &OS, const MachineModuleInfo &MMI,
-                    const MachineFunction &MF) {
-  printMF(OS, MMI, MF);
+void llvm::printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
+                    FunctionAnalysisManager *FAM, const MachineFunction &MF) {
+  if (MMI) {
+    printMF(
+        OS, [&](const Function &F) { return MMI->getMachineFunction(F); }, MF);
+  } else {
+    printMF(
+        OS,
+        [&](const Function &F) {
+          return &FAM->getResult<MachineFunctionAnalysis>(
+                         const_cast<Function &>(F))
+                        .getMF();
+        },
+        MF);
+  }
 }
diff --git a/llvm/lib/CodeGen/MIRPrintingPass.cpp b/llvm/lib/CodeGen/MIRPrintingPass.cpp
index 28aeb7f116c6c..f8215076ec009 100644
--- a/llvm/lib/CodeGen/MIRPrintingPass.cpp
+++ b/llvm/lib/CodeGen/MIRPrintingPass.cpp
@@ -27,12 +27,10 @@ PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
 
 PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
                                     MachineFunctionAnalysisManager &MFAM) {
-  auto &MAMP = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF);
-  Module *M = MF.getFunction().getParent();
-  const MachineModuleInfo &MMI =
-      MAMP.getCachedResult<MachineModuleAnalysis>(*M)->getMMI();
+  auto &FAM = MFAM.getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF)
+                  .getManager();
 
-  printMIR(OS, MMI, MF);
+  printMIR(OS, nullptr, &FAM, MF);
   return PreservedAnalyses::all();
 }
 
@@ -59,10 +57,10 @@ struct MIRPrintingPass : public MachineFunctionPass {
     std::string Str;
     raw_string_ostream StrOS(Str);
 
-    const MachineModuleInfo &MMI =
-        getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
+    MachineModuleInfo *MMI =
+        &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
 
-    printMIR(StrOS, MMI, MF);
+    printMIR(StrOS, MMI, nullptr, MF);
     MachineFunctions.append(Str);
     return false;
   }
diff --git a/llvm/lib/CodeGen/MachineModuleSlotTracker.cpp b/llvm/lib/CodeGen/MachineModuleSlotTracker.cpp
index 8f10435e7998e..ad2a4acc690d4 100644
--- a/llvm/lib/CodeGen/MachineModuleSlotTracker.cpp
+++ b/llvm/lib/CodeGen/MachineModuleSlotTracker.cpp
@@ -39,7 +39,7 @@ void MachineModuleSlotTracker::processMachineModule(
       if (&F != &TheFunction)
         continue;
       MDNStartSlot = AST->getNextMetadataSlot();
-      if (auto *MF = TheMMI.getMachineFunction(F))
+      if (auto *MF = MachineFunctionGetterFn(F))
         processMachineFunctionMetadata(AST, *MF);
       MDNEndSlot = AST->getNextMetadataSlot();
       break;
@@ -52,7 +52,7 @@ void MachineModuleSlotTracker::processMachineFunction(
     bool ShouldInitializeAllMetadata) {
   if (!ShouldInitializeAllMetadata && F == &TheFunction) {
     MDNStartSlot = AST->getNextMetadataSlot();
-    if (auto *MF = TheMMI.getMachineFunction(*F))
+    if (auto *MF = MachineFunctionGetterFn(*F))
       processMachineFunctionMetadata(AST, *MF);
     MDNEndSlot = AST->getNextMetadataSlot();
   }
@@ -64,11 +64,10 @@ void MachineModuleSlotTracker::collectMachineMDNodes(
 }
 
 MachineModuleSlotTracker::MachineModuleSlotTracker(
-    const MachineModuleInfo &MMI, const MachineFunction *MF,
-    bool ShouldInitializeAllMetadata)
+    MFGetterFnT Fn, const MachineFunction *MF, bool ShouldInitializeAllMetadata)
     : ModuleSlotTracker(MF->getFunction().getParent(),
                         ShouldInitializeAllMetadata),
-      TheFunction(MF->getFunction()), TheMMI(MMI) {
+      TheFunction(MF->getFunction()), MachineFunctionGetterFn(Fn) {
   setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M,
                         bool ShouldInitializeAllMetadata) {
     this->processMachineModule(AST, M, ShouldInitializeAllMetadata);
diff --git a/llvm/tools/llvm-reduce/ReducerWorkItem.cpp b/llvm/tools/llvm-reduce/ReducerWorkItem.cpp
index c479233a712e7..2745a80621f8f 100644
--- a/llvm/tools/llvm-reduce/ReducerWorkItem.cpp
+++ b/llvm/tools/llvm-reduce/ReducerWorkItem.cpp
@@ -450,7 +450,7 @@ void ReducerWorkItem::print(raw_ostream &ROS, void *p) const {
     printMIR(ROS, *M);
     for (Function &F : *M) {
       if (auto *MF = MMI->getMachineFunction(F))
-        printMIR(ROS, *MMI, *MF);
+        printMIR(ROS, MMI.get(), nullptr, *MF);
     }
   } else {
     M->print(ROS, /*AssemblyAnnotationWriter=*/nullptr,
diff --git a/llvm/unittests/MIR/MachineMetadata.cpp b/llvm/unittests/MIR/MachineMetadata.cpp
index 8c3637704fc71..4e1f86710347d 100644
--- a/llvm/unittests/MIR/MachineMetadata.cpp
+++ b/llvm/unittests/MIR/MachineMetadata.cpp
@@ -250,7 +250,7 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST(MMI, MF);
+  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ("%1:gpr32 = LDRWui %0, 0 :: (load (s32) from %ir.p, "
@@ -274,7 +274,7 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
+  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -400,7 +400,7 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST(MMI, MF);
+  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ("%1:gr32 = MOV32rm %0, 1, $noreg, 0, $noreg :: (load (s32) from %ir.p, "
@@ -424,7 +424,7 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
+  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -498,7 +498,7 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST(MMI, MF);
+  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ(
@@ -523,7 +523,7 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
+  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}

>From b7f25c6655cff7c566fe17849f0b72bdc83a8548 Mon Sep 17 00:00:00 2001
From: Teja Alaghari <teja.alaghari at amd.com>
Date: Tue, 9 Dec 2025 10:17:31 +0530
Subject: [PATCH 2/3] Format code in MachineModuleSlotTracker patch

---
 llvm/lib/CodeGen/MIRPrinter.cpp        |  2 +-
 llvm/unittests/MIR/MachineMetadata.cpp | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp
index b1f188b3782bc..7b89a38e3400f 100644
--- a/llvm/lib/CodeGen/MIRPrinter.cpp
+++ b/llvm/lib/CodeGen/MIRPrinter.cpp
@@ -1032,7 +1032,7 @@ void llvm::printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
         [&](const Function &F) {
           return &FAM->getResult<MachineFunctionAnalysis>(
                          const_cast<Function &>(F))
-                        .getMF();
+                      .getMF();
         },
         MF);
   }
diff --git a/llvm/unittests/MIR/MachineMetadata.cpp b/llvm/unittests/MIR/MachineMetadata.cpp
index 4e1f86710347d..f5831bf2cf8a6 100644
--- a/llvm/unittests/MIR/MachineMetadata.cpp
+++ b/llvm/unittests/MIR/MachineMetadata.cpp
@@ -250,7 +250,8 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
+  MachineModuleSlotTracker MST(
+      [&](const Function &F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ("%1:gpr32 = LDRWui %0, 0 :: (load (s32) from %ir.p, "
@@ -274,7 +275,8 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
+  std::string Output =
+      print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -400,7 +402,8 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
+  MachineModuleSlotTracker MST(
+      [&](const Function &F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ("%1:gr32 = MOV32rm %0, 1, $noreg, 0, $noreg :: (load (s32) from %ir.p, "
@@ -424,7 +427,8 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
+  std::string Output =
+      print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -498,7 +502,8 @@ body:             |
   auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
   MI.setMemRefs(*MF, NewMMO);
 
-  MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
+  MachineModuleSlotTracker MST(
+      [&](const Function &F) { return MMI.getMachineFunction(F); }, MF);
   // Print that MI with new machine metadata, which slot numbers should be
   // assigned.
   EXPECT_EQ(
@@ -523,7 +528,8 @@ body:             |
   EXPECT_EQ(Collected, Generated);
 
   // FileCheck the output from MIR printer.
-  std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
+  std::string Output =
+      print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
   std::string CheckString = R"(
 CHECK: machineMetadataNodes:
 CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}

>From f4391e67b1f092ea2cdb7b47cc1f1f11e93c2bcd Mon Sep 17 00:00:00 2001
From: Teja Alaghari <teja.alaghari at amd.com>
Date: Tue, 9 Dec 2025 11:03:20 +0530
Subject: [PATCH 3/3] - Add missing <functional> header file inclusion for
 std::function - Fix parameter name inconsistency (MFA -> FAM) in header

---
 llvm/include/llvm/CodeGen/MIRPrinter.h               | 7 ++++---
 llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h | 1 +
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MIRPrinter.h b/llvm/include/llvm/CodeGen/MIRPrinter.h
index f90d23c2d993b..a7db47f7e7093 100644
--- a/llvm/include/llvm/CodeGen/MIRPrinter.h
+++ b/llvm/include/llvm/CodeGen/MIRPrinter.h
@@ -48,10 +48,11 @@ class PrintMIRPass : public PassInfoMixin<PrintMIRPass> {
 /// Print LLVM IR using the MIR serialization format to the given output stream.
 LLVM_ABI void printMIR(raw_ostream &OS, const Module &M);
 
-/// Print a machine function using the MIR serialization format to the given
-/// output stream.
+/// This function supports both Legacy Pass Manager and New Pass Manager:
+/// - Legacy PM: Pass non-null \p MMI and null \p FAM
+/// - New PM: Pass null \p MMI and non-null \p FAM
 LLVM_ABI void printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
-                       FunctionAnalysisManager *MFA, const MachineFunction &MF);
+                       FunctionAnalysisManager *FAM, const MachineFunction &MF);
 
 /// Determine a possible list of successors of a basic block based on the
 /// basic block machine operand being used inside the block. This should give
diff --git a/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h b/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
index 0f99dac3a759c..77f3884702f04 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
@@ -11,6 +11,7 @@
 
 #include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/Support/Compiler.h"
+#include <functional>
 
 namespace llvm {
 



More information about the llvm-commits mailing list