Skip to content

[mlir][python][cmake] add EXTRA_INCLUDES to declare_mlir_dialect_python_bindings #76204

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Dec 22, 2023

This PR adds an EXTRA_INCLUDES arg to declare_mlir_dialect_python_bindings to be forwarded to mlir_tablegen. The use case I think is obvious but my personal need was to experiment with generating python bindings for some of flang's dialects. And it worked! How does the adage go? "You can write fortran in any language"? Well now you really can!

Anyway I left the flang cmake target in here as a demo/test (in a separate commit) but I don't intend to keep it (will drop commit after CI passes).

@makslevental makslevental force-pushed the extra_includes_pytblgen branch from 2c19b80 to 28d1c7d Compare December 22, 2023 05:07
@makslevental makslevental marked this pull request as ready for review December 22, 2023 05:08
@llvmbot llvmbot added mlir:python MLIR Python bindings mlir labels Dec 22, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2023

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

This PR adds an EXTRA_INCLUDES arg to declare_mlir_dialect_python_bindings to be forwarded to mlir_tablegen. The use case I think is obvious but my personal need was to experiment with generating python bindings for some of flang's dialects. And it worked! How does the adage go? "You can write fortran in any language"? Well now you really can!

Anyway I left the flang cmake target in here as a demo/test (in a separate commit) but I don't intend to keep it.


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

5 Files Affected:

  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+3-3)
  • (modified) mlir/python/CMakeLists.txt (+11)
  • (added) mlir/python/mlir/dialects/FIRAttrs.td (+14)
  • (added) mlir/python/mlir/dialects/FIROps.td (+14)
  • (added) mlir/python/mlir/dialects/fir.py (+6)
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 012380603a4c45..052dbff3b4d14a 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -286,7 +286,7 @@ function(declare_mlir_dialect_python_bindings)
   cmake_parse_arguments(ARG
     "GEN_ENUM_BINDINGS"
     "ROOT_DIR;ADD_TO_PARENT;TD_FILE;DIALECT_NAME"
-    "SOURCES;SOURCES_GLOB;DEPENDS;GEN_ENUM_BINDINGS_TD_FILE"
+    "SOURCES;SOURCES_GLOB;DEPENDS;GEN_ENUM_BINDINGS_TD_FILE;EXTRA_INCLUDES"
     ${ARGN})
   # Sources.
   set(_dialect_target "${ARG_ADD_TO_PARENT}.${ARG_DIALECT_NAME}")
@@ -307,7 +307,7 @@ function(declare_mlir_dialect_python_bindings)
     set(LLVM_TARGET_DEFINITIONS ${td_file})
     mlir_tablegen("${dialect_filename}"
       -gen-python-op-bindings -bind-dialect=${ARG_DIALECT_NAME}
-      DEPENDS ${ARG_DEPENDS}
+      DEPENDS ${ARG_DEPENDS} EXTRA_INCLUDES ${ARG_EXTRA_INCLUDES}
     )
     add_public_tablegen_target(${tblgen_target})
 
@@ -318,7 +318,7 @@ function(declare_mlir_dialect_python_bindings)
         set(LLVM_TARGET_DEFINITIONS ${td_file})
       endif()
       set(enum_filename "${relative_td_directory}/_${ARG_DIALECT_NAME}_enum_gen.py")
-      mlir_tablegen(${enum_filename} -gen-python-enum-bindings)
+      mlir_tablegen(${enum_filename} -gen-python-enum-bindings EXTRA_INCLUDES ${ARG_EXTRA_INCLUDES})
       list(APPEND _sources ${enum_filename})
     endif()
 
diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index 3c9cf304d88a27..28f522b608a3ff 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -403,6 +403,17 @@ declare_mlir_dialect_python_bindings(
   GEN_ENUM_BINDINGS_TD_FILE
     "dialects/VectorAttributes.td")
 
+declare_mlir_dialect_python_bindings(
+  ADD_TO_PARENT MLIRPythonSources.Dialects
+  ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
+  TD_FILE dialects/FIROps.td
+  SOURCES dialects/fir.py
+  DIALECT_NAME fir
+  GEN_ENUM_BINDINGS_TD_FILE
+    "dialects/FIRAttrs.td"
+  EXTRA_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/mlir/../../../flang/include"
+)
+
 ################################################################################
 # Python extensions.
 # The sources for these are all in lib/Bindings/Python, but since they have to
diff --git a/mlir/python/mlir/dialects/FIRAttrs.td b/mlir/python/mlir/dialects/FIRAttrs.td
new file mode 100644
index 00000000000000..382a5df03db88e
--- /dev/null
+++ b/mlir/python/mlir/dialects/FIRAttrs.td
@@ -0,0 +1,14 @@
+//===-- ControlFlowOps.td - Python ControlFlowOps bindings -*- tablegen -*-===//
+//
+// 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
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef PYTHON_BINDINGS_FIR_ATTRS
+#define PYTHON_BINDINGS_FIR_ATTRS
+
+include "flang/Optimizer/Dialect/FIRAttr.td"
+
+#endif
diff --git a/mlir/python/mlir/dialects/FIROps.td b/mlir/python/mlir/dialects/FIROps.td
new file mode 100644
index 00000000000000..025b6deb98e355
--- /dev/null
+++ b/mlir/python/mlir/dialects/FIROps.td
@@ -0,0 +1,14 @@
+//===-- ControlFlowOps.td - Python ControlFlowOps bindings -*- tablegen -*-===//
+//
+// 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
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef PYTHON_BINDINGS_FIR_OPS
+#define PYTHON_BINDINGS_FIR_OPS
+
+include "flang/Optimizer/Dialect/FIROps.td"
+
+#endif
diff --git a/mlir/python/mlir/dialects/fir.py b/mlir/python/mlir/dialects/fir.py
new file mode 100644
index 00000000000000..ca09e96a1742c2
--- /dev/null
+++ b/mlir/python/mlir/dialects/fir.py
@@ -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 ._fir_ops_gen import *
+from ._fir_enum_gen import *

"dialects/FIRAttrs.td"
EXTRA_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/mlir/../../../flang/include"
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Flang is a separate project from MLIR, I don't see this belonging here right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course - like I mentioned in the description - this is just a test of the extra argument to the cmake function and I will drop the commit after CI passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:python MLIR Python bindings mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants