Skip to content
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

[flang] Introducing a method to dynamically and conditionally register dialect interfaces. #80881

Merged
merged 2 commits into from
Feb 7, 2024

Conversation

VijayKandiah
Copy link
Contributor

This change introduces the addFIRExtensions method to dynamically and conditionally register dialect interfaces. As a use case of addFIRExtensions, this change moves the static registration of FIRInlinerInterface out of the constructor of FIROpsDialect to be dynamically registered while loading the necessary MLIR dialects required by Flang. This registration of FIRInlinerInterface is also guarded by a boolean addFIRInlinerInterface which defaults to true.

Copy link

github-actions bot commented Feb 6, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@VijayKandiah VijayKandiah marked this pull request as ready for review February 6, 2024 17:46
@llvmbot llvmbot added flang:driver flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 6, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 6, 2024

@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-flang-fir-hlfir

Author: Vijay Kandiah (VijayKandiah)

Changes

This change introduces the addFIRExtensions method to dynamically and conditionally register dialect interfaces. As a use case of addFIRExtensions, this change moves the static registration of FIRInlinerInterface out of the constructor of FIROpsDialect to be dynamically registered while loading the necessary MLIR dialects required by Flang. This registration of FIRInlinerInterface is also guarded by a boolean addFIRInlinerInterface which defaults to true.


Full diff: https://github.com/llvm/llvm-project/pull/80881.diff

7 Files Affected:

  • (modified) flang/include/flang/Optimizer/Dialect/FIRDialect.h (+3)
  • (modified) flang/include/flang/Optimizer/Support/InitFIR.h (+7)
  • (modified) flang/lib/Frontend/FrontendActions.cpp (+4)
  • (modified) flang/lib/Optimizer/Dialect/FIRDialect.cpp (+8-1)
  • (modified) flang/tools/bbc/bbc.cpp (+1)
  • (modified) flang/tools/fir-opt/fir-opt.cpp (+1)
  • (modified) flang/tools/tco/tco.cpp (+1)
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 &registry);
+
 } // 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 &registry) {
   registry.insert<FLANG_CODEGEN_DIALECT_LIST>();
 }
 
+// Register FIR Extensions
+inline void addFIRExtensions(mlir::DialectRegistry &registry,
+                             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 &registry) {
+  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);

@VijayKandiah
Copy link
Contributor Author

addInterfaces<FIRInlinerInterface>();
}

// register the FIRInlinerInterface to FIROpsDialect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// register the FIRInlinerInterface to FIROpsDialect
// register the FIRInlinerInterface to FIROpsDialect

Copy link
Contributor

@jeanPerier jeanPerier left a 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.

flang/lib/Frontend/FrontendActions.cpp Show resolved Hide resolved
Copy link
Contributor

@jeanPerier jeanPerier left a 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.

@VijayKandiah
Copy link
Contributor Author

Thanks Jean. I addressed Valentin's comments in my latest commit.

Copy link
Contributor

@vzakhari vzakhari left a 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!

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@vzakhari vzakhari merged commit 369b822 into llvm:main Feb 7, 2024
4 checks passed
Copy link

github-actions bot commented Feb 7, 2024

@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
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@VijayKandiah VijayKandiah deleted the vk-dyn-interface-reg branch February 7, 2024 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:driver flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants