from mlir.ir import *
import mlir.dialects.python_test as test
def run(f):
print("\nTEST:", f.__name__)
f()
return f
@run
def testAttributes():
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
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 inferReturnTypes():
with Context() as ctx, Location.unknown(ctx):
test.register_python_test_dialect(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):
test.register_python_test_dialect(ctx)
module = Module.create()
with InsertionPoint(module.body):
inferred = test.InferResultsOp()
same = test.SameOperandAndResultTypeOp([inferred.results[0]])
print(same.one.type)
print(same.two.type)
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)
@run
def testOptionalOperandOp():
with Context() as ctx, Location.unknown():
test.register_python_test_dialect(ctx)
module = Module.create()
with InsertionPoint(module.body):
op1 = test.OptionalOperandOp()
print(f"op1.input is None: {op1.input is None}")
op2 = test.OptionalOperandOp(input=op1)
print(f"op2.input is None: {op2.input is None}")
@run
def testCustomAttribute():
with Context() as ctx:
test.register_python_test_dialect(ctx)
a = test.TestAttr.get()
print(a)
b = test.TestAttr(a)
unit = UnitAttr.get()
try:
test.TestAttr(unit)
except ValueError as e:
assert "Cannot cast attribute to TestAttr" in str(e)
else:
raise
try:
test.TestAttr(42)
except TypeError as e:
assert "Expected an MLIR object" in str(e)
else:
raise
try:
test.TestAttr(42, 56)
except TypeError:
pass
else:
raise
@run
def testCustomType():
with Context() as ctx:
test.register_python_test_dialect(ctx)
a = test.TestType.get()
print(a)
b = test.TestType(a)
i8 = IntegerType.get_signless(8)
try:
test.TestType(i8)
except ValueError as e:
assert "Cannot cast type to TestType" in str(e)
else:
raise
try:
test.TestType(42)
except TypeError as e:
assert "Expected an MLIR object" in str(e)
else:
raise
try:
test.TestType(42, 56)
except TypeError:
pass
else:
raise