Skip to content

[MLIR][LLVM] Turn the inliner interface into a promised interface #103927

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 3 commits into from
Aug 14, 2024

Conversation

Dinistro
Copy link
Contributor

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.

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.
@llvmbot llvmbot added mlir:llvm mlir flang Flang issues not falling into any other category flang:fir-hlfir labels Aug 14, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 14, 2024

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

@llvm/pr-subscribers-mlir

Author: Christian Ulmann (Dinistro)

Changes

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.


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

7 Files Affected:

  • (modified) flang/include/flang/Optimizer/Support/InitFIR.h (+1)
  • (renamed) mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h (+8-13)
  • (modified) mlir/include/mlir/InitAllDialects.h (+2)
  • (modified) mlir/lib/Dialect/LLVMIR/CMakeLists.txt (-1)
  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp (+2-2)
  • (modified) mlir/lib/Dialect/LLVMIR/Transforms/CMakeLists.txt (+1)
  • (renamed) mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp (+6-4)
diff --git a/flang/include/flang/Optimizer/Support/InitFIR.h b/flang/include/flang/Optimizer/Support/InitFIR.h
index 48cc1cbc645684..0192d7fdbea099 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::registerLLVMInlinerInterface(registry);
 }
 
 /// Register all the dialects used by flang.
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h b/mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
similarity index 64%
rename from mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.h
rename to mlir/include/mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h
index c6f75d5657c3be..9c94cf6512195f 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 registerLLVMInlinerInterface(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 549c26c72d8a1e..cdece616e4cf7c 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"
@@ -163,6 +164,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
   cf::registerBufferizableOpInterfaceExternalModels(registry);
   cf::registerBufferDeallocationOpInterfaceExternalModels(registry);
   gpu::registerBufferDeallocationOpInterfaceExternalModels(registry);
+  LLVM::registerLLVMInlinerInterface(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 90610118a45cd2..476281d44e4306 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"
@@ -3200,7 +3200,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..55bf99f4228ae4 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::registerLLVMInlinerInterface(DialectRegistry &registry) {
+  registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
+    dialect->addInterfaces<LLVMInlinerInterface>();
+  });
 }

Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

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

LGTM

void addLLVMInlinerInterface(LLVMDialect *dialect);

} // namespace detail
void registerLLVMInlinerInterface(DialectRegistry &registry);
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
void registerLLVMInlinerInterface(DialectRegistry &registry);
void registerInlinerInterface(DialectRegistry &registry);

Nit: since it is in the LLVM namespace it may make sense to drop it in the function name?

Copy link
Contributor

Choose a reason for hiding this comment

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

The Impl naming seems weird, especially in a public header file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems to be the standard for such kind of things.

Copy link
Contributor

@definelicht definelicht left a comment

Choose a reason for hiding this comment

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

LGTM

@Dinistro Dinistro merged commit 4c2f90f into main Aug 14, 2024
6 of 7 checks passed
@Dinistro Dinistro deleted the users/dinistro/llvm-inlining-interface-move branch August 14, 2024 15:29
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 14, 2024

LLVM Buildbot has detected a new failure on builder mlir-nvidia-gcc7 running on mlir-nvidia while building flang,mlir at step 5 "build-check-mlir-build-only".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/116/builds/2372

Here is the relevant piece of the build log for the reference:

Step 5 (build-check-mlir-build-only) failure: build (failure)
...
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRModule.h: In instantiation of ‘static void mlir::python::PyConcreteType<DerivedTy, BaseTy>::bind(pybind11::module&) [with DerivedTy = {anonymous}::PyOpaqueType; BaseTy = mlir::python::PyType; pybind11::module = pybind11::module_]’:
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRTypes.cpp:903:17:   required from here
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRModule.h:986:5: warning: the address of ‘MlirTypeID mlirOpaqueTypeGetTypeID()’ will never be NULL [-Waddress]
     if (DerivedTy::getTypeIdFunction) {
     ^~
39.425 [323/16/4174] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlir.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/Pass.cpp.o
39.456 [322/16/4175] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlir.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/Rewrite.cpp.o
39.485 [321/16/4176] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirExecutionEngine.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp.o
39.513 [320/16/4177] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirRegisterEverything.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/RegisterEverything.cpp.o
39.544 [319/16/4178] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirPythonTest.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/test/python/lib/PythonTestModule.cpp.o
command timed out: 1200 seconds without output running [b'ninja', b'-j', b'16', b'check-mlir-build-only'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2412.979753

@Dinistro
Copy link
Contributor Author

LLVM Buildbot has detected a new failure on builder mlir-nvidia-gcc7 running on mlir-nvidia while building flang,mlir at step 5 "build-check-mlir-build-only".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/116/builds/2372

Here is the relevant piece of the build log for the reference:

Step 5 (build-check-mlir-build-only) failure: build (failure)
...
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRModule.h: In instantiation of ‘static void mlir::python::PyConcreteType<DerivedTy, BaseTy>::bind(pybind11::module&) [with DerivedTy = {anonymous}::PyOpaqueType; BaseTy = mlir::python::PyType; pybind11::module = pybind11::module_]’:
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRTypes.cpp:903:17:   required from here
/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/IRModule.h:986:5: warning: the address of ‘MlirTypeID mlirOpaqueTypeGetTypeID()’ will never be NULL [-Waddress]
     if (DerivedTy::getTypeIdFunction) {
     ^~
39.425 [323/16/4174] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlir.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/Pass.cpp.o
39.456 [322/16/4175] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlir.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/Rewrite.cpp.o
39.485 [321/16/4176] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirExecutionEngine.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp.o
39.513 [320/16/4177] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirRegisterEverything.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/lib/Bindings/Python/RegisterEverything.cpp.o
39.544 [319/16/4178] Building CXX object tools/mlir/python/CMakeFiles/MLIRPythonModules.extension._mlirPythonTest.dso.dir/vol/worker/mlir-nvidia/mlir-nvidia-gcc7/llvm.src/mlir/test/python/lib/PythonTestModule.cpp.o
command timed out: 1200 seconds without output running [b'ninja', b'-j', b'16', b'check-mlir-build-only'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2412.979753

This seems to be a build timeout and should not be related to this PR.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 14, 2024

LLVM Buildbot has detected a new failure on builder mlir-nvidia running on mlir-nvidia while building flang,mlir at step 2 "checkout".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/138/builds/2442

Here is the relevant piece of the build log for the reference:

Step 2 (checkout) failure: update (failure)
command timed out: 1200 seconds without output running [b'git', b'--version'], attempting to kill
process killed by signal 15
program finished with exit code -1
elapsedTime=1639.782699

fabianschuiki added a commit to llvm/circt that referenced this pull request Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category mlir:llvm mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants