[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-08-29 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai edited 
https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-08-30 Thread Matin Raayai via cfe-commits

matinraayai wrote:

@aeubanks should be ready for review again.

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

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

This PR makes the following changes, addressing issues raised in #104834 and 
#98770:
1. MMIWP behaves like the new PM's analysis Pass; It only takes a reference to 
an externally created MMI.
2. Removed the move constructor; Disallowed copy construction and copy 
assignment.
3. MMI does not create its own MCContext, and take a reference to an 
externally-created context.

As expected, this change touched upon a lot of places in the LLVM monorepo; But 
I wanted to point out some breaking ones:

1. The `TargetMachine` interface functions `addPassesToEmitFile` and 
`addPassesToEmitMC` now require a reference to an MMI; This IMO breaks the 
abstraction of the `TargetMachine`, since an `MMI` requires a 
`LLVMTargetMachine`, and if you have a `TargetMachine` you should do the 
dreaded casting to `LLVMTargetMachine` in order to create it. Now I created a 
factory method for MMIs in the `TargetMachine` interface to remedy this issue, 
but I still don't like it since again, it is only implemented for 
`LLVMTargetMachine`.
2. Enforcing an externally created context for every user of MMI requires 
explicit reseting of the MCContext if it is reused; This can be seen in llc and 
MCJIt, and I think must be documented explicitly.
4. Some places in the code only created an MMI just to get an MCContext; I've 
replaced them with a manually constructed MCContext but I think it can be 
simplified to having a factory method for MCContext in the `TargetMachine` 
interface.

For now this PR is a WIP; The first two changes are necessary, and will have to 
be done sometime in the future as a part of the migration effort to the new PM; 
The 3rd change (only taking external MCContext) makes MMI's code cleaner, but 
might break code in other places where it relies on the MMIWP implicitly 
reseting its managed context. It might be possible to break this PR down to two 
parts instead if it proves to be challenging to merge.

CC: @arsenm @weiweichen 

>From 4030c78148a2cb9b584038bd2ce522906b710f7e Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Tue, 20 Aug 2024 14:22:11 -0400
Subject: [PATCH 1/2] Made MMI only use external MCContext.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 28 +---
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 45 +++
 2 files changed, 18 insertions(+), 55 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..3fb0979ec20990 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -83,13 +83,11 @@ class MachineModuleInfo {
   friend class MachineModuleInfoWrapperPass;
   friend class MachineModuleAnalysis;
 
+  /// This is the TargetMachine used for the entire code generator.
   const LLVMTargetMachine &TM;
 
   /// This is the MCContext used for the entire code generator.
-  MCContext Context;
-  // This is an external context, that if assigned, will be used instead of the
-  // internal context.
-  MCContext *ExternalContext = nullptr;
+  MCContext &Context;
 
   /// This is the LLVM Module being worked on.
   const Module *TheModule = nullptr;
@@ -106,15 +104,15 @@ 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 LLVMTargetMachine *TM = nullptr);
+  explicit MachineModuleInfo(const LLVMTargetMachine &TM,
+ MCContext &Context);
 
-  explicit MachineModuleInfo(const LLVMTargetMachine *TM,
- MCContext *ExtContext);
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMII) = delete;
 
   ~MachineModuleInfo();
 
@@ -124,10 +122,10 @@ class MachineModuleInfo {
   const LLVMTargetMachine &getTarget() const { return TM; }
 
   const MCContext &getContext() const {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
   MCContext &getContext() {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
 
   const Module *getModule() const { return TheModule; }
@@ -169,14 +167,12 @@ 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 LLVMTargetMachine *TM = nullptr);
 
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-   

[clang] [llvm] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai converted_to_draft 
https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai edited 
https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/105541

>From 4030c78148a2cb9b584038bd2ce522906b710f7e Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Tue, 20 Aug 2024 14:22:11 -0400
Subject: [PATCH 1/3] Made MMI only use external MCContext.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 28 +---
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 45 +++
 2 files changed, 18 insertions(+), 55 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..3fb0979ec20990 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -83,13 +83,11 @@ class MachineModuleInfo {
   friend class MachineModuleInfoWrapperPass;
   friend class MachineModuleAnalysis;
 
+  /// This is the TargetMachine used for the entire code generator.
   const LLVMTargetMachine &TM;
 
   /// This is the MCContext used for the entire code generator.
-  MCContext Context;
-  // This is an external context, that if assigned, will be used instead of the
-  // internal context.
-  MCContext *ExternalContext = nullptr;
+  MCContext &Context;
 
   /// This is the LLVM Module being worked on.
   const Module *TheModule = nullptr;
@@ -106,15 +104,15 @@ 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 LLVMTargetMachine *TM = nullptr);
+  explicit MachineModuleInfo(const LLVMTargetMachine &TM,
+ MCContext &Context);
 
-  explicit MachineModuleInfo(const LLVMTargetMachine *TM,
- MCContext *ExtContext);
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMII) = delete;
 
   ~MachineModuleInfo();
 
@@ -124,10 +122,10 @@ class MachineModuleInfo {
   const LLVMTargetMachine &getTarget() const { return TM; }
 
   const MCContext &getContext() const {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
   MCContext &getContext() {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
 
   const Module *getModule() const { return TheModule; }
@@ -169,14 +167,12 @@ 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 LLVMTargetMachine *TM = nullptr);
 
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..55ef4916cd0101 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -30,39 +30,14 @@ void MachineModuleInfo::initialize() {
 }
 
 void MachineModuleInfo::finalize() {
-  Context.reset();
-  // We don't clear the ExternalContext.
-
   delete ObjFileMMI;
   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)) {
+MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine &TM,
+ MCContext &Context)
+: TM(TM), Context(Context) {
   Context.setObjectFileInfo(TM.getObjFileLowering());
-  ObjFileMMI = MMI.ObjFileMMI;
-  ExternalContext = MMI.ExternalContext;
-  TheModule = MMI.TheModule;
-}
-
-MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
-: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
-   TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
-   nullptr, &TM->Options.MCOptions, false) {
-  Context.setObjectFileInfo(TM->getObjFileLowering());
-  initialize();
-}
-
-MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
- MCContext *ExtContext)
-: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
-   TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
-   nullptr, &TM->Option

[clang] [llvm] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/105541

>From 4030c78148a2cb9b584038bd2ce522906b710f7e Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Tue, 20 Aug 2024 14:22:11 -0400
Subject: [PATCH 1/3] Made MMI only use external MCContext.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 28 +---
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 45 +++
 2 files changed, 18 insertions(+), 55 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..3fb0979ec20990 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -83,13 +83,11 @@ class MachineModuleInfo {
   friend class MachineModuleInfoWrapperPass;
   friend class MachineModuleAnalysis;
 
+  /// This is the TargetMachine used for the entire code generator.
   const LLVMTargetMachine &TM;
 
   /// This is the MCContext used for the entire code generator.
-  MCContext Context;
-  // This is an external context, that if assigned, will be used instead of the
-  // internal context.
-  MCContext *ExternalContext = nullptr;
+  MCContext &Context;
 
   /// This is the LLVM Module being worked on.
   const Module *TheModule = nullptr;
@@ -106,15 +104,15 @@ 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 LLVMTargetMachine *TM = nullptr);
+  explicit MachineModuleInfo(const LLVMTargetMachine &TM,
+ MCContext &Context);
 
-  explicit MachineModuleInfo(const LLVMTargetMachine *TM,
- MCContext *ExtContext);
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMII) = delete;
 
   ~MachineModuleInfo();
 
@@ -124,10 +122,10 @@ class MachineModuleInfo {
   const LLVMTargetMachine &getTarget() const { return TM; }
 
   const MCContext &getContext() const {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
   MCContext &getContext() {
-return ExternalContext ? *ExternalContext : Context;
+return Context;
   }
 
   const Module *getModule() const { return TheModule; }
@@ -169,14 +167,12 @@ 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 LLVMTargetMachine *TM = nullptr);
 
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..55ef4916cd0101 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -30,39 +30,14 @@ void MachineModuleInfo::initialize() {
 }
 
 void MachineModuleInfo::finalize() {
-  Context.reset();
-  // We don't clear the ExternalContext.
-
   delete ObjFileMMI;
   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)) {
+MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine &TM,
+ MCContext &Context)
+: TM(TM), Context(Context) {
   Context.setObjectFileInfo(TM.getObjFileLowering());
-  ObjFileMMI = MMI.ObjFileMMI;
-  ExternalContext = MMI.ExternalContext;
-  TheModule = MMI.TheModule;
-}
-
-MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
-: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
-   TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
-   nullptr, &TM->Options.MCOptions, false) {
-  Context.setObjectFileInfo(TM->getObjFileLowering());
-  initialize();
-}
-
-MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
- MCContext *ExtContext)
-: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
-   TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
-   nullptr, &TM->Option

[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-21 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai ready_for_review 
https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Remove Move Constructor of MMI + Make MMI Only Use and Externally-Created MCContext (PR #105541)

2024-08-22 Thread Matin Raayai via cfe-commits

matinraayai wrote:

> > The TargetMachine interface functions addPassesToEmitFile and 
> > addPassesToEmitMC now require a reference to an MMI; This IMO breaks the 
> > abstraction of the TargetMachine, since an MMI requires a 
> > LLVMTargetMachine, and if you have a TargetMachine you should do the 
> > dreaded casting to LLVMTargetMachine in order to create it. Now I created a 
> > factory method for MMIs in the TargetMachine interface to remedy this 
> > issue, but I still don't like it since again, it is only implemented for 
> > LLVMTargetMachine.
> 
> I don't quite follow what the issue is here, but I'm off until Tuesday so 
> I'll look again next week (I hope)

It's just an abstraction issue; I think it becomes more clear once you read the 
diffs. 
Enjoy your time off.

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-16 Thread Matin Raayai via cfe-commits

matinraayai wrote:

@aeubanks @arsenm any updates on this?

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-04 Thread Matin Raayai via cfe-commits

matinraayai wrote:

I dug up the commit that introduced `LLVMTargetMachine`: 
https://github.com/llvm/llvm-project/commit/12e97307a10bbac6bf9e6733833b84faf06dee88.
 It dates back to before when the MC layer was created. It seems the motivation 
was to allow a hypothetical target to generate code using whatever code 
generator it wants internally (be it the one provided by LLVM at the time or 
some other external library). The MC stuff was later on added on top of it 
around 2010, which I think made this abstraction a bit pointless, since MC and 
CodeGen integrate tightly together, and there's no point to have support for 
only one layer.

Given the issues faced when joining `TM` and `LLVMTM`, I think this abstraction 
should be respected. The key takeaway from this abstraction is that `TM` 
__must__ have a function that generates object files/MC; It's just that those 
interface functions should be void of any CodeGen related constructs (even the 
`MMIWP`). TLDR: We should follow this rule of thumb: __If it uses LLVM's 
CodeGen it belongs to the `LLVMTM` class, otherwise it belongs to `TM`__ (The 
same goes for all the `MachineFunctionInfo` stuff; They should be moved to 
`LLVMTM`).

This is a cause of concern for managing the lifetime of `MMI`: For `TM` 
interfaces, `MMI`'s lifetime should be managed by the `MMIWP` pass, otherwise 
it will get deleted when it goes out of the scope of the pass building 
function. For `LLVMTM` interface, however, `MMI` should be managed externally 
by the interface user. I think both should exist.

This also relates to a question that I had regarding the new PM codegen 
interface `buildCodeGenPipeline` @aeubanks: how exactly is the `MMI`'s lifetime 
managed after calling this function? If it's possible I want to talk more about 
it offline (You can find me on LLVM's Discord).

> I see that MMI really is a Codegen concept and not a Target concept, so 
> that's why it takes an LLVMTargetMachine instead of TargetMachine. However, 
> the `static_cast`s everywhere are extremely unfortunate. And it doesn't make 
> sense to make the return type of `Target::createTargetMachine()` 
> `LLVMTargetMachine` instead of `TargetMachine`. Perhaps alternatively we make 
> the MMI constructor take `TargetMachine` and cast it inside the constructor 
> to `LLVMTargetMachine`, so we only pay the cost of this weird distinction in 
> one place rather than everywhere? wdyt?

To get back to your question @aeubanks  I don't think we should force any casts 
in the `MMI` constructor; Instead we should address the `TM`/`LLVMTM` 
abstraction issue. The casting will then take care of itself. Also there should 
be a `Target::createLLVMTargetMachine()` for those who want to explicitly 
manage `MMI`'s lifetime and want to use LLVm CodeGen.

https://github.com/llvm/llvm-project/pull/110443
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-04 Thread Matin Raayai via cfe-commits


@@ -1162,6 +1165,7 @@ void EmitAssemblyHelper::RunCodegenPipeline(
   // does not work with the codegen pipeline.
   // FIXME: make the new PM work with the codegen pipeline.
   legacy::PassManager CodeGenPasses;
+  std::unique_ptr MMI;

matinraayai wrote:

I'll change it to a normal construction with the `TM`.

https://github.com/llvm/llvm-project/pull/110443
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-09-29 Thread Matin Raayai via cfe-commits

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

Following up on discussions in #105541, this PR is the first part of #105541, 
which removes ownership of the `llvm::MachineModuleInfoWrapperPass` over its 
encapsulated `llvm::MachineModuleInfo`, allowing better control over the MMI's 
lifetime.

After this PR is merged, I plan to:
1. Move the MC emission functions in `TargetMachine` to `LLVMTargetMachine`. 
With the changes in this PR, we explicitly assume in both `addPassesToEmitFile` 
and `addPassesToEmitMC` that the `TargetMachine` is an `LLVMTargetMachine`; 
Hence it does not make sense for these functions to be present in the 
`TargetMachine` interface.
2. Make MMI only take external context, which originally was the second part of 
 #105541.

cc @aeubanks @arsenm 

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/14] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/14] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--

[clang] [flang] [llvm] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-09-29 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/15] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/15] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-09-29 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/17] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/17] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-09-29 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/16] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/16] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-22 Thread Matin Raayai via cfe-commits

matinraayai wrote:

> > @aeubanks @arsenm after looking into this in more detail, I realized that 
> > the `getContext` method of `MMI` is heavily used in the `AsmPrinter` to 
> > create symbols. Also not having it makes it harder for the `MMI` to create 
> > machine functions using `getOrCreateMachineFunction`.
> 
> The AsmPrinter is just an ordinary ModulePass. The initialization can just 
> set a MMI member?

I agree that separating `MCContext` from `MMI` is not an issue for 
`AsmPrinter`; But I don't see a way to do that with 
`MachineModuleInfo::getOrCreateMachineFunction`, besides making it take an 
explicit `MCContext` argument here:

```c++

MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F, 
MCContext &MCCtx) {
  // Shortcut for the common case where a sequence of MachineFunctionPasses
  // all query for the same Function.
  if (LastRequest == &F)
return *LastResult;

  auto I = MachineFunctions.insert(
  std::make_pair(&F, std::unique_ptr()));
  MachineFunction *MF;
  if (I.second) {
// No pre-existing machine function, create a new one.
const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
MF = new MachineFunction(F, TM, STI, MCCtx, NextFnNum++);
MF->initTargetMachineFunctionInfo(STI);

// MRI callback for target specific initializations.
TM.registerMachineRegisterInfoCallback(*MF);

// Update the set entry.
I.first->second.reset(MF);
  } else {
MF = I.first->second.get();
  }

  LastRequest = &F;
  LastResult = MF;
  return *MF;
}
```

Also the constructor for MMI sets the context's object file info here:
```c++
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
   TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
   nullptr, &TM->Options.MCOptions, false) {
  Context.setObjectFileInfo(TM->getObjFileLowering());
  initialize();
}
```

Again, for both these cases, it's possible to remove the `MCContext` from 
`MMI`; However doing so will make it harder to use in my opinion. 




https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-21 Thread Matin Raayai via cfe-commits

matinraayai wrote:

@aeubanks @arsenm after looking into this in more detail, I realized that the 
`getContext` method of `MMI` is heavily used in the `AsmPrinter` to create 
symbols. Also not having it makes it harder for the `MMI` to create machine 
functions using `getOrCreateMachineFunction`. 

Hence I don't think it's a good idea to remove the `MCContext` reference from 
`MMI`. 

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-23 Thread Matin Raayai via cfe-commits

matinraayai wrote:

@aeubanks sure I can split the PR into two parts:
1. Make MMIWP not have ownership over MMI.
2. Make MMI only use an external context.

I will make a new PR for each of them and close this one once both are merged.

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-19 Thread Matin Raayai via cfe-commits

matinraayai wrote:

> sorry for the delay
> 
> after looking at MMI/MCContext, I agree that MMI shouldn't own MCContext, but 
> do we even need a reference from  MMI to MCContext? they are different layers 
> of codegen IIUC. if it's possible to completely separate them we should do 
> that (please correct me if this doesn't make sense since I haven't spent too 
> much time in codegen)

@aeubanks  It's not impossible to separate them completely. `MCContext` is 
needed during initialization and finalization of the 
`MachineModuleInfoWrapperPass` (and its new pass manager variant) to set the 
diagnostics handler. 

In theory, you can just pass the context to the wrapper pass instead. @arsenm 
any thoughts on this?

https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make MMIWP not have ownership over MMI + Make MMI Only Use an External MCContext (PR #105541)

2024-09-20 Thread Matin Raayai via cfe-commits

matinraayai wrote:

In that case, I will modify this PR so that:
1. MMI does not take a reference to the MCContext.
2. MMIWP takes a reference for its initialize/finalize methods.

I'll report back in case I run into unexpected issues.


https://github.com/llvm/llvm-project/pull/105541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-09-30 Thread Matin Raayai via cfe-commits

matinraayai wrote:

> > * Move the MC emission functions in `TargetMachine` to `LLVMTargetMachine`. 
> > With the changes in this PR, we explicitly assume in both 
> > `addPassesToEmitFile` and `addPassesToEmitMC` that the `TargetMachine` is 
> > an `LLVMTargetMachine`; Hence it does not make sense for these functions to 
> > be present in the `TargetMachine` interface.
> 
> Was this already implicitly assumed? IIRC there was some layering reason why 
> this is the way it was. There were previous attempts to merge these before, 
> which were abandoned:
> 
> https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html
> 
> https://reviews.llvm.org/D38482 https://reviews.llvm.org/D38489

My bad, I forgot to correct my comment. Initially I was going to move the MC 
file emission functions to `LLVMTargetMachine` but I decided against it, as it 
would make the PR harder to review.

I don't mind merging those interfaces together in a future PR, as all the past 
work mention, `LLVMTargetMachine` functionality is always assumed for all 
targets and no target directly extends `TargetMachine`.

https://github.com/llvm/llvm-project/pull/110443
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-01 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/18] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/18] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-01 Thread Matin Raayai via cfe-commits

matinraayai wrote:

@MatzeB @arsenm in the latest commit I had to fix the issue you ran into in the 
following links @arsenm mentioned earlier:
https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html

https://reviews.llvm.org/D38482
https://reviews.llvm.org/D38489

I can live with `TargetMachine` and `LLVMTargetMachine` being separate 
entities, with `TargetMachine` being the "frontend" and `LLVMTargetMachine` 
being any concrete subclass with codegen support as @MatzeB explained in the 
past.

With this patch, if any library requires MC code emission of any kind, it has 
to explicitly link to libCodeGen, since it has to be able to access the `MMI` 
constructor/destructor. I still believe that `addPassesToEmitMC` and 
`addPassesToEmitFile` need to be moved to `LLVMTargetMachine` to further 
enforce the distinction between `LLVMTargetMachine` and `TargetMachine`, as 
well as any other interface that explicitly uses CodeGen or MC primitives (e.g. 
MachineFunctionInfoYaml things).

https://github.com/llvm/llvm-project/pull/110443
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-02 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/19] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/19] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-02 Thread Matin Raayai via cfe-commits


@@ -0,0 +1,102 @@
+//===-- LLVMTargetMachineC.cpp 
===//
+//
+// 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
+//
+//===--===//
+//
+// This file implements the LLVM-C part of TargetMachine.h that directly
+// depends on the CodeGen library.
+//
+//===--===//
+
+#include "llvm-c/Core.h"
+#include "llvm-c/TargetMachine.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetMachine.h"
+
+using namespace llvm;
+
+static TargetMachine *unwrap(LLVMTargetMachineRef P) {
+  return reinterpret_cast(P);
+}
+
+static Target *unwrap(LLVMTargetRef P) { return reinterpret_cast(P); 
}
+
+static LLVMTargetMachineRef wrap(const TargetMachine *P) {
+  return reinterpret_cast(const_cast(P));
+}
+
+static LLVMTargetRef wrap(const Target *P) {
+  return reinterpret_cast(const_cast(P));
+}
+
+static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
+  raw_pwrite_stream &OS,
+  LLVMCodeGenFileType codegen,
+  char **ErrorMessage) {
+  TargetMachine *TM = unwrap(T);
+  Module *Mod = unwrap(M);
+
+  legacy::PassManager pass;
+  MachineModuleInfo MMI(static_cast(TM));
+
+  std::string error;
+
+  Mod->setDataLayout(TM->createDataLayout());
+
+  CodeGenFileType ft;
+  switch (codegen) {
+  case LLVMAssemblyFile:
+ft = CodeGenFileType::AssemblyFile;
+break;
+  default:
+ft = CodeGenFileType::ObjectFile;
+break;
+  }
+  if (TM->addPassesToEmitFile(pass, MMI, OS, nullptr, ft)) {
+error = "TargetMachine can't emit a file of this type";
+*ErrorMessage = strdup(error.c_str());
+return true;
+  }
+
+  pass.run(*Mod);
+
+  OS.flush();
+  return false;
+}
+
+LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
+ const char *Filename,
+ LLVMCodeGenFileType codegen,
+ char **ErrorMessage) {
+  std::error_code EC;
+  raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
+  if (EC) {
+*ErrorMessage = strdup(EC.message().c_str());
+return true;
+  }
+  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
+  dest.flush();
+  return Result;
+}
+
+LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
+ LLVMModuleRef M,
+ LLVMCodeGenFileType codegen,
+ char **ErrorMessage,
+ LLVMMemoryBufferRef *OutMemBuf) {
+  SmallString<0> CodeString;
+  raw_svector_ostream OStream(CodeString);
+  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
+
+  StringRef Data = OStream.str();
+  *OutMemBuf =
+  LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
+  return Result;
+}

matinraayai wrote:

Should be resolved.

https://github.com/llvm/llvm-project/pull/110443
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-02 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/20] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/20] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-02 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/19] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/19] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.

[clang] [flang] [llvm] [mlir] Make Ownership of MachineModuleInfo in Its Wrapper Pass External (PR #110443)

2024-10-02 Thread Matin Raayai via cfe-commits

https://github.com/matinraayai updated 
https://github.com/llvm/llvm-project/pull/110443

>From 6a78a683f4834049c07f9672c358dcbb44ac14e7 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 16:54:50 -0400
Subject: [PATCH 01/21] Made MMIWP not have control over the lifetime of MMI.

---
 llvm/include/llvm/CodeGen/MachineModuleInfo.h | 15 +++--
 llvm/lib/CodeGen/MachineModuleInfo.cpp| 21 ++-
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h 
b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
index 310cc4b2abb772..8c81df23c29984 100644
--- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h
@@ -106,7 +106,11 @@ class MachineModuleInfo {
   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
 
-  MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
+  /// Deleted copy constructor
+  MachineModuleInfo(MachineModuleInfo &MMI) = delete;
+
+  /// Deleted copy assignment operator
+  MachineModuleInfo &operator=(MachineModuleInfo &MMI) = delete;
 
 public:
   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
@@ -114,8 +118,6 @@ class MachineModuleInfo {
   explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  MCContext *ExtContext);
 
-  MachineModuleInfo(MachineModuleInfo &&MMII);
-
   ~MachineModuleInfo();
 
   void initialize();
@@ -169,14 +171,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 LLVMTargetMachine *TM = nullptr);
-
-  explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
-MCContext *ExtContext);
+  explicit MachineModuleInfoWrapperPass(MachineModuleInfo &MMI);
 
   // Initialization and Finalization
   bool doInitialization(Module &) override;
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp 
b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index c66495969b4e67..8ecc53e66d6d25 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 LLVMTargetMachine *TM)
 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
@@ -151,14 +140,8 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
 }
 
 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM)
-: ImmutablePass(ID), MMI(TM) {
-  initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
-}
-
-MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
-const LLVMTargetMachine *TM, MCContext *ExtContext)
-: ImmutablePass(ID), MMI(TM, ExtContext) {
+MachineModuleInfo &MMI)
+: ImmutablePass(ID), MMI(MMI) {
   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
 }
 

>From c2d984a2bb1323e389c6778689818149c4e2b939 Mon Sep 17 00:00:00 2001
From: matinraayai <30674652+matinraa...@users.noreply.github.com>
Date: Sun, 29 Sep 2024 17:28:58 -0400
Subject: [PATCH 02/21] Moved the MC pass creation functions from TargetMachine
 to LLVMTargetMachine.

---
 llvm/include/llvm/Target/TargetMachine.h | 51 ++--
 llvm/lib/CodeGen/LLVMTargetMachine.cpp   | 23 +--
 2 files changed, 22 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetMachine.h 
b/llvm/include/llvm/Target/TargetMachine.h
index c3e9d41315f617..ed04da6dcd9558 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -375,31 +375,6 @@ class TargetMachine {
   /// with the new pass manager. Only affects the "default" AAManager.
   virtual void registerDefaultAliasAnalyses(AAManager &) {}
 
-  /// 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.