-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Add framework for CIR to LLVM IR lowering #124650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ class CIRGenerator : public clang::ASTConsumer { | |
| const clang::CodeGenOptions &codeGenOpts; | ||
|
|
||
| protected: | ||
| std::unique_ptr<mlir::MLIRContext> mlirContext; | ||
| std::shared_ptr<mlir::MLIRContext> mlirContext; | ||
| std::unique_ptr<clang::CIRGen::CIRGenModule> cgm; | ||
|
|
||
| public: | ||
|
|
@@ -55,8 +55,8 @@ class CIRGenerator : public clang::ASTConsumer { | |
| void Initialize(clang::ASTContext &astContext) override; | ||
| bool HandleTopLevelDecl(clang::DeclGroupRef group) override; | ||
| mlir::ModuleOp getModule() const; | ||
| std::unique_ptr<mlir::MLIRContext> takeContext() { | ||
| return std::move(mlirContext); | ||
| std::shared_ptr<mlir::MLIRContext> getContext() { | ||
|
||
| return mlirContext; | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ getBackendActionFromOutputType(CIRGenAction::OutputType Action) { | |
|
|
||
| static std::unique_ptr<llvm::Module> lowerFromCIRToLLVMIR( | ||
| const clang::FrontendOptions &FEOpts, mlir::ModuleOp MLIRModule, | ||
| std::unique_ptr<mlir::MLIRContext> MLIRCtx, llvm::LLVMContext &LLVMCtx) { | ||
| std::shared_ptr<mlir::MLIRContext> MLIRCtx, llvm::LLVMContext &LLVMCtx) { | ||
| return direct::lowerDirectlyFromCIRToLLVMIR(MLIRModule, LLVMCtx); | ||
| } | ||
|
|
||
|
|
@@ -72,7 +72,7 @@ class CIRGenConsumer : public clang::ASTConsumer { | |
| void HandleTranslationUnit(ASTContext &C) override { | ||
| Gen->HandleTranslationUnit(C); | ||
| mlir::ModuleOp MlirModule = Gen->getModule(); | ||
| auto MLIRCtx = Gen->takeContext(); | ||
| std::shared_ptr<mlir::MLIRContext> MLIRCtx = Gen->getContext(); | ||
| switch (Action) { | ||
| case CIRGenAction::OutputType::EmitCIR: | ||
| if (OutputStream && MlirModule) { | ||
|
|
@@ -84,7 +84,7 @@ class CIRGenConsumer : public clang::ASTConsumer { | |
| case CIRGenAction::OutputType::EmitLLVM: { | ||
| llvm::LLVMContext LLVMCtx; | ||
| auto LLVMModule = lowerFromCIRToLLVMIR(CI.getFrontendOpts(), MlirModule, | ||
|
||
| std::move(MLIRCtx), LLVMCtx); | ||
| MLIRCtx, LLVMCtx); | ||
|
|
||
| BackendAction BEAction = getBackendActionFromOutputType(Action); | ||
| emitBackendOutput( | ||
|
|
@@ -135,4 +135,4 @@ EmitCIRAction::EmitCIRAction(mlir::MLIRContext *MLIRCtx) | |
|
|
||
| void EmitLLVMAction::anchor() {} | ||
| EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *MLIRCtx) | ||
| : CIRGenAction(OutputType::EmitLLVM, MLIRCtx) {} | ||
| : CIRGenAction(OutputType::EmitLLVM, MLIRCtx) {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| add_subdirectory(DirectToLLVM) | ||
| add_subdirectory(DirectToLLVM) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I don't think the shared pointer is the way here, we should have one of them own, and the other observe. What are the lifetimes of these two objects? Is there one of these that has a lifetime that is clearly larger than the other that can be the owner, and the other be a reference to it?
@bcardosolopes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I thought shared pointer was what you were suggesting.
Here's what I'm seeing. CIRGenerator::Initialize creates the mlirContext and uses it. That function also passes it, as a raw pointer, to CIRGenModule, which keeps a copy of the pointer throughout its lifetime. CIRGenerator doesn't use it again, and was previously handing it off in CIRGenConsumer::handleTranslationUnit() via takeContext(). CIRGenConsumer::handleTranslationUnit(), in turn, passes it to a few calls as a raw pointer (in the incubator, not yet here) before handing it off, as a unique pointer, to lowerFromCIRToLLVMIR() which hands it, as a unique pointer, to lowerFromCIRToMLIRToLLVMIR() in the incubator implementation. There it is used as a raw pointer.
All of the uses after the call to takeContext() have lifetimes that should be limited to the scope of CIRGenConsumer::handleTranslationUnit(), so I think we should be able to just use it as a raw pointer here instead of taking it and leaving CIRGenerator with a null pointer, but CIRGenerator shouldn't need it again either.
@bcardosolopes Have I misunderstood anything here? What's your recommendation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it's also relevant to note that I don't seem to need the context here in the current patch. It's only used as a unique pointer in the path where we're lowering CIR to other MLIR dialects before generating LLVM IR. So, I could remove this part of the change and defer the decision until it is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
CIRGeneratorown this and just share it withlowerFromCIRToLLVMIR/etc via reference? CIRGenerator seems to be the entry point into all of CIR, so it makes sense for me for it to 'own' the lifetime.I don't see why
handleTranslationUnitneeds to take ownership of it. I also wonder if that taking of ownership is problematic, since of course, clang can in some configurations handle multiple translation units, which this could potentially lose (though IDK if we ended up enabling that functionality).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think hoisting it to the
CIRGenConsumerand just letting theCIRGeneratorget a handle to it makes sense.Alternatively, the
CIRGeneratoris done at this point. We could have some sort of destruction of theCIRGeneratorhere and have it give up it's state contents to theCIRGenConsumer. cc @bcardosolopesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We run passes post CIRGen (LLVM lowering is one example) and at some point we were trying to build CIR in Sema and hand it over to codegen if already produced, so this probably dates back to that. Right now CIRGenerator holds the whole lifetime it's totally fine to go for a more clean solution here.
Btw, If you want to double check, you could run the cir tests with a ASAN enabled clang build, I usually use that from time to time to make sure the incubator isn't leaking.