[Mlir-commits] [llvm] [mlir] [mlir python] Add nanobind support for standalone dialects. (PR #117922)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Nov 27 12:44:49 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 866755f8da588ec2efbcac60679b9d9f5c4636a9 bdee4180687b1485ad4a950ebcf840c853856263 --extensions cpp,h -- 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/PybindAdaptors.h mlir/lib/Bindings/Python/DialectLLVM.cpp mlir/lib/Bindings/Python/TransformInterpreter.cpp mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp 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/Diagnostics.h b/mlir/include/mlir/Bindings/Python/Diagnostics.h
index ecda96e524..ea80e14dde 100644
--- a/mlir/include/mlir/Bindings/Python/Diagnostics.h
+++ b/mlir/include/mlir/Bindings/Python/Diagnostics.h
@@ -12,9 +12,9 @@
 #include <cassert>
 #include <string>
 
-#include "llvm/ADT/StringRef.h"
 #include "mlir-c/Diagnostics.h"
 #include "mlir-c/IR.h"
+#include "llvm/ADT/StringRef.h"
 
 namespace mlir {
 namespace python {
@@ -22,7 +22,7 @@ namespace python {
 /// RAII scope intercepting all diagnostics into a string. The message must be
 /// checked before this goes out of scope.
 class CollectDiagnosticsToStringScope {
- public:
+public:
   explicit CollectDiagnosticsToStringScope(MlirContext ctx) : context(ctx) {
     handlerID = mlirContextAttachDiagnosticHandler(ctx, &handler, &errorMessage,
                                                    /*deleteUserData=*/nullptr);
@@ -34,7 +34,7 @@ class CollectDiagnosticsToStringScope {
 
   [[nodiscard]] std::string takeMessage() { return std::move(errorMessage); }
 
- private:
+private:
   static MlirLogicalResult handler(MlirDiagnostic diag, void *data) {
     auto printer = +[](MlirStringRef message, void *data) {
       *static_cast<std::string *>(data) +=
@@ -53,7 +53,7 @@ class CollectDiagnosticsToStringScope {
   std::string errorMessage = "";
 };
 
-}  // namespace python
-}  // namespace mlir
+} // namespace python
+} // namespace mlir
 
-#endif  // MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
+#endif // MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
diff --git a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
index e62ea19f0f..5e01cebcb0 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
@@ -24,10 +24,10 @@
 
 #include <cstdint>
 
-#include "llvm/ADT/Twine.h"
 #include "mlir-c/Bindings/Python/Interop.h"
 #include "mlir-c/Diagnostics.h"
 #include "mlir-c/IR.h"
+#include "llvm/ADT/Twine.h"
 
 // Raw CAPI type casters need to be declared before use, so always include them
 // first.
@@ -233,7 +233,8 @@ struct type_caster<MlirOperation> {
   }
   static handle from_cpp(MlirOperation v, rv_policy,
                          cleanup_list *cleanup) noexcept {
-    if (v.ptr == nullptr) return nanobind::none();
+    if (v.ptr == nullptr)
+      return nanobind::none();
     nanobind::object capsule =
         nanobind::steal<nanobind::object>(mlirPythonOperationToCapsule(v));
     return nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
@@ -254,7 +255,8 @@ struct type_caster<MlirValue> {
   }
   static handle from_cpp(MlirValue v, rv_policy,
                          cleanup_list *cleanup) noexcept {
-    if (v.ptr == nullptr) return nanobind::none();
+    if (v.ptr == nullptr)
+      return nanobind::none();
     nanobind::object capsule =
         nanobind::steal<nanobind::object>(mlirPythonValueToCapsule(v));
     return nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
@@ -287,7 +289,8 @@ struct type_caster<MlirTypeID> {
   }
   static handle from_cpp(MlirTypeID v, rv_policy,
                          cleanup_list *cleanup) noexcept {
-    if (v.ptr == nullptr) return nanobind::none();
+    if (v.ptr == nullptr)
+      return nanobind::none();
     nanobind::object capsule =
         nanobind::steal<nanobind::object>(mlirPythonTypeIDToCapsule(v));
     return nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
@@ -318,8 +321,8 @@ struct type_caster<MlirType> {
   }
 };
 
-}  // namespace detail
-}  // namespace nanobind
+} // namespace detail
+} // namespace nanobind
 
 namespace mlir {
 namespace python {
@@ -337,7 +340,7 @@ namespace nanobind_adaptors {
 ///   (plus a fair amount of extra curricular poking)
 ///   TODO: If this proves useful, see about including it in nanobind.
 class pure_subclass {
- public:
+public:
   pure_subclass(nanobind::handle scope, const char *derivedClassName,
                 const nanobind::object &superClass) {
     nanobind::object pyType =
@@ -379,7 +382,7 @@ class pure_subclass {
                   "function pointer");
     nanobind::object cf = nanobind::cpp_function(
         std::forward<Func>(f),
-        nanobind::name(name),  // nanobind::scope(thisClass),
+        nanobind::name(name), // nanobind::scope(thisClass),
         extra...);
     thisClass.attr(name) = cf;
     return *this;
@@ -393,7 +396,7 @@ class pure_subclass {
                   "function pointer");
     nanobind::object cf = nanobind::cpp_function(
         std::forward<Func>(f),
-        nanobind::name(name),  // nanobind::scope(thisClass),
+        nanobind::name(name), // nanobind::scope(thisClass),
         extra...);
     thisClass.attr(name) =
         nanobind::borrow<nanobind::object>(PyClassMethod_New(cf.ptr()));
@@ -402,7 +405,7 @@ class pure_subclass {
 
   nanobind::object get_class() const { return thisClass; }
 
- protected:
+protected:
   nanobind::object superClass;
   nanobind::object thisClass;
 };
@@ -410,7 +413,7 @@ class pure_subclass {
 /// Creates a custom subclass of mlir.ir.Attribute, implementing a casting
 /// constructor and type checking methods.
 class mlir_attribute_subclass : public pure_subclass {
- public:
+public:
   using IsAFunctionTy = bool (*)(MlirAttribute);
   using GetTypeIDFunctionTy = MlirTypeID (*)();
 
@@ -442,7 +445,7 @@ class mlir_attribute_subclass : public pure_subclass {
     // have no additional members, we can just return the instance thus created
     // without amending it.
     std::string captureTypeName(
-        typeClassName);  // As string in case if typeClassName is not static.
+        typeClassName); // As string in case if typeClassName is not static.
     nanobind::object newCf = nanobind::cpp_function(
         [superCls, isaFunction, captureTypeName](
             nanobind::object cls, nanobind::object otherAttribute) {
@@ -488,7 +491,7 @@ class mlir_attribute_subclass : public pure_subclass {
 /// Creates a custom subclass of mlir.ir.Type, implementing a casting
 /// constructor and type checking methods.
 class mlir_type_subclass : public pure_subclass {
- public:
+public:
   using IsAFunctionTy = bool (*)(MlirType);
   using GetTypeIDFunctionTy = MlirTypeID (*)();
 
@@ -520,7 +523,7 @@ class mlir_type_subclass : public pure_subclass {
     // have no additional members, we can just return the instance thus created
     // without amending it.
     std::string captureTypeName(
-        typeClassName);  // As string in case if typeClassName is not static.
+        typeClassName); // As string in case if typeClassName is not static.
     nanobind::object newCf = nanobind::cpp_function(
         [superCls, isaFunction, captureTypeName](nanobind::object cls,
                                                  nanobind::object otherType) {
@@ -570,7 +573,7 @@ class mlir_type_subclass : public pure_subclass {
 /// Creates a custom subclass of mlir.ir.Value, implementing a casting
 /// constructor and type checking methods.
 class mlir_value_subclass : public pure_subclass {
- public:
+public:
   using IsAFunctionTy = bool (*)(MlirValue);
 
   /// Subclasses by looking up the super-class dynamically.
@@ -598,7 +601,7 @@ class mlir_value_subclass : public pure_subclass {
     // have no additional members, we can just return the instance thus created
     // without amending it.
     std::string captureValueName(
-        valueClassName);  // As string in case if valueClassName is not static.
+        valueClassName); // As string in case if valueClassName is not static.
     nanobind::object newCf = nanobind::cpp_function(
         [superCls, isaFunction, captureValueName](nanobind::object cls,
                                                   nanobind::object otherValue) {
@@ -626,12 +629,12 @@ class mlir_value_subclass : public pure_subclass {
   }
 };
 
-}  // namespace nanobind_adaptors
+} // namespace nanobind_adaptors
 
 /// RAII scope intercepting all diagnostics into a string. The message must be
 /// checked before this goes out of scope.
 class CollectDiagnosticsToStringScope {
- public:
+public:
   explicit CollectDiagnosticsToStringScope(MlirContext ctx) : context(ctx) {
     handlerID = mlirContextAttachDiagnosticHandler(ctx, &handler, &errorMessage,
                                                    /*deleteUserData=*/nullptr);
@@ -643,7 +646,7 @@ class CollectDiagnosticsToStringScope {
 
   [[nodiscard]] std::string takeMessage() { return std::move(errorMessage); }
 
- private:
+private:
   static MlirLogicalResult handler(MlirDiagnostic diag, void *data) {
     auto printer = +[](MlirStringRef message, void *data) {
       *static_cast<std::string *>(data) +=
@@ -662,7 +665,7 @@ class CollectDiagnosticsToStringScope {
   std::string errorMessage = "";
 };
 
-}  // namespace python
-}  // namespace mlir
+} // namespace python
+} // namespace mlir
 
-#endif  // MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
+#endif // MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H

``````````

</details>


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


More information about the Mlir-commits mailing list