-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[flang] Introducing a method to dynamically and conditionally register dialect interfaces. #80881
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-flang-driver @llvm/pr-subscribers-flang-fir-hlfir Author: Vijay Kandiah (VijayKandiah) ChangesThis change introduces the Full diff: https://github.com/llvm/llvm-project/pull/80881.diff 7 Files Affected:
diff --git a/flang/include/flang/Optimizer/Dialect/FIRDialect.h b/flang/include/flang/Optimizer/Dialect/FIRDialect.h
index e8bf9f0cf503b..f3b54d151be7d 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRDialect.h
+++ b/flang/include/flang/Optimizer/Dialect/FIRDialect.h
@@ -67,6 +67,9 @@ bool canLegallyInline(mlir::Operation *op, mlir::Region *reg, bool,
mlir::IRMapping &map);
bool canLegallyInline(mlir::Operation *, mlir::Operation *, bool);
+// register the FIRInlinerInterface to FIROpsDialect
+void addFIRInlinerExtension(mlir::DialectRegistry ®istry);
+
} // namespace fir
#endif // FORTRAN_OPTIMIZER_DIALECT_FIRDIALECT_H
diff --git a/flang/include/flang/Optimizer/Support/InitFIR.h b/flang/include/flang/Optimizer/Support/InitFIR.h
index b5c41699205f4..f376840afd842 100644
--- a/flang/include/flang/Optimizer/Support/InitFIR.h
+++ b/flang/include/flang/Optimizer/Support/InitFIR.h
@@ -53,6 +53,13 @@ inline void registerDialects(mlir::DialectRegistry ®istry) {
registry.insert<FLANG_CODEGEN_DIALECT_LIST>();
}
+// Register FIR Extensions
+inline void addFIRExtensions(mlir::DialectRegistry ®istry,
+ bool addFIRInlinerInterface = true) {
+ if (addFIRInlinerInterface)
+ addFIRInlinerExtension(registry);
+}
+
inline void loadNonCodegenDialects(mlir::MLIRContext &context) {
mlir::DialectRegistry registry;
registerNonCodegenDialects(registry);
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 18bc1689c8547..44e80e946ed83 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -786,6 +786,10 @@ void CodeGenAction::generateLLVMIR() {
llvm::OptimizationLevel level = mapToLevel(opts);
fir::support::loadDialects(*mlirCtx);
+ mlir::DialectRegistry registry;
+ fir::support::registerNonCodegenDialects(registry);
+ fir::support::addFIRExtensions(registry);
+ mlirCtx->appendDialectRegistry(registry);
fir::support::registerLLVMTranslation(*mlirCtx);
// Set-up the MLIR pass manager
diff --git a/flang/lib/Optimizer/Dialect/FIRDialect.cpp b/flang/lib/Optimizer/Dialect/FIRDialect.cpp
index d0176731c292d..89b2a4f82fe3e 100644
--- a/flang/lib/Optimizer/Dialect/FIRDialect.cpp
+++ b/flang/lib/Optimizer/Dialect/FIRDialect.cpp
@@ -67,7 +67,14 @@ fir::FIROpsDialect::FIROpsDialect(mlir::MLIRContext *ctx)
#include "flang/Optimizer/Dialect/FIROps.cpp.inc"
>();
registerOpExternalInterfaces();
- addInterfaces<FIRInlinerInterface>();
+}
+
+// register the FIRInlinerInterface to FIROpsDialect
+void fir::addFIRInlinerExtension(mlir::DialectRegistry ®istry) {
+ registry.addExtension(
+ +[](mlir::MLIRContext *ctx, fir::FIROpsDialect *dialect) {
+ dialect->addInterface<FIRInlinerInterface>();
+ });
}
// anchor the class vtable to this compilation unit
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 98d9258e023e5..9d5caf5c6804e 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -326,6 +326,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
// translate to FIR dialect of MLIR
mlir::DialectRegistry registry;
fir::support::registerNonCodegenDialects(registry);
+ fir::support::addFIRExtensions(registry);
mlir::MLIRContext ctx(registry);
fir::support::loadNonCodegenDialects(ctx);
auto &defKinds = semanticsContext.defaultKinds();
diff --git a/flang/tools/fir-opt/fir-opt.cpp b/flang/tools/fir-opt/fir-opt.cpp
index 92af79e50fa4e..1846c1b317848 100644
--- a/flang/tools/fir-opt/fir-opt.cpp
+++ b/flang/tools/fir-opt/fir-opt.cpp
@@ -40,6 +40,7 @@ int main(int argc, char **argv) {
#endif
DialectRegistry registry;
fir::support::registerDialects(registry);
+ fir::support::addFIRExtensions(registry);
return failed(MlirOptMain(argc, argv, "FIR modular optimizer driver\n",
registry));
}
diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp
index 2153e0e3e6249..45284e74f5845 100644
--- a/flang/tools/tco/tco.cpp
+++ b/flang/tools/tco/tco.cpp
@@ -90,6 +90,7 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
mlir::DialectRegistry registry;
fir::support::registerDialects(registry);
+ fir::support::addFIRExtensions(registry);
mlir::MLIRContext context(registry);
fir::support::loadDialects(context);
fir::support::registerLLVMTranslation(context);
|
addInterfaces<FIRInlinerInterface>(); | ||
} | ||
|
||
// register the FIRInlinerInterface to FIROpsDialect |
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.
// register the FIRInlinerInterface to FIROpsDialect | |
// register the FIRInlinerInterface to FIROpsDialect |
@@ -67,6 +67,9 @@ bool canLegallyInline(mlir::Operation *op, mlir::Region *reg, bool, | |||
mlir::IRMapping &map); | |||
bool canLegallyInline(mlir::Operation *, mlir::Operation *, bool); | |||
|
|||
// register the FIRInlinerInterface to FIROpsDialect |
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.
// register the FIRInlinerInterface to FIROpsDialect | |
// register the FIRInlinerInterface to FIROpsDialect |
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.
Thanks @VijayKandiah, that will also help me conditionally enabling type alias in FIR assembly printing.
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.
LGTM, please take care of Valentin's comments before merging.
Thanks Jean. I addressed Valentin's comments in my latest commit. |
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.
Thank you for the change!
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.
LGTM. Thanks!
@VijayKandiah Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This change introduces the
addFIRExtensions
method to dynamically and conditionally register dialect interfaces. As a use case ofaddFIRExtensions
, this change moves the static registration ofFIRInlinerInterface
out of the constructor ofFIROpsDialect
to be dynamically registered while loading the necessary MLIR dialects required by Flang. This registration ofFIRInlinerInterface
is also guarded by a booleanaddFIRInlinerInterface
which defaults to true.