[llvm] Add A Version Of `MachineModuleInfoWrapperPass` That Does Not Own Its Underlying `MachineModuleInfo` (PR #134554)

Matin Raayai via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 6 10:48:23 PDT 2025


https://github.com/matinraayai created https://github.com/llvm/llvm-project/pull/134554

This PR makes the following changes to the `MachineModuleInfoWrapperPass`:
1. `MachineModuleInfoWrapperPass` is now an interface pass with its own typeid.
2. Two kinds of `MachineModuleInfoWrapperPass` are introduced, one is `OwningMachineModuleInfoWrapperPass`, which behaves the same way as the original `MachineModuleInfoWrapperPass`, and one is `NonOwningMachineModuleInfoWrapperPass`, which instead takes a reference to an already created `MachineModuleInfo`.
Also, this PR removes the move constructor for `MachineModuleInfo` since it was not correct to begin with.

Originally, I planned on only having the non-owning version available, but then I realized that many users of `TargetMachine::addPassesToEmitFile` don't link against `CodeGen`, which means they cannot create a `MachineModuleInfo` and manage its lifetime. I also think its not ideal to explicitly link against `CodeGen`.

Feedback is greatly appreciated.
cc: @aeubanks @arsenm 

>From ed5c3322ebd3b08d83247326210a5fd04d488982 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraayai at users.noreply.github.com>
Date: Sat, 5 Apr 2025 15:28:40 -0400
Subject: [PATCH 1/5] - Removed move constructor for MMI. - Made MMIWP not own
 the MMI storage.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 11 ++-------
 llvm/lib/CodeGen/MachineModuleInfo.cpp        | 24 ++++---------------
 2 files changed, 7 insertions(+), 28 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index bec500dc609f3..a7d951d7b55fc 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,15 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
-
 public:
   explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
 
   explicit MachineModuleInfo(const TargetMachine *TM, MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -168,14 +164,11 @@ class MachineModuleInfo {
 }; // End class MachineModuleInfo
 
 class MachineModuleInfoWrapperPass : public ImmutablePass {
-  MachineModuleInfo MMI;
+  MachineModuleInfo &MMI;
 
 public:
   static char ID; // Pass identification, replacement for typeid
-  explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const TargetMachine *TM,
-                                        MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo *MMI = nullptr);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index 6167e99aecf03..1d838871b4de0 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -37,17 +37,6 @@ void MachineModuleInfo::finalize() {
   ObjFileMMI = nullptr;
 }
 
-MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
-    : TM(std::move(MMI.TM)),
-      Context(TM.getTargetTriple(), TM.getMCAsmInfo(), TM.getMCRegisterInfo(),
-              TM.getMCSubtargetInfo(), nullptr, &TM.Options.MCOptions, false),
-      MachineFunctions(std::move(MMI.MachineFunctions)) {
-  Context.setObjectFileInfo(TM.getObjFileLowering());
-  ObjFileMMI = MMI.ObjFileMMI;
-  ExternalContext = MMI.ExternalContext;
-  TheModule = MMI.TheModule;
-}
-
 MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
     : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
                        TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,11 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-    const TargetMachine *TM)
-    : ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-    const TargetMachine *TM, MCContext *ExtContext)
-    : ImmutablePass(ID), MMI(TM, ExtContext) {
+    MachineModuleInfo *MMI)
+    : ImmutablePass(ID), MMI([&] -> MachineModuleInfo & {
+        assert(MMI != nullptr, "MMI is nullptr");
+        return *MMI;
+      }()) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From 1a297793adfe6cabb338831670a13963308d3d2c Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraayai at users.noreply.github.com>
Date: Sat, 5 Apr 2025 15:39:13 -0400
Subject: [PATCH 2/5] Changed TargetMachine and CodeGenTargetMachineImpl
 interfaces to use externally-initialized MMIs.

---
 .../llvm/CodeGen/CodeGenTargetMachineImpl.h   | 29 +++++++------
 llvm/include/llvm/Target/TargetMachine.h      | 42 ++++++++++++-------
 llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp | 33 ++++++++++-----
 3 files changed, 65 insertions(+), 39 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
index 7bb4420e555fb..7f1a33ebbf1f5 100644
--- a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
+++ b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
@@ -43,22 +43,27 @@ class CodeGenTargetMachineImpl : public TargetMachine {
   /// for generating a pipeline of CodeGen passes.
   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
 
+  /// Creates a new \c MachineModuleInfo from this \c TargetMachine
+  std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const override;
+
   /// Add passes to the specified pass manager to get the specified file
-  /// emitted.  Typically this will involve several steps of code generation.
-  /// \p MMIWP is an optional parameter that, if set to non-nullptr,
-  /// will be used to set the MachineModuloInfo for this PM.
-  bool
-  addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
-                      raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
-                      bool DisableVerify = true,
-                      MachineModuleInfoWrapperPass *MMIWP = nullptr) override;
+  /// emitted. Typically, this will involve several steps of code generation.
+  /// This method should return true if emission of this file type is not
+  /// supported, or false on success.
+  /// \p MMI a \c MachineModuleInfo that holds the generated machine code
+  /// throughout the code generation process
+  bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
+                           raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
+                           MachineModuleInfo *MMI,
+                           bool DisableVerify = true) override;
 
   /// Add passes to the specified pass manager to get machine code emitted with
-  /// the MCJIT. This method returns true if machine code is not supported. It
-  /// fills the MCContext Ctx pointer which can be used to build custom
-  /// MCStreamer.
-  bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
+  /// the MCJIT. This method returns true if machine code is not supported.
+  /// \p MMI a \c MachineModuleInfo that holds the generated machine code
+  /// throughout the code generation process
+  bool addPassesToEmitMC(PassManagerBase &PM,
                          raw_pwrite_stream &Out,
+                         MachineModuleInfo *MMI,
                          bool DisableVerify = true) override;
 
   /// Adds an AsmPrinter pass to the pipeline that prints assembly or
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index 27eeb415ed644..7f883cb07e674 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -38,7 +38,7 @@ using ModulePassManager = PassManager<Module>;
 
 class Function;
 class GlobalValue;
-class MachineModuleInfoWrapperPass;
+class MachineModuleInfo;
 struct MachineSchedContext;
 class Mangler;
 class MCAsmInfo;
@@ -195,7 +195,7 @@ class TargetMachine {
   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
   /// returned is of the correct type.
   template <typename STC> const STC &getSubtarget(const Function &F) const {
-    return *static_cast<const STC*>(getSubtargetImpl(F));
+    return *static_cast<const STC *>(getSubtargetImpl(F));
   }
 
   /// Create a DataLayout.
@@ -400,27 +400,37 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
+  /// For targets that utilize the target-independent code generator
+  /// (CodeGen), this method creates a new \c MachineModuleInfo from this
+  /// \c TargetMachine and returns it; For targets that don't use CodeGen,
+  /// returns nullptr
+  virtual std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const {
+    return nullptr;
+  }
+
   /// Add passes to the specified pass manager to get the specified file
-  /// emitted.  Typically this will involve several steps of code generation.
+  /// emitted. Typically, this will involve several steps of code generation.
   /// This method should return true if emission of this file type is not
   /// supported, or false on success.
-  /// \p MMIWP is an optional parameter that, if set to non-nullptr,
-  /// will be used to set the MachineModuloInfo for this PM.
-  virtual bool
-  addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
-                      raw_pwrite_stream *, CodeGenFileType,
-                      bool /*DisableVerify*/ = true,
-                      MachineModuleInfoWrapperPass *MMIWP = nullptr) {
+  /// For targets that utilize the target-independent code generator
+  /// (CodeGen) to emit the file, \c MachineModuleInfo pointer will hold the
+  /// generated machine code; For targets that don't use CodeGen, it is set
+  /// to \c nullptr
+  virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
+                                   raw_pwrite_stream *, CodeGenFileType,
+                                   MachineModuleInfo *,
+                                   bool /*DisableVerify*/ = true) {
     return true;
   }
 
   /// Add passes to the specified pass manager to get machine code emitted with
-  /// the MCJIT. This method returns true if machine code is not supported. It
-  /// fills the MCContext Ctx pointer which can be used to build custom
-  /// MCStreamer.
-  ///
-  virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
-                                 raw_pwrite_stream &,
+  /// the MCJIT. This method returns true if machine code is not supported.
+  /// For targets that utilize the target-independent code generator
+  /// (CodeGen), \c MachineModuleInfo pointer will hold the generated
+  /// machine code; For targets that don't use CodeGen, it must be set
+  /// to \c nullptr
+  virtual bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &,
+                                 MachineModuleInfo *,
                                  bool /*DisableVerify*/ = true) {
     return true;
   }
diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
index 5757ca1b3adf8..e45a14f31fb93 100644
--- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
+++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
@@ -208,20 +208,27 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
   return std::move(AsmStreamer);
 }
 
+std::unique_ptr<MachineModuleInfo>
+CodeGenTargetMachineImpl::createMachineModuleInfo() const override {
+  return std::make_unique<MachineModuleInfo>(this);
+}
+
 bool CodeGenTargetMachineImpl::addPassesToEmitFile(
     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
-    CodeGenFileType FileType, bool DisableVerify,
-    MachineModuleInfoWrapperPass *MMIWP) {
-  // Add common CodeGen passes.
-  if (!MMIWP)
-    MMIWP = new MachineModuleInfoWrapperPass(this);
+    CodeGenFileType FileType, MachineModuleInfo *MMI, bool DisableVerify) {
+  // Check if MMI is nullptr, or if MMI pointer is valid but the target
+  // machine of the MMI is not the same as this target machine
+  if (!MMI || &MMI->getTarget() != this)
+    return true;
+  // Add the wrapper pass for MMI
+  auto *MMIWP = new MachineModuleInfoWrapperPass(MMI);
   TargetPassConfig *PassConfig =
       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
   if (!PassConfig)
     return true;
 
   if (TargetPassConfig::willCompleteCodeGenPipeline()) {
-    if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext()))
+    if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
       return true;
   } else {
     // MIR printing is redundant with -filetype=null.
@@ -239,11 +246,15 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile(
 /// used to build custom MCStreamer.
 ///
 bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
-                                                 MCContext *&Ctx,
                                                  raw_pwrite_stream &Out,
+                                                 MachineModuleInfo *MMI,
                                                  bool DisableVerify) {
+  // Check if MMI is nullptr, or if MMI pointer is valid but the target
+  // machine of the MMI is not the same as this target machine
+  if (!MMI || &MMI->getTarget() != this)
+    return true;
   // Add common CodeGen passes.
-  MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this);
+  MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(MMI);
   TargetPassConfig *PassConfig =
       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
   if (!PassConfig)
@@ -251,7 +262,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
   assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
          "Cannot emit MC with limited codegen pipeline");
 
-  Ctx = &MMIWP->getMMI().getContext();
+  MCContext &Ctx = MMI->getContext();
   // libunwind is unable to load compact unwind dynamically, so we must generate
   // DWARF unwind info for the JIT.
   Options.MCOptions.EmitDwarfUnwind = EmitDwarfUnwindType::Always;
@@ -261,7 +272,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
   const MCRegisterInfo &MRI = *getMCRegisterInfo();
   std::unique_ptr<MCCodeEmitter> MCE(
-      getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx));
+      getTarget().createMCCodeEmitter(*getMCInstrInfo(), Ctx));
   if (!MCE)
     return true;
   MCAsmBackend *MAB =
@@ -271,7 +282,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
 
   const Triple &T = getTargetTriple();
   std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
-      T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
+      T, Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
       std::move(MCE), STI));
 
   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.

>From 8715ed6fbcfbb4a89327ff687f95f3c61263d3d6 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraayai at users.noreply.github.com>
Date: Sat, 5 Apr 2025 23:52:01 -0400
Subject: [PATCH 3/5] Created owning and non-owning variants of MMIWP.

---
 .../llvm/CodeGen/CodeGenTargetMachineImpl.h   | 22 ++++-----
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 40 +++++++++++++--
 llvm/include/llvm/Target/TargetMachine.h      | 28 ++++-------
 llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp | 49 ++++++++++---------
 llvm/lib/CodeGen/MachineModuleInfo.cpp        | 11 ++---
 llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp      |  7 ++-
 llvm/lib/ExecutionEngine/MCJIT/MCJIT.h        |  1 -
 llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp |  3 +-
 llvm/lib/LTO/ThinLTOCodeGenerator.cpp         |  4 +-
 .../Target/DirectX/DirectXTargetMachine.cpp   | 20 +++++---
 .../lib/Target/DirectX/DirectXTargetMachine.h |  6 +--
 llvm/lib/Target/NVPTX/NVPTXTargetMachine.h    |  4 +-
 llvm/lib/Target/SPIRV/SPIRVAPI.cpp            |  9 ++--
 offload/plugins-nextgen/common/src/JIT.cpp    |  5 +-
 14 files changed, 118 insertions(+), 91 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
index 7f1a33ebbf1f5..1491eb3d42964 100644
--- a/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
+++ b/llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h
@@ -43,28 +43,24 @@ class CodeGenTargetMachineImpl : public TargetMachine {
   /// for generating a pipeline of CodeGen passes.
   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
 
-  /// Creates a new \c MachineModuleInfo from this \c TargetMachine
-  std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const override;
-
   /// Add passes to the specified pass manager to get the specified file
   /// emitted. Typically, this will involve several steps of code generation.
   /// This method should return true if emission of this file type is not
   /// supported, or false on success.
-  /// \p MMI a \c MachineModuleInfo that holds the generated machine code
-  /// throughout the code generation process
+  /// \p MMI an optional, externally-managed \c MachineModuleInfo to
+  /// hold the generated machine code even after the \p PM is destroyed
   bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
                            raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
-                           MachineModuleInfo *MMI,
-                           bool DisableVerify = true) override;
+                           bool DisableVerify = true,
+                           MachineModuleInfo *MMI = nullptr) override;
 
   /// Add passes to the specified pass manager to get machine code emitted with
   /// the MCJIT. This method returns true if machine code is not supported.
-  /// \p MMI a \c MachineModuleInfo that holds the generated machine code
-  /// throughout the code generation process
-  bool addPassesToEmitMC(PassManagerBase &PM,
-                         raw_pwrite_stream &Out,
-                         MachineModuleInfo *MMI,
-                         bool DisableVerify = true) override;
+  /// \p MMI an optional, externally-managed \c MachineModuleInfo to
+  /// hold the generated machine code even after the \p PM is destroyed
+  bool addPassesToEmitMC(PassManagerBase &PM, raw_pwrite_stream &Out,
+                         bool DisableVerify = true,
+                         MachineModuleInfo *MMI = nullptr) override;
 
   /// Adds an AsmPrinter pass to the pipeline that prints assembly or
   /// machine code from the MI representation.
diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index a7d951d7b55fc..4e4582fa0021a 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -163,19 +163,51 @@ class MachineModuleInfo {
   /// \}
 }; // End class MachineModuleInfo
 
+/// \brief Interface for pass that provide access to \c MachineModuleInfo
+/// being worked on
 class MachineModuleInfoWrapperPass : public ImmutablePass {
-  MachineModuleInfo &MMI;
 
 public:
   static char ID; // Pass identification, replacement for typeid
-  explicit MachineModuleInfoWrapperPass(MachineModuleInfo *MMI = nullptr);
+  MachineModuleInfoWrapperPass();
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
   bool doFinalization(Module &) override;
 
-  MachineModuleInfo &getMMI() { return MMI; }
-  const MachineModuleInfo &getMMI() const { return MMI; }
+  virtual MachineModuleInfo &getMMI() = 0;
+  virtual const MachineModuleInfo &getMMI() const = 0;
+};
+
+/// \brief a version of \c MachineModuleInfoWrapperPass that manages the
+/// lifetime of its \c MachineModuleInfo
+class OwningMachineModuleInfoWrapperPass : public MachineModuleInfoWrapperPass {
+  MachineModuleInfo MMI;
+
+public:
+  explicit OwningMachineModuleInfoWrapperPass(const TargetMachine &TM)
+      : MMI(&TM) {};
+
+  OwningMachineModuleInfoWrapperPass(const TargetMachine &TM,
+                                     MCContext &ExtContext)
+      : MMI(&TM, &ExtContext) {};
+
+  MachineModuleInfo &getMMI() override { return MMI; }
+  const MachineModuleInfo &getMMI() const override { return MMI; }
+};
+
+/// \brief a version of \c MachineModuleInfoWrapperPass that does not manage the
+/// lifetime of its \c MachineModuleInfo
+class NonOwningMachineModuleInfoWrapperPass
+    : public MachineModuleInfoWrapperPass {
+  MachineModuleInfo &MMI;
+
+public:
+  explicit NonOwningMachineModuleInfoWrapperPass(MachineModuleInfo &MMI)
+      : MMI(MMI) {};
+
+  MachineModuleInfo &getMMI() override { return MMI; }
+  const MachineModuleInfo &getMMI() const override { return MMI; }
 };
 
 /// An analysis that produces \c MachineModuleInfo for a module.
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index 7f883cb07e674..bb44dcbcafd87 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -400,38 +400,30 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// For targets that utilize the target-independent code generator
-  /// (CodeGen), this method creates a new \c MachineModuleInfo from this
-  /// \c TargetMachine and returns it; For targets that don't use CodeGen,
-  /// returns nullptr
-  virtual std::unique_ptr<MachineModuleInfo> createMachineModuleInfo() const {
-    return nullptr;
-  }
-
   /// Add passes to the specified pass manager to get the specified file
   /// emitted. Typically, this will involve several steps of code generation.
   /// This method should return true if emission of this file type is not
   /// supported, or false on success.
   /// For targets that utilize the target-independent code generator
-  /// (CodeGen) to emit the file, \c MachineModuleInfo pointer will hold the
-  /// generated machine code; For targets that don't use CodeGen, it is set
-  /// to \c nullptr
+  /// (CodeGen), an externally-managed \c MachineModuleInfo can be provided
+  /// to ensure persistence of the generated machine code even after the pass
+  /// manager is destroyed
   virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
                                    raw_pwrite_stream *, CodeGenFileType,
-                                   MachineModuleInfo *,
-                                   bool /*DisableVerify*/ = true) {
+                                   bool /*DisableVerify*/ = true,
+                                   MachineModuleInfo * = nullptr) {
     return true;
   }
 
   /// Add passes to the specified pass manager to get machine code emitted with
   /// the MCJIT. This method returns true if machine code is not supported.
   /// For targets that utilize the target-independent code generator
-  /// (CodeGen), \c MachineModuleInfo pointer will hold the generated
-  /// machine code; For targets that don't use CodeGen, it must be set
-  /// to \c nullptr
+  /// (CodeGen), an externally-managed \c MachineModuleInfo can be provided
+  /// to ensure persistence of the generated machine code even after the pass
+  /// manager is destroyed
   virtual bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &,
-                                 MachineModuleInfo *,
-                                 bool /*DisableVerify*/ = true) {
+                                 bool /*DisableVerify*/ = true,
+                                 MachineModuleInfo * = nullptr) {
     return true;
   }
 
diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
index e45a14f31fb93..3495a45859054 100644
--- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
+++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
@@ -208,20 +208,22 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
   return std::move(AsmStreamer);
 }
 
-std::unique_ptr<MachineModuleInfo>
-CodeGenTargetMachineImpl::createMachineModuleInfo() const override {
-  return std::make_unique<MachineModuleInfo>(this);
-}
-
 bool CodeGenTargetMachineImpl::addPassesToEmitFile(
     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
-    CodeGenFileType FileType, MachineModuleInfo *MMI, bool DisableVerify) {
-  // Check if MMI is nullptr, or if MMI pointer is valid but the target
-  // machine of the MMI is not the same as this target machine
-  if (!MMI || &MMI->getTarget() != this)
-    return true;
+    CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) {
   // Add the wrapper pass for MMI
-  auto *MMIWP = new MachineModuleInfoWrapperPass(MMI);
+  MachineModuleInfoWrapperPass *MMIWP;
+  if (MMI) {
+    MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
+  } else {
+    MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
+    MMI = &MMIWP->getMMI();
+  }
+  // Check (for the case of externally-managed MMI) if the TM of MMI is
+  // the same as this target machine
+  if (&MMI->getTarget() != this)
+    return true;
+
   TargetPassConfig *PassConfig =
       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
   if (!PassConfig)
@@ -240,21 +242,22 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile(
   return false;
 }
 
-/// addPassesToEmitMC - Add passes to the specified pass manager to get
-/// machine code emitted with the MCJIT. This method returns true if machine
-/// code is not supported. It fills the MCContext Ctx pointer which can be
-/// used to build custom MCStreamer.
-///
 bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
                                                  raw_pwrite_stream &Out,
-                                                 MachineModuleInfo *MMI,
-                                                 bool DisableVerify) {
-  // Check if MMI is nullptr, or if MMI pointer is valid but the target
-  // machine of the MMI is not the same as this target machine
-  if (!MMI || &MMI->getTarget() != this)
+                                                 bool DisableVerify,
+                                                 MachineModuleInfo *MMI) {
+  // Add the wrapper pass for MMI
+  MachineModuleInfoWrapperPass *MMIWP;
+  if (MMI) {
+    MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
+  } else {
+    MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
+    MMI = &MMIWP->getMMI();
+  }
+  // Check (for the case of externally-managed MMI) if the TM of MMI is
+  // the same as this target machine
+  if (&MMI->getTarget() != this)
     return true;
-  // Add common CodeGen passes.
-  MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(MMI);
   TargetPassConfig *PassConfig =
       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
   if (!PassConfig)
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index 1d838871b4de0..76841d4116ef4 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -139,12 +139,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
   return new FreeMachineFunction();
 }
 
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-    MachineModuleInfo *MMI)
-    : ImmutablePass(ID), MMI([&] -> MachineModuleInfo & {
-        assert(MMI != nullptr, "MMI is nullptr");
-        return *MMI;
-      }()) {
+MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass()
+    : ImmutablePass(ID) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 
@@ -179,6 +175,7 @@ static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
 }
 
 bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
+  MachineModuleInfo &MMI = getMMI();
   MMI.initialize();
   MMI.TheModule = &M;
   LLVMContext &Ctx = M.getContext();
@@ -196,7 +193,7 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
 }
 
 bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
-  MMI.finalize();
+  getMMI().finalize();
   return false;
 }
 
diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp
index 5f3067b2a97ea..e73db347e4c01 100644
--- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp
+++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp
@@ -66,9 +66,8 @@ MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
              std::shared_ptr<MCJITMemoryManager> MemMgr,
              std::shared_ptr<LegacyJITSymbolResolver> Resolver)
     : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
-      Ctx(nullptr), MemMgr(std::move(MemMgr)),
-      Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
-      ObjCache(nullptr) {
+      MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
+      Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
   // FIXME: We are managing our modules, so we do not want the base class
   // ExecutionEngine to manage them as well. To avoid double destruction
   // of the first (and only) module added in ExecutionEngine constructor
@@ -163,7 +162,7 @@ std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
 
   // Turn the machine code intermediate representation into bytes in memory
   // that may be executed.
-  if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
+  if (TM->addPassesToEmitMC(PM, ObjStream, !getVerifyModules()))
     report_fatal_error("Target does not support MC emission!");
 
   // Initialize passes.
diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h
index fbe64fabd3240..596b83dbb8a05 100644
--- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h
+++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.h
@@ -168,7 +168,6 @@ class MCJIT : public ExecutionEngine {
   };
 
   std::unique_ptr<TargetMachine> TM;
-  MCContext *Ctx;
   std::shared_ptr<MCJITMemoryManager> MemMgr;
   LinkingSymbolResolver Resolver;
   RuntimeDyld Dyld;
diff --git a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp
index c4d65af1b57f8..b09cc738996a5 100644
--- a/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp
@@ -46,8 +46,7 @@ Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
     raw_svector_ostream ObjStream(ObjBufferSV);
 
     legacy::PassManager PM;
-    MCContext *Ctx;
-    if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
+    if (TM.addPassesToEmitMC(PM, ObjStream))
       return make_error<StringError>("Target does not support MC emission",
                                      inconvertibleErrorCode());
     PM.run(M);
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index 9e7f8187fe49c..198187bd740ea 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -331,9 +331,11 @@ static std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
   {
     raw_svector_ostream OS(OutputBuffer);
     legacy::PassManager PM;
+    std::unique_ptr<MachineModuleInfo> MMI = TM.createMachineModuleInfo();
 
-    // Setup the codegen now.
+    // Setup the codegen pipeline.
     if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
+                               MMI.get(),
                                /* DisableVerify */ true))
       report_fatal_error("Failed to setup codegen");
 
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index ce408b4034f83..d7a05ebf1f738 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -133,8 +133,7 @@ void DirectXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
 
 bool DirectXTargetMachine::addPassesToEmitFile(
     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
-    CodeGenFileType FileType, bool DisableVerify,
-    MachineModuleInfoWrapperPass *MMIWP) {
+    CodeGenFileType FileType, bool DisableVerify, MachineModuleInfo *MMI) {
   TargetPassConfig *PassConfig = createPassConfig(PM);
   PassConfig->addCodeGenPrepare();
 
@@ -150,8 +149,17 @@ bool DirectXTargetMachine::addPassesToEmitFile(
       // globals don't pollute the DXIL.
       PM.add(createDXContainerGlobalsPass());
 
-      if (!MMIWP)
-        MMIWP = new MachineModuleInfoWrapperPass(this);
+      MachineModuleInfoWrapperPass *MMIWP;
+      if (MMI) {
+        MMIWP = new NonOwningMachineModuleInfoWrapperPass(*MMI);
+      } else {
+        MMIWP = new OwningMachineModuleInfoWrapperPass(*this);
+        MMI = &MMIWP->getMMI();
+      }
+      // Check (for the case of externally-managed MMI) if the TM of MMI is
+      // the same as this target machine
+      if (&MMI->getTarget() != this)
+        return true;
       PM.add(MMIWP);
       if (addAsmPrinter(PM, Out, DwoOut, FileType,
                         MMIWP->getMMI().getContext()))
@@ -166,9 +174,9 @@ bool DirectXTargetMachine::addPassesToEmitFile(
 }
 
 bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
-                                             MCContext *&Ctx,
                                              raw_pwrite_stream &Out,
-                                             bool DisableVerify) {
+                                             bool DisableVerify,
+                                             MachineModuleInfo *MMI) {
   return true;
 }
 
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.h b/llvm/lib/Target/DirectX/DirectXTargetMachine.h
index 7ba80d2ba5de1..21c50d4c996a9 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.h
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.h
@@ -33,10 +33,10 @@ class DirectXTargetMachine : public CodeGenTargetMachineImpl {
   bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
                            raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
                            bool DisableVerify,
-                           MachineModuleInfoWrapperPass *MMIWP) override;
+                           MachineModuleInfo *MMIWP) override;
 
-  bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
-                         raw_pwrite_stream &Out, bool DisableVerify) override;
+  bool addPassesToEmitMC(PassManagerBase &PM, raw_pwrite_stream &Out,
+                         bool DisableVerify, MachineModuleInfo *MMIWP) override;
 
   const DirectXSubtarget *getSubtargetImpl(const Function &) const override;
 
diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h
index 34d841cd28404..d3755c6d54d59 100644
--- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h
+++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h
@@ -52,8 +52,8 @@ class NVPTXTargetMachine : public CodeGenTargetMachineImpl {
   TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
 
   // Emission of machine code through MCJIT is not supported.
-  bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &,
-                         bool = true) override {
+  bool addPassesToEmitMC(PassManagerBase &, raw_pwrite_stream &,
+                         bool = true, MachineModuleInfo *) override {
     return true;
   }
   TargetLoweringObjectFile *getObjFileLowering() const override {
diff --git a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
index 145285a31dc10..e3159c83c4eb2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
@@ -121,15 +121,14 @@ SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg,
   TargetLibraryInfoImpl TLII(M->getTargetTriple());
   legacy::PassManager PM;
   PM.add(new TargetLibraryInfoWrapperPass(TLII));
-  std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
-      new MachineModuleInfoWrapperPass(Target.get()));
-  const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
-      ->Initialize(MMIWP.get()->getMMI().getContext(), *Target);
+
+  MachineModuleInfo MMI(Target.get());
+  Target->getObjFileLowering()->Initialize(MMI.getContext(), *Target);
 
   SmallString<4096> OutBuffer;
   raw_svector_ostream OutStream(OutBuffer);
   if (Target->addPassesToEmitFile(PM, OutStream, nullptr,
-                                  CodeGenFileType::ObjectFile)) {
+                                  CodeGenFileType::ObjectFile, true, &MMI)) {
     ErrMsg = "Target machine cannot emit a file of this type";
     return false;
   }
diff --git a/offload/plugins-nextgen/common/src/JIT.cpp b/offload/plugins-nextgen/common/src/JIT.cpp
index affedb1a33687..bf4b7441e503c 100644
--- a/offload/plugins-nextgen/common/src/JIT.cpp
+++ b/offload/plugins-nextgen/common/src/JIT.cpp
@@ -175,11 +175,12 @@ void JITEngine::codegen(TargetMachine *TM, TargetLibraryInfoImpl *TLII,
                         Module &M, raw_pwrite_stream &OS) {
   legacy::PassManager PM;
   PM.add(new TargetLibraryInfoWrapperPass(*TLII));
-  MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(TM);
+  MachineModuleInfo MMI(TM);
   TM->addPassesToEmitFile(PM, OS, nullptr,
                           TT.isNVPTX() ? CodeGenFileType::AssemblyFile
                                        : CodeGenFileType::ObjectFile,
-                          /*DisableVerify=*/false, MMIWP);
+                          /*DisableVerify=*/false,
+                          &MMI);
 
   PM.run(M);
 }

>From 832e08867616699b0b8539b4a659b5b6084ecca7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraayai at users.noreply.github.com>
Date: Sun, 6 Apr 2025 00:51:42 -0400
Subject: [PATCH 4/5] Fixed remaining issues with MMI/MMIWP constructions.

---
 llvm/lib/LTO/ThinLTOCodeGenerator.cpp      |  4 +---
 llvm/tools/llc/llc.cpp                     | 14 +++++---------
 llvm/tools/llvm-exegesis/lib/Assembler.cpp |  8 ++++----
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index 198187bd740ea..9e7f8187fe49c 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -331,11 +331,9 @@ static std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
   {
     raw_svector_ostream OS(OutputBuffer);
     legacy::PassManager PM;
-    std::unique_ptr<MachineModuleInfo> MMI = TM.createMachineModuleInfo();
 
-    // Setup the codegen pipeline.
+    // Setup the codegen now.
     if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
-                               MMI.get(),
                                /* DisableVerify */ true))
       report_fatal_error("Failed to setup codegen");
 
diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index a97d07dec722b..27ac543327f4e 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -671,8 +671,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
     }
 
     const char *argv0 = argv[0];
-    MachineModuleInfoWrapperPass *MMIWP =
-        new MachineModuleInfoWrapperPass(Target.get());
+    MachineModuleInfo MMI(Target.get());
 
     // Construct a custom pass pipeline that starts after instruction
     // selection.
@@ -680,7 +679,6 @@ static int compileModule(char **argv, LLVMContext &Context) {
       if (!MIR) {
         WithColor::error(errs(), argv[0])
             << "run-pass is for .mir file only.\n";
-        delete MMIWP;
         return 1;
       }
       TargetPassConfig *PTPC = Target->createPassConfig(PM);
@@ -690,13 +688,12 @@ static int compileModule(char **argv, LLVMContext &Context) {
             << "run-pass cannot be used with "
             << TPC.getLimitedCodeGenPipelineReason() << ".\n";
         delete PTPC;
-        delete MMIWP;
         return 1;
       }
 
       TPC.setDisableVerify(NoVerify);
       PM.add(&TPC);
-      PM.add(MMIWP);
+      PM.add(new NonOwningMachineModuleInfoWrapperPass(MMI));
       TPC.printAndVerify("");
       for (const std::string &RunPassName : getRunPassNames()) {
         if (addPass(PM, argv0, RunPassName, TPC))
@@ -707,15 +704,14 @@ static int compileModule(char **argv, LLVMContext &Context) {
       PM.add(createFreeMachineFunctionPass());
     } else if (Target->addPassesToEmitFile(
                    PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
-                   codegen::getFileType(), NoVerify, MMIWP)) {
+                   codegen::getFileType(), NoVerify, &MMI)) {
       reportError("target does not support generation of this file type");
     }
 
     const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
-        ->Initialize(MMIWP->getMMI().getContext(), *Target);
+        ->Initialize(MMI.getContext(), *Target);
     if (MIR) {
-      assert(MMIWP && "Forgot to create MMIWP?");
-      if (MIR->parseMachineFunctions(*M, MMIWP->getMMI()))
+      if (MIR->parseMachineFunctions(*M, MMI))
         return 1;
     }
 
diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
index f638478e0c51d..729c829b106e8 100644
--- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp
+++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
@@ -233,9 +233,9 @@ createModule(const std::unique_ptr<LLVMContext> &Context, const DataLayout &DL)
 BitVector getFunctionReservedRegs(const TargetMachine &TM) {
   std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>();
   std::unique_ptr<Module> Module = createModule(Context, TM.createDataLayout());
-  auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(&TM);
-  MachineFunction &MF = createVoidVoidPtrMachineFunction(
-      FunctionID, Module.get(), &MMIWP->getMMI());
+  auto MMI = std::make_unique<MachineModuleInfo>(&TM);
+  MachineFunction &MF =
+      createVoidVoidPtrMachineFunction(FunctionID, Module.get(), MMI.get());
   // Saving reserved registers for client.
   return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF);
 }
@@ -248,7 +248,7 @@ Error assembleToStream(const ExegesisTarget &ET,
   auto Context = std::make_unique<LLVMContext>();
   std::unique_ptr<Module> Module =
       createModule(Context, TM->createDataLayout());
-  auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(TM.get());
+  auto MMIWP = std::make_unique<OwningMachineModuleInfoWrapperPass>(*TM);
   MachineFunction &MF = createVoidVoidPtrMachineFunction(
       FunctionID, Module.get(), &MMIWP.get()->getMMI());
   MF.ensureAlignment(kFunctionAlignment);

>From cdb9975ab23621d9696ca3473abdf4ccb140db38 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraayai at users.noreply.github.com>
Date: Sun, 6 Apr 2025 13:38:48 -0400
Subject: [PATCH 5/5] Revert back style change.

---
 llvm/include/llvm/Target/TargetMachine.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index bb44dcbcafd87..3d8c6694297c9 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -195,7 +195,7 @@ class TargetMachine {
   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
   /// returned is of the correct type.
   template <typename STC> const STC &getSubtarget(const Function &F) const {
-    return *static_cast<const STC *>(getSubtargetImpl(F));
+    return *static_cast<const STC*>(getSubtargetImpl(F));
   }
 
   /// Create a DataLayout.



More information about the llvm-commits mailing list