aboutsummaryrefslogtreecommitdiff
path: root/mlir/test/python/ir/location.py
blob: 3e54dc922cd67fd8654514cf8e4b15332ec1bea5 (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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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


# CHECK-LABEL: TEST: testUnknown
def testUnknown():
    with Context() as ctx:
        loc = Location.unknown()
    assert loc.context is ctx
    ctx = None
    gc.collect()
    # CHECK: unknown str: loc(unknown)
    print("unknown str:", str(loc))
    # CHECK: unknown repr: loc(unknown)
    print("unknown repr:", repr(loc))


run(testUnknown)


# CHECK-LABEL: TEST: testLocationAttr
def testLocationAttr():
    with Context() as ctxt:
        loc = Location.unknown()
        attr = loc.attr
        clone = Location.from_attr(attr)
    gc.collect()
    # CHECK: loc: loc(unknown)
    print("loc:", str(loc))
    # CHECK: clone: loc(unknown)
    print("clone:", str(clone))
    assert loc == clone


run(testLocationAttr)


# CHECK-LABEL: TEST: testFileLineCol
def testFileLineCol():
    with Context() as ctx:
        loc = Location.file("foo1.txt", 123, 56)
        range = Location.file("foo2.txt", 123, 56, 124, 100)

    ctx = None
    gc.collect()

    # CHECK: file str: loc("foo1.txt":123:56)
    print("file str:", str(loc))
    # CHECK: file repr: loc("foo1.txt":123:56)
    print("file repr:", repr(loc))
    # CHECK: file range str: loc("foo2.txt":123:56 to 124:100)
    print("file range str:", str(range))
    # CHECK: file range repr: loc("foo2.txt":123:56 to 124:100)
    print("file range repr:", repr(range))

    assert loc.is_a_file()
    assert not loc.is_a_name()
    assert not loc.is_a_callsite()
    assert not loc.is_a_fused()

    # CHECK: file filename: foo1.txt
    print("file filename:", loc.filename)
    # CHECK: file start_line: 123
    print("file start_line:", loc.start_line)
    # CHECK: file start_col: 56
    print("file start_col:", loc.start_col)
    # CHECK: file end_line: 123
    print("file end_line:", loc.end_line)
    # CHECK: file end_col: 56
    print("file end_col:", loc.end_col)

    assert range.is_a_file()
    # CHECK: file filename: foo2.txt
    print("file filename:", range.filename)
    # CHECK: file start_line: 123
    print("file start_line:", range.start_line)
    # CHECK: file start_col: 56
    print("file start_col:", range.start_col)
    # CHECK: file end_line: 124
    print("file end_line:", range.end_line)
    # CHECK: file end_col: 100
    print("file end_col:", range.end_col)

    with Context() as ctx:
        ctx.allow_unregistered_dialects = True
        loc = Location.file("foo3.txt", 127, 61)
        with loc:
            i32 = IntegerType.get_signless(32)
            module = Module.create()
            with InsertionPoint(module.body):
                new_value = Operation.create("custom.op1", results=[i32]).result
                # CHECK: new_value location: loc("foo3.txt":127:61)
                print("new_value location: ", new_value.location)


run(testFileLineCol)


# CHECK-LABEL: TEST: testName
def testName():
    with Context() as ctx:
        loc = Location.name("nombre")
        loc_with_child_loc = Location.name("naam", loc)

    ctx = None
    gc.collect()

    # CHECK: name str: loc("nombre")
    print("name str:", str(loc))
    # CHECK: name repr: loc("nombre")
    print("name repr:", repr(loc))
    # CHECK: name str: loc("naam"("nombre"))
    print("name str:", str(loc_with_child_loc))
    # CHECK: name repr: loc("naam"("nombre"))
    print("name repr:", repr(loc_with_child_loc))

    assert loc.is_a_name()
    # CHECK: name name_str: nombre
    print("name name_str:", loc.name_str)
    # CHECK: name child_loc: loc(unknown)
    print("name child_loc:", loc.child_loc)

    assert loc_with_child_loc.is_a_name()
    # CHECK: name name_str: naam
    print("name name_str:", loc_with_child_loc.name_str)
    # CHECK: name child_loc_with_child_loc: loc("nombre")
    print("name child_loc_with_child_loc:", loc_with_child_loc.child_loc)


run(testName)


# CHECK-LABEL: TEST: testCallSite
def testCallSite():
    with Context() as ctx:
        loc = Location.callsite(
            Location.file("foo.text", 123, 45),
            [Location.file("util.foo", 379, 21), Location.file("main.foo", 100, 63)],
        )
    ctx = None
    # CHECK: callsite str: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))
    print("callsite str:", str(loc))
    # CHECK: callsite repr: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))
    print("callsite repr:", repr(loc))

    assert loc.is_a_callsite()

    # CHECK: callsite callee: loc("foo.text":123:45)
    print("callsite callee:", loc.callee)
    # CHECK: callsite caller: loc(callsite("util.foo":379:21 at "main.foo":100:63))
    print("callsite caller:", loc.caller)


run(testCallSite)


# CHECK-LABEL: TEST: testFused
def testFused():
    with Context() as ctx:
        loc_single = Location.fused([Location.name("apple")])
        loc = Location.fused([Location.name("apple"), Location.name("banana")])
        attr = Attribute.parse('"sauteed"')
        loc_attr = Location.fused(
            [Location.name("carrot"), Location.name("potatoes")], attr
        )
        loc_empty = Location.fused([])
        loc_empty_attr = Location.fused([], attr)
        loc_single_attr = Location.fused([Location.name("apple")], attr)

    ctx = None

    assert not loc_single.is_a_fused()
    # CHECK: fused str: loc("apple")
    print("fused str:", str(loc_single))
    # CHECK: fused repr: loc("apple")
    print("fused repr:", repr(loc_single))
    # # CHECK: fused locations: []
    print("fused locations:", loc_single.locations)

    assert loc.is_a_fused()
    # CHECK: fused str: loc(fused["apple", "banana"])
    print("fused str:", str(loc))
    # CHECK: fused repr: loc(fused["apple", "banana"])
    print("fused repr:", repr(loc))
    # CHECK: fused locations: [loc("apple"), loc("banana")]
    print("fused locations:", loc.locations)

    assert loc_attr.is_a_fused()
    # CHECK: fused str: loc(fused<"sauteed">["carrot", "potatoes"])
    print("fused str:", str(loc_attr))
    # CHECK: fused repr: loc(fused<"sauteed">["carrot", "potatoes"])
    print("fused repr:", repr(loc_attr))
    # CHECK: fused locations: [loc("carrot"), loc("potatoes")]
    print("fused locations:", loc_attr.locations)

    assert not loc_empty.is_a_fused()
    # CHECK: fused str: loc(unknown)
    print("fused str:", str(loc_empty))
    # CHECK: fused repr: loc(unknown)
    print("fused repr:", repr(loc_empty))
    # CHECK: fused locations: []
    print("fused locations:", loc_empty.locations)

    assert loc_empty_attr.is_a_fused()
    # CHECK: fused str: loc(fused<"sauteed">[unknown])
    print("fused str:", str(loc_empty_attr))
    # CHECK: fused repr: loc(fused<"sauteed">[unknown])
    print("fused repr:", repr(loc_empty_attr))
    # CHECK: fused locations: [loc(unknown)]
    print("fused locations:", loc_empty_attr.locations)

    assert loc_single_attr.is_a_fused()
    # CHECK: fused str: loc(fused<"sauteed">["apple"])
    print("fused str:", str(loc_single_attr))
    # CHECK: fused repr: loc(fused<"sauteed">["apple"])
    print("fused repr:", repr(loc_single_attr))
    # CHECK: fused locations: [loc("apple")]
    print("fused locations:", loc_single_attr.locations)


run(testFused)


# CHECK-LABEL: TEST: testLocationCapsule
def testLocationCapsule():
    with Context() as ctx:
        loc1 = Location.file("foo.txt", 123, 56)
    # CHECK: mlir.ir.Location._CAPIPtr
    loc_capsule = loc1._CAPIPtr
    print(loc_capsule)
    loc2 = Location._CAPICreate(loc_capsule)
    assert loc2 == loc1
    assert loc2.context is ctx


run(testLocationCapsule)