1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
# RUN: %PYTHON %s | FileCheck %s
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=[])
# CHECK: NUM_REGIONS: 2
print(f"NUM_REGIONS: {len(op.regions)}")
# Including a regions= that matches should be fine.
op = TestFixedRegionsOp.build_generic(results=[], operands=[], regions=2)
print(f"NUM_REGIONS: {len(op.regions)}")
# Reject greater than.
try:
op = TestFixedRegionsOp.build_generic(
results=[], operands=[], regions=3
)
except ValueError as e:
# CHECK: ERROR:Operation "custom.test_op" requires a maximum of 2 regions but was built with regions=3
print(f"ERROR:{e}")
# Reject less than.
try:
op = TestFixedRegionsOp.build_generic(
results=[], operands=[], regions=1
)
except ValueError as e:
# CHECK: ERROR:Operation "custom.test_op" requires a minimum of 2 regions but was built with regions=1
print(f"ERROR:{e}")
# If no regions specified for a variadic region op, build the minimum.
op = TestVariadicRegionsOp.build_generic(results=[], operands=[])
# CHECK: DEFAULT_NUM_REGIONS: 2
print(f"DEFAULT_NUM_REGIONS: {len(op.regions)}")
# Should also accept an explicit regions= that matches the minimum.
op = TestVariadicRegionsOp.build_generic(results=[], operands=[], regions=2)
# CHECK: EQ_NUM_REGIONS: 2
print(f"EQ_NUM_REGIONS: {len(op.regions)}")
# And accept greater than minimum.
# Should also accept an explicit regions= that matches the minimum.
op = TestVariadicRegionsOp.build_generic(results=[], operands=[], regions=3)
# CHECK: GT_NUM_REGIONS: 3
print(f"GT_NUM_REGIONS: {len(op.regions)}")
# Should reject less than minimum.
try:
op = TestVariadicRegionsOp.build_generic(
results=[], operands=[], regions=1
)
except ValueError as e:
# CHECK: ERROR:Operation "custom.test_any_regions_op" requires a minimum of 2 regions but was built with regions=1
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])
# CHECK: %[[V0:.+]] = "custom.value"
# CHECK: %[[V1:.+]] = "custom.value"
# CHECK: "custom.test_op"(%[[V0]], %[[V1]])
# CHECK-NOT: operandSegmentSizes
# CHECK-NOT: resultSegmentSizes
# CHECK-SAME: : (i32, i32) -> (i8, i16)
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)
# CHECK: %[[V0:.+]] = "custom.value"
# CHECK: %[[V1:.+]] = "custom.value"
# CHECK: %[[V2:.+]] = "custom.value"
# CHECK: %[[V3:.+]] = "custom.value"
# CHECK: "custom.test_op"(%[[V0]], %[[V1]], %[[V2]], %[[V3]])
# CHECK-SAME: operandSegmentSizes = array<i32: 1, 2, 1>
# CHECK-SAME: resultSegmentSizes = array<i32: 2, 1, 1>
# CHECK-SAME: : (i32, i32, i32, i32) -> (i8, i16, i32, i64)
op = TestOp.build_generic(
results=[[t0, t1], t2, t3], operands=[v0, [v1, v2], v3]
)
# Now test with optional omitted.
# CHECK: "custom.test_op"(%[[V0]])
# CHECK-SAME: operandSegmentSizes = array<i32: 1, 0, 0>
# CHECK-SAME: resultSegmentSizes = array<i32: 0, 0, 1>
# CHECK-SAME: (i32) -> i64
op = TestOp.build_generic(
results=[None, None, t3], operands=[v0, None, None]
)
print(m)
# And verify that errors are raised for None in a required operand.
try:
op = TestOp.build_generic(
results=[None, None, t3], operands=[None, None, None]
)
except ValueError as e:
# CHECK: OPERAND_CAST_ERROR:Operand 0 of operation "custom.test_op" must be a Value (was None and operand is not optional)
print(f"OPERAND_CAST_ERROR:{e}")
# And verify that errors are raised for None in a required result.
try:
op = TestOp.build_generic(
results=[None, None, None], operands=[v0, None, None]
)
except ValueError as e:
# CHECK: RESULT_CAST_ERROR:Result 2 of operation "custom.test_op" must be a Type (was None and result is not optional)
print(f"RESULT_CAST_ERROR:{e}")
# Variadic lists with None elements should reject.
try:
op = TestOp.build_generic(
results=[None, None, t3], operands=[v0, [None], None]
)
except ValueError as e:
# CHECK: OPERAND_LIST_CAST_ERROR:Operand 1 of operation "custom.test_op" must be a Sequence of Values (contained a None item)
print(f"OPERAND_LIST_CAST_ERROR:{e}")
try:
op = TestOp.build_generic(
results=[[None], None, t3], operands=[v0, None, None]
)
except ValueError as e:
# CHECK: RESULT_LIST_CAST_ERROR:Result 0 of operation "custom.test_op" must be a Sequence of Types (contained a None item)
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:
# CHECK: ERROR: Operand 0 of operation "custom.test_op" must be a Value
print(f"ERROR: {e}")
try:
op = TestOp.build_generic(results=[t0, None], operands=[v0, v1])
except ValueError as e:
# CHECK: Result 1 of operation "custom.test_op" must be a Type
print(f"ERROR: {e}")
run(testOdsBuildDefaultCastError)
|