Skip to content

Commit 61bf308

Browse files
authored
Revert "[mlir python] Add nanobind support for standalone dialects." (llvm#118517)
Reverts llvm#117922 because deps aren't met on some of the post-commit build bots.
1 parent 04996a2 commit 61bf308

File tree

25 files changed

+105
-1184
lines changed

25 files changed

+105
-1184
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,10 @@ endfunction()
114114
# EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
115115
# on. These will be collected for all extensions and put into an
116116
# aggregate dylib that is linked against.
117-
# PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
118117
function(declare_mlir_python_extension name)
119118
cmake_parse_arguments(ARG
120119
""
121-
"ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY"
120+
"ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
122121
"SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
123122
${ARGN})
124123

@@ -127,20 +126,15 @@ function(declare_mlir_python_extension name)
127126
endif()
128127
set(_install_destination "src/python/${name}")
129128

130-
if(NOT ARG_PYTHON_BINDINGS_LIBRARY)
131-
set(ARG_PYTHON_BINDINGS_LIBRARY "pybind11")
132-
endif()
133-
134129
add_library(${name} INTERFACE)
135130
set_target_properties(${name} PROPERTIES
136131
# Yes: Leading-lowercase property names are load bearing and the recommended
137132
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
138-
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY"
133+
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
139134
mlir_python_SOURCES_TYPE extension
140135
mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
141136
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
142137
mlir_python_DEPENDS ""
143-
mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY}"
144138
)
145139

146140
# Set the interface source and link_libs properties of the target
@@ -229,14 +223,12 @@ function(add_mlir_python_modules name)
229223
elseif(_source_type STREQUAL "extension")
230224
# Native CPP extension.
231225
get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
232-
get_target_property(_bindings_library ${sources_target} mlir_python_BINDINGS_LIBRARY)
233226
# Transform relative source to based on root dir.
234227
set(_extension_target "${modules_target}.extension.${_module_name}.dso")
235228
add_mlir_python_extension(${_extension_target} "${_module_name}"
236229
INSTALL_COMPONENT ${modules_target}
237230
INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
238231
OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
239-
PYTHON_BINDINGS_LIBRARY ${_bindings_library}
240232
LINK_LIBS PRIVATE
241233
${sources_target}
242234
${ARG_COMMON_CAPI_LINK_LIBS}
@@ -642,7 +634,7 @@ endfunction()
642634
function(add_mlir_python_extension libname extname)
643635
cmake_parse_arguments(ARG
644636
""
645-
"INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY;PYTHON_BINDINGS_LIBRARY"
637+
"INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
646638
"SOURCES;LINK_LIBS"
647639
${ARGN})
648640
if(ARG_UNPARSED_ARGUMENTS)
@@ -652,16 +644,9 @@ function(add_mlir_python_extension libname extname)
652644
# The actual extension library produces a shared-object or DLL and has
653645
# sources that must be compiled in accordance with pybind11 needs (RTTI and
654646
# exceptions).
655-
if(NOT DEFINED ARG_PYTHON_BINDINGS_LIBRARY OR ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11")
656-
pybind11_add_module(${libname}
657-
${ARG_SOURCES}
658-
)
659-
elseif(ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind")
660-
nanobind_add_module(${libname}
661-
NB_DOMAIN mlir
662-
${ARG_SOURCES}
663-
)
664-
endif()
647+
pybind11_add_module(${libname}
648+
${ARG_SOURCES}
649+
)
665650

666651
# The extension itself must be compiled with RTTI and exceptions enabled.
667652
# Also, some warning classes triggered by pybind11 are disabled.

mlir/cmake/modules/MLIRDetectPythonEnv.cmake

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ macro(mlir_configure_python_dev_packages)
2121

2222
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
2323
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
24-
25-
# It's a little silly to detect Python a second time, but nanobind's cmake
26-
# code looks for Python_ not Python3_.
27-
find_package(Python ${LLVM_MINIMUM_PYTHON_VERSION}
28-
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
29-
3024
unset(_python_development_component)
3125
message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
3226
message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
@@ -37,13 +31,6 @@ macro(mlir_configure_python_dev_packages)
3731
message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
3832
"suffix = '${PYTHON_MODULE_SUFFIX}', "
3933
"extension = '${PYTHON_MODULE_EXTENSION}")
40-
41-
mlir_detect_nanobind_install()
42-
find_package(nanobind 2.2 CONFIG REQUIRED)
43-
message(STATUS "Found nanobind v${nanobind_VERSION}: ${nanobind_INCLUDE_DIR}")
44-
message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
45-
"suffix = '${PYTHON_MODULE_SUFFIX}', "
46-
"extension = '${PYTHON_MODULE_EXTENSION}")
4734
endif()
4835
endmacro()
4936

@@ -71,29 +58,3 @@ function(mlir_detect_pybind11_install)
7158
set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
7259
endif()
7360
endfunction()
74-
75-
76-
# Detects a nanobind package installed in the current python environment
77-
# and sets variables to allow it to be found. This allows nanobind to be
78-
# installed via pip, which typically yields a much more recent version than
79-
# the OS install, which will be available otherwise.
80-
function(mlir_detect_nanobind_install)
81-
if(nanobind_DIR)
82-
message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
83-
else()
84-
message(STATUS "Checking for nanobind in python path...")
85-
execute_process(
86-
COMMAND "${Python3_EXECUTABLE}"
87-
-c "import nanobind;print(nanobind.cmake_dir(), end='')"
88-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
89-
RESULT_VARIABLE STATUS
90-
OUTPUT_VARIABLE PACKAGE_DIR
91-
ERROR_QUIET)
92-
if(NOT STATUS EQUAL "0")
93-
message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
94-
return()
95-
endif()
96-
message(STATUS "found (${PACKAGE_DIR})")
97-
set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
98-
endif()
99-
endfunction()

mlir/docs/Bindings/Python.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,14 +1138,12 @@ attributes and types must connect to the relevant C APIs for building and
11381138
inspection, which must be provided first. Bindings for `Attribute` and `Type`
11391139
subclasses can be defined using
11401140
[`include/mlir/Bindings/Python/PybindAdaptors.h`](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/PybindAdaptors.h)
1141-
or
1142-
[`include/mlir/Bindings/Python/NanobindAdaptors.h`](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h)
1143-
utilities that mimic pybind11/nanobind API for defining functions and
1144-
properties. These bindings are to be included in a separate module. The
1145-
utilities also provide automatic casting between C API handles `MlirAttribute`
1146-
and `MlirType` and their Python counterparts so that the C API handles can be
1147-
used directly in binding implementations. The methods and properties provided by
1148-
the bindings should follow the principles discussed above.
1141+
utilities that mimic pybind11 API for defining functions and properties. These
1142+
bindings are to be included in a separate pybind11 module. The utilities also
1143+
provide automatic casting between C API handles `MlirAttribute` and `MlirType`
1144+
and their Python counterparts so that the C API handles can be used directly in
1145+
binding implementations. The methods and properties provided by the bindings
1146+
should follow the principles discussed above.
11491147

11501148
The attribute and type bindings for a dialect can be located in
11511149
`lib/Bindings/Python/Dialect<Name>.cpp` and should be compiled into a separate
@@ -1181,9 +1179,7 @@ make the passes available along with the dialect.
11811179
Dialect functionality other than IR objects or passes, such as helper functions,
11821180
can be exposed to Python similarly to attributes and types. C API is expected to
11831181
exist for this functionality, which can then be wrapped using pybind11 and
1184-
`[include/mlir/Bindings/Python/PybindAdaptors.h](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/PybindAdaptors.h)`,
1185-
or nanobind and
1186-
`[include/mlir/Bindings/Python/NanobindAdaptors.h](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h)`
1182+
`[include/mlir/Bindings/Python/PybindAdaptors.h](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/PybindAdaptors.h)`
11871183
utilities to connect to the rest of Python API. The bindings can be located in a
1188-
separate module or in the same module as attributes and types, and
1184+
separate pybind11 module or in the same module as attributes and types, and
11891185
loaded along with the dialect.

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,18 @@ declare_mlir_dialect_python_bindings(
1717
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir_standalone"
1818
TD_FILE dialects/StandaloneOps.td
1919
SOURCES
20-
dialects/standalone_pybind11.py
21-
dialects/standalone_nanobind.py
20+
dialects/standalone.py
2221
DIALECT_NAME standalone)
2322

24-
25-
declare_mlir_python_extension(StandalonePythonSources.Pybind11Extension
26-
MODULE_NAME _standaloneDialectsPybind11
27-
ADD_TO_PARENT StandalonePythonSources
28-
SOURCES
29-
StandaloneExtensionPybind11.cpp
30-
EMBED_CAPI_LINK_LIBS
31-
StandaloneCAPI
32-
PYTHON_BINDINGS_LIBRARY pybind11
33-
)
34-
35-
declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
36-
MODULE_NAME _standaloneDialectsNanobind
23+
declare_mlir_python_extension(StandalonePythonSources.Extension
24+
MODULE_NAME _standaloneDialects
3725
ADD_TO_PARENT StandalonePythonSources
3826
SOURCES
39-
StandaloneExtensionNanobind.cpp
27+
StandaloneExtension.cpp
4028
EMBED_CAPI_LINK_LIBS
4129
StandaloneCAPI
42-
PYTHON_BINDINGS_LIBRARY nanobind
4330
)
4431

45-
4632
################################################################################
4733
# Common CAPI
4834
################################################################################

mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp renamed to mlir/examples/standalone/python/StandaloneExtension.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
//===- StandaloneExtensionPybind11.cpp - Extension module -----------------===//
2-
//
3-
// This is the pybind11 version of the example module. There is also a nanobind
4-
// example in StandaloneExtensionNanobind.cpp.
1+
//===- StandaloneExtension.cpp - Extension module -------------------------===//
52
//
63
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
74
// See https://llvm.org/LICENSE.txt for license information.
@@ -14,7 +11,7 @@
1411

1512
using namespace mlir::python::adaptors;
1613

17-
PYBIND11_MODULE(_standaloneDialectsPybind11, m) {
14+
PYBIND11_MODULE(_standaloneDialects, m) {
1815
//===--------------------------------------------------------------------===//
1916
// standalone dialect
2017
//===--------------------------------------------------------------------===//

mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

mlir/examples/standalone/python/mlir_standalone/dialects/standalone_nanobind.py renamed to mlir/examples/standalone/python/mlir_standalone/dialects/standalone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

55
from ._standalone_ops_gen import *
6-
from .._mlir_libs._standaloneDialectsNanobind.standalone import *
6+
from .._mlir_libs._standaloneDialects.standalone import *

mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

mlir/examples/standalone/test/python/smoketest.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
# RUN: %python %s pybind11 | FileCheck %s
2-
# RUN: %python %s nanobind | FileCheck %s
1+
# RUN: %python %s | FileCheck %s
32

4-
import sys
53
from mlir_standalone.ir import *
6-
from mlir_standalone.dialects import builtin as builtin_d
7-
8-
if sys.argv[1] == "pybind11":
9-
from mlir_standalone.dialects import standalone_pybind11 as standalone_d
10-
elif sys.argv[1] == "nanobind":
11-
from mlir_standalone.dialects import standalone_nanobind as standalone_d
12-
else:
13-
raise ValueError("Expected either pybind11 or nanobind as arguments")
14-
4+
from mlir_standalone.dialects import builtin as builtin_d, standalone as standalone_d
155

166
with Context():
177
standalone_d.register_dialect()

mlir/include/mlir/Bindings/Python/Diagnostics.h

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)