Skip to content

Commit 6ef1f74

Browse files
committed
fix some signatures
1 parent dd83c63 commit 6ef1f74

File tree

13 files changed

+466
-282
lines changed

13 files changed

+466
-282
lines changed

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,6 @@ add_mlir_python_common_capi_library(StandalonePythonCAPI
6969
################################################################################
7070

7171
set(StandalonePythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
72-
add_mlir_python_modules(StandalonePythonModules
73-
ROOT_PREFIX "${StandalonePythonModules_ROOT_PREFIX}"
74-
INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}"
75-
DECLARED_SOURCES
76-
StandalonePythonSources
77-
# TODO: Remove this in favor of showing fine grained registration once
78-
# available.
79-
MLIRPythonExtension.RegisterEverything
80-
MLIRPythonSources.Core
81-
MLIRPythonSources.Dialects.builtin
82-
COMMON_CAPI_LINK_LIBS
83-
StandalonePythonCAPI
84-
)
8572

8673
# Everything here is very tightly coupled. See the ample descriptions at the bottom of
8774
# mlir/python/CMakeLists.txt.
@@ -108,3 +95,17 @@ declare_mlir_python_sources(
10895
_mlir_libs/_standaloneDialectsNanobind/__init__.pyi
10996
_mlir_libs/_standaloneDialectsNanobind/standalone.pyi
11097
)
98+
99+
add_mlir_python_modules(StandalonePythonModules
100+
ROOT_PREFIX "${StandalonePythonModules_ROOT_PREFIX}"
101+
INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}"
102+
DECLARED_SOURCES
103+
StandalonePythonSources
104+
# TODO: Remove this in favor of showing fine grained registration once
105+
# available.
106+
MLIRPythonExtension.RegisterEverything
107+
MLIRPythonSources.Core
108+
MLIRPythonSources.Dialects.builtin
109+
COMMON_CAPI_LINK_LIBS
110+
StandalonePythonCAPI
111+
)

mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ NB_MODULE(_standaloneDialectsNanobind, m) {
3030
mlirDialectHandleLoadDialect(handle, context);
3131
}
3232
},
33+
// clang-format off
34+
nb::sig("def register_dialect(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None"));
35+
// clang-format on
3336
nb::arg("context").none() = nb::none(), nb::arg("load") = true);
3437
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,13 @@ class mlir_attribute_subclass : public pure_subclass {
512512
.attr("replace")(superCls.attr("__name__"), captureTypeName);
513513
});
514514
if (getTypeIDFunction) {
515-
def_staticmethod("get_static_typeid",
516-
[getTypeIDFunction]() { return getTypeIDFunction(); });
515+
def_staticmethod(
516+
"get_static_typeid",
517+
[getTypeIDFunction]() { return getTypeIDFunction(); },
518+
// clang-format off
519+
nanobind::sig("def get_static_typeid() -> " MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID"))
520+
// clang-format on
521+
);
517522
nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
518523
.attr(MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR)(
519524
getTypeIDFunction())(nanobind::cpp_function(
@@ -582,8 +587,9 @@ class mlir_type_subclass : public pure_subclass {
582587

583588
// 'isinstance' method.
584589
static const char kIsinstanceSig[] =
585-
"def isinstance(other_type: " MAKE_MLIR_PYTHON_QUALNAME(
586-
"ir") ".Type) -> bool";
590+
// clang-format off
591+
"def isinstance(other_type: " MAKE_MLIR_PYTHON_QUALNAME("ir.Type") ") -> bool";
592+
// clang-format on
587593
def_staticmethod(
588594
"isinstance",
589595
[isaFunction](MlirType other) { return isaFunction(other); },
@@ -599,8 +605,13 @@ class mlir_type_subclass : public pure_subclass {
599605
// `def_property_readonly_static` is not available in `pure_subclass` and
600606
// we do not want to introduce the complexity that pybind uses to
601607
// implement it.
602-
def_staticmethod("get_static_typeid",
603-
[getTypeIDFunction]() { return getTypeIDFunction(); });
608+
def_staticmethod(
609+
"get_static_typeid",
610+
[getTypeIDFunction]() { return getTypeIDFunction(); },
611+
// clang-format off
612+
nanobind::sig("def get_static_typeid() -> " MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID"))
613+
// clang-format on
614+
);
604615
nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
605616
.attr(MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR)(
606617
getTypeIDFunction())(nanobind::cpp_function(
@@ -665,8 +676,9 @@ class mlir_value_subclass : public pure_subclass {
665676

666677
// 'isinstance' method.
667678
static const char kIsinstanceSig[] =
668-
"def isinstance(other_value: " MAKE_MLIR_PYTHON_QUALNAME(
669-
"ir") ".Value) -> bool";
679+
// clang-format off
680+
"def isinstance(other_value: " MAKE_MLIR_PYTHON_QUALNAME("ir.Value") ") -> bool";
681+
// clang-format on
670682
def_staticmethod(
671683
"isinstance",
672684
[isaFunction](MlirValue other) { return isaFunction(other); },

mlir/lib/Bindings/Python/DialectPDL.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ static void populateDialectPDLSubmodule(const nanobind::module_ &m) {
6868
rangeType.def_property_readonly(
6969
"element_type",
7070
[](MlirType type) { return mlirPDLRangeTypeGetElementType(type); },
71-
nb::sig("def element_type(self) -> Type"), "Get the element type.");
71+
nb::sig(
72+
"def element_type(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Type")),
73+
"Get the element type.");
7274

7375
//===-------------------------------------------------------------------===//
7476
// TypeType

mlir/lib/Bindings/Python/IRAttributes.cpp

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ struct nb_buffer_info {
167167
};
168168

169169
class nb_buffer : public nb::object {
170-
NB_OBJECT_DEFAULT(nb_buffer, object, "buffer", PyObject_CheckBuffer);
170+
NB_OBJECT_DEFAULT(nb_buffer, object, "Buffer", PyObject_CheckBuffer);
171171

172172
nb_buffer_info request() const {
173173
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
@@ -252,9 +252,12 @@ class PyAffineMapAttribute : public PyConcreteAttribute<PyAffineMapAttribute> {
252252
return PyAffineMapAttribute(affineMap.getContext(), attr);
253253
},
254254
nb::arg("affine_map"), "Gets an attribute wrapping an AffineMap.");
255-
c.def_prop_ro("value", mlirAffineMapAttrGetValue,
256-
nb::sig("def value(self) -> AffineMap"),
257-
"Returns the value of the AffineMap attribute");
255+
c.def_prop_ro(
256+
"value", mlirAffineMapAttrGetValue,
257+
// clang-format off
258+
nb::sig("def value(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.AffineMap")),
259+
// clang-format on
260+
"Returns the value of the AffineMap attribute");
258261
}
259262
};
260263

@@ -492,7 +495,9 @@ class PyArrayAttribute : public PyConcreteAttribute<PyArrayAttribute> {
492495
nb::class_<PyArrayAttributeIterator>(m, "ArrayAttributeIterator")
493496
.def("__iter__", &PyArrayAttributeIterator::dunderIter)
494497
.def("__next__", &PyArrayAttributeIterator::dunderNext,
495-
nb::sig("def __next__(self) -> Attribute"));
498+
// clang-format off
499+
nb::sig("def __next__(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute")));
500+
// clang-format on
496501
}
497502

498503
private:
@@ -526,7 +531,9 @@ class PyArrayAttribute : public PyConcreteAttribute<PyArrayAttribute> {
526531
throw nb::index_error("ArrayAttribute index out of range");
527532
return arr.getItem(i);
528533
},
529-
nb::sig("def __getitem__(self, arg: int, /) -> Attribute"))
534+
// clang-format off
535+
nb::sig("def __getitem__(self, arg: int, /) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute")))
536+
// clang-format on
530537
.def("__len__",
531538
[](const PyArrayAttribute &arr) {
532539
return mlirArrayAttrGetNumElements(arr);
@@ -620,7 +627,9 @@ class PyIntegerAttribute : public PyConcreteAttribute<PyIntegerAttribute> {
620627
[](nb::object & /*class*/) -> MlirTypeID {
621628
return mlirIntegerAttrGetTypeID();
622629
},
623-
nb::sig("def static_typeid(/) -> TypeID"));
630+
// clang-format off
631+
nb::sig("def static_typeid(/) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID")));
632+
// clang-format on
624633
}
625634

626635
private:
@@ -679,16 +688,18 @@ class PySymbolRefAttribute : public PyConcreteAttribute<PySymbolRefAttribute> {
679688
}
680689

681690
static void bindDerived(ClassTy &c) {
682-
c.def_static(
683-
"get",
684-
[](const std::vector<std::string> &symbols,
685-
DefaultingPyMlirContext context) {
686-
return PySymbolRefAttribute::fromList(symbols, context.resolve());
687-
},
688-
nb::arg("symbols"), nb::arg("context") = nb::none(),
689-
nb::sig("def get(symbols: Sequence[str], context: mlir.ir.Context | "
690-
"None = None) -> Attribute"),
691-
"Gets a uniqued SymbolRef attribute from a list of symbol names");
691+
c
692+
.def_static(
693+
"get",
694+
[](const std::vector<std::string> &symbols,
695+
DefaultingPyMlirContext context) {
696+
return PySymbolRefAttribute::fromList(symbols, context.resolve());
697+
},
698+
nb::arg("symbols"), nb::arg("context") = nb::none(),
699+
// clang-format off
700+
nb::sig("def get(symbols: Sequence[str], context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") " | None = None) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute")),
701+
// clang-format on
702+
"Gets a uniqued SymbolRef attribute from a list of symbol names");
692703
c.def_prop_ro(
693704
"value",
694705
[](PySymbolRefAttribute &self) {
@@ -754,7 +765,11 @@ class PyOpaqueAttribute : public PyConcreteAttribute<PyOpaqueAttribute> {
754765
return PyOpaqueAttribute(context->getRef(), attr);
755766
},
756767
nb::arg("dialect_namespace"), nb::arg("buffer"), nb::arg("type"),
757-
nb::arg("context") = nb::none(), "Gets an Opaque attribute.");
768+
nb::arg("context") = nb::none(),
769+
// clang-format off
770+
nb::sig("def get(dialect_namespace: str, buffer: typing_extensions.Buffer, type: " MAKE_MLIR_PYTHON_QUALNAME("ir.Type") ", context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") " | None = None) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.OpaqueAttr")),
771+
// clang-format on
772+
"Gets an Opaque attribute.");
758773
c.def_prop_ro(
759774
"dialect_namespace",
760775
[](PyOpaqueAttribute &self) {
@@ -1040,6 +1055,9 @@ class PyDenseElementsAttribute
10401055
nb::arg("array"), nb::arg("signless") = true,
10411056
nb::arg("type") = nb::none(), nb::arg("shape") = nb::none(),
10421057
nb::arg("context") = nb::none(),
1058+
// clang-format off
1059+
nb::sig("def get(array: typing_extensions.Buffer, signless: bool = True, type: " MAKE_MLIR_PYTHON_QUALNAME("ir.Type") " | None = None, shape: Sequence[int] | None = None, context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") " | None = None) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.DenseElementsAttr")),
1060+
// clang-format on
10431061
kDenseElementsAttrGetDocstring)
10441062
.def_static("get", PyDenseElementsAttribute::getFromList,
10451063
nb::arg("attrs"), nb::arg("type") = nb::none(),
@@ -1060,7 +1078,9 @@ class PyDenseElementsAttribute
10601078
"get_splat_value called on a non-splat attribute");
10611079
return mlirDenseElementsAttrGetSplatValue(self);
10621080
},
1063-
nb::sig("def get_splat_value(self) -> Attribute"));
1081+
// clang-format off
1082+
nb::sig("def get_splat_value(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute")));
1083+
// clang-format on
10641084
}
10651085

10661086
static PyType_Slot slots[];
@@ -1520,6 +1540,9 @@ class PyDenseResourceElementsAttribute
15201540
nb::arg("array"), nb::arg("name"), nb::arg("type"),
15211541
nb::arg("alignment") = nb::none(),
15221542
nb::arg("is_mutable") = false, nb::arg("context") = nb::none(),
1543+
// clang-format off
1544+
nb::sig("def get_from_buffer(array: typing_extensions.Buffer, name: str, type: " MAKE_MLIR_PYTHON_QUALNAME("ir.Type") ", alignment: int | None = None, is_mutable: bool = False, context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") " | None = None) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.DenseResourceElementsAttr")),
1545+
// clang-format on
15231546
kDenseResourceElementsAttrGetFromBufferDocstring);
15241547
}
15251548
};
@@ -1571,7 +1594,9 @@ class PyDictAttribute : public PyConcreteAttribute<PyDictAttribute> {
15711594
throw nb::key_error("attempt to access a non-existent attribute");
15721595
return attr;
15731596
},
1574-
nb::sig("def __getitem__(self, arg: str, /) -> Attribute"));
1597+
// clang-format off
1598+
nb::sig("def __getitem__(self, arg: str, /) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute")));
1599+
// clang-format on
15751600
c.def("__getitem__", [](PyDictAttribute &self, intptr_t index) {
15761601
if (index < 0 || index >= self.dunderLen()) {
15771602
throw nb::index_error("attempt to access out of bounds attribute");
@@ -1640,7 +1665,7 @@ class PyTypeAttribute : public PyConcreteAttribute<PyTypeAttribute> {
16401665
c.def_prop_ro(
16411666
"value",
16421667
[](PyTypeAttribute &self) { return mlirTypeAttrGetValue(self.get()); },
1643-
nb::sig("def value(self) -> Type"));
1668+
nb::sig("def value(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Type")));
16441669
}
16451670
};
16461671

@@ -1777,6 +1802,7 @@ nb::object symbolRefOrFlatSymbolRefAttributeCaster(PyAttribute &pyAttribute) {
17771802
} // namespace
17781803

17791804
void mlir::python::populateIRAttributes(nb::module_ &m) {
1805+
17801806
PyAffineMapAttribute::bind(m);
17811807
PyDenseBoolArrayAttribute::bind(m);
17821808
PyDenseBoolArrayAttribute::PyDenseArrayIterator::bind(m);

0 commit comments

Comments
 (0)