* As discussed, fixes the ordering or (operands, results) -> (results, operands) in various `create` like methods. * Fixes a syntax error in an ODS accessor method. * Removes the linalg example in favor of a test case that exercises the same. * Fixes FuncOp visibility to properly use None instead of the empty string and defaults it to None. * Implements what was documented for requiring that trailing __init__ args `loc` and `ip` are keyword only. * Adds a check to `InsertionPoint.insert` so that if attempting to insert past the terminator, an exception is raised telling you what to do instead. Previously, this would crash downstream (i.e. when trying to print the resultant module). * Renames `_ods_build_default` -> `build_generic` and documents it. * Removes `result` from the list of prohibited words and for single-result ops, defaults to naming the result `result`, thereby matching expectations and what is already implemented on the base class. * This was intended to be a relatively small set of changes to be inlined with the broader support for ODS generating the most specific builder, but it spidered out once actually testing various combinations, so rolling up separately. Differential Revision: https://reviews.llvm.org/D95320
211 lines
7.4 KiB
Python
211 lines
7.4 KiB
Python
# 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.at_block_terminator(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.at_block_terminator(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: operand_segment_sizes
|
|
# CHECK-NOT: result_segment_sizes
|
|
# 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.at_block_terminator(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: operand_segment_sizes = dense<[1, 2, 1]> : vector<3xi64>
|
|
# CHECK-SAME: result_segment_sizes = dense<[2, 1, 1]> : vector<3xi64>
|
|
# 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: operand_segment_sizes = dense<[1, 0, 0]>
|
|
# CHECK-SAME: result_segment_sizes = dense<[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.at_block_terminator(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)
|