import sys
import typing
from typing import Union, Optional
from mlir.ir import *
import mlir.dialects.func as func
import mlir.dialects.python_test as test
import mlir.dialects.tensor as tensor
import mlir.dialects.arith as arith
if sys.argv[1] == "pybind11":
from mlir._mlir_libs._mlirPythonTestPybind11 import (
TestAttr,
TestType,
TestTensorValue,
TestIntegerRankedTensorType,
)
test.register_python_test_dialect(get_dialect_registry(), use_nanobind=False)
elif sys.argv[1] == "nanobind":
from mlir._mlir_libs._mlirPythonTestNanobind import (
TestAttr,
TestType,
TestTensorValue,
TestIntegerRankedTensorType,
)
test.register_python_test_dialect(get_dialect_registry(), use_nanobind=True)
else:
raise ValueError("Expected pybind11 or nanobind as argument")
def run(f):
print("\nTEST:", f.__name__)
f()
return f
@run
def testAttributes():
with Context() as ctx, Location.unknown():
i32 = IntegerType.get_signless(32)
one = IntegerAttr.get(i32, 1)
two = IntegerAttr.get(i32, 2)
unit = UnitAttr.get()
op = test.AttributedOp(one, optional_i32=two, unit=unit)
print(f"{op}")
op2 = test.AttributedOp(two)
print(f"{op2}")
assert "additional" not in op.attributes
op2.attributes["additional"] = one
print(f"{op2}")
op2.attributes["additional"] = two
print(f"{op2}")
del op2.attributes["additional"]
print(f"{op2}")
try:
print(op.attributes["additional"])
except KeyError:
pass
else:
assert False, "expected KeyError on unknown attribute key"
print(f"Mandatory: {op.mandatory_i32.value}")
print(f"Optional: {op.optional_i32.value}")
print(f"Unit: {op.unit}")
print(f"Mandatory: {op2.mandatory_i32.value}")
print(f"Optional: {op2.optional_i32}")
print(f"Unit: {op2.unit}")
op.mandatory_i32 = two
op.optional_i32 = None
op.unit = False
print(f"Mandatory: {op.mandatory_i32.value}")
print(f"Optional: {op.optional_i32}")
print(f"Unit: {op.unit}")
assert "optional_i32" not in op.attributes
assert "unit" not in op.attributes
try:
op.mandatory_i32 = None
except ValueError:
pass
else:
assert False, "expected ValueError on setting a mandatory attribute to None"
op.optional_i32 = two
print(f"Optional: {op.optional_i32.value}")
del op.optional_i32
print(f"Optional: {op.optional_i32}")
op.unit = None
print(f"Unit: {op.unit}")
assert "unit" not in op.attributes
op.unit = True
print(f"Unit: {op.unit}")
del op.unit
print(f"Unit: {op.unit}")
@run
def attrBuilder():
with Context() as ctx, Location.unknown():
op = test.AttributesOp(
x_affinemap=AffineMap.get_constant(2),
x_affinemaparr=[AffineMap.get_identity(3)],
x_arr=[BoolAttr.get(True), StringAttr.get("x")],
x_boolarr=[False, True],
x_bool=True,
x_dboolarr=[True, False],
x_df16arr=[21, 22],
x_df32arr=[23, 24],
x_df64arr=[25, 26],
x_di32arr=[0, 1],
x_di64arr=[1, 2],
x_di8arr=[2, 3],
x_dictarr=[{"a": BoolAttr.get(False)}],
x_dict={"b": BoolAttr.get(True)},
x_f32=-2.25,
x_f32arr=[2.0, 3.0],
x_f64=4.25,
x_f64arr=[4.0, 8.0],
x_f64elems=[8.0, 16.0],
x_flatsymrefarr=["symbol1", "symbol2"],
x_flatsymref="symbol3",
x_i1=0,
x_i16=42,
x_i32=6,
x_i32arr=[4, 5],
x_i32elems=[5, 6],
x_i64=9,
x_i64arr=[7, 8],
x_i64elems=[8, 9],
x_i64svecarr=[10, 11],
x_i8=11,
x_idx=10,
x_idxelems=[11, 12],
x_idxlistarr=[[13], [14, 15]],
x_si1=-1,
x_si16=-2,
x_si32=-3,
x_si64=-123,
x_si8=-4,
x_strarr=["hello", "world"],
x_str="hello world!",
x_symrefarr=["flatsym", ["deep", "sym"]],
x_symref=["deep", "sym2"],
x_sym="symbol",
x_typearr=[F32Type.get()],
x_type=F64Type.get(),
x_ui1=1,
x_ui16=2,
x_ui32=3,
x_ui64=4,
x_ui8=5,
x_unit=True,
)
op.verify()
op.print(use_local_scope=True)
assert typing.get_type_hints(test.AttributesOp.x_affinemaparr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_affinemaparr.fget)["return"] is ArrayAttr
assert type(op.x_affinemaparr) is typing.get_type_hints(test.AttributesOp.x_affinemaparr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_affinemap.fset)["value"] is AffineMapAttr
assert typing.get_type_hints(test.AttributesOp.x_affinemap.fget)["return"] is AffineMapAttr
assert type(op.x_affinemap) is typing.get_type_hints(test.AttributesOp.x_affinemap.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_arr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_arr.fget)["return"] is ArrayAttr
assert type(op.x_arr) is typing.get_type_hints(test.AttributesOp.x_arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_boolarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_boolarr.fget)["return"] is ArrayAttr
assert type(op.x_boolarr) is typing.get_type_hints(test.AttributesOp.x_boolarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_bool.fset)["value"] is BoolAttr
assert typing.get_type_hints(test.AttributesOp.x_bool.fget)["return"] is BoolAttr
assert type(op.x_bool) is typing.get_type_hints(test.AttributesOp.x_bool.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_dboolarr.fset)["value"] is DenseBoolArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_dboolarr.fget)["return"] is DenseBoolArrayAttr
assert type(op.x_dboolarr) is typing.get_type_hints(test.AttributesOp.x_dboolarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_df32arr.fset)["value"] is DenseF32ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_df32arr.fget)["return"] is DenseF32ArrayAttr
assert type(op.x_df32arr) is typing.get_type_hints(test.AttributesOp.x_df32arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_df64arr.fset)["value"] is DenseF64ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_df64arr.fget)["return"] is DenseF64ArrayAttr
assert type(op.x_df64arr) is typing.get_type_hints(test.AttributesOp.x_df64arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_df16arr.fset)["value"] is DenseI16ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_df16arr.fget)["return"] is DenseI16ArrayAttr
assert type(op.x_df16arr) is typing.get_type_hints(test.AttributesOp.x_df16arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_di32arr.fset)["value"] is DenseI32ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_di32arr.fget)["return"] is DenseI32ArrayAttr
assert type(op.x_di32arr) is typing.get_type_hints(test.AttributesOp.x_di32arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_di64arr.fset)["value"] is DenseI64ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_di64arr.fget)["return"] is DenseI64ArrayAttr
assert type(op.x_di64arr) is typing.get_type_hints(test.AttributesOp.x_di64arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_di8arr.fset)["value"] is DenseI8ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_di8arr.fget)["return"] is DenseI8ArrayAttr
assert type(op.x_di8arr) is typing.get_type_hints(test.AttributesOp.x_di8arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_dictarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_dictarr.fget)["return"] is ArrayAttr
assert type(op.x_dictarr) is typing.get_type_hints(test.AttributesOp.x_dictarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_dict.fset)["value"] is DictAttr
assert typing.get_type_hints(test.AttributesOp.x_dict.fget)["return"] is DictAttr
assert type(op.x_dict) is typing.get_type_hints(test.AttributesOp.x_dict.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_f32arr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_f32arr.fget)["return"] is ArrayAttr
assert type(op.x_f32arr) is typing.get_type_hints(test.AttributesOp.x_f32arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_f32.fset)["value"] is FloatAttr
assert typing.get_type_hints(test.AttributesOp.x_f32.fget)["return"] is FloatAttr
assert type(op.x_f32) is typing.get_type_hints(test.AttributesOp.x_f32.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_f64arr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_f64arr.fget)["return"] is ArrayAttr
assert type(op.x_f64arr) is typing.get_type_hints(test.AttributesOp.x_f64arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_f64.fset)["value"] is FloatAttr
assert typing.get_type_hints(test.AttributesOp.x_f64.fget)["return"] is FloatAttr
assert type(op.x_f64) is typing.get_type_hints(test.AttributesOp.x_f64.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_f64elems.fset)["value"] is DenseFPElementsAttr
assert typing.get_type_hints(test.AttributesOp.x_f64elems.fget)["return"] is DenseFPElementsAttr
assert type(op.x_f64elems) is typing.get_type_hints(test.AttributesOp.x_f64elems.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_flatsymrefarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_flatsymrefarr.fget)["return"] is ArrayAttr
assert type(op.x_flatsymrefarr) is typing.get_type_hints(test.AttributesOp.x_flatsymrefarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_flatsymref.fset)["value"] is FlatSymbolRefAttr
assert typing.get_type_hints(test.AttributesOp.x_flatsymref.fget)["return"] is FlatSymbolRefAttr
assert type(op.x_flatsymref) is typing.get_type_hints(test.AttributesOp.x_flatsymref.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i16.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_i16.fget)["return"] is IntegerAttr
assert type(op.x_i16) is typing.get_type_hints(test.AttributesOp.x_i16.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i1.fset)["value"] is BoolAttr
assert typing.get_type_hints(test.AttributesOp.x_i1.fget)["return"] is BoolAttr
assert type(op.x_i1) is typing.get_type_hints(test.AttributesOp.x_i1.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i32arr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_i32arr.fget)["return"] is ArrayAttr
assert type(op.x_i32arr) is typing.get_type_hints(test.AttributesOp.x_i32arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i32.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_i32.fget)["return"] is IntegerAttr
assert type(op.x_i32) is typing.get_type_hints(test.AttributesOp.x_i32.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i32elems.fset)["value"] is DenseIntElementsAttr
assert typing.get_type_hints(test.AttributesOp.x_i32elems.fget)["return"] is DenseIntElementsAttr
assert type(op.x_i32elems) is typing.get_type_hints(test.AttributesOp.x_i32elems.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i64arr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_i64arr.fget)["return"] is ArrayAttr
assert type(op.x_i64arr) is typing.get_type_hints(test.AttributesOp.x_i64arr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i64.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_i64.fget)["return"] is IntegerAttr
assert type(op.x_i64) is typing.get_type_hints(test.AttributesOp.x_i64.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i64elems.fset)["value"] is DenseIntElementsAttr
assert typing.get_type_hints(test.AttributesOp.x_i64elems.fget)["return"] is DenseIntElementsAttr
assert type(op.x_i64elems) is typing.get_type_hints(test.AttributesOp.x_i64elems.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i64svecarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_i64svecarr.fget)["return"] is ArrayAttr
assert type(op.x_i64svecarr) is typing.get_type_hints(test.AttributesOp.x_i64svecarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_i8.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_i8.fget)["return"] is IntegerAttr
assert type(op.x_i8) is typing.get_type_hints(test.AttributesOp.x_i8.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_idx.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_idx.fget)["return"] is IntegerAttr
assert type(op.x_idx) is typing.get_type_hints(test.AttributesOp.x_idx.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_idxelems.fset)["value"] is DenseIntElementsAttr
assert typing.get_type_hints(test.AttributesOp.x_idxelems.fget)["return"] is DenseIntElementsAttr
assert type(op.x_idxelems) is typing.get_type_hints(test.AttributesOp.x_idxelems.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_idxlistarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_idxlistarr.fget)["return"] is ArrayAttr
assert type(op.x_idxlistarr) is typing.get_type_hints(test.AttributesOp.x_idxlistarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_si16.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_si16.fget)["return"] is IntegerAttr
assert type(op.x_si16) is typing.get_type_hints(test.AttributesOp.x_si16.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_si1.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_si1.fget)["return"] is IntegerAttr
assert type(op.x_si1) is typing.get_type_hints(test.AttributesOp.x_si1.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_si32.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_si32.fget)["return"] is IntegerAttr
assert type(op.x_si32) is typing.get_type_hints(test.AttributesOp.x_si32.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_si64.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_si64.fget)["return"] is IntegerAttr
assert type(op.x_si64) is typing.get_type_hints(test.AttributesOp.x_si64.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_si8.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_si8.fget)["return"] is IntegerAttr
assert type(op.x_si8) is typing.get_type_hints(test.AttributesOp.x_si8.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_strarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_strarr.fget)["return"] is ArrayAttr
assert type(op.x_strarr) is typing.get_type_hints(test.AttributesOp.x_strarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_str.fset)["value"] is StringAttr
assert typing.get_type_hints(test.AttributesOp.x_str.fget)["return"] is StringAttr
assert type(op.x_str) is typing.get_type_hints(test.AttributesOp.x_str.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_sym.fset)["value"] is StringAttr
assert typing.get_type_hints(test.AttributesOp.x_sym.fget)["return"] is StringAttr
assert type(op.x_sym) is typing.get_type_hints(test.AttributesOp.x_sym.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_symrefarr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_symrefarr.fget)["return"] is ArrayAttr
assert type(op.x_symrefarr) is typing.get_type_hints(test.AttributesOp.x_symrefarr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_symref.fset)["value"] is SymbolRefAttr
assert typing.get_type_hints(test.AttributesOp.x_symref.fget)["return"] is SymbolRefAttr
assert type(op.x_symref) is typing.get_type_hints(test.AttributesOp.x_symref.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_typearr.fset)["value"] is ArrayAttr
assert typing.get_type_hints(test.AttributesOp.x_typearr.fget)["return"] is ArrayAttr
assert type(op.x_typearr) is typing.get_type_hints(test.AttributesOp.x_typearr.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_type.fset)["value"] is TypeAttr
assert typing.get_type_hints(test.AttributesOp.x_type.fget)["return"] is TypeAttr
assert type(op.x_type) is typing.get_type_hints(test.AttributesOp.x_type.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_ui16.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_ui16.fget)["return"] is IntegerAttr
assert type(op.x_ui16) is typing.get_type_hints(test.AttributesOp.x_ui16.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_ui1.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_ui1.fget)["return"] is IntegerAttr
assert type(op.x_ui1) is typing.get_type_hints(test.AttributesOp.x_ui1.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_ui32.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_ui32.fget)["return"] is IntegerAttr
assert type(op.x_ui32) is typing.get_type_hints(test.AttributesOp.x_ui32.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_ui64.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_ui64.fget)["return"] is IntegerAttr
assert type(op.x_ui64) is typing.get_type_hints(test.AttributesOp.x_ui64.fget)["return"]
assert typing.get_type_hints(test.AttributesOp.x_ui8.fset)["value"] is IntegerAttr
assert typing.get_type_hints(test.AttributesOp.x_ui8.fget)["return"] is IntegerAttr
assert type(op.x_ui8) is typing.get_type_hints(test.AttributesOp.x_ui8.fget)["return"]
@run
def inferReturnTypes():
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
op = test.InferResultsOp()
dummy = test.DummyOp()
iface = InferTypeOpInterface(op)
print(iface.inferReturnTypes())
iface_static = InferTypeOpInterface(test.InferResultsOp)
print(iface.inferReturnTypes())
assert isinstance(iface.opview, test.InferResultsOp)
assert iface.opview == iface.operation.opview
try:
iface_static.opview
except TypeError:
pass
else:
assert False, (
"not expected to be able to obtain an opview from a static" " interface"
)
try:
InferTypeOpInterface(dummy)
except ValueError:
pass
else:
assert False, "not expected dummy op to implement the interface"
try:
InferTypeOpInterface(test.DummyOp)
except ValueError:
pass
else:
assert False, "not expected dummy op class to implement the interface"
@run
def resultTypesDefinedByTraits():
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
inferred = test.InferResultsOp()
print(inferred.single.type, inferred.doubled.type)
same = test.SameOperandAndResultTypeOp([inferred.results[0]])
print(same.one.type)
print(same.two.type)
assert (
typing.get_type_hints(test.SameOperandAndResultTypeOp.one.fget)[
"return"
]
is OpResult
)
assert type(same.one) is OpResult
first_type_attr = test.FirstAttrDeriveTypeAttrOp(
inferred.results[1], TypeAttr.get(IndexType.get())
)
print(first_type_attr.one.type)
print(first_type_attr.two.type)
first_attr = test.FirstAttrDeriveAttrOp(FloatAttr.get(F32Type.get(), 3.14))
print(first_attr.one.type)
print(first_attr.two.type)
print(first_attr.three.type)
implied = test.InferResultsImpliedOp()
print(implied.integer.type)
print(implied.flt.type)
print(implied.index.type)
f64 = F64Type.get()
no_imply = test.InferResultsImpliedOp(results=[f64, f64, f64])
print(no_imply.integer.type, no_imply.flt.type, no_imply.index.type)
no_infer = test.InferResultsOp(results=[F32Type.get(), IndexType.get()])
print(no_infer.single.type, no_infer.doubled.type)
@run
def testOptionalOperandOp():
with Context() as ctx, Location.unknown():
module = Module.create()
with InsertionPoint(module.body):
op1 = test.OptionalOperandOp()
print(f"op1.input is None: {op1.input is None}")
assert (
typing.get_type_hints(test.OptionalOperandOp.input.fget)["return"]
is Optional[Value]
)
assert (
typing.get_type_hints(test.OptionalOperandOp.result.fget)["return"]
== OpResult[IntegerType]
)
assert type(op1.result) is OpResult
op2 = test.OptionalOperandOp(input=op1)
print(f"op2.input is None: {op2.input is None}")
@run
def testCustomAttribute():
with Context() as ctx, Location.unknown():
a = TestAttr.get()
print(a)
op2 = test.CustomAttributedOp(a)
print(f"{op2}")
print(f"{op2.test_attr}")
print(repr(op2.test_attr))
b = TestAttr(a)
unit = UnitAttr.get()
try:
TestAttr(unit)
except ValueError as e:
assert "Cannot cast attribute to TestAttr" in str(e)
else:
raise
try:
TestAttr(42)
except TypeError as e:
assert "Expected an MLIR object (got 42)" in str(e)
except ValueError as e:
assert "Cannot cast attribute to TestAttr (from 42)" in str(e)
else:
raise
try:
TestAttr(42, 56)
except TypeError:
pass
else:
raise
@run
def testCustomType():
with Context() as ctx:
a = TestType.get()
print(a)
b = TestType(a)
assert isinstance(b.typeid, TypeID)
try:
b.static_typeid
except AttributeError as e:
print(e)
i8 = IntegerType.get_signless(8)
try:
TestType(i8)
except ValueError as e:
assert "Cannot cast type to TestType" in str(e)
else:
raise
try:
TestType(42)
except TypeError as e:
assert "Expected an MLIR object (got 42)" in str(e)
except ValueError as e:
assert "Cannot cast type to TestType (from 42)" in str(e)
else:
raise
try:
TestType(42, 56)
except TypeError:
pass
else:
raise
@run
def testValue():
assert hasattr(Value, "__class_getitem__")
@run
def testTensorValue():
with Context() as ctx, Location.unknown():
i8 = IntegerType.get_signless(8)
class Tensor(TestTensorValue):
def __str__(self):
return super().__str__().replace("Value", "Tensor")
module = Module.create()
with InsertionPoint(module.body):
t = tensor.EmptyOp([10, 10], i8).result
print(Value(t))
tt = Tensor(t)
print(tt)
print(tt.is_null())
assert isinstance(TestIntegerRankedTensorType.static_typeid, TypeID)
assert TestIntegerRankedTensorType.static_typeid == t.type.typeid
d = tensor.EmptyOp([1, 2, 3], IntegerType.get_signless(5)).result
print(d)
print(repr(d))
@run
def inferReturnTypeComponents():
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
i32 = IntegerType.get_signless(32)
with InsertionPoint(module.body):
resultType = UnrankedTensorType.get(i32)
operandTypes = [
RankedTensorType.get([1, 3, 10, 10], i32),
UnrankedTensorType.get(i32),
]
f = func.FuncOp(
"test_inferReturnTypeComponents", (operandTypes, [resultType])
)
entry_block = Block.create_at_start(f.operation.regions[0], operandTypes)
with InsertionPoint(entry_block):
ranked_op = test.InferShapedTypeComponentsOp(
resultType, entry_block.arguments[0]
)
unranked_op = test.InferShapedTypeComponentsOp(
resultType, entry_block.arguments[1]
)
iface = InferShapedTypeOpInterface(ranked_op)
shaped_type_components = iface.inferReturnTypeComponents(
operands=[ranked_op.operand]
)[0]
print("has rank:", shaped_type_components.has_rank)
print("rank:", shaped_type_components.rank)
print("element type:", shaped_type_components.element_type)
print("shape:", shaped_type_components.shape)
iface = InferShapedTypeOpInterface(unranked_op)
shaped_type_components = iface.inferReturnTypeComponents(
operands=[unranked_op.operand]
)[0]
print("has rank:", shaped_type_components.has_rank)
print("rank:", shaped_type_components.rank)
print("element type:", shaped_type_components.element_type)
print("shape:", shaped_type_components.shape)
@run
def testCustomTypeTypeCaster():
with Context() as ctx, Location.unknown():
a = TestType.get()
assert a.typeid is not None
b = Type.parse("!python_test.test_type")
print(b)
print(repr(b))
c = TestIntegerRankedTensorType.get([10, 10], 5)
print(c)
print(repr(c))
try:
@register_type_caster(c.typeid)
def type_caster(pytype):
return TestIntegerRankedTensorType(pytype)
except RuntimeError as e:
print(e)
@register_type_caster(c.typeid, replace=True)
def type_caster(pytype):
return RankedTensorType(pytype)
d = tensor.EmptyOp([10, 10], IntegerType.get_signless(5)).result
print(d.type)
print("ranked tensor type", repr(d.type))
@register_type_caster(c.typeid, replace=True)
def type_caster(pytype):
return TestIntegerRankedTensorType(pytype)
d = tensor.EmptyOp([10, 10], IntegerType.get_signless(5)).result
print(d.type)
print(repr(d.type))
@run
def testInferTypeOpInterface():
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
i64 = IntegerType.get_signless(64)
zero = arith.ConstantOp(i64, 0)
one_operand = test.InferResultsVariadicInputsOp(single=zero, doubled=None)
print(one_operand.result.type)
two_operands = test.InferResultsVariadicInputsOp(single=zero, doubled=zero)
print(two_operands.result.type)
assert (
typing.get_type_hints(test.infer_results_variadic_inputs_op)["return"]
is OpResult
)
assert (
type(test.infer_results_variadic_inputs_op(single=zero, doubled=zero))
is OpResult
)
@run
def testVariadicOperandAccess():
def values(lst):
return [str(e) for e in lst]
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
i32 = IntegerType.get_signless(32)
zero = arith.ConstantOp(i32, 0)
one = arith.ConstantOp(i32, 1)
two = arith.ConstantOp(i32, 2)
three = arith.ConstantOp(i32, 3)
four = arith.ConstantOp(i32, 4)
variadic_operands = test.SameVariadicOperandSizeOp(
[zero, one], two, [three, four]
)
print(variadic_operands.non_variadic)
assert (
typing.get_type_hints(test.SameVariadicOperandSizeOp.non_variadic.fget)[
"return"
]
is Value
)
assert type(variadic_operands.non_variadic) is Value
print(values(variadic_operands.variadic1))
assert (
typing.get_type_hints(test.SameVariadicOperandSizeOp.variadic1.fget)[
"return"
]
is OpOperandList
)
assert type(variadic_operands.variadic1) is OpOperandList
print(values(variadic_operands.variadic2))
assert type(variadic_operands.variadic2) is OpOperandList
assert (
typing.get_type_hints(test.same_variadic_operand)["return"]
is test.SameVariadicOperandSizeOp
)
assert (
type(test.same_variadic_operand([zero, one], two, [three, four]))
is test.SameVariadicOperandSizeOp
)
@run
def testVariadicResultAccess():
def types(lst):
return [e.type for e in lst]
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
i = [IntegerType.get_signless(k) for k in range(7)]
op = test.SameVariadicResultSizeOpVFV([i[0], i[1]], i[2], [i[3], i[4]])
print(op.non_variadic.type)
print(types(op.variadic1))
print(types(op.variadic2))
assert (
typing.get_type_hints(test.same_variadic_result_vfv)["return"]
== Union[OpResult, OpResultList, test.SameVariadicResultSizeOpVFV]
)
assert (
type(test.same_variadic_result_vfv([i[0], i[1]], i[2], [i[3], i[4]]))
is OpResultList
)
op = test.SameVariadicResultSizeOpVVV(
[i[0], i[1]], [i[2], i[3]], [i[4], i[5]]
)
print(types(op.variadic1))
print(types(op.variadic2))
print(types(op.variadic3))
op = test.SameVariadicResultSizeOpFFV(i[0], i[1], [i[2], i[3], i[4]])
print(op.non_variadic1.type)
print(op.non_variadic2.type)
print(types(op.variadic))
assert (
typing.get_type_hints(test.SameVariadicResultSizeOpFFV.variadic.fget)[
"return"
]
is OpResultList
)
assert type(op.variadic) is OpResultList
op = test.SameVariadicResultSizeOpVVF(
[i[0], i[1], i[2]], [i[3], i[4], i[5]], i[6]
)
print(types(op.variadic1))
print(types(op.variadic2))
print(op.non_variadic.type)
op = test.SameVariadicResultSizeOpFVFVF(
i[0], [i[1], i[2]], i[3], [i[4], i[5]], i[6]
)
print(op.non_variadic1.type)
print(types(op.variadic1))
print(op.non_variadic2.type)
print(types(op.variadic2))
print(op.non_variadic3.type)
op = test.SameVariadicResultSizeOpFVFVF(i[0], [], i[1], [], i[2])
print(op.non_variadic1.type)
print(types(op.variadic1))
print(op.non_variadic2.type)
print(types(op.variadic2))
print(op.non_variadic3.type)
op = test.SameVariadicResultSizeOpFVFVF(i[0], [i[1]], i[2], [i[3]], i[4])
print(op.non_variadic1.type)
print(types(op.variadic1))
print(op.non_variadic2.type)
print(types(op.variadic2))
print(op.non_variadic3.type)
assert (
typing.get_type_hints(test.results_variadic)["return"]
== Union[OpResult, OpResultList, test.ResultsVariadicOp]
)
assert type(test.results_variadic([i[0]])) is OpResult
op_res_variadic = test.ResultsVariadicOp([i[0]])
assert (
typing.get_type_hints(test.ResultsVariadicOp.res.fget)["return"]
is OpResultList
)
assert type(op_res_variadic.res) is OpResultList
@run
def testVariadicAndNormalRegionOp():
with Context() as ctx, Location.unknown(ctx):
module = Module.create()
with InsertionPoint(module.body):
region_op = test.VariadicAndNormalRegionOp(2)
assert (
typing.get_type_hints(test.VariadicAndNormalRegionOp.region.fget)[
"return"
]
is Region
)
assert type(region_op.region) is Region
assert (
typing.get_type_hints(test.VariadicAndNormalRegionOp.variadic.fget)[
"return"
]
is RegionSequence
)
assert type(region_op.variadic) is RegionSequence
assert isinstance(region_op.opview, OpView)
assert isinstance(region_op.operation.opview, OpView)