Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 3db5742

Browse files
authored
[mlir python] Add nanobind support for standalone dialects. (#117922)
This PR allows out-of-tree dialects to write Python dialect modules using nanobind instead of pybind11. It may make sense to migrate in-tree dialects and some of the ODS Python infrastructure to nanobind, but that is a topic for a future change. This PR makes the following changes: * adds nanobind to the CMake and Bazel build systems. We also add robin_map to the Bazel build, which is a dependency of nanobind. * adds a PYTHON_BINDING_LIBRARY option to various CMake functions, such as declare_mlir_python_extension, allowing users to select a Python binding library. * creates a fork of mlir/include/mlir/Bindings/Python/PybindAdaptors.h named NanobindAdaptors.h. This plays the same role, using nanobind instead of pybind11. * splits CollectDiagnosticsToStringScope out of PybindAdaptors.h and into a new header mlir/include/mlir/Bindings/Python/Diagnostics.h, since it is code that is no way related to pybind11 or for that matter, Python. * changed the standalone Python extension example to have both pybind11 and nanobind variants. * changed mlir/python/mlir/dialects/python_test.py to have both pybind11 and nanobind variants. Notes: * A slightly unfortunate thing that I needed to do in the CMake integration was to use FindPython in addition to FindPython3, since nanobind's CMake integration expects the Python_ names for variables. Perhaps there's a better way to do this.
1 parent 8755b2d commit 3db5742

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

mlir/lib/Bindings/Python/DialectLLVM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <string>
10+
911
#include "mlir-c/Dialect/LLVM.h"
1012
#include "mlir-c/IR.h"
1113
#include "mlir-c/Support.h"
14+
#include "mlir/Bindings/Python/Diagnostics.h"
1215
#include "mlir/Bindings/Python/PybindAdaptors.h"
13-
#include <string>
1416

1517
namespace py = pybind11;
1618
using namespace llvm;

mlir/lib/Bindings/Python/TransformInterpreter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include <pybind11/detail/common.h>
14+
#include <pybind11/pybind11.h>
15+
1316
#include "mlir-c/Dialect/Transform/Interpreter.h"
1417
#include "mlir-c/IR.h"
1518
#include "mlir-c/Support.h"
19+
#include "mlir/Bindings/Python/Diagnostics.h"
1620
#include "mlir/Bindings/Python/PybindAdaptors.h"
1721

18-
#include <pybind11/detail/common.h>
19-
#include <pybind11/pybind11.h>
20-
2122
namespace py = pybind11;
2223

2324
namespace {

0 commit comments

Comments
 (0)