Skip to content

Commit afdc309

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

35 files changed

+360
-365
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -670,26 +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()
686-
687-
if(APPLE)
688-
# NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
689-
# doesn't declare this API as undefined in its linker flags. So we need to declare it as such
690-
# for downstream users that do not do something like `-undefined dynamic_lookup`.
691-
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-U -Wl,_PyClassMethod_New")
692-
endif()
693673
endif()
694674

695675
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
}

0 commit comments

Comments
 (0)