[flang-commits] [flang] 4c2f90f - [MLIR][LLVM] Turn the inliner interface into a promised interface (#103927)

via flang-commits flang-commits at lists.llvm.org
Wed Aug 14 08:28:57 PDT 2024


Author: Christian Ulmann
Date: 2024-08-14T17:28:54+02:00
New Revision: 4c2f90f362e401162533ffede924ac3890c70577

URL: https://github.com/llvm/llvm-project/commit/4c2f90f362e401162533ffede924ac3890c70577
DIFF: https://github.com/llvm/llvm-project/commit/4c2f90f362e401162533ffede924ac3890c70577.diff

LOG: [MLIR][LLVM] Turn the inliner interface into a promised interface (#103927)

This commit changes the LLVM dialect's inliner interface to no longer be
registered at dialect initialization. Instead, it is now a promised
interface, that needs to be registered explicitly. This change is
desired to avoid pulling in a lot of dependencies into the
`MLIRLLVMDialect` library, especially considering future patches that
plan to extend it further with strong IR analysis.

Added: 
    mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
    mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp

Modified: 
    flang/include/flang/Optimizer/Support/InitFIR.h
    mlir/examples/toy/Ch6/toyc.cpp
    mlir/examples/toy/Ch7/toyc.cpp
    mlir/include/mlir/InitAllDialects.h
    mlir/lib/Dialect/LLVMIR/CMakeLists.txt
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
    mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt

Removed: 
    mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp
    mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h


################################################################################
diff  --git a/flang/include/flang/Optimizer/Support/InitFIR.h b/flang/include/flang/Optimizer/Support/InitFIR.h
index 48cc1cbc645684..04a5dd323e5508 100644
--- a/flang/include/flang/Optimizer/Support/InitFIR.h
+++ b/flang/include/flang/Optimizer/Support/InitFIR.h
@@ -46,6 +46,7 @@ namespace fir::support {
 inline void registerNonCodegenDialects(mlir::DialectRegistry &registry) {
   registry.insert<FLANG_NONCODEGEN_DIALECT_LIST>();
   mlir::func::registerInlinerExtension(registry);
+  mlir::LLVM::registerInlinerInterface(registry);
 }
 
 /// Register all the dialects used by flang.

diff  --git a/mlir/examples/toy/Ch6/toyc.cpp b/mlir/examples/toy/Ch6/toyc.cpp
index c244b31434ba98..dccab91944fe1d 100644
--- a/mlir/examples/toy/Ch6/toyc.cpp
+++ b/mlir/examples/toy/Ch6/toyc.cpp
@@ -12,6 +12,7 @@
 
 #include "mlir/Dialect/Func/Extensions/AllExtensions.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
 #include "toy/AST.h"
 #include "toy/Dialect.h"
 #include "toy/Lexer.h"
@@ -299,6 +300,7 @@ int main(int argc, char **argv) {
   // If we aren't dumping the AST, then we are compiling with/to MLIR.
   mlir::DialectRegistry registry;
   mlir::func::registerAllExtensions(registry);
+  mlir::LLVM::registerInlinerInterface(registry);
 
   mlir::MLIRContext context(registry);
   // Load our Dialect in this MLIR Context.

diff  --git a/mlir/examples/toy/Ch7/toyc.cpp b/mlir/examples/toy/Ch7/toyc.cpp
index fea56796adf10e..dd862656b9db88 100644
--- a/mlir/examples/toy/Ch7/toyc.cpp
+++ b/mlir/examples/toy/Ch7/toyc.cpp
@@ -12,6 +12,7 @@
 
 #include "mlir/Dialect/Func/Extensions/AllExtensions.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
 #include "toy/AST.h"
 #include "toy/Dialect.h"
 #include "toy/Lexer.h"
@@ -300,6 +301,7 @@ int main(int argc, char **argv) {
   // If we aren't dumping the AST, then we are compiling with/to MLIR.
   mlir::DialectRegistry registry;
   mlir::func::registerAllExtensions(registry);
+  mlir::LLVM::registerInlinerInterface(registry);
 
   mlir::MLIRContext context(registry);
   // Load our Dialect in this MLIR Context.

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h b/mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
similarity index 65%
rename from mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h
rename to mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
index c6f75d5657c3be..e99b0476a6b107 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h
+++ b/mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
@@ -1,4 +1,4 @@
-//===- LLVMInlining.h - Registration of LLVMInlinerInterface ----*- C++ -*-===//
+//===- InlinerInterfaceImpl.h - Inlining for LLVM the dialect ---*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,28 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// Allows registering the LLVM DialectInlinerInterface with the LLVM dialect
-// during initialization.
+// Allows registering the LLVM DialectInlinerInterface with the LLVM dialect.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef DIALECT_LLVMIR_IR_LLVMINLINING_H
-#define DIALECT_LLVMIR_IR_LLVMINLINING_H
+#ifndef MLIR_DIALECT_LLVMIR_TRANSFORMS_INLINERINTERFACEIMPL_H
+#define MLIR_DIALECT_LLVMIR_TRANSFORMS_INLINERINTERFACEIMPL_H
 
 namespace mlir {
-namespace LLVM {
-
-class LLVMDialect;
+class DialectRegistry;
 
-namespace detail {
+namespace LLVM {
 
 /// Register the `LLVMInlinerInterface` implementation of
 /// `DialectInlinerInterface` with the LLVM dialect.
-void addLLVMInlinerInterface(LLVMDialect *dialect);
-
-} // namespace detail
+void registerInlinerInterface(DialectRegistry &registry);
 
 } // namespace LLVM
 } // namespace mlir
 
-#endif // DIALECT_LLVMIR_IR_LLVMINLINING_H
+#endif // MLIR_DIALECT_LLVMIR_TRANSFORMS_INLINERINTERFACEIMPL_H

diff  --git a/mlir/include/mlir/InitAllDialects.h b/mlir/include/mlir/InitAllDialects.h
index 01f28c5d21b37d..ab81832cdbee55 100644
--- a/mlir/include/mlir/InitAllDialects.h
+++ b/mlir/include/mlir/InitAllDialects.h
@@ -43,6 +43,7 @@
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/LLVMIR/NVVMDialect.h"
 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
+#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
 #include "mlir/Dialect/Linalg/IR/Linalg.h"
 #include "mlir/Dialect/Linalg/Transforms/AllInterfaces.h"
 #include "mlir/Dialect/Linalg/Transforms/RuntimeOpVerification.h"
@@ -164,6 +165,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
   cf::registerBufferizableOpInterfaceExternalModels(registry);
   cf::registerBufferDeallocationOpInterfaceExternalModels(registry);
   gpu::registerBufferDeallocationOpInterfaceExternalModels(registry);
+  LLVM::registerInlinerInterface(registry);
   linalg::registerAllDialectInterfaceImplementations(registry);
   linalg::registerRuntimeVerifiableOpInterfaceExternalModels(registry);
   memref::registerAllocationOpInterfaceExternalModels(registry);

diff  --git a/mlir/lib/Dialect/LLVMIR/CMakeLists.txt b/mlir/lib/Dialect/LLVMIR/CMakeLists.txt
index 392065b859ee54..fce24b556036f3 100644
--- a/mlir/lib/Dialect/LLVMIR/CMakeLists.txt
+++ b/mlir/lib/Dialect/LLVMIR/CMakeLists.txt
@@ -4,7 +4,6 @@ add_mlir_dialect_library(MLIRLLVMDialect
   IR/FunctionCallUtils.cpp
   IR/LLVMAttrs.cpp
   IR/LLVMDialect.cpp
-  IR/LLVMInlining.cpp
   IR/LLVMInterfaces.cpp
   IR/LLVMMemorySlot.cpp
   IR/LLVMTypes.cpp

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 07262bb8e1bacb..92f3984e5e6db6 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
-#include "LLVMInlining.h"
 #include "TypeDetail.h"
 #include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
@@ -24,6 +23,7 @@
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/Matchers.h"
 #include "mlir/Interfaces/FunctionImplementation.h"
+#include "mlir/Transforms/InliningUtils.h"
 
 #include "llvm/ADT/SCCIterator.h"
 #include "llvm/ADT/TypeSwitch.h"
@@ -3252,7 +3252,7 @@ void LLVMDialect::initialize() {
   // clang-format off
   addInterfaces<LLVMOpAsmDialectInterface>();
   // clang-format on
-  detail::addLLVMInlinerInterface(this);
+  declarePromisedInterface<DialectInlinerInterface, LLVMDialect>();
 }
 
 #define GET_OP_CLASSES

diff  --git a/mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt b/mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt
index 728885fcbeaf38..d4ff0955c5d0e2 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt
@@ -3,6 +3,7 @@ add_mlir_dialect_library(MLIRLLVMIRTransforms
   DIExpressionLegalization.cpp
   DIExpressionRewriter.cpp
   DIScopeForLLVMFuncOp.cpp
+  InlinerInterfaceImpl.cpp
   LegalizeForExport.cpp
   OptimizeForNVVM.cpp
   RequestCWrappers.cpp

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
similarity index 99%
rename from mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp
rename to mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
index 137c1962b100af..8eba76a9abee8d 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
@@ -1,4 +1,4 @@
-//===- LLVMInlining.cpp - LLVM inlining interface and logic -----*- C++ -*-===//
+//===- InlinerInterfaceImpl.cpp - Inlining for LLVM the dialect -----------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,7 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "LLVMInlining.h"
+#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/IR/Matchers.h"
 #include "mlir/Interfaces/DataLayoutInterfaces.h"
@@ -850,6 +850,8 @@ struct LLVMInlinerInterface : public DialectInlinerInterface {
 
 } // end anonymous namespace
 
-void LLVM::detail::addLLVMInlinerInterface(LLVM::LLVMDialect *dialect) {
-  dialect->addInterfaces<LLVMInlinerInterface>();
+void mlir::LLVM::registerInlinerInterface(DialectRegistry &registry) {
+  registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
+    dialect->addInterfaces<LLVMInlinerInterface>();
+  });
 }


        


More information about the flang-commits mailing list