Exemple #1
0
def testCustomType():
    with Context() as ctx:
        test.register_python_test_dialect(ctx)
        a = test.TestType.get()
        # CHECK: !python_test.test_type
        print(a)

        # The following cast must not assert.
        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

        # The following must trigger a TypeError from our adaptors and must not
        # crash.
        try:
            test.TestType(42)
        except TypeError as e:
            assert "Expected an MLIR object" in str(e)
        else:
            raise

        # The following must trigger a TypeError from pybind (therefore, not
        # checking its message) and must not crash.
        try:
            test.TestType(42, 56)
        except TypeError:
            pass
        else:
            raise
Exemple #2
0
def testCustomAttribute():
    with Context() as ctx:
        test.register_python_test_dialect(ctx)
        a = test.TestAttr.get()
        # CHECK: #python_test.test_attr
        print(a)

        # The following cast must not assert.
        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

        # The following must trigger a TypeError from our adaptors and must not
        # crash.
        try:
            test.TestAttr(42)
        except TypeError as e:
            assert "Expected an MLIR object" in str(e)
        else:
            raise

        # The following must trigger a TypeError from pybind (therefore, not
        # checking its message) and must not crash.
        try:
            test.TestAttr(42, 56)
        except TypeError:
            pass
        else:
            raise
Exemple #3
0
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]])
            # CHECK-COUNT-2: i32
            print(same.one.type)
            print(same.two.type)

            first_type_attr = test.FirstAttrDeriveTypeAttrOp(
                inferred.results[1], TypeAttr.get(IndexType.get()))
            # CHECK-COUNT-2: index
            print(first_type_attr.one.type)
            print(first_type_attr.two.type)

            first_attr = test.FirstAttrDeriveAttrOp(
                FloatAttr.get(F32Type.get(), 3.14))
            # CHECK-COUNT-3: f32
            print(first_attr.one.type)
            print(first_attr.two.type)
            print(first_attr.three.type)

            implied = test.InferResultsImpliedOp()
            # CHECK: i32
            print(implied.integer.type)
            # CHECK: f64
            print(implied.flt.type)
            # CHECK: index
            print(implied.index.type)
Exemple #4
0
def testOptionalOperandOp():
    with Context() as ctx, Location.unknown():
        test.register_python_test_dialect(ctx)

        module = Module.create()
        with InsertionPoint(module.body):

            op1 = test.OptionalOperandOp()
            # CHECK: op1.input is None: True
            print(f"op1.input is None: {op1.input is None}")

            op2 = test.OptionalOperandOp(input=op1)
            # CHECK: op2.input is None: False
            print(f"op2.input is None: {op2.input is None}")
Exemple #5
0
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()

        # CHECK: [Type(i32), Type(i64)]
        iface = InferTypeOpInterface(op)
        print(iface.inferReturnTypes())

        # CHECK: [Type(i32), Type(i64)]
        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"