[mlir] Register Linalg passes in C API and Python Bindings
authorAlex Zinenko <zinenko@google.com>
Fri, 26 Mar 2021 18:04:36 +0000 (19:04 +0100)
committerAlex Zinenko <zinenko@google.com>
Sat, 27 Mar 2021 08:57:56 +0000 (09:57 +0100)
Provide a registration mechanism for Linalg dialect-specific passes in C
API and Python bindings. These are being built into the dialect library
but exposed in separate headers (C) or modules (Python).

Differential Revision: https://reviews.llvm.org/D99431

mlir/include/mlir-c/Dialect/Linalg.h
mlir/include/mlir/Dialect/Linalg/CMakeLists.txt
mlir/lib/Bindings/Python/CMakeLists.txt
mlir/lib/Bindings/Python/LinalgPasses.cpp [new file with mode: 0644]
mlir/lib/Bindings/Python/mlir/dialects/linalg/passes/__init__.py [new file with mode: 0644]
mlir/lib/CAPI/Dialect/CMakeLists.txt
mlir/lib/CAPI/Dialect/LinalgPasses.cpp [new file with mode: 0644]

index 56258ac..be73a5c 100644 (file)
@@ -11,6 +11,7 @@
 #define MLIR_C_DIALECT_LINALG_H
 
 #include "mlir-c/Registration.h"
+#include "mlir-c/Support.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -22,4 +23,6 @@ MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Linalg, linalg);
 }
 #endif
 
+#include "mlir/Dialect/Linalg/Passes.capi.h.inc"
+
 #endif // MLIR_C_DIALECT_LINALG_H
index d0edae3..2c74207 100644 (file)
@@ -2,6 +2,8 @@ add_subdirectory(IR)
 
 set(LLVM_TARGET_DEFINITIONS Passes.td)
 mlir_tablegen(Passes.h.inc -gen-pass-decls -name Linalg)
+mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix Linalg)
+mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix Linalg)
 add_public_tablegen_target(MLIRLinalgPassIncGen)
 
 add_mlir_doc(Passes -gen-pass-doc LinalgPasses ./)
index 5fefa80..43d6275 100644 (file)
@@ -118,3 +118,11 @@ endif()
 
 add_subdirectory(Transforms)
 add_subdirectory(Conversions)
+
+add_mlir_python_extension(MLIRLinalgPassesBindingsPythonExtension _mlirLinalgPasses
+  INSTALL_DIR
+    python
+  SOURCES
+    LinalgPasses.cpp
+)
+add_dependencies(MLIRBindingsPythonExtension MLIRLinalgPassesBindingsPythonExtension)
diff --git a/mlir/lib/Bindings/Python/LinalgPasses.cpp b/mlir/lib/Bindings/Python/LinalgPasses.cpp
new file mode 100644 (file)
index 0000000..3f23020
--- /dev/null
@@ -0,0 +1,22 @@
+//===- LinalgPasses.cpp - Pybind module for the Linalg passes -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir-c/Dialect/Linalg.h"
+
+#include <pybind11/pybind11.h>
+
+// -----------------------------------------------------------------------------
+// Module initialization.
+// -----------------------------------------------------------------------------
+
+PYBIND11_MODULE(_mlirLinalgPasses, m) {
+  m.doc() = "MLIR Linalg Dialect Passes";
+
+  // Register all Linalg passes on load.
+  mlirRegisterLinalgPasses();
+}
diff --git a/mlir/lib/Bindings/Python/mlir/dialects/linalg/passes/__init__.py b/mlir/lib/Bindings/Python/mlir/dialects/linalg/passes/__init__.py
new file mode 100644 (file)
index 0000000..6555ad6
--- /dev/null
@@ -0,0 +1,6 @@
+#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+#  See https://llvm.org/LICENSE.txt for license information.
+#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+from ...._cext_loader import _load_extension
+_cextLinalgPasses = _load_extension("_mlirLinalgPasses")
index d256309..41c659d 100644 (file)
@@ -1,6 +1,7 @@
 # TODO: Make the check source feature optional as an argument on *_add_library.
 set(LLVM_OPTIONAL_SOURCES
   Linalg.cpp
+  LinalgPasses.cpp
   SCF.cpp
   Shape.cpp
   Standard.cpp
@@ -9,10 +10,16 @@ set(LLVM_OPTIONAL_SOURCES
 
 add_mlir_public_c_api_library(MLIRCAPILinalg
   Linalg.cpp
+  LinalgPasses.cpp
+
+  DEPENDS
+  MLIRLinalgPassIncGen
 
   LINK_LIBS PUBLIC
   MLIRCAPIIR
   MLIRLinalg
+  MLIRPass
+  MLIRLinalgTransforms
 )
 
 add_mlir_public_c_api_library(MLIRCAPISCF
diff --git a/mlir/lib/CAPI/Dialect/LinalgPasses.cpp b/mlir/lib/CAPI/Dialect/LinalgPasses.cpp
new file mode 100644 (file)
index 0000000..6677476
--- /dev/null
@@ -0,0 +1,26 @@
+//===- LinalgPasses.cpp - C API for Linalg Dialect Passes -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/CAPI/Pass.h"
+#include "mlir/Dialect/Linalg/Passes.h"
+#include "mlir/Pass/Pass.h"
+
+// Must include the declarations as they carry important visibility attributes.
+#include "mlir/Dialect/Linalg/Passes.capi.h.inc"
+
+using namespace mlir;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mlir/Dialect/Linalg/Passes.capi.cpp.inc"
+
+#ifdef __cplusplus
+}
+#endif