[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
Tue Apr 8 13:52:40 PDT 2025


================
@@ -167,22 +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(const TargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const TargetMachine *TM,
-                                        MCContext *ExtContext);
+  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;
----------------
matinraayai wrote:

I can combine the two passes together into a single `MachineModuleInfoWrapperPass` that looks like the following:
```c++
class MachineModuleInfoWrapperPass : public ImmutablePass {
  MachineModuleInfo *MMI{nullptr};
  bool OwnsMMI{true};
  
  public:
  explicit MachineModuleInfoWrapperPass(const TargetMachine &TM)
      : MMI(new MachineModuleInfo(&TM)), OwnsMMI(true) {};

  MachineModuleInfoWrapperPass(const TargetMachine &TM,
                                     MCContext &ExtContext)
      : MMI(new MachineModuleInfo(&TM, &ExtContext)), OwnsMMI(true) {};
  
  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI) : MMI(&MMI), OwnsMMI(false) {};
  
  MachineModuleInfo &getMMI() override { return MMI; }
  const MachineModuleInfo &getMMI() const override { return MMI; }
};


bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
  getMMI().finalize();
  if (OwnsMMI)
    delete MMI;
  return false;
};
```
This way, there is only a single version of the pass, and it will continue to behave the same way as it did before, while supporting the new functionality.
I'm curious, how does `addPassestoEmitFiles` for the new `PM` handles the issue of not owning the `MMI` reference? Does it force its users to link against CodeGen?

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


More information about the llvm-commits mailing list