[Mlir-commits] [llvm] [mlir] [mlir python] Port Python core code to nanobind. (PR #118583)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Dec 3 19:08:30 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff f6f16b5f541773bb074dd042746456deff169de2 522a30dd23baa61b1a47f449b579c9bfbbe4d35b --extensions h,cpp -- mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp mlir/include/mlir/Bindings/Python/Diagnostics.h mlir/include/mlir/Bindings/Python/NanobindAdaptors.h mlir/test/python/lib/PythonTestModuleNanobind.cpp mlir/include/mlir/Bindings/Python/IRTypes.h mlir/include/mlir/Bindings/Python/PybindAdaptors.h mlir/lib/Bindings/Python/DialectLLVM.cpp mlir/lib/Bindings/Python/Globals.h mlir/lib/Bindings/Python/IRAffine.cpp mlir/lib/Bindings/Python/IRAttributes.cpp mlir/lib/Bindings/Python/IRCore.cpp mlir/lib/Bindings/Python/IRInterfaces.cpp mlir/lib/Bindings/Python/IRModule.cpp mlir/lib/Bindings/Python/IRModule.h mlir/lib/Bindings/Python/IRTypes.cpp mlir/lib/Bindings/Python/MainModule.cpp mlir/lib/Bindings/Python/Pass.cpp mlir/lib/Bindings/Python/Pass.h mlir/lib/Bindings/Python/Rewrite.cpp mlir/lib/Bindings/Python/Rewrite.h mlir/lib/Bindings/Python/TransformInterpreter.cpp mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp mlir/lib/Bindings/Python/NanobindUtils.h mlir/test/python/lib/PythonTestModulePybind11.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
index ba3f78ecca..edc69774be 100644
--- a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
@@ -64,7 +64,8 @@ static py::object mlirApiObjectToCapsule(py::handle apiObject) {
 // ownership is unclear.
 
 /// Casts object <-> MlirAffineMap.
-template <> struct type_caster<MlirAffineMap> {
+template <>
+struct type_caster<MlirAffineMap> {
   PYBIND11_TYPE_CASTER(MlirAffineMap, _("MlirAffineMap"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -85,7 +86,8 @@ template <> struct type_caster<MlirAffineMap> {
 };
 
 /// Casts object <-> MlirAttribute.
-template <> struct type_caster<MlirAttribute> {
+template <>
+struct type_caster<MlirAttribute> {
   PYBIND11_TYPE_CASTER(MlirAttribute, _("MlirAttribute"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -104,7 +106,8 @@ template <> struct type_caster<MlirAttribute> {
 };
 
 /// Casts object -> MlirBlock.
-template <> struct type_caster<MlirBlock> {
+template <>
+struct type_caster<MlirBlock> {
   PYBIND11_TYPE_CASTER(MlirBlock, _("MlirBlock"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -114,7 +117,8 @@ template <> struct type_caster<MlirBlock> {
 };
 
 /// Casts object -> MlirContext.
-template <> struct type_caster<MlirContext> {
+template <>
+struct type_caster<MlirContext> {
   PYBIND11_TYPE_CASTER(MlirContext, _("MlirContext"));
   bool load(handle src, bool) {
     if (src.is_none()) {
@@ -133,7 +137,8 @@ template <> struct type_caster<MlirContext> {
 };
 
 /// Casts object <-> MlirDialectRegistry.
-template <> struct type_caster<MlirDialectRegistry> {
+template <>
+struct type_caster<MlirDialectRegistry> {
   PYBIND11_TYPE_CASTER(MlirDialectRegistry, _("MlirDialectRegistry"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -151,7 +156,8 @@ template <> struct type_caster<MlirDialectRegistry> {
 };
 
 /// Casts object <-> MlirLocation.
-template <> struct type_caster<MlirLocation> {
+template <>
+struct type_caster<MlirLocation> {
   PYBIND11_TYPE_CASTER(MlirLocation, _("MlirLocation"));
   bool load(handle src, bool) {
     if (src.is_none()) {
@@ -175,7 +181,8 @@ template <> struct type_caster<MlirLocation> {
 };
 
 /// Casts object <-> MlirModule.
-template <> struct type_caster<MlirModule> {
+template <>
+struct type_caster<MlirModule> {
   PYBIND11_TYPE_CASTER(MlirModule, _("MlirModule"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -193,7 +200,8 @@ template <> struct type_caster<MlirModule> {
 };
 
 /// Casts object <-> MlirFrozenRewritePatternSet.
-template <> struct type_caster<MlirFrozenRewritePatternSet> {
+template <>
+struct type_caster<MlirFrozenRewritePatternSet> {
   PYBIND11_TYPE_CASTER(MlirFrozenRewritePatternSet,
                        _("MlirFrozenRewritePatternSet"));
   bool load(handle src, bool) {
@@ -213,7 +221,8 @@ template <> struct type_caster<MlirFrozenRewritePatternSet> {
 };
 
 /// Casts object <-> MlirOperation.
-template <> struct type_caster<MlirOperation> {
+template <>
+struct type_caster<MlirOperation> {
   PYBIND11_TYPE_CASTER(MlirOperation, _("MlirOperation"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -233,7 +242,8 @@ template <> struct type_caster<MlirOperation> {
 };
 
 /// Casts object <-> MlirValue.
-template <> struct type_caster<MlirValue> {
+template <>
+struct type_caster<MlirValue> {
   PYBIND11_TYPE_CASTER(MlirValue, _("MlirValue"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -254,7 +264,8 @@ template <> struct type_caster<MlirValue> {
 };
 
 /// Casts object -> MlirPassManager.
-template <> struct type_caster<MlirPassManager> {
+template <>
+struct type_caster<MlirPassManager> {
   PYBIND11_TYPE_CASTER(MlirPassManager, _("MlirPassManager"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -264,7 +275,8 @@ template <> struct type_caster<MlirPassManager> {
 };
 
 /// Casts object <-> MlirTypeID.
-template <> struct type_caster<MlirTypeID> {
+template <>
+struct type_caster<MlirTypeID> {
   PYBIND11_TYPE_CASTER(MlirTypeID, _("MlirTypeID"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
@@ -284,7 +296,8 @@ template <> struct type_caster<MlirTypeID> {
 };
 
 /// Casts object <-> MlirType.
-template <> struct type_caster<MlirType> {
+template <>
+struct type_caster<MlirType> {
   PYBIND11_TYPE_CASTER(MlirType, _("MlirType"));
   bool load(handle src, bool) {
     py::object capsule = mlirApiObjectToCapsule(src);
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 12e66a759d..f01037a90f 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -179,39 +179,51 @@ class nb_buffer : public nb::object {
   }
 };
 
-template <typename T> struct nb_format_descriptor {};
+template <typename T>
+struct nb_format_descriptor {};
 
-template <> struct nb_format_descriptor<bool> {
+template <>
+struct nb_format_descriptor<bool> {
   static const char *format() { return "?"; }
 };
-template <> struct nb_format_descriptor<signed char> {
+template <>
+struct nb_format_descriptor<signed char> {
   static const char *format() { return "b"; }
 };
-template <> struct nb_format_descriptor<unsigned char> {
+template <>
+struct nb_format_descriptor<unsigned char> {
   static const char *format() { return "B"; }
 };
-template <> struct nb_format_descriptor<short> {
+template <>
+struct nb_format_descriptor<short> {
   static const char *format() { return "h"; }
 };
-template <> struct nb_format_descriptor<unsigned short> {
+template <>
+struct nb_format_descriptor<unsigned short> {
   static const char *format() { return "H"; }
 };
-template <> struct nb_format_descriptor<int> {
+template <>
+struct nb_format_descriptor<int> {
   static const char *format() { return "i"; }
 };
-template <> struct nb_format_descriptor<unsigned int> {
+template <>
+struct nb_format_descriptor<unsigned int> {
   static const char *format() { return "I"; }
 };
-template <> struct nb_format_descriptor<long> {
+template <>
+struct nb_format_descriptor<long> {
   static const char *format() { return "l"; }
 };
-template <> struct nb_format_descriptor<unsigned long> {
+template <>
+struct nb_format_descriptor<unsigned long> {
   static const char *format() { return "L"; }
 };
-template <> struct nb_format_descriptor<float> {
+template <>
+struct nb_format_descriptor<float> {
   static const char *format() { return "f"; }
 };
-template <> struct nb_format_descriptor<double> {
+template <>
+struct nb_format_descriptor<double> {
   static const char *format() { return "d"; }
 };
 
@@ -264,7 +276,8 @@ public:
   }
 };
 
-template <typename T> static T pyTryCast(nb::handle object) {
+template <typename T>
+static T pyTryCast(nb::handle object) {
   try {
     return nb::cast<T>(object);
   } catch (nb::cast_error &err) {
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 8ccccefcb3..adcdaa2bf4 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -2220,7 +2220,8 @@ namespace {
 /// CRTP base class for Python MLIR values that subclass Value and should be
 /// castable from it. The value hierarchy is one level deep and is not supposed
 /// to accommodate other levels unless core MLIR changes.
-template <typename DerivedTy> class PyConcreteValue : public PyValue {
+template <typename DerivedTy>
+class PyConcreteValue : public PyValue {
 public:
   // Derived classes must define statics for:
   //   IsAFunctionTy isaFunction
diff --git a/mlir/lib/Bindings/Python/IRInterfaces.cpp b/mlir/lib/Bindings/Python/IRInterfaces.cpp
index 8c41f70f56..ff7cb8b808 100644
--- a/mlir/lib/Bindings/Python/IRInterfaces.cpp
+++ b/mlir/lib/Bindings/Python/IRInterfaces.cpp
@@ -143,7 +143,8 @@ wrapRegions(std::optional<std::vector<PyRegion>> regions) {
 /// method to check whether the interface object was constructed from a class or
 /// an operation/opview instance. The `getOpName` always succeeds and returns a
 /// canonical name of the operation suitable for lookups.
-template <typename ConcreteIface> class PyConcreteOpInterface {
+template <typename ConcreteIface>
+class PyConcreteOpInterface {
 protected:
   using ClassTy = nb::class_<ConcreteIface>;
   using GetTypeIDFunctionTy = MlirTypeID (*)();
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 35dde3a073..d5c1a1784b 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -49,7 +49,8 @@ class PyValue;
 
 /// Template for a reference to a concrete type which captures a python
 /// reference to its underlying python object.
-template <typename T> class PyObjectRef {
+template <typename T>
+class PyObjectRef {
 public:
   PyObjectRef(T *referrent, nanobind::object object)
       : referrent(referrent), object(std::move(object)) {
diff --git a/mlir/lib/Bindings/Python/NanobindUtils.h b/mlir/lib/Bindings/Python/NanobindUtils.h
index fb62749510..6252ca2ac6 100644
--- a/mlir/lib/Bindings/Python/NanobindUtils.h
+++ b/mlir/lib/Bindings/Python/NanobindUtils.h
@@ -17,7 +17,8 @@
 
 #include <nanobind/nanobind.h>
 
-template <> struct std::iterator_traits<nanobind::detail::fast_iterator> {
+template <>
+struct std::iterator_traits<nanobind::detail::fast_iterator> {
   using value_type = nanobind::handle;
   using reference = const value_type;
   using pointer = void;
@@ -43,7 +44,8 @@ namespace python {
 /// error messages about mismatched types:
 ///   static constexpr const char kTypeDescription[] = "<Description>";
 
-template <typename DerivedTy, typename T> class Defaulting {
+template <typename DerivedTy, typename T>
+class Defaulting {
 public:
   using ReferrentTy = T;
   /// Type casters require the type to be default constructible, but using
@@ -64,7 +66,8 @@ private:
 namespace nanobind {
 namespace detail {
 
-template <typename DefaultingTy> struct MlirDefaultingCaster {
+template <typename DefaultingTy>
+struct MlirDefaultingCaster {
   NB_TYPE_CASTER(DefaultingTy, const_name(DefaultingTy::kTypeDescription));
 
   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
@@ -211,7 +214,8 @@ private:
 /// A derived class may additionally define:
 ///   - a `static void bindDerived(ClassTy &)` method to bind additional methods
 ///     the python class.
-template <typename Derived, typename ElementTy> class Sliceable {
+template <typename Derived, typename ElementTy>
+class Sliceable {
 protected:
   using ClassTy = nanobind::class_<Derived>;
 
@@ -371,7 +375,8 @@ private:
 
 namespace llvm {
 
-template <> struct DenseMapInfo<MlirTypeID> {
+template <>
+struct DenseMapInfo<MlirTypeID> {
   static inline MlirTypeID getEmptyKey() {
     auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
     return mlirTypeIDCreate(pointer);

``````````

</details>


https://github.com/llvm/llvm-project/pull/118583


More information about the Mlir-commits mailing list