summaryrefslogtreecommitdiff
path: root/mlir/test/python/dialects/pdl_types.py
blob: 16a41e2a4c1ce176c0f11380a3312a0b62b9aadf (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
143
144
145
146
147
148
149
150
# RUN: %PYTHON %s | FileCheck %s

from mlir.ir import *
from mlir.dialects import pdl


def run(f):
  print("\nTEST:", f.__name__)
  f()
  return f


# CHECK-LABEL: TEST: test_attribute_type
@run
def test_attribute_type():
  with Context():
    parsedType = Type.parse("!pdl.attribute")
    constructedType = pdl.AttributeType.get()

    assert pdl.AttributeType.isinstance(parsedType)
    assert not pdl.OperationType.isinstance(parsedType)
    assert not pdl.RangeType.isinstance(parsedType)
    assert not pdl.TypeType.isinstance(parsedType)
    assert not pdl.ValueType.isinstance(parsedType)

    assert pdl.AttributeType.isinstance(constructedType)
    assert not pdl.OperationType.isinstance(constructedType)
    assert not pdl.RangeType.isinstance(constructedType)
    assert not pdl.TypeType.isinstance(constructedType)
    assert not pdl.ValueType.isinstance(constructedType)

    assert parsedType == constructedType

    # CHECK: !pdl.attribute
    print(parsedType)
    # CHECK: !pdl.attribute
    print(constructedType)


# CHECK-LABEL: TEST: test_operation_type
@run
def test_operation_type():
  with Context():
    parsedType = Type.parse("!pdl.operation")
    constructedType = pdl.OperationType.get()

    assert not pdl.AttributeType.isinstance(parsedType)
    assert pdl.OperationType.isinstance(parsedType)
    assert not pdl.RangeType.isinstance(parsedType)
    assert not pdl.TypeType.isinstance(parsedType)
    assert not pdl.ValueType.isinstance(parsedType)

    assert not pdl.AttributeType.isinstance(constructedType)
    assert pdl.OperationType.isinstance(constructedType)
    assert not pdl.RangeType.isinstance(constructedType)
    assert not pdl.TypeType.isinstance(constructedType)
    assert not pdl.ValueType.isinstance(constructedType)

    assert parsedType == constructedType

    # CHECK: !pdl.operation
    print(parsedType)
    # CHECK: !pdl.operation
    print(constructedType)


# CHECK-LABEL: TEST: test_range_type
@run
def test_range_type():
  with Context():
    typeType = Type.parse("!pdl.type")
    parsedType = Type.parse("!pdl.range<type>")
    constructedType = pdl.RangeType.get(typeType)
    elementType = constructedType.element_type

    assert not pdl.AttributeType.isinstance(parsedType)
    assert not pdl.OperationType.isinstance(parsedType)
    assert pdl.RangeType.isinstance(parsedType)
    assert not pdl.TypeType.isinstance(parsedType)
    assert not pdl.ValueType.isinstance(parsedType)

    assert not pdl.AttributeType.isinstance(constructedType)
    assert not pdl.OperationType.isinstance(constructedType)
    assert pdl.RangeType.isinstance(constructedType)
    assert not pdl.TypeType.isinstance(constructedType)
    assert not pdl.ValueType.isinstance(constructedType)

    assert parsedType == constructedType
    assert elementType == typeType

    # CHECK: !pdl.range<type>
    print(parsedType)
    # CHECK: !pdl.range<type>
    print(constructedType)
    # CHECK: !pdl.type
    print(elementType)


# CHECK-LABEL: TEST: test_type_type
@run
def test_type_type():
  with Context():
    parsedType = Type.parse("!pdl.type")
    constructedType = pdl.TypeType.get()

    assert not pdl.AttributeType.isinstance(parsedType)
    assert not pdl.OperationType.isinstance(parsedType)
    assert not pdl.RangeType.isinstance(parsedType)
    assert pdl.TypeType.isinstance(parsedType)
    assert not pdl.ValueType.isinstance(parsedType)

    assert not pdl.AttributeType.isinstance(constructedType)
    assert not pdl.OperationType.isinstance(constructedType)
    assert not pdl.RangeType.isinstance(constructedType)
    assert pdl.TypeType.isinstance(constructedType)
    assert not pdl.ValueType.isinstance(constructedType)

    assert parsedType == constructedType

    # CHECK: !pdl.type
    print(parsedType)
    # CHECK: !pdl.type
    print(constructedType)


# CHECK-LABEL: TEST: test_value_type
@run
def test_value_type():
  with Context():
    parsedType = Type.parse("!pdl.value")
    constructedType = pdl.ValueType.get()

    assert not pdl.AttributeType.isinstance(parsedType)
    assert not pdl.OperationType.isinstance(parsedType)
    assert not pdl.RangeType.isinstance(parsedType)
    assert not pdl.TypeType.isinstance(parsedType)
    assert pdl.ValueType.isinstance(parsedType)

    assert not pdl.AttributeType.isinstance(constructedType)
    assert not pdl.OperationType.isinstance(constructedType)
    assert not pdl.RangeType.isinstance(constructedType)
    assert not pdl.TypeType.isinstance(constructedType)
    assert pdl.ValueType.isinstance(constructedType)

    assert parsedType == constructedType

    # CHECK: !pdl.value
    print(parsedType)
    # CHECK: !pdl.value
    print(constructedType)