[flang-commits] [flang] [flang] Introducing a method to dynamically and conditionally register dialect interfaces. (PR #80881)
Vijay Kandiah via flang-commits
flang-commits at lists.llvm.org
Tue Feb 6 09:42:03 PST 2024
https://github.com/VijayKandiah created https://github.com/llvm/llvm-project/pull/80881
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.
>From d32949b14332d85917cd7d3d7d069496f506aaab Mon Sep 17 00:00:00 2001
From: Vijay Kandiah <vkandiah at sky6.pgi.net>
Date: Tue, 6 Feb 2024 09:39:28 -0800
Subject: [PATCH] [flang] Dynamic + conditional registration of dialect
interfaces.
---
flang/include/flang/Optimizer/Dialect/FIRDialect.h | 3 +++
flang/include/flang/Optimizer/Support/InitFIR.h | 7 +++++++
flang/lib/Frontend/FrontendActions.cpp | 4 ++++
flang/lib/Optimizer/Dialect/FIRDialect.cpp | 9 ++++++++-
flang/tools/bbc/bbc.cpp | 1 +
flang/tools/fir-opt/fir-opt.cpp | 1 +
flang/tools/tco/tco.cpp | 1 +
7 files changed, 25 insertions(+), 1 deletion(-)
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);
More information about the flang-commits
mailing list