import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
def add_dummy_value():
return Operation.create(
"custom.value",
results=[IntegerType.get_signless(32)]).result
def testOdsBuildDefaultImplicitRegions():
class TestFixedRegionsOp(OpView):
OPERATION_NAME = "custom.test_op"
_ODS_REGIONS = (2, True)
class TestVariadicRegionsOp(OpView):
OPERATION_NAME = "custom.test_any_regions_op"
_ODS_REGIONS = (2, False)
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
m = Module.create()
with InsertionPoint(m.body):
op = TestFixedRegionsOp.build_generic(results=[], operands=[])
print(f"NUM_REGIONS: {len(op.regions)}")
op = TestFixedRegionsOp.build_generic(results=[], operands=[], regions=2)
print(f"NUM_REGIONS: {len(op.regions)}")
try:
op = TestFixedRegionsOp.build_generic(results=[], operands=[], regions=3)
except ValueError as e:
print(f"ERROR:{e}")
try:
op = TestFixedRegionsOp.build_generic(results=[], operands=[], regions=1)
except ValueError as e:
print(f"ERROR:{e}")
op = TestVariadicRegionsOp.build_generic(results=[], operands=[])
print(f"DEFAULT_NUM_REGIONS: {len(op.regions)}")
op = TestVariadicRegionsOp.build_generic(
results=[], operands=[], regions=2)
print(f"EQ_NUM_REGIONS: {len(op.regions)}")
op = TestVariadicRegionsOp.build_generic(
results=[], operands=[], regions=3)
print(f"GT_NUM_REGIONS: {len(op.regions)}")
try:
op = TestVariadicRegionsOp.build_generic(results=[], operands=[], regions=1)
except ValueError as e:
print(f"ERROR:{e}")
run(testOdsBuildDefaultImplicitRegions)
def testOdsBuildDefaultNonVariadic():
class TestOp(OpView):
OPERATION_NAME = "custom.test_op"
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
m = Module.create()
with InsertionPoint(m.body):
v0 = add_dummy_value()
v1 = add_dummy_value()
t0 = IntegerType.get_signless(8)
t1 = IntegerType.get_signless(16)
op = TestOp.build_generic(results=[t0, t1], operands=[v0, v1])
print(m)
run(testOdsBuildDefaultNonVariadic)
def testOdsBuildDefaultSizedVariadic():
class TestOp(OpView):
OPERATION_NAME = "custom.test_op"
_ODS_OPERAND_SEGMENTS = [1, -1, 0]
_ODS_RESULT_SEGMENTS = [-1, 0, 1]
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
m = Module.create()
with InsertionPoint(m.body):
v0 = add_dummy_value()
v1 = add_dummy_value()
v2 = add_dummy_value()
v3 = add_dummy_value()
t0 = IntegerType.get_signless(8)
t1 = IntegerType.get_signless(16)
t2 = IntegerType.get_signless(32)
t3 = IntegerType.get_signless(64)
op = TestOp.build_generic(
results=[[t0, t1], t2, t3],
operands=[v0, [v1, v2], v3])
op = TestOp.build_generic(
results=[None, None, t3],
operands=[v0, None, None])
print(m)
try:
op = TestOp.build_generic(
results=[None, None, t3],
operands=[None, None, None])
except ValueError as e:
print(f"OPERAND_CAST_ERROR:{e}")
try:
op = TestOp.build_generic(
results=[None, None, None],
operands=[v0, None, None])
except ValueError as e:
print(f"RESULT_CAST_ERROR:{e}")
try:
op = TestOp.build_generic(
results=[None, None, t3],
operands=[v0, [None], None])
except ValueError as e:
print(f"OPERAND_LIST_CAST_ERROR:{e}")
try:
op = TestOp.build_generic(
results=[[None], None, t3],
operands=[v0, None, None])
except ValueError as e:
print(f"RESULT_LIST_CAST_ERROR:{e}")
run(testOdsBuildDefaultSizedVariadic)
def testOdsBuildDefaultCastError():
class TestOp(OpView):
OPERATION_NAME = "custom.test_op"
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
m = Module.create()
with InsertionPoint(m.body):
v0 = add_dummy_value()
v1 = add_dummy_value()
t0 = IntegerType.get_signless(8)
t1 = IntegerType.get_signless(16)
try:
op = TestOp.build_generic(
results=[t0, t1],
operands=[None, v1])
except ValueError as e:
print(f"ERROR: {e}")
try:
op = TestOp.build_generic(
results=[t0, None],
operands=[v0, v1])
except ValueError as e:
print(f"ERROR: {e}")
run(testOdsBuildDefaultCastError)