Skip to content

Commit 6e8b3a3

Browse files
committed
Revert "[mlir python] Port Python core code to nanobind. (#118583)"
This reverts commit 41bd35b. Breakage detected, rolling back.
1 parent 41bd35b commit 6e8b3a3

File tree

22 files changed

+1570
-1870
lines changed

22 files changed

+1570
-1870
lines changed

mlir/cmake/modules/MLIRDetectPythonEnv.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ macro(mlir_configure_python_dev_packages)
3939
"extension = '${PYTHON_MODULE_EXTENSION}")
4040

4141
mlir_detect_nanobind_install()
42-
find_package(nanobind 2.4 CONFIG REQUIRED)
42+
find_package(nanobind 2.2 CONFIG REQUIRED)
4343
message(STATUS "Found nanobind v${nanobind_VERSION}: ${nanobind_INCLUDE_DIR}")
4444
message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
4545
"suffix = '${PYTHON_MODULE_SUFFIX}', "

mlir/include/mlir/Bindings/Python/IRTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef MLIR_BINDINGS_PYTHON_IRTYPES_H
1010
#define MLIR_BINDINGS_PYTHON_IRTYPES_H
1111

12-
#include "mlir/Bindings/Python/NanobindAdaptors.h"
12+
#include "mlir/Bindings/Python/PybindAdaptors.h"
1313

1414
namespace mlir {
1515

mlir/include/mlir/Bindings/Python/PybindAdaptors.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,9 @@ class pure_subclass {
374374
static_assert(!std::is_member_function_pointer<Func>::value,
375375
"def_staticmethod(...) called with a non-static member "
376376
"function pointer");
377-
py::cpp_function cf(std::forward<Func>(f), py::name(name),
378-
py::scope(thisClass), extra...);
377+
py::cpp_function cf(
378+
std::forward<Func>(f), py::name(name), py::scope(thisClass),
379+
py::sibling(py::getattr(thisClass, name, py::none())), extra...);
379380
thisClass.attr(cf.name()) = py::staticmethod(cf);
380381
return *this;
381382
}
@@ -386,8 +387,9 @@ class pure_subclass {
386387
static_assert(!std::is_member_function_pointer<Func>::value,
387388
"def_classmethod(...) called with a non-static member "
388389
"function pointer");
389-
py::cpp_function cf(std::forward<Func>(f), py::name(name),
390-
py::scope(thisClass), extra...);
390+
py::cpp_function cf(
391+
std::forward<Func>(f), py::name(name), py::scope(thisClass),
392+
py::sibling(py::getattr(thisClass, name, py::none())), extra...);
391393
thisClass.attr(cf.name()) =
392394
py::reinterpret_borrow<py::object>(PyClassMethod_New(cf.ptr()));
393395
return *this;

mlir/lib/Bindings/Python/Globals.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
#ifndef MLIR_BINDINGS_PYTHON_GLOBALS_H
1010
#define MLIR_BINDINGS_PYTHON_GLOBALS_H
1111

12-
#include <optional>
13-
#include <string>
14-
#include <vector>
12+
#include "PybindUtils.h"
1513

16-
#include "NanobindUtils.h"
1714
#include "mlir-c/IR.h"
1815
#include "mlir/CAPI/Support.h"
1916
#include "llvm/ADT/DenseMap.h"
2017
#include "llvm/ADT/StringRef.h"
2118
#include "llvm/ADT/StringSet.h"
2219

20+
#include <optional>
21+
#include <string>
22+
#include <vector>
23+
2324
namespace mlir {
2425
namespace python {
2526

@@ -56,71 +57,71 @@ class PyGlobals {
5657
/// Raises an exception if the mapping already exists and replace == false.
5758
/// This is intended to be called by implementation code.
5859
void registerAttributeBuilder(const std::string &attributeKind,
59-
nanobind::callable pyFunc,
60+
pybind11::function pyFunc,
6061
bool replace = false);
6162

6263
/// Adds a user-friendly type caster. Raises an exception if the mapping
6364
/// already exists and replace == false. This is intended to be called by
6465
/// implementation code.
65-
void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster,
66+
void registerTypeCaster(MlirTypeID mlirTypeID, pybind11::function typeCaster,
6667
bool replace = false);
6768

6869
/// Adds a user-friendly value caster. Raises an exception if the mapping
6970
/// already exists and replace == false. This is intended to be called by
7071
/// implementation code.
7172
void registerValueCaster(MlirTypeID mlirTypeID,
72-
nanobind::callable valueCaster,
73+
pybind11::function valueCaster,
7374
bool replace = false);
7475

7576
/// Adds a concrete implementation dialect class.
7677
/// Raises an exception if the mapping already exists.
7778
/// This is intended to be called by implementation code.
7879
void registerDialectImpl(const std::string &dialectNamespace,
79-
nanobind::object pyClass);
80+
pybind11::object pyClass);
8081

8182
/// Adds a concrete implementation operation class.
8283
/// Raises an exception if the mapping already exists and replace == false.
8384
/// This is intended to be called by implementation code.
8485
void registerOperationImpl(const std::string &operationName,
85-
nanobind::object pyClass, bool replace = false);
86+
pybind11::object pyClass, bool replace = false);
8687

8788
/// Returns the custom Attribute builder for Attribute kind.
88-
std::optional<nanobind::callable>
89+
std::optional<pybind11::function>
8990
lookupAttributeBuilder(const std::string &attributeKind);
9091

9192
/// Returns the custom type caster for MlirTypeID mlirTypeID.
92-
std::optional<nanobind::callable> lookupTypeCaster(MlirTypeID mlirTypeID,
93+
std::optional<pybind11::function> lookupTypeCaster(MlirTypeID mlirTypeID,
9394
MlirDialect dialect);
9495

9596
/// Returns the custom value caster for MlirTypeID mlirTypeID.
96-
std::optional<nanobind::callable> lookupValueCaster(MlirTypeID mlirTypeID,
97+
std::optional<pybind11::function> lookupValueCaster(MlirTypeID mlirTypeID,
9798
MlirDialect dialect);
9899

99100
/// Looks up a registered dialect class by namespace. Note that this may
100101
/// trigger loading of the defining module and can arbitrarily re-enter.
101-
std::optional<nanobind::object>
102+
std::optional<pybind11::object>
102103
lookupDialectClass(const std::string &dialectNamespace);
103104

104105
/// Looks up a registered operation class (deriving from OpView) by operation
105106
/// name. Note that this may trigger a load of the dialect, which can
106107
/// arbitrarily re-enter.
107-
std::optional<nanobind::object>
108+
std::optional<pybind11::object>
108109
lookupOperationClass(llvm::StringRef operationName);
109110

110111
private:
111112
static PyGlobals *instance;
112113
/// Module name prefixes to search under for dialect implementation modules.
113114
std::vector<std::string> dialectSearchPrefixes;
114115
/// Map of dialect namespace to external dialect class object.
115-
llvm::StringMap<nanobind::object> dialectClassMap;
116+
llvm::StringMap<pybind11::object> dialectClassMap;
116117
/// Map of full operation name to external operation class object.
117-
llvm::StringMap<nanobind::object> operationClassMap;
118+
llvm::StringMap<pybind11::object> operationClassMap;
118119
/// Map of attribute ODS name to custom builder.
119-
llvm::StringMap<nanobind::callable> attributeBuilderMap;
120+
llvm::StringMap<pybind11::object> attributeBuilderMap;
120121
/// Map of MlirTypeID to custom type caster.
121-
llvm::DenseMap<MlirTypeID, nanobind::callable> typeCasterMap;
122+
llvm::DenseMap<MlirTypeID, pybind11::object> typeCasterMap;
122123
/// Map of MlirTypeID to custom value caster.
123-
llvm::DenseMap<MlirTypeID, nanobind::callable> valueCasterMap;
124+
llvm::DenseMap<MlirTypeID, pybind11::object> valueCasterMap;
124125
/// Set of dialect namespaces that we have attempted to import implementation
125126
/// modules for.
126127
llvm::StringSet<> loadedDialectModules;

0 commit comments

Comments
 (0)