|
25 | 25 | #include <utility> |
26 | 26 |
|
27 | 27 | namespace py = pybind11; |
| 28 | +using namespace py::literals; |
28 | 29 | using namespace mlir; |
29 | 30 | using namespace mlir::python; |
30 | 31 |
|
@@ -2121,13 +2122,12 @@ class PyOpResult : public PyConcreteValue<PyOpResult> { |
2121 | 2122 |
|
2122 | 2123 | /// Returns the list of types of the values held by container. |
2123 | 2124 | template <typename Container> |
2124 | | -static std::vector<PyType> getValueTypes(Container &container, |
2125 | | - PyMlirContextRef &context) { |
2126 | | - std::vector<PyType> result; |
| 2125 | +static std::vector<MlirType> getValueTypes(Container &container, |
| 2126 | + PyMlirContextRef &context) { |
| 2127 | + std::vector<MlirType> result; |
2127 | 2128 | result.reserve(container.size()); |
2128 | 2129 | for (int i = 0, e = container.size(); i < e; ++i) { |
2129 | | - result.push_back( |
2130 | | - PyType(context, mlirValueGetType(container.getElement(i).get()))); |
| 2130 | + result.push_back(mlirValueGetType(container.getElement(i).get())); |
2131 | 2131 | } |
2132 | 2132 | return result; |
2133 | 2133 | } |
@@ -3148,11 +3148,8 @@ void mlir::python::populateIRCore(py::module &m) { |
3148 | 3148 | "context", |
3149 | 3149 | [](PyAttribute &self) { return self.getContext().getObject(); }, |
3150 | 3150 | "Context that owns the Attribute") |
3151 | | - .def_property_readonly("type", |
3152 | | - [](PyAttribute &self) { |
3153 | | - return PyType(self.getContext()->getRef(), |
3154 | | - mlirAttributeGetType(self)); |
3155 | | - }) |
| 3151 | + .def_property_readonly( |
| 3152 | + "type", [](PyAttribute &self) { return mlirAttributeGetType(self); }) |
3156 | 3153 | .def( |
3157 | 3154 | "get_named", |
3158 | 3155 | [](PyAttribute &self, std::string name) { |
@@ -3247,7 +3244,7 @@ void mlir::python::populateIRCore(py::module &m) { |
3247 | 3244 | mlirTypeParseGet(context->get(), toMlirStringRef(typeSpec)); |
3248 | 3245 | if (mlirTypeIsNull(type)) |
3249 | 3246 | throw MLIRError("Unable to parse type", errors.take()); |
3250 | | - return PyType(context->getRef(), type); |
| 3247 | + return type; |
3251 | 3248 | }, |
3252 | 3249 | py::arg("asm"), py::arg("context") = py::none(), |
3253 | 3250 | kContextParseTypeDocstring) |
@@ -3284,6 +3281,18 @@ void mlir::python::populateIRCore(py::module &m) { |
3284 | 3281 | printAccum.parts.append(")"); |
3285 | 3282 | return printAccum.join(); |
3286 | 3283 | }) |
| 3284 | + .def(MLIR_PYTHON_MAYBE_DOWNCAST_ATTR, |
| 3285 | + [](PyType &self) { |
| 3286 | + MlirTypeID mlirTypeID = mlirTypeGetTypeID(self); |
| 3287 | + assert(!mlirTypeIDIsNull(mlirTypeID) && |
| 3288 | + "mlirTypeID was expected to be non-null."); |
| 3289 | + std::optional<pybind11::function> typeCaster = |
| 3290 | + PyGlobals::get().lookupTypeCaster(mlirTypeID, |
| 3291 | + mlirTypeGetDialect(self)); |
| 3292 | + if (!typeCaster) |
| 3293 | + return py::cast(self); |
| 3294 | + return typeCaster.value()(self); |
| 3295 | + }) |
3287 | 3296 | .def_property_readonly("typeid", [](PyType &self) -> MlirTypeID { |
3288 | 3297 | MlirTypeID mlirTypeID = mlirTypeGetTypeID(self); |
3289 | 3298 | if (!mlirTypeIDIsNull(mlirTypeID)) |
@@ -3387,12 +3396,8 @@ void mlir::python::populateIRCore(py::module &m) { |
3387 | 3396 | return printAccum.join(); |
3388 | 3397 | }, |
3389 | 3398 | py::arg("use_local_scope") = false, kGetNameAsOperand) |
3390 | | - .def_property_readonly("type", |
3391 | | - [](PyValue &self) { |
3392 | | - return PyType( |
3393 | | - self.getParentOperation()->getContext(), |
3394 | | - mlirValueGetType(self.get())); |
3395 | | - }) |
| 3399 | + .def_property_readonly( |
| 3400 | + "type", [](PyValue &self) { return mlirValueGetType(self.get()); }) |
3396 | 3401 | .def( |
3397 | 3402 | "replace_all_uses_with", |
3398 | 3403 | [](PyValue &self, PyValue &with) { |
|
0 commit comments