Skip to content

Commit d4b994c

Browse files
committed
Revert "[mlir python] Port in-tree dialects to nanobind. (llvm#119924)"
This reverts commit 5cd4274.
1 parent b5f3be2 commit d4b994c

35 files changed

+360
-358
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -670,19 +670,6 @@ function(add_mlir_python_extension libname extname)
670670
NB_DOMAIN mlir
671671
${ARG_SOURCES}
672672
)
673-
674-
if (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
675-
# Avoids warnings from upstream nanobind.
676-
target_compile_options(nanobind-static
677-
PRIVATE
678-
-Wno-cast-qual
679-
-Wno-zero-length-array
680-
-Wno-nested-anon-types
681-
-Wno-c++98-compat-extra-semi
682-
-Wno-covered-switch-default
683-
${eh_rtti_enable}
684-
)
685-
endif()
686673
endif()
687674

688675
target_compile_options(${libname} PRIVATE ${eh_rtti_enable})

mlir/cmake/modules/MLIRDetectPythonEnv.cmake

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,5 @@ function(mlir_detect_nanobind_install)
9595
endif()
9696
message(STATUS "found (${PACKAGE_DIR})")
9797
set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
98-
execute_process(
99-
COMMAND "${Python3_EXECUTABLE}"
100-
-c "import nanobind;print(nanobind.include_dir(), end='')"
101-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
102-
RESULT_VARIABLE STATUS
103-
OUTPUT_VARIABLE PACKAGE_DIR
104-
ERROR_QUIET)
105-
if(NOT STATUS EQUAL "0")
106-
message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
107-
return()
108-
endif()
109-
set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
11098
endif()
11199
endfunction()

mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
#include <nanobind/nanobind.h>
13+
1214
#include "Standalone-c/Dialects.h"
13-
#include "mlir/Bindings/Python/Nanobind.h"
1415
#include "mlir/Bindings/Python/NanobindAdaptors.h"
1516

1617
namespace nb = nanobind;

mlir/include/mlir/Bindings/Python/Nanobind.h

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

mlir/include/mlir/Bindings/Python/NanobindAdaptors.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#ifndef MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
2020
#define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
2121

22+
#include <nanobind/nanobind.h>
23+
#include <nanobind/stl/string.h>
24+
2225
#include <cstdint>
2326

27+
#include "mlir-c/Bindings/Python/Interop.h"
2428
#include "mlir-c/Diagnostics.h"
2529
#include "mlir-c/IR.h"
26-
#include "mlir/Bindings/Python/Nanobind.h"
27-
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
2830
#include "llvm/ADT/Twine.h"
2931

3032
// Raw CAPI type casters need to be declared before use, so always include them
@@ -629,6 +631,40 @@ class mlir_value_subclass : public pure_subclass {
629631

630632
} // namespace nanobind_adaptors
631633

634+
/// RAII scope intercepting all diagnostics into a string. The message must be
635+
/// checked before this goes out of scope.
636+
class CollectDiagnosticsToStringScope {
637+
public:
638+
explicit CollectDiagnosticsToStringScope(MlirContext ctx) : context(ctx) {
639+
handlerID = mlirContextAttachDiagnosticHandler(ctx, &handler, &errorMessage,
640+
/*deleteUserData=*/nullptr);
641+
}
642+
~CollectDiagnosticsToStringScope() {
643+
assert(errorMessage.empty() && "unchecked error message");
644+
mlirContextDetachDiagnosticHandler(context, handlerID);
645+
}
646+
647+
[[nodiscard]] std::string takeMessage() { return std::move(errorMessage); }
648+
649+
private:
650+
static MlirLogicalResult handler(MlirDiagnostic diag, void *data) {
651+
auto printer = +[](MlirStringRef message, void *data) {
652+
*static_cast<std::string *>(data) +=
653+
llvm::StringRef(message.data, message.length);
654+
};
655+
MlirLocation loc = mlirDiagnosticGetLocation(diag);
656+
*static_cast<std::string *>(data) += "at ";
657+
mlirLocationPrint(loc, printer, data);
658+
*static_cast<std::string *>(data) += ": ";
659+
mlirDiagnosticPrint(diag, printer, data);
660+
return mlirLogicalResultSuccess();
661+
}
662+
663+
MlirContext context;
664+
MlirDiagnosticHandlerID handlerID;
665+
std::string errorMessage = "";
666+
};
667+
632668
} // namespace python
633669
} // namespace mlir
634670

mlir/lib/Bindings/Python/AsyncPasses.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
#include "mlir-c/Dialect/Async.h"
1010

11-
#include "mlir/Bindings/Python/Nanobind.h"
11+
#include <pybind11/detail/common.h>
12+
#include <pybind11/pybind11.h>
1213

1314
// -----------------------------------------------------------------------------
1415
// Module initialization.
1516
// -----------------------------------------------------------------------------
1617

17-
NB_MODULE(_mlirAsyncPasses, m) {
18+
PYBIND11_MODULE(_mlirAsyncPasses, m) {
1819
m.doc() = "MLIR Async Dialect Passes";
1920

2021
// Register all Async passes on load.

mlir/lib/Bindings/Python/DialectGPU.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
#include "mlir-c/Dialect/GPU.h"
1010
#include "mlir-c/IR.h"
1111
#include "mlir-c/Support.h"
12-
#include "mlir/Bindings/Python/NanobindAdaptors.h"
13-
#include "mlir/Bindings/Python/Nanobind.h"
12+
#include "mlir/Bindings/Python/PybindAdaptors.h"
1413

15-
namespace nb = nanobind;
16-
using namespace nanobind::literals;
14+
#include <pybind11/detail/common.h>
15+
#include <pybind11/pybind11.h>
1716

17+
namespace py = pybind11;
1818
using namespace mlir;
1919
using namespace mlir::python;
20-
using namespace mlir::python::nanobind_adaptors;
20+
using namespace mlir::python::adaptors;
2121

2222
// -----------------------------------------------------------------------------
2323
// Module initialization.
2424
// -----------------------------------------------------------------------------
2525

26-
NB_MODULE(_mlirDialectsGPU, m) {
26+
PYBIND11_MODULE(_mlirDialectsGPU, m) {
2727
m.doc() = "MLIR GPU Dialect";
2828
//===-------------------------------------------------------------------===//
2929
// AsyncTokenType
@@ -34,11 +34,11 @@ NB_MODULE(_mlirDialectsGPU, m) {
3434

3535
mlirGPUAsyncTokenType.def_classmethod(
3636
"get",
37-
[](nb::object cls, MlirContext ctx) {
37+
[](py::object cls, MlirContext ctx) {
3838
return cls(mlirGPUAsyncTokenTypeGet(ctx));
3939
},
40-
"Gets an instance of AsyncTokenType in the same context", nb::arg("cls"),
41-
nb::arg("ctx").none() = nb::none());
40+
"Gets an instance of AsyncTokenType in the same context", py::arg("cls"),
41+
py::arg("ctx") = py::none());
4242

4343
//===-------------------------------------------------------------------===//
4444
// ObjectAttr
@@ -47,12 +47,12 @@ NB_MODULE(_mlirDialectsGPU, m) {
4747
mlir_attribute_subclass(m, "ObjectAttr", mlirAttributeIsAGPUObjectAttr)
4848
.def_classmethod(
4949
"get",
50-
[](nb::object cls, MlirAttribute target, uint32_t format,
51-
nb::bytes object, std::optional<MlirAttribute> mlirObjectProps,
50+
[](py::object cls, MlirAttribute target, uint32_t format,
51+
py::bytes object, std::optional<MlirAttribute> mlirObjectProps,
5252
std::optional<MlirAttribute> mlirKernelsAttr) {
53-
MlirStringRef objectStrRef = mlirStringRefCreate(
54-
static_cast<char *>(const_cast<void *>(object.data())),
55-
object.size());
53+
py::buffer_info info(py::buffer(object).request());
54+
MlirStringRef objectStrRef =
55+
mlirStringRefCreate(static_cast<char *>(info.ptr), info.size);
5656
return cls(mlirGPUObjectAttrGetWithKernels(
5757
mlirAttributeGetContext(target), target, format, objectStrRef,
5858
mlirObjectProps.has_value() ? *mlirObjectProps
@@ -61,7 +61,7 @@ NB_MODULE(_mlirDialectsGPU, m) {
6161
: MlirAttribute{nullptr}));
6262
},
6363
"cls"_a, "target"_a, "format"_a, "object"_a,
64-
"properties"_a.none() = nb::none(), "kernels"_a.none() = nb::none(),
64+
"properties"_a = py::none(), "kernels"_a = py::none(),
6565
"Gets a gpu.object from parameters.")
6666
.def_property_readonly(
6767
"target",
@@ -73,18 +73,18 @@ NB_MODULE(_mlirDialectsGPU, m) {
7373
"object",
7474
[](MlirAttribute self) {
7575
MlirStringRef stringRef = mlirGPUObjectAttrGetObject(self);
76-
return nb::bytes(stringRef.data, stringRef.length);
76+
return py::bytes(stringRef.data, stringRef.length);
7777
})
7878
.def_property_readonly("properties",
79-
[](MlirAttribute self) -> nb::object {
79+
[](MlirAttribute self) {
8080
if (mlirGPUObjectAttrHasProperties(self))
81-
return nb::cast(
81+
return py::cast(
8282
mlirGPUObjectAttrGetProperties(self));
83-
return nb::none();
83+
return py::none().cast<py::object>();
8484
})
85-
.def_property_readonly("kernels", [](MlirAttribute self) -> nb::object {
85+
.def_property_readonly("kernels", [](MlirAttribute self) {
8686
if (mlirGPUObjectAttrHasKernels(self))
87-
return nb::cast(mlirGPUObjectAttrGetKernels(self));
88-
return nb::none();
87+
return py::cast(mlirGPUObjectAttrGetKernels(self));
88+
return py::none().cast<py::object>();
8989
});
9090
}

mlir/lib/Bindings/Python/DialectLLVM.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
#include "mlir-c/IR.h"
1313
#include "mlir-c/Support.h"
1414
#include "mlir/Bindings/Python/Diagnostics.h"
15-
#include "mlir/Bindings/Python/NanobindAdaptors.h"
16-
#include "mlir/Bindings/Python/Nanobind.h"
17-
18-
namespace nb = nanobind;
19-
20-
using namespace nanobind::literals;
15+
#include "mlir/Bindings/Python/PybindAdaptors.h"
2116

17+
namespace py = pybind11;
2218
using namespace llvm;
2319
using namespace mlir;
2420
using namespace mlir::python;
25-
using namespace mlir::python::nanobind_adaptors;
21+
using namespace mlir::python::adaptors;
2622

27-
void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
23+
void populateDialectLLVMSubmodule(const pybind11::module &m) {
2824

2925
//===--------------------------------------------------------------------===//
3026
// StructType
@@ -35,58 +31,58 @@ void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
3531

3632
llvmStructType.def_classmethod(
3733
"get_literal",
38-
[](nb::object cls, const std::vector<MlirType> &elements, bool packed,
34+
[](py::object cls, const std::vector<MlirType> &elements, bool packed,
3935
MlirLocation loc) {
4036
CollectDiagnosticsToStringScope scope(mlirLocationGetContext(loc));
4137

4238
MlirType type = mlirLLVMStructTypeLiteralGetChecked(
4339
loc, elements.size(), elements.data(), packed);
4440
if (mlirTypeIsNull(type)) {
45-
throw nb::value_error(scope.takeMessage().c_str());
41+
throw py::value_error(scope.takeMessage());
4642
}
4743
return cls(type);
4844
},
49-
"cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
50-
"loc"_a.none() = nb::none());
45+
"cls"_a, "elements"_a, py::kw_only(), "packed"_a = false,
46+
"loc"_a = py::none());
5147

5248
llvmStructType.def_classmethod(
5349
"get_identified",
54-
[](nb::object cls, const std::string &name, MlirContext context) {
50+
[](py::object cls, const std::string &name, MlirContext context) {
5551
return cls(mlirLLVMStructTypeIdentifiedGet(
5652
context, mlirStringRefCreate(name.data(), name.size())));
5753
},
58-
"cls"_a, "name"_a, nb::kw_only(), "context"_a.none() = nb::none());
54+
"cls"_a, "name"_a, py::kw_only(), "context"_a = py::none());
5955

6056
llvmStructType.def_classmethod(
6157
"get_opaque",
62-
[](nb::object cls, const std::string &name, MlirContext context) {
58+
[](py::object cls, const std::string &name, MlirContext context) {
6359
return cls(mlirLLVMStructTypeOpaqueGet(
6460
context, mlirStringRefCreate(name.data(), name.size())));
6561
},
66-
"cls"_a, "name"_a, "context"_a.none() = nb::none());
62+
"cls"_a, "name"_a, "context"_a = py::none());
6763

6864
llvmStructType.def(
6965
"set_body",
7066
[](MlirType self, const std::vector<MlirType> &elements, bool packed) {
7167
MlirLogicalResult result = mlirLLVMStructTypeSetBody(
7268
self, elements.size(), elements.data(), packed);
7369
if (!mlirLogicalResultIsSuccess(result)) {
74-
throw nb::value_error(
70+
throw py::value_error(
7571
"Struct body already set to different content.");
7672
}
7773
},
78-
"elements"_a, nb::kw_only(), "packed"_a = false);
74+
"elements"_a, py::kw_only(), "packed"_a = false);
7975

8076
llvmStructType.def_classmethod(
8177
"new_identified",
82-
[](nb::object cls, const std::string &name,
78+
[](py::object cls, const std::string &name,
8379
const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
8480
return cls(mlirLLVMStructTypeIdentifiedNewGet(
8581
ctx, mlirStringRefCreate(name.data(), name.length()),
8682
elements.size(), elements.data(), packed));
8783
},
88-
"cls"_a, "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
89-
"context"_a.none() = nb::none());
84+
"cls"_a, "name"_a, "elements"_a, py::kw_only(), "packed"_a = false,
85+
"context"_a = py::none());
9086

9187
llvmStructType.def_property_readonly(
9288
"name", [](MlirType type) -> std::optional<std::string> {
@@ -97,12 +93,12 @@ void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
9793
return StringRef(stringRef.data, stringRef.length).str();
9894
});
9995

100-
llvmStructType.def_property_readonly("body", [](MlirType type) -> nb::object {
96+
llvmStructType.def_property_readonly("body", [](MlirType type) -> py::object {
10197
// Don't crash in absence of a body.
10298
if (mlirLLVMStructTypeIsOpaque(type))
103-
return nb::none();
99+
return py::none();
104100

105-
nb::list body;
101+
py::list body;
106102
for (intptr_t i = 0, e = mlirLLVMStructTypeGetNumElementTypes(type); i < e;
107103
++i) {
108104
body.append(mlirLLVMStructTypeGetElementType(type, i));
@@ -123,24 +119,24 @@ void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
123119
mlir_type_subclass(m, "PointerType", mlirTypeIsALLVMPointerType)
124120
.def_classmethod(
125121
"get",
126-
[](nb::object cls, std::optional<unsigned> addressSpace,
122+
[](py::object cls, std::optional<unsigned> addressSpace,
127123
MlirContext context) {
128124
CollectDiagnosticsToStringScope scope(context);
129125
MlirType type = mlirLLVMPointerTypeGet(
130126
context, addressSpace.has_value() ? *addressSpace : 0);
131127
if (mlirTypeIsNull(type)) {
132-
throw nb::value_error(scope.takeMessage().c_str());
128+
throw py::value_error(scope.takeMessage());
133129
}
134130
return cls(type);
135131
},
136-
"cls"_a, "address_space"_a.none() = nb::none(), nb::kw_only(),
137-
"context"_a.none() = nb::none())
132+
"cls"_a, "address_space"_a = py::none(), py::kw_only(),
133+
"context"_a = py::none())
138134
.def_property_readonly("address_space", [](MlirType type) {
139135
return mlirLLVMPointerTypeGetAddressSpace(type);
140136
});
141137
}
142138

143-
NB_MODULE(_mlirDialectsLLVM, m) {
139+
PYBIND11_MODULE(_mlirDialectsLLVM, m) {
144140
m.doc() = "MLIR LLVM Dialect";
145141

146142
populateDialectLLVMSubmodule(m);

0 commit comments

Comments
 (0)