Debian Codegraph @

llvm-project/mlir/lib/Bindings/Python/IRTypes.cpp

raw
1 //===- IRTypes.cpp - Exports builtin and standard types -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "IRModule.h"
10
11 #include "PybindUtils.h"
12
13 #include "mlir-c/BuiltinAttributes.h"
14 #include "mlir-c/BuiltinTypes.h"
15 #include <optional>
16
17 namespace py = pybind11;
18 using namespace mlir;
19 using namespace mlir::python;
20
21 using llvm::SmallVector;
22 using llvm::Twine;
23
24 namespace {
25
26 /// Checks whether the given type is an integer or float type.
27 static int mlirTypeIsAIntegerOrFloat(MlirType type) {
28 return mlirTypeIsAInteger(type) || mlirTypeIsABF16(type) ||
29 mlirTypeIsAF16(type) || mlirTypeIsAF32(type) || mlirTypeIsAF64(type);
30 }
31
32 class PyIntegerType : public PyConcreteType<PyIntegerType> {
33 public:
34 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAInteger;
35 static constexpr const char *pyClassName = "IntegerType";
36 using PyConcreteType::PyConcreteType;
37
38 static void bindDerived(ClassTy &c) {
39 c.def_static(
40 "get_signless",
41 [](unsigned width, DefaultingPyMlirContext context) {
42 MlirType t = mlirIntegerTypeGet(context->get(), width);
43 return PyIntegerType(context->getRef(), t);
44 },
45 py::arg("width"), py::arg("context") = py::none(),
46 "Create a signless integer type");
47 c.def_static(
48 "get_signed",
49 [](unsigned width, DefaultingPyMlirContext context) {
50 MlirType t = mlirIntegerTypeSignedGet(context->get(), width);
51 return PyIntegerType(context->getRef(), t);
52 },
53 py::arg("width"), py::arg("context") = py::none(),
54 "Create a signed integer type");
55 c.def_static(
56 "get_unsigned",
57 [](unsigned width, DefaultingPyMlirContext context) {
58 MlirType t = mlirIntegerTypeUnsignedGet(context->get(), width);
59 return PyIntegerType(context->getRef(), t);
60 },
61 py::arg("width"), py::arg("context") = py::none(),
62 "Create an unsigned integer type");
63 c.def_property_readonly(
64 "width",
65 [](PyIntegerType &self) { return mlirIntegerTypeGetWidth(self); },
66 "Returns the width of the integer type");
67 c.def_property_readonly(
68 "is_signless",
69 [](PyIntegerType &self) -> bool {
70 return mlirIntegerTypeIsSignless(self);
71 },
72 "Returns whether this is a signless integer");
73 c.def_property_readonly(
74 "is_signed",
75 [](PyIntegerType &self) -> bool {
76 return mlirIntegerTypeIsSigned(self);
77 },
78 "Returns whether this is a signed integer");
79 c.def_property_readonly(
80 "is_unsigned",
81 [](PyIntegerType &self) -> bool {
82 return mlirIntegerTypeIsUnsigned(self);
83 },
84 "Returns whether this is an unsigned integer");
85 }
86 };
87
88 /// Index Type subclass - IndexType.
89 class PyIndexType : public PyConcreteType<PyIndexType> {
90 public:
91 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAIndex;
92 static constexpr const char *pyClassName = "IndexType";
93 using PyConcreteType::PyConcreteType;
94
95 static void bindDerived(ClassTy &c) {
96 c.def_static(
97 "get",
98 [](DefaultingPyMlirContext context) {
99 MlirType t = mlirIndexTypeGet(context->get());
100 return PyIndexType(context->getRef(), t);
101 },
102 py::arg("context") = py::none(), "Create a index type.");
103 }
104 };
105
106 /// Floating Point Type subclass - Float8E4M3FNType.
107 class PyFloat8E4M3FNType : public PyConcreteType<PyFloat8E4M3FNType> {
108 public:
109 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E4M3FN;
110 static constexpr const char *pyClassName = "Float8E4M3FNType";
111 using PyConcreteType::PyConcreteType;
112
113 static void bindDerived(ClassTy &c) {
114 c.def_static(
115 "get",
116 [](DefaultingPyMlirContext context) {
117 MlirType t = mlirFloat8E4M3FNTypeGet(context->get());
118 return PyFloat8E4M3FNType(context->getRef(), t);
119 },
120 py::arg("context") = py::none(), "Create a float8_e4m3fn type.");
121 }
122 };
123
124 /// Floating Point Type subclass - Float8M5E2Type.
125 class PyFloat8E5M2Type : public PyConcreteType<PyFloat8E5M2Type> {
126 public:
127 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E5M2;
128 static constexpr const char *pyClassName = "Float8E5M2Type";
129 using PyConcreteType::PyConcreteType;
130
131 static void bindDerived(ClassTy &c) {
132 c.def_static(
133 "get",
134 [](DefaultingPyMlirContext context) {
135 MlirType t = mlirFloat8E5M2TypeGet(context->get());
136 return PyFloat8E5M2Type(context->getRef(), t);
137 },
138 py::arg("context") = py::none(), "Create a float8_e5m2 type.");
139 }
140 };
141
142 /// Floating Point Type subclass - BF16Type.
143 class PyBF16Type : public PyConcreteType<PyBF16Type> {
144 public:
145 static constexpr IsAFunctionTy isaFunction = mlirTypeIsABF16;
146 static constexpr const char *pyClassName = "BF16Type";
147 using PyConcreteType::PyConcreteType;
148
149 static void bindDerived(ClassTy &c) {
150 c.def_static(
151 "get",
152 [](DefaultingPyMlirContext context) {
153 MlirType t = mlirBF16TypeGet(context->get());
154 return PyBF16Type(context->getRef(), t);
155 },
156 py::arg("context") = py::none(), "Create a bf16 type.");
157 }
158 };
159
160 /// Floating Point Type subclass - F16Type.
161 class PyF16Type : public PyConcreteType<PyF16Type> {
162 public:
163 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF16;
164 static constexpr const char *pyClassName = "F16Type";
165 using PyConcreteType::PyConcreteType;
166
167 static void bindDerived(ClassTy &c) {
168 c.def_static(
169 "get",
170 [](DefaultingPyMlirContext context) {
171 MlirType t = mlirF16TypeGet(context->get());
172 return PyF16Type(context->getRef(), t);
173 },
174 py::arg("context") = py::none(), "Create a f16 type.");
175 }
176 };
177
178 /// Floating Point Type subclass - F32Type.
179 class PyF32Type : public PyConcreteType<PyF32Type> {
180 public:
181 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF32;
182 static constexpr const char *pyClassName = "F32Type";
183 using PyConcreteType::PyConcreteType;
184
185 static void bindDerived(ClassTy &c) {
186 c.def_static(
187 "get",
188 [](DefaultingPyMlirContext context) {
189 MlirType t = mlirF32TypeGet(context->get());
190 return PyF32Type(context->getRef(), t);
191 },
192 py::arg("context") = py::none(), "Create a f32 type.");
193 }
194 };
195
196 /// Floating Point Type subclass - F64Type.
197 class PyF64Type : public PyConcreteType<PyF64Type> {
198 public:
199 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAF64;
200 static constexpr const char *pyClassName = "F64Type";
201 using PyConcreteType::PyConcreteType;
202
203 static void bindDerived(ClassTy &c) {
204 c.def_static(
205 "get",
206 [](DefaultingPyMlirContext context) {
207 MlirType t = mlirF64TypeGet(context->get());
208 return PyF64Type(context->getRef(), t);
209 },
210 py::arg("context") = py::none(), "Create a f64 type.");
211 }
212 };
213
214 /// None Type subclass - NoneType.
215 class PyNoneType : public PyConcreteType<PyNoneType> {
216 public:
217 static constexpr IsAFunctionTy isaFunction = mlirTypeIsANone;
218 static constexpr const char *pyClassName = "NoneType";
219 using PyConcreteType::PyConcreteType;
220
221 static void bindDerived(ClassTy &c) {
222 c.def_static(
223 "get",
224 [](DefaultingPyMlirContext context) {
225 MlirType t = mlirNoneTypeGet(context->get());
226 return PyNoneType(context->getRef(), t);
227 },
228 py::arg("context") = py::none(), "Create a none type.");
229 }
230 };
231
232 /// Complex Type subclass - ComplexType.
233 class PyComplexType : public PyConcreteType<PyComplexType> {
234 public:
235 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAComplex;
236 static constexpr const char *pyClassName = "ComplexType";
237 using PyConcreteType::PyConcreteType;
238
239 static void bindDerived(ClassTy &c) {
240 c.def_static(
241 "get",
242 [](PyType &elementType) {
243 // The element must be a floating point or integer scalar type.
244 if (mlirTypeIsAIntegerOrFloat(elementType)) {
245 MlirType t = mlirComplexTypeGet(elementType);
246 return PyComplexType(elementType.getContext(), t);
247 }
248 throw SetPyError(
249 PyExc_ValueError,
250 Twine("invalid '") +
251 py::repr(py::cast(elementType)).cast<std::string>() +
252 "' and expected floating point or integer type.");
253 },
254 "Create a complex type");
255 c.def_property_readonly(
256 "element_type",
257 [](PyComplexType &self) -> PyType {
258 MlirType t = mlirComplexTypeGetElementType(self);
259 return PyType(self.getContext(), t);
260 },
261 "Returns element type.");
262 }
263 };
264
265 class PyShapedType : public PyConcreteType<PyShapedType> {
266 public:
267 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAShaped;
268 static constexpr const char *pyClassName = "ShapedType";
269 using PyConcreteType::PyConcreteType;
270
271 static void bindDerived(ClassTy &c) {
272 c.def_property_readonly(
273 "element_type",
274 [](PyShapedType &self) {
275 MlirType t = mlirShapedTypeGetElementType(self);
276 return PyType(self.getContext(), t);
277 },
278 "Returns the element type of the shaped type.");
279 c.def_property_readonly(
280 "has_rank",
281 [](PyShapedType &self) -> bool { return mlirShapedTypeHasRank(self); },
282 "Returns whether the given shaped type is ranked.");
283 c.def_property_readonly(
284 "rank",
285 [](PyShapedType &self) {
286 self.requireHasRank();
287 return mlirShapedTypeGetRank(self);
288 },
289 "Returns the rank of the given ranked shaped type.");
290 c.def_property_readonly(
291 "has_static_shape",
292 [](PyShapedType &self) -> bool {
293 return mlirShapedTypeHasStaticShape(self);
294 },
295 "Returns whether the given shaped type has a static shape.");
296 c.def(
297 "is_dynamic_dim",
298 [](PyShapedType &self, intptr_t dim) -> bool {
299 self.requireHasRank();
300 return mlirShapedTypeIsDynamicDim(self, dim);
301 },
302 py::arg("dim"),
303 "Returns whether the dim-th dimension of the given shaped type is "
304 "dynamic.");
305 c.def(
306 "get_dim_size",
307 [](PyShapedType &self, intptr_t dim) {
308 self.requireHasRank();
309 return mlirShapedTypeGetDimSize(self, dim);
310 },
311 py::arg("dim"),
312 "Returns the dim-th dimension of the given ranked shaped type.");
313 c.def_static(
314 "is_dynamic_size",
315 [](int64_t size) -> bool { return mlirShapedTypeIsDynamicSize(size); },
316 py::arg("dim_size"),
317 "Returns whether the given dimension size indicates a dynamic "
318 "dimension.");
319 c.def(
320 "is_dynamic_stride_or_offset",
321 [](PyShapedType &self, int64_t val) -> bool {
322 self.requireHasRank();
323 return mlirShapedTypeIsDynamicStrideOrOffset(val);
324 },
325 py::arg("dim_size"),
326 "Returns whether the given value is used as a placeholder for dynamic "
327 "strides and offsets in shaped types.");
328 c.def_property_readonly(
329 "shape",
330 [](PyShapedType &self) {
331 self.requireHasRank();
332
333 std::vector<int64_t> shape;
334 int64_t rank = mlirShapedTypeGetRank(self);
335 shape.reserve(rank);
336 for (int64_t i = 0; i < rank; ++i)
337 shape.push_back(mlirShapedTypeGetDimSize(self, i));
338 return shape;
339 },
340 "Returns the shape of the ranked shaped type as a list of integers.");
341 c.def_static(
342 "get_dynamic_size", []() { return mlirShapedTypeGetDynamicSize(); },
343 "Returns the value used to indicate dynamic dimensions in shaped "
344 "types.");
345 c.def_static(
346 "get_dynamic_stride_or_offset",
347 []() { return mlirShapedTypeGetDynamicStrideOrOffset(); },
348 "Returns the value used to indicate dynamic strides or offsets in "
349 "shaped types.");
350 }
351
352 private:
353 void requireHasRank() {
354 if (!mlirShapedTypeHasRank(*this)) {
355 throw SetPyError(
356 PyExc_ValueError,
357 "calling this method requires that the type has a rank.");
358 }
359 }
360 };
361
362 /// Vector Type subclass - VectorType.
363 class PyVectorType : public PyConcreteType<PyVectorType, PyShapedType> {
364 public:
365 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAVector;
366 static constexpr const char *pyClassName = "VectorType";
367 using PyConcreteType::PyConcreteType;
368
369 static void bindDerived(ClassTy &c) {
370 c.def_static(
371 "get",
372 [](std::vector<int64_t> shape, PyType &elementType,
373 DefaultingPyLocation loc) {
374 MlirType t = mlirVectorTypeGetChecked(loc, shape.size(), shape.data(),
375 elementType);
376 // TODO: Rework error reporting once diagnostic engine is exposed
377 // in C API.
378 if (mlirTypeIsNull(t)) {
379 throw SetPyError(
380 PyExc_ValueError,
381 Twine("invalid '") +
382 py::repr(py::cast(elementType)).cast<std::string>() +
383 "' and expected floating point or integer type.");
384 }
385 return PyVectorType(elementType.getContext(), t);
386 },
387 py::arg("shape"), py::arg("elementType"), py::arg("loc") = py::none(),
388 "Create a vector type");
389 }
390 };
391
392 /// Ranked Tensor Type subclass - RankedTensorType.
393 class PyRankedTensorType
394 : public PyConcreteType<PyRankedTensorType, PyShapedType> {
395 public:
396 static constexpr IsAFunctionTy isaFunction = mlirTypeIsARankedTensor;
397 static constexpr const char *pyClassName = "RankedTensorType";
398 using PyConcreteType::PyConcreteType;
399
400 static void bindDerived(ClassTy &c) {
401 c.def_static(
402 "get",
403 [](std::vector<int64_t> shape, PyType &elementType,
404 std::optional<PyAttribute> &encodingAttr, DefaultingPyLocation loc) {
405 MlirType t = mlirRankedTensorTypeGetChecked(
406 loc, shape.size(), shape.data(), elementType,
407 encodingAttr ? encodingAttr->get() : mlirAttributeGetNull());
408 // TODO: Rework error reporting once diagnostic engine is exposed
409 // in C API.
410 if (mlirTypeIsNull(t)) {
411 throw SetPyError(
412 PyExc_ValueError,
413 Twine("invalid '") +
414 py::repr(py::cast(elementType)).cast<std::string>() +
415 "' and expected floating point, integer, vector or "
416 "complex "
417 "type.");
418 }
419 return PyRankedTensorType(elementType.getContext(), t);
420 },
421 py::arg("shape"), py::arg("element_type"),
422 py::arg("encoding") = py::none(), py::arg("loc") = py::none(),
423 "Create a ranked tensor type");
424 c.def_property_readonly(
425 "encoding", [](PyRankedTensorType &self) -> std::optional<PyAttribute> {
426 MlirAttribute encoding = mlirRankedTensorTypeGetEncoding(self.get());
427 if (mlirAttributeIsNull(encoding))
428 return std::nullopt;
429 return PyAttribute(self.getContext(), encoding);
430 });
431 }
432 };
433
434 /// Unranked Tensor Type subclass - UnrankedTensorType.
435 class PyUnrankedTensorType
436 : public PyConcreteType<PyUnrankedTensorType, PyShapedType> {
437 public:
438 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAUnrankedTensor;
439 static constexpr const char *pyClassName = "UnrankedTensorType";
440 using PyConcreteType::PyConcreteType;
441
442 static void bindDerived(ClassTy &c) {
443 c.def_static(
444 "get",
445 [](PyType &elementType, DefaultingPyLocation loc) {
446 MlirType t = mlirUnrankedTensorTypeGetChecked(loc, elementType);
447 // TODO: Rework error reporting once diagnostic engine is exposed
448 // in C API.
449 if (mlirTypeIsNull(t)) {
450 throw SetPyError(
451 PyExc_ValueError,
452 Twine("invalid '") +
453 py::repr(py::cast(elementType)).cast<std::string>() +
454 "' and expected floating point, integer, vector or "
455 "complex "
456 "type.");
457 }
458 return PyUnrankedTensorType(elementType.getContext(), t);
459 },
460 py::arg("element_type"), py::arg("loc") = py::none(),
461 "Create a unranked tensor type");
462 }
463 };
464
465 /// Ranked MemRef Type subclass - MemRefType.
466 class PyMemRefType : public PyConcreteType<PyMemRefType, PyShapedType> {
467 public:
468 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAMemRef;
469 static constexpr const char *pyClassName = "MemRefType";
470 using PyConcreteType::PyConcreteType;
471
472 static void bindDerived(ClassTy &c) {
473 c.def_static(
474 "get",
475 [](std::vector<int64_t> shape, PyType &elementType,
476 PyAttribute *layout, PyAttribute *memorySpace,
477 DefaultingPyLocation loc) {
478 MlirAttribute layoutAttr = layout ? *layout : mlirAttributeGetNull();
479 MlirAttribute memSpaceAttr =
480 memorySpace ? *memorySpace : mlirAttributeGetNull();
481 MlirType t =
482 mlirMemRefTypeGetChecked(loc, elementType, shape.size(),
483 shape.data(), layoutAttr, memSpaceAttr);
484 // TODO: Rework error reporting once diagnostic engine is exposed
485 // in C API.
486 if (mlirTypeIsNull(t)) {
487 throw SetPyError(
488 PyExc_ValueError,
489 Twine("invalid '") +
490 py::repr(py::cast(elementType)).cast<std::string>() +
491 "' and expected floating point, integer, vector or "
492 "complex "
493 "type.");
494 }
495 return PyMemRefType(elementType.getContext(), t);
496 },
497 py::arg("shape"), py::arg("element_type"),
498 py::arg("layout") = py::none(), py::arg("memory_space") = py::none(),
499 py::arg("loc") = py::none(), "Create a memref type")
500 .def_property_readonly(
501 "layout",
502 [](PyMemRefType &self) -> PyAttribute {
503 MlirAttribute layout = mlirMemRefTypeGetLayout(self);
504 return PyAttribute(self.getContext(), layout);
505 },
506 "The layout of the MemRef type.")
507 .def_property_readonly(
508 "affine_map",
509 [](PyMemRefType &self) -> PyAffineMap {
510 MlirAffineMap map = mlirMemRefTypeGetAffineMap(self);
511 return PyAffineMap(self.getContext(), map);
512 },
513 "The layout of the MemRef type as an affine map.")
514 .def_property_readonly(
515 "memory_space",
516 [](PyMemRefType &self) -> PyAttribute {
517 MlirAttribute a = mlirMemRefTypeGetMemorySpace(self);
518 return PyAttribute(self.getContext(), a);
519 },
520 "Returns the memory space of the given MemRef type.");
521 }
522 };
523
524 /// Unranked MemRef Type subclass - UnrankedMemRefType.
525 class PyUnrankedMemRefType
526 : public PyConcreteType<PyUnrankedMemRefType, PyShapedType> {
527 public:
528 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAUnrankedMemRef;
529 static constexpr const char *pyClassName = "UnrankedMemRefType";
530 using PyConcreteType::PyConcreteType;
531
532 static void bindDerived(ClassTy &c) {
533 c.def_static(
534 "get",
535 [](PyType &elementType, PyAttribute *memorySpace,
536 DefaultingPyLocation loc) {
537 MlirAttribute memSpaceAttr = {};
538 if (memorySpace)
539 memSpaceAttr = *memorySpace;
540
541 MlirType t =
542 mlirUnrankedMemRefTypeGetChecked(loc, elementType, memSpaceAttr);
543 // TODO: Rework error reporting once diagnostic engine is exposed
544 // in C API.
545 if (mlirTypeIsNull(t)) {
546 throw SetPyError(
547 PyExc_ValueError,
548 Twine("invalid '") +
549 py::repr(py::cast(elementType)).cast<std::string>() +
550 "' and expected floating point, integer, vector or "
551 "complex "
552 "type.");
553 }
554 return PyUnrankedMemRefType(elementType.getContext(), t);
555 },
556 py::arg("element_type"), py::arg("memory_space"),
557 py::arg("loc") = py::none(), "Create a unranked memref type")
558 .def_property_readonly(
559 "memory_space",
560 [](PyUnrankedMemRefType &self) -> PyAttribute {
561 MlirAttribute a = mlirMemRefTypeGetMemorySpace(self);
562 return PyAttribute(self.getContext(), a);
563 },
564 "Returns the memory space of the given Unranked MemRef type.");
565 }
566 };
567
568 /// Tuple Type subclass - TupleType.
569 class PyTupleType : public PyConcreteType<PyTupleType> {
570 public:
571 static constexpr IsAFunctionTy isaFunction = mlirTypeIsATuple;
572 static constexpr const char *pyClassName = "TupleType";
573 using PyConcreteType::PyConcreteType;
574
575 static void bindDerived(ClassTy &c) {
576 c.def_static(
577 "get_tuple",
578 [](py::list elementList, DefaultingPyMlirContext context) {
579 intptr_t num = py::len(elementList);
580 // Mapping py::list to SmallVector.
581 SmallVector<MlirType, 4> elements;
582 for (auto element : elementList)
583 elements.push_back(element.cast<PyType>());
584 MlirType t = mlirTupleTypeGet(context->get(), num, elements.data());
585 return PyTupleType(context->getRef(), t);
586 },
587 py::arg("elements"), py::arg("context") = py::none(),
588 "Create a tuple type");
589 c.def(
590 "get_type",
591 [](PyTupleType &self, intptr_t pos) -> PyType {
592 MlirType t = mlirTupleTypeGetType(self, pos);
593 return PyType(self.getContext(), t);
594 },
595 py::arg("pos"), "Returns the pos-th type in the tuple type.");
596 c.def_property_readonly(
597 "num_types",
598 [](PyTupleType &self) -> intptr_t {
599 return mlirTupleTypeGetNumTypes(self);
600 },
601 "Returns the number of types contained in a tuple.");
602 }
603 };
604
605 /// Function type.
606 class PyFunctionType : public PyConcreteType<PyFunctionType> {
607 public:
608 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFunction;
609 static constexpr const char *pyClassName = "FunctionType";
610 using PyConcreteType::PyConcreteType;
611
612 static void bindDerived(ClassTy &c) {
613 c.def_static(
614 "get",
615 [](std::vector<PyType> inputs, std::vector<PyType> results,
616 DefaultingPyMlirContext context) {
617 SmallVector<MlirType, 4> inputsRaw(inputs.begin(), inputs.end());
618 SmallVector<MlirType, 4> resultsRaw(results.begin(), results.end());
619 MlirType t = mlirFunctionTypeGet(context->get(), inputsRaw.size(),
620 inputsRaw.data(), resultsRaw.size(),
621 resultsRaw.data());
622 return PyFunctionType(context->getRef(), t);
623 },
624 py::arg("inputs"), py::arg("results"), py::arg("context") = py::none(),
625 "Gets a FunctionType from a list of input and result types");
626 c.def_property_readonly(
627 "inputs",
628 [](PyFunctionType &self) {
629 MlirType t = self;
630 auto contextRef = self.getContext();
631 py::list types;
632 for (intptr_t i = 0, e = mlirFunctionTypeGetNumInputs(self); i < e;
633 ++i) {
634 types.append(PyType(contextRef, mlirFunctionTypeGetInput(t, i)));
635 }
636 return types;
637 },
638 "Returns the list of input types in the FunctionType.");
639 c.def_property_readonly(
640 "results",
641 [](PyFunctionType &self) {
642 auto contextRef = self.getContext();
643 py::list types;
644 for (intptr_t i = 0, e = mlirFunctionTypeGetNumResults(self); i < e;
645 ++i) {
646 types.append(
647 PyType(contextRef, mlirFunctionTypeGetResult(self, i)));
648 }
649 return types;
650 },
651 "Returns the list of result types in the FunctionType.");
652 }
653 };
654
655 static MlirStringRef toMlirStringRef(const std::string &s) {
656 return mlirStringRefCreate(s.data(), s.size());
657 }
658
659 /// Opaque Type subclass - OpaqueType.
660 class PyOpaqueType : public PyConcreteType<PyOpaqueType> {
661 public:
662 static constexpr IsAFunctionTy isaFunction = mlirTypeIsAOpaque;
663 static constexpr const char *pyClassName = "OpaqueType";
664 using PyConcreteType::PyConcreteType;
665
666 static void bindDerived(ClassTy &c) {
667 c.def_static(
668 "get",
669 [](std::string dialectNamespace, std::string typeData,
670 DefaultingPyMlirContext context) {
671 MlirType type = mlirOpaqueTypeGet(context->get(),
672 toMlirStringRef(dialectNamespace),
673 toMlirStringRef(typeData));
674 return PyOpaqueType(context->getRef(), type);
675 },
676 py::arg("dialect_namespace"), py::arg("buffer"),
677 py::arg("context") = py::none(),
678 "Create an unregistered (opaque) dialect type.");
679 c.def_property_readonly(
680 "dialect_namespace",
681 [](PyOpaqueType &self) {
682 MlirStringRef stringRef = mlirOpaqueTypeGetDialectNamespace(self);
683 return py::str(stringRef.data, stringRef.length);
684 },
685 "Returns the dialect namespace for the Opaque type as a string.");
686 c.def_property_readonly(
687 "data",
688 [](PyOpaqueType &self) {
689 MlirStringRef stringRef = mlirOpaqueTypeGetData(self);
690 return py::str(stringRef.data, stringRef.length);
691 },
692 "Returns the data for the Opaque type as a string.");
693 }
694 };
695
696 } // namespace
697
698 void mlir::python::populateIRTypes(py::module &m) {
699 PyIntegerType::bind(m);
700 PyIndexType::bind(m);
701 PyFloat8E4M3FNType::bind(m);
702 PyFloat8E5M2Type::bind(m);
703 PyBF16Type::bind(m);
704 PyF16Type::bind(m);
705 PyF32Type::bind(m);
706 PyF64Type::bind(m);
707 PyNoneType::bind(m);
708 PyComplexType::bind(m);
709 PyShapedType::bind(m);
710 PyVectorType::bind(m);
711 PyRankedTensorType::bind(m);
712 PyUnrankedTensorType::bind(m);
713 PyMemRefType::bind(m);
714 PyUnrankedMemRefType::bind(m);
715 PyTupleType::bind(m);
716 PyFunctionType::bind(m);
717 PyOpaqueType::bind(m);
718 }