aboutsummaryrefslogtreecommitdiff
path: root/mlir/test/python/ir/integer_set.py
blob: 9fe0480c33a2aefde774903240e8f2422bd1163c (plain)
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
# 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
    return f


# CHECK-LABEL: TEST: testIntegerSetCapsule
@run
def testIntegerSetCapsule():
    with Context() as ctx:
        is1 = IntegerSet.get_empty(1, 1, ctx)
    capsule = is1._CAPIPtr
    # CHECK: mlir.ir.IntegerSet._CAPIPtr
    print(capsule)
    is2 = IntegerSet._CAPICreate(capsule)
    assert is1 == is2
    assert is2.context is ctx


# CHECK-LABEL: TEST: testIntegerSetGet
@run
def testIntegerSetGet():
    with Context():
        d0 = AffineDimExpr.get(0)
        d1 = AffineDimExpr.get(1)
        s0 = AffineSymbolExpr.get(0)
        c42 = AffineConstantExpr.get(42)

        # CHECK: (d0, d1)[s0] : (d0 - d1 == 0, s0 - 42 >= 0)
        set0 = IntegerSet.get(2, 1, [d0 - d1, s0 - c42], [True, False])
        print(set0)

        # CHECK: (d0)[s0] : (1 == 0)
        set1 = IntegerSet.get_empty(1, 1)
        print(set1)

        # CHECK: (d0)[s0, s1] : (d0 - s1 == 0, s0 - 42 >= 0)
        set2 = set0.get_replaced([d0, AffineSymbolExpr.get(1)], [s0], 1, 2)
        print(set2)

        try:
            IntegerSet.get(2, 1, [], [])
        except ValueError as e:
            # CHECK: Expected non-empty list of constraints
            print(e)

        try:
            IntegerSet.get(2, 1, [d0 - d1], [True, False])
        except ValueError as e:
            # CHECK: Expected the number of constraints to match that of equality flags
            print(e)

        try:
            IntegerSet.get(2, 1, [0], [True])
        except RuntimeError as e:
            # CHECK: Invalid expression when attempting to create an IntegerSet
            print(e)

        try:
            IntegerSet.get(2, 1, [None], [True])
        except RuntimeError as e:
            # CHECK: Invalid expression (None?) when attempting to create an IntegerSet
            print(e)

        try:
            set0.get_replaced([d0], [s0], 1, 1)
        except ValueError as e:
            # CHECK: Expected the number of dimension replacement expressions to match that of dimensions
            print(e)

        try:
            set0.get_replaced([d0, d1], [s0, s0], 1, 1)
        except ValueError as e:
            # CHECK: Expected the number of symbol replacement expressions to match that of symbols
            print(e)

        try:
            set0.get_replaced([d0, 1], [s0], 1, 1)
        except RuntimeError as e:
            # CHECK: Invalid expression when attempting to create an IntegerSet by replacing dimensions
            print(e)

        try:
            set0.get_replaced([d0, d1], [None], 1, 1)
        except RuntimeError as e:
            # CHECK: Invalid expression (None?) when attempting to create an IntegerSet by replacing symbols
            print(e)


# CHECK-LABEL: TEST: testIntegerSetProperties
@run
def testIntegerSetProperties():
    with Context():
        d0 = AffineDimExpr.get(0)
        d1 = AffineDimExpr.get(1)
        s0 = AffineSymbolExpr.get(0)
        c42 = AffineConstantExpr.get(42)

        set0 = IntegerSet.get(2, 1, [d0 - d1, s0 - c42, s0 - d0], [True, False, False])
        # CHECK: 2
        print(set0.n_dims)
        # CHECK: 1
        print(set0.n_symbols)
        # CHECK: 3
        print(set0.n_inputs)
        # CHECK: 1
        print(set0.n_equalities)
        # CHECK: 2
        print(set0.n_inequalities)

        # CHECK: 3
        print(len(set0.constraints))

        # CHECK-DAG: d0 - d1 == 0
        # CHECK-DAG: s0 - 42 >= 0
        # CHECK-DAG: -d0 + s0 >= 0
        for cstr in set0.constraints:
            print(cstr.expr, end="")
            print(" == 0" if cstr.is_eq else " >= 0")


# TODO-LABEL: TEST: testHash
@run
def testHash():
    with Context():
        d0 = AffineDimExpr.get(0)
        d1 = AffineDimExpr.get(1)
        set = IntegerSet.get(2, 0, [d0 + d1], [True])

        assert hash(set) == hash(IntegerSet.get(2, 0, [d0 + d1], [True]))

        dictionary = dict()
        dictionary[set] = 42
        assert set in dictionary