diff --git a/mlir/include/mlir-c/Bindings/Python/Interop.h b/mlir/include/mlir-c/Bindings/Python/Interop.h --- a/mlir/include/mlir-c/Bindings/Python/Interop.h +++ b/mlir/include/mlir-c/Bindings/Python/Interop.h @@ -80,6 +80,8 @@ #define MLIR_PYTHON_CAPSULE_PASS_MANAGER \ MAKE_MLIR_PYTHON_QUALNAME("passmanager.PassManager._CAPIPtr") #define MLIR_PYTHON_CAPSULE_VALUE MAKE_MLIR_PYTHON_QUALNAME("ir.Value._CAPIPtr") +#define MLIR_PYTHON_CAPSULE_TYPEID \ + MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID._CAPIPtr") /** Attribute on MLIR Python objects that expose their C-API pointer. * This will be a type-specific capsule created as per one of the helpers @@ -287,6 +289,17 @@ return type; } +static inline PyObject *mlirPythonTypeIDToCapsule(MlirTypeID typeID) { + return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(typeID), + MLIR_PYTHON_CAPSULE_TYPEID, NULL); +} + +static inline MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule) { + void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_TYPEID); + MlirTypeID typeID = {ptr}; + return typeID; +} + /** Creates a capsule object encapsulating the raw C-API MlirAffineMap. * The returned capsule does not extend or affect ownership of any Python * objects that reference the type in any way. diff --git a/mlir/include/mlir-c/BuiltinTypes.h b/mlir/include/mlir-c/BuiltinTypes.h --- a/mlir/include/mlir-c/BuiltinTypes.h +++ b/mlir/include/mlir-c/BuiltinTypes.h @@ -18,6 +18,36 @@ extern "C" { #endif +#define FORALL_CONCRETETYPES(_) \ + _(BFloat16Type) \ + _(ComplexType) \ + _(Float8E4M3B11FNUZType) \ + _(Float8E4M3FNType) \ + _(Float8E4M3FNUZType) \ + _(Float8E5M2Type) \ + _(Float8E5M2FNUZType) \ + _(Float16Type) \ + _(Float32Type) \ + _(Float64Type) \ + _(Float80Type) \ + _(Float128Type) \ + _(FunctionType) \ + _(IndexType) \ + _(IntegerType) \ + _(MemRefType) \ + _(NoneType) \ + _(OpaqueType) \ + _(RankedTensorType) \ + _(TupleType) \ + _(UnrankedMemRefType) \ + _(UnrankedTensorType) \ + _(VectorType) + +#define DECLARETYPEID(TYPE) \ + MLIR_CAPI_EXPORTED MlirTypeID mlir##TYPE##GetTypeID(); +FORALL_CONCRETETYPES(DECLARETYPEID) +#undef DECLARETYPEID + //===----------------------------------------------------------------------===// // Integer types. //===----------------------------------------------------------------------===// @@ -396,4 +426,6 @@ } #endif +#undef TypeID + #endif // MLIR_C_BUILTINTYPES_H diff --git a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h --- a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h +++ b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h @@ -236,6 +236,27 @@ } }; +/// Casts object <-> MlirTypeID. +template <> +struct type_caster { + PYBIND11_TYPE_CASTER(MlirTypeID, _("MlirTypeID")); + bool load(handle src, bool) { + py::object capsule = mlirApiObjectToCapsule(src); + value = mlirPythonCapsuleToTypeID(capsule.ptr()); + return !mlirTypeIDIsNull(value); + } + static handle cast(MlirTypeID v, return_value_policy, handle) { + if (v.ptr == nullptr) + return py::none(); + py::object capsule = + py::reinterpret_steal(mlirPythonTypeIDToCapsule(v)); + return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir")) + .attr("TypeID") + .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule) + .release(); + }; +}; + /// Casts object <-> MlirType. template <> struct type_caster { @@ -248,13 +269,22 @@ static handle cast(MlirType t, return_value_policy, handle) { py::object capsule = py::reinterpret_steal(mlirPythonTypeToCapsule(t)); + + MlirTypeID mlirTypeId = mlirTypeGetTypeID(t); + auto globals = py::module::import(MAKE_MLIR_PYTHON_QUALNAME("dialects")) + .attr("_ods_common") + .attr("_cext") + .attr("globals"); + py::object typeBuilder = globals.attr("lookupTypeCaster")(mlirTypeId); + if (!typeBuilder.is_none()) + return typeBuilder.cast()(capsule).release(); + return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir")) .attr("Type") .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule) .release(); } }; - } // namespace detail } // namespace pybind11 diff --git a/mlir/lib/Bindings/Python/Globals.h b/mlir/lib/Bindings/Python/Globals.h --- a/mlir/lib/Bindings/Python/Globals.h +++ b/mlir/lib/Bindings/Python/Globals.h @@ -9,12 +9,14 @@ #ifndef MLIR_BINDINGS_PYTHON_GLOBALS_H #define MLIR_BINDINGS_PYTHON_GLOBALS_H +#include #include #include -#include #include "PybindUtils.h" +#include "mlir-c/IR.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" @@ -64,6 +66,9 @@ void registerAttributeBuilder(const std::string &attributeKind, pybind11::function pyFunc); + void registerTypeCaster(MlirTypeID mlirTypeId, pybind11::function typeFunc, + const std::string &typeName); + /// Adds a concrete implementation dialect class. /// Raises an exception if the mapping already exists. /// This is intended to be called by implementation code. @@ -80,6 +85,8 @@ std::optional lookupAttributeBuilder(const std::string &attributeKind); + std::optional lookupTypeCaster(MlirTypeID mlirTypeId); + /// Looks up a registered dialect class by namespace. Note that this may /// trigger loading of the defining module and can arbitrarily re-enter. std::optional @@ -102,6 +109,13 @@ /// Map of attribute ODS name to custom builder. llvm::StringMap attributeBuilderMap; + std::unordered_map< + MlirTypeID, pybind11::object, + std::integral_constant, + std::integral_constant> + typeCasterMap; + /// Set of dialect namespaces that we have attempted to import implementation /// modules for. llvm::StringSet<> loadedDialectModulesCache; diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp --- a/mlir/lib/Bindings/Python/IRAttributes.cpp +++ b/mlir/lib/Bindings/Python/IRAttributes.cpp @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include #include +#include #include "IRModule.h" @@ -15,6 +15,7 @@ #include "mlir-c/BuiltinAttributes.h" #include "mlir-c/BuiltinTypes.h" +#include "mlir/Bindings/Python/PybindAdaptors.h" namespace py = pybind11; using namespace mlir; @@ -1028,8 +1029,7 @@ py::arg("value"), py::arg("context") = py::none(), "Gets a uniqued Type attribute"); c.def_property_readonly("value", [](PyTypeAttribute &self) { - return PyType(self.getContext()->getRef(), - mlirTypeAttrGetValue(self.get())); + return mlirTypeAttrGetValue(self.get()); }); } }; diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -17,6 +17,7 @@ #include "mlir-c/Diagnostics.h" #include "mlir-c/IR.h" #include "mlir-c/Support.h" +#include "mlir/Bindings/Python/PybindAdaptors.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" @@ -1807,6 +1808,17 @@ rawType); } +py::object PyTypeID::getCapsule() { + return py::reinterpret_steal(mlirPythonTypeIDToCapsule(*this)); +} + +PyTypeID PyTypeID::createFromCapsule(py::object capsule) { + MlirTypeID rawRegistry = mlirPythonCapsuleToTypeID(capsule.ptr()); + if (mlirTypeIDIsNull(rawRegistry)) + throw py::error_already_set(); + return PyTypeID(rawRegistry); +} + //------------------------------------------------------------------------------ // PyValue and subclases. //------------------------------------------------------------------------------ @@ -2085,13 +2097,12 @@ /// Returns the list of types of the values held by container. template -static std::vector getValueTypes(Container &container, - PyMlirContextRef &context) { - std::vector result; +static std::vector getValueTypes(Container &container, + PyMlirContextRef &context) { + std::vector result; result.reserve(container.size()); for (int i = 0, e = container.size(); i < e; ++i) { - result.push_back( - PyType(context, mlirValueGetType(container.getElement(i).get()))); + result.push_back(mlirValueGetType(container.getElement(i).get())); } return result; } @@ -3145,11 +3156,8 @@ "context", [](PyAttribute &self) { return self.getContext().getObject(); }, "Context that owns the Attribute") - .def_property_readonly("type", - [](PyAttribute &self) { - return PyType(self.getContext()->getRef(), - mlirAttributeGetType(self)); - }) + .def_property_readonly( + "type", [](PyAttribute &self) { return mlirAttributeGetType(self); }) .def( "get_named", [](PyAttribute &self, std::string name) { @@ -3244,7 +3252,7 @@ mlirTypeParseGet(context->get(), toMlirStringRef(typeSpec)); if (mlirTypeIsNull(type)) throw MLIRError("Unable to parse type", errors.take()); - return PyType(context->getRef(), type); + return type; }, py::arg("asm"), py::arg("context") = py::none(), kContextParseTypeDocstring) @@ -3280,6 +3288,10 @@ return printAccum.join(); }); + py::class_(m, "TypeID", py::module_local()) + .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyTypeID::getCapsule) + .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyTypeID::createFromCapsule); + //---------------------------------------------------------------------------- // Mapping of Value. //---------------------------------------------------------------------------- @@ -3353,12 +3365,8 @@ return printAccum.join(); }, py::arg("use_local_scope") = false, kGetNameAsOperand) - .def_property_readonly("type", - [](PyValue &self) { - return PyType( - self.getParentOperation()->getContext(), - mlirValueGetType(self.get())); - }) + .def_property_readonly( + "type", [](PyValue &self) { return mlirValueGetType(self.get()); }) .def( "replace_all_uses_with", [](PyValue &self, PyValue &with) { diff --git a/mlir/lib/Bindings/Python/IRInterfaces.cpp b/mlir/lib/Bindings/Python/IRInterfaces.cpp --- a/mlir/lib/Bindings/Python/IRInterfaces.cpp +++ b/mlir/lib/Bindings/Python/IRInterfaces.cpp @@ -321,11 +321,7 @@ py::module_local()) .def_property_readonly( "element_type", - [](PyShapedTypeComponents &self) { - return PyType(PyMlirContext::forContext( - mlirTypeGetContext(self.elementType)), - self.elementType); - }, + [](PyShapedTypeComponents &self) { return self.elementType; }, "Returns the element type of the shaped type components.") .def_static( "get", diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -13,10 +13,12 @@ #include #include +#include "Globals.h" #include "PybindUtils.h" #include "mlir-c/AffineExpr.h" #include "mlir-c/AffineMap.h" +#include "mlir-c/Bindings/Python/Interop.h" #include "mlir-c/Diagnostics.h" #include "mlir-c/IR.h" #include "mlir-c/IntegerSet.h" @@ -826,6 +828,18 @@ MlirType type; }; +class PyTypeID { +public: + PyTypeID(MlirTypeID typeID) : typeID(typeID) {} + pybind11::object getCapsule(); + static PyTypeID createFromCapsule(pybind11::object capsule); + operator MlirTypeID() const { return typeID; } + MlirTypeID get() { return typeID; } + +private: + MlirTypeID typeID; +}; + /// CRTP base classes for Python types that subclass Type and should be /// castable from it (i.e. via something like IntegerType(t)). /// By default, type class hierarchies are one level deep (i.e. a @@ -839,6 +853,8 @@ // const char *pyClassName using ClassTy = pybind11::class_; using IsAFunctionTy = bool (*)(MlirType); + using GetTypeIDFunctionTy = MlirTypeID (*)(); + static constexpr GetTypeIDFunctionTy getTypeIdFunction = nullptr; PyConcreteType() = default; PyConcreteType(PyMlirContextRef contextRef, MlirType t) @@ -866,6 +882,25 @@ return DerivedTy::isaFunction(otherType); }, pybind11::arg("other")); + cls.def("__repr__", [](DerivedTy &self) { + PyPrintAccumulator printAccum; + printAccum.parts.append(DerivedTy::pyClassName); + printAccum.parts.append("("); + mlirTypePrint(self, printAccum.getCallback(), printAccum.getUserData()); + printAccum.parts.append(")"); + return printAccum.join(); + }); + + if (DerivedTy::getTypeIdFunction) { + PyGlobals::get().registerTypeCaster( + DerivedTy::getTypeIdFunction(), + pybind11::cpp_function([](pybind11::object capsule) -> DerivedTy { + auto pyt = PyType::createFromCapsule(std::move(capsule)); + return pyt; + }), + DerivedTy::pyClassName); + } + DerivedTy::bindDerived(cls); } @@ -960,9 +995,8 @@ return DerivedTy::isaFunction(otherAttr); }, pybind11::arg("other")); - cls.def_property_readonly("type", [](PyAttribute &attr) { - return PyType(attr.getContext(), mlirAttributeGetType(attr)); - }); + cls.def_property_readonly( + "type", [](PyAttribute &attr) { return mlirAttributeGetType(attr); }); DerivedTy::bindDerived(cls); } @@ -1141,12 +1175,15 @@ namespace pybind11 { namespace detail { +using namespace mlir::python; +namespace py = pybind11; + template <> -struct type_caster - : MlirDefaultingCaster {}; +struct type_caster + : MlirDefaultingCaster {}; template <> -struct type_caster - : MlirDefaultingCaster {}; +struct type_caster + : MlirDefaultingCaster {}; } // namespace detail } // namespace pybind11 diff --git a/mlir/lib/Bindings/Python/IRModule.cpp b/mlir/lib/Bindings/Python/IRModule.cpp --- a/mlir/lib/Bindings/Python/IRModule.cpp +++ b/mlir/lib/Bindings/Python/IRModule.cpp @@ -10,8 +10,8 @@ #include "Globals.h" #include "PybindUtils.h" -#include #include +#include #include "mlir-c/Bindings/Python/Interop.h" @@ -72,6 +72,18 @@ found = std::move(pyFunc); } +void PyGlobals::registerTypeCaster(MlirTypeID mlirTypeId, + pybind11::function typeFunc, + const std::string &typeName) { + pybind11::object &found = typeCasterMap[mlirTypeId]; + if (found) { + throw std::runtime_error((llvm::Twine("Type caster for '") + typeName + + "' is already registered") + .str()); + } + found = std::move(typeFunc); +} + void PyGlobals::registerDialectImpl(const std::string &dialectNamespace, py::object pyClass) { py::object &found = dialectClassMap[dialectNamespace]; @@ -110,6 +122,22 @@ return std::nullopt; } +std::optional +PyGlobals::lookupTypeCaster(MlirTypeID mlirTypeId) { + // Fast match against the class map first (common case). + const auto foundIt = typeCasterMap.find(mlirTypeId); + if (foundIt != typeCasterMap.end()) { + if (foundIt->second.is_none()) + return std::nullopt; + assert(foundIt->second && "py::function is defined"); + return foundIt->second; + } + + // Not found and loading did not yield a registration. Negative cache. + typeCasterMap[mlirTypeId] = py::none(); + return std::nullopt; +} + std::optional PyGlobals::lookupDialectClass(const std::string &dialectNamespace) { loadDialectModule(dialectNamespace); diff --git a/mlir/lib/Bindings/Python/IRTypes.cpp b/mlir/lib/Bindings/Python/IRTypes.cpp --- a/mlir/lib/Bindings/Python/IRTypes.cpp +++ b/mlir/lib/Bindings/Python/IRTypes.cpp @@ -12,6 +12,8 @@ #include "mlir-c/BuiltinAttributes.h" #include "mlir-c/BuiltinTypes.h" +#include "mlir/Bindings/Python/PybindAdaptors.h" + #include namespace py = pybind11; @@ -33,6 +35,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAInteger; static constexpr const char *pyClassName = "IntegerType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirIntegerTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -90,6 +94,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAIndex; static constexpr const char *pyClassName = "IndexType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirIndexTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -108,6 +114,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E4M3FN; static constexpr const char *pyClassName = "Float8E4M3FNType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat8E4M3FNTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -126,6 +134,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E5M2; static constexpr const char *pyClassName = "Float8E5M2Type"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat8E5M2TypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -144,6 +154,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E4M3FNUZ; static constexpr const char *pyClassName = "Float8E4M3FNUZType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat8E4M3FNUZTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -162,6 +174,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E4M3B11FNUZ; static constexpr const char *pyClassName = "Float8E4M3B11FNUZType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat8E4M3B11FNUZTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -180,6 +194,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E5M2FNUZ; static constexpr const char *pyClassName = "Float8E5M2FNUZType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat8E5M2FNUZTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -198,6 +214,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsABF16; static constexpr const char *pyClassName = "BF16Type"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirBFloat16TypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -216,6 +234,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF16; static constexpr const char *pyClassName = "F16Type"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat16TypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -234,6 +254,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF32; static constexpr const char *pyClassName = "F32Type"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat32TypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -252,6 +274,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF64; static constexpr const char *pyClassName = "F64Type"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFloat64TypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -270,6 +294,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsANone; static constexpr const char *pyClassName = "NoneType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirNoneTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -288,6 +314,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAComplex; static constexpr const char *pyClassName = "ComplexType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirComplexTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -308,10 +336,7 @@ "Create a complex type"); c.def_property_readonly( "element_type", - [](PyComplexType &self) -> PyType { - MlirType t = mlirComplexTypeGetElementType(self); - return PyType(self.getContext(), t); - }, + [](PyComplexType &self) { return mlirComplexTypeGetElementType(self); }, "Returns element type."); } }; @@ -325,10 +350,7 @@ static void bindDerived(ClassTy &c) { c.def_property_readonly( "element_type", - [](PyShapedType &self) { - MlirType t = mlirShapedTypeGetElementType(self); - return PyType(self.getContext(), t); - }, + [](PyShapedType &self) { return mlirShapedTypeGetElementType(self); }, "Returns the element type of the shaped type."); c.def_property_readonly( "has_rank", @@ -418,6 +440,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAVector; static constexpr const char *pyClassName = "VectorType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirVectorTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -443,6 +467,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsARankedTensor; static constexpr const char *pyClassName = "RankedTensorType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirRankedTensorTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -477,6 +503,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAUnrankedTensor; static constexpr const char *pyClassName = "UnrankedTensorType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirUnrankedTensorTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -499,6 +527,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAMemRef; static constexpr const char *pyClassName = "MemRefType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirMemRefTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -551,6 +581,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAUnrankedMemRef; static constexpr const char *pyClassName = "UnrankedMemRefType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirUnrankedMemRefTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -586,6 +618,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsATuple; static constexpr const char *pyClassName = "TupleType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirTupleTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -604,9 +638,8 @@ "Create a tuple type"); c.def( "get_type", - [](PyTupleType &self, intptr_t pos) -> PyType { - MlirType t = mlirTupleTypeGetType(self, pos); - return PyType(self.getContext(), t); + [](PyTupleType &self, intptr_t pos) { + return mlirTupleTypeGetType(self, pos); }, py::arg("pos"), "Returns the pos-th type in the tuple type."); c.def_property_readonly( @@ -623,6 +656,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFunction; static constexpr const char *pyClassName = "FunctionType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirFunctionTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { @@ -647,7 +682,7 @@ py::list types; for (intptr_t i = 0, e = mlirFunctionTypeGetNumInputs(self); i < e; ++i) { - types.append(PyType(contextRef, mlirFunctionTypeGetInput(t, i))); + types.append(mlirFunctionTypeGetInput(t, i)); } return types; }, @@ -659,8 +694,7 @@ py::list types; for (intptr_t i = 0, e = mlirFunctionTypeGetNumResults(self); i < e; ++i) { - types.append( - PyType(contextRef, mlirFunctionTypeGetResult(self, i))); + types.append(mlirFunctionTypeGetResult(self, i)); } return types; }, @@ -677,6 +711,8 @@ public: static constexpr IsAFunctionTy isaFunction = mlirTypeIsAOpaque; static constexpr const char *pyClassName = "OpaqueType"; + static constexpr GetTypeIDFunctionTy getTypeIdFunction = + &mlirOpaqueTypeGetTypeID; using PyConcreteType::PyConcreteType; static void bindDerived(ClassTy &c) { diff --git a/mlir/lib/Bindings/Python/MainModule.cpp b/mlir/lib/Bindings/Python/MainModule.cpp --- a/mlir/lib/Bindings/Python/MainModule.cpp +++ b/mlir/lib/Bindings/Python/MainModule.cpp @@ -14,6 +14,8 @@ #include "IRModule.h" #include "Pass.h" +#include "mlir/Bindings/Python/PybindAdaptors.h" + namespace py = pybind11; using namespace mlir; using namespace mlir::python; @@ -41,6 +43,8 @@ "Testing hook for directly registering a dialect") .def("_register_operation_impl", &PyGlobals::registerOperationImpl, py::arg("operation_name"), py::arg("operation_class"), + "Testing hook for directly registering an operation") + .def("lookupTypeCaster", &PyGlobals::lookupTypeCaster, py::arg("type_id"), "Testing hook for directly registering an operation"); // Aside from making the globals accessible to python, having python manage diff --git a/mlir/lib/CAPI/IR/BuiltinTypes.cpp b/mlir/lib/CAPI/IR/BuiltinTypes.cpp --- a/mlir/lib/CAPI/IR/BuiltinTypes.cpp +++ b/mlir/lib/CAPI/IR/BuiltinTypes.cpp @@ -18,6 +18,36 @@ using namespace mlir; +#define FORALL_CONCRETETYPES(_) \ + _(BFloat16Type) \ + _(ComplexType) \ + _(Float8E4M3B11FNUZType) \ + _(Float8E4M3FNType) \ + _(Float8E4M3FNUZType) \ + _(Float8E5M2Type) \ + _(Float8E5M2FNUZType) \ + _(Float16Type) \ + _(Float32Type) \ + _(Float64Type) \ + _(Float80Type) \ + _(Float128Type) \ + _(FunctionType) \ + _(IndexType) \ + _(IntegerType) \ + _(MemRefType) \ + _(NoneType) \ + _(OpaqueType) \ + _(RankedTensorType) \ + _(TupleType) \ + _(UnrankedMemRefType) \ + _(UnrankedTensorType) \ + _(VectorType) + +#define DEFINETYPEID(TYPE) \ + MlirTypeID mlir##TYPE##GetTypeID() { return wrap(TYPE::getTypeID()); } +FORALL_CONCRETETYPES(DEFINETYPEID) +#undef DEFINETYPEID + //===----------------------------------------------------------------------===// // Integer types. //===----------------------------------------------------------------------===// diff --git a/mlir/test/python/ir/attributes.py b/mlir/test/python/ir/attributes.py --- a/mlir/test/python/ir/attributes.py +++ b/mlir/test/python/ir/attributes.py @@ -557,3 +557,57 @@ print(f"rank: {len(attr.strides)}") # CHECK: strides are dynamic: [True, True, True] print(f"strides are dynamic: {[s == dynamic for s in attr.strides]}") + + +# CHECK-LABEL: TEST: testConcreteTypesRoundTrip +@run +def testConcreteTypesRoundTrip(): + with Context(), Location.unknown(): + def print_item(attr): + print(repr(attr.type)) + + # CHECK: F32Type(f32) + print_item(Attribute.parse("42.0 : f32")) + # CHECK: F32Type(f32) + print_item(FloatAttr.get_f32(42.0)) + # CHECK: F64Type(f64) + print_item(FloatAttr.get_f64(42.0)) + # CHECK: IntegerType(i32) + print_item(IntegerAttr.get(IntegerType.get_signless(32), 42)) + # CHECK: IntegerType(i64) + print_item(IntegerAttr.get(IntegerType.get_signless(64), 42)) + + def print_container_item(attr_asm): + attr = DenseElementsAttr(Attribute.parse(attr_asm)) + print(repr(attr.type)) + print(repr(attr.type.element_type)) + + # CHECK: RankedTensorType(tensor) + # CHECK: IntegerType(i16) + print_container_item("dense<123> : tensor") + # CHECK: RankedTensorType(tensor) + # CHECK: IntegerType(i32) + print_container_item("dense<123> : tensor") + # CHECK: RankedTensorType(tensor) + # CHECK: IntegerType(i64) + print_container_item("dense<123> : tensor") + + # CHECK: RankedTensorType(tensor) + # CHECK: F16Type(f16) + print_container_item("dense<1.0> : tensor") + # CHECK: RankedTensorType(tensor) + # CHECK: F32Type(f32) + print_container_item("dense<1.0> : tensor") + # CHECK: RankedTensorType(tensor) + # CHECK: F64Type(f64) + print_container_item("dense<1.0> : tensor") + + raw = Attribute.parse("vector<4xf32>") + # CHECK: attr: vector<4xf32> + print("attr:", raw) + type_attr = TypeAttr(raw) + + # CHECK: VectorType(vector<4xf32>) + print(repr(type_attr.value)) + # CHECK: F32Type(f32) + print(repr(type_attr.value.element_type)) diff --git a/mlir/test/python/ir/builtin_types.py b/mlir/test/python/ir/builtin_types.py --- a/mlir/test/python/ir/builtin_types.py +++ b/mlir/test/python/ir/builtin_types.py @@ -2,6 +2,7 @@ import gc from mlir.ir import * +from mlir.dialects import arith, tensor, func, memref def run(f): print("\nTEST:", f.__name__) @@ -380,15 +381,15 @@ f32 = F32Type.get() shape = [2, 3] loc = Location.unknown() - memref = MemRefType.get(shape, f32, memory_space=Attribute.parse("2")) + memref_f32 = MemRefType.get(shape, f32, memory_space=Attribute.parse("2")) # CHECK: memref type: memref<2x3xf32, 2> - print("memref type:", memref) + print("memref type:", memref_f32) # CHECK: memref layout: affine_map<(d0, d1) -> (d0, d1)> - print("memref layout:", memref.layout) + print("memref layout:", memref_f32.layout) # CHECK: memref affine map: (d0, d1) -> (d0, d1) - print("memref affine map:", memref.affine_map) + print("memref affine map:", memref_f32.affine_map) # CHECK: memory space: 2 - print("memory space:", memref.memory_space) + print("memory space:", memref_f32.memory_space) layout = AffineMapAttr.get(AffineMap.get_permutation([1, 0])) memref_layout = MemRefType.get(shape, f32, layout=layout) @@ -411,7 +412,7 @@ else: print("Exception not produced") - assert memref.shape == shape + assert memref_f32.shape == shape # CHECK-LABEL: TEST: testUnrankedMemRefType @@ -481,9 +482,9 @@ IntegerType.get_signless(16)] result_types = [IndexType.get()] func = FunctionType.get(input_types, result_types) - # CHECK: INPUTS: [Type(i32), Type(i16)] + # CHECK: INPUTS: [IntegerType(i32), IntegerType(i16)] print("INPUTS:", func.inputs) - # CHECK: RESULTS: [Type(index)] + # CHECK: RESULTS: [IndexType(index)] print("RESULTS:", func.results) @@ -509,3 +510,122 @@ print(type(ShapedType.get_dynamic_size())) # CHECK: print(type(ShapedType.get_dynamic_stride_or_offset())) + + +# CHECK-LABEL: TEST: testConcreteTypesRoundTrip +@run +def testConcreteTypesRoundTrip(): + with Context() as ctx, Location.unknown(): + ctx.allow_unregistered_dialects = True + def print_item(typ, v): + cst = arith.ConstantOp(typ, v).result + print(type(cst.type).__name__) + print(repr(cst.type)) + + # CHECK: F16Type + # CHECK: F16Type(f16) + print_item(F16Type.get(), 0.0) + # CHECK: F32Type + # CHECK: F32Type(f32) + print_item(F32Type.get(), 0.0) + # CHECK: F64Type + # CHECK: F64Type(f64) + print_item(F64Type.get(), 0.0) + # CHECK: Float8E4M3B11FNUZType + # CHECK: Float8E4M3B11FNUZType(f8E4M3B11FNUZ) + print_item(Float8E4M3B11FNUZType.get(), 0.0) + # CHECK: Float8E4M3FNType + # CHECK: Float8E4M3FNType(f8E4M3FN) + print_item(Float8E4M3FNType.get(), 0.0) + # CHECK: Float8E4M3FNUZType + # CHECK: Float8E4M3FNUZType(f8E4M3FNUZ) + print_item(Float8E4M3FNUZType.get(), 0.0) + # CHECK: Float8E5M2Type + # CHECK: Float8E5M2Type(f8E5M2) + print_item(Float8E5M2Type.get(), 0.0) + # CHECK: Float8E5M2FNUZType + # CHECK: Float8E5M2FNUZType(f8E5M2FNUZ) + print_item(Float8E5M2FNUZType.get(), 0.0) + # CHECK: BF16Type + # CHECK: BF16Type(bf16) + print_item(BF16Type.get(), 0.0) + # CHECK: IndexType + # CHECK: IndexType(index) + print_item(IndexType.get(), 0) + # CHECK: IntegerType + # CHECK: IntegerType(i32) + print_item(IntegerType.get_signless(32), 0) + + f32 = F32Type.get() + ranked_tensor = tensor.EmptyOp([10, 10], f32).result + # CHECK: RankedTensorType + print(type(ranked_tensor.type).__name__) + # CHECK: RankedTensorType(tensor<10x10xf32>) + print(repr(ranked_tensor.type)) + + cf32 = ComplexType.get(f32) + # CHECK: ComplexType + print(type(cf32).__name__) + # CHECK: ComplexType(complex) + print(repr(cf32)) + + ranked_tensor = tensor.EmptyOp([10, 10], f32).result + # CHECK: RankedTensorType + print(type(ranked_tensor.type).__name__) + # CHECK: RankedTensorType(tensor<10x10xf32>) + print(repr(ranked_tensor.type)) + + vector = VectorType.get([10, 10], f32) + tuple_type = TupleType.get_tuple([f32, vector]) + # CHECK: TupleType + print(type(tuple_type).__name__) + # CHECK: TupleType(tuple>) + print(repr(tuple_type)) + # CHECK: F32Type(f32) + print(repr(tuple_type.get_type(0))) + # CHECK: VectorType(vector<10x10xf32>) + print(repr(tuple_type.get_type(1))) + + index_type = IndexType.get() + @func.FuncOp.from_py_func() + def default_builder(): + c0 = arith.ConstantOp(f32, 0.0) + unranked_tensor_type = UnrankedTensorType.get(f32) + unranked_tensor = tensor.FromElementsOp(unranked_tensor_type, [c0]).result + # CHECK: UnrankedTensorType + print(type(unranked_tensor.type).__name__) + # CHECK: UnrankedTensorType(tensor<*xf32>) + print(repr(unranked_tensor.type)) + + c10 = arith.ConstantOp(index_type, 10) + memref_f32_t = MemRefType.get([10, 10], f32) + memref_f32 = memref.AllocOp(memref_f32_t, [c10, c10], []).result + # CHECK: MemRefType + print(type(memref_f32.type).__name__) + # CHECK: MemRefType(memref<10x10xf32>) + print(repr(memref_f32.type)) + + unranked_memref_t = UnrankedMemRefType.get(f32, Attribute.parse("2")) + memref_f32 = memref.AllocOp(unranked_memref_t, [c10, c10], []).result + # CHECK: UnrankedMemRefType + print(type(memref_f32.type).__name__) + # CHECK: UnrankedMemRefType(memref<*xf32, 2>) + print(repr(memref_f32.type)) + + tuple_type = Operation.parse(f'"test.make_tuple"() : () -> tuple').result + # CHECK: TupleType + print(type(tuple_type.type).__name__) + # CHECK: TupleType(tuple) + print(repr(tuple_type.type)) + + return c0, c10 + + func_op = default_builder.func_op + # CHECK: FunctionType + print(type(func_op.type).__name__) + # CHECK: FunctionType(() -> (f32, index)) + print(repr(func_op.type)) + # CHECK: [] + print(func_op.type.inputs) + # CHECK: [F32Type(f32), IndexType(index)] + print(func_op.type.results)