246 lines · python
1# RUN: %PYTHON %s | FileCheck %s2 3import gc4from mlir.ir import *5 6 7def run(f):8 print("\nTEST:", f.__name__)9 f()10 gc.collect()11 assert Context._get_live_count() == 012 13 14# CHECK-LABEL: TEST: testUnknown15def testUnknown():16 with Context() as ctx:17 loc = Location.unknown()18 assert loc.context is ctx19 ctx = None20 gc.collect()21 # CHECK: unknown str: loc(unknown)22 print("unknown str:", str(loc))23 # CHECK: unknown repr: loc(unknown)24 print("unknown repr:", repr(loc))25 26 27run(testUnknown)28 29 30# CHECK-LABEL: TEST: testLocationAttr31def testLocationAttr():32 with Context() as ctxt:33 loc = Location.unknown()34 attr = loc.attr35 clone = Location.from_attr(attr)36 gc.collect()37 # CHECK: loc: loc(unknown)38 print("loc:", str(loc))39 # CHECK: clone: loc(unknown)40 print("clone:", str(clone))41 assert loc == clone42 43 44run(testLocationAttr)45 46 47# CHECK-LABEL: TEST: testFileLineCol48def testFileLineCol():49 with Context() as ctx:50 loc = Location.file("foo1.txt", 123, 56)51 range = Location.file("foo2.txt", 123, 56, 124, 100)52 53 ctx = None54 gc.collect()55 56 # CHECK: file str: loc("foo1.txt":123:56)57 print("file str:", str(loc))58 # CHECK: file repr: loc("foo1.txt":123:56)59 print("file repr:", repr(loc))60 # CHECK: file range str: loc("foo2.txt":123:56 to 124:100)61 print("file range str:", str(range))62 # CHECK: file range repr: loc("foo2.txt":123:56 to 124:100)63 print("file range repr:", repr(range))64 65 assert loc.is_a_file()66 assert not loc.is_a_name()67 assert not loc.is_a_callsite()68 assert not loc.is_a_fused()69 70 # CHECK: file filename: foo1.txt71 print("file filename:", loc.filename)72 # CHECK: file start_line: 12373 print("file start_line:", loc.start_line)74 # CHECK: file start_col: 5675 print("file start_col:", loc.start_col)76 # CHECK: file end_line: 12377 print("file end_line:", loc.end_line)78 # CHECK: file end_col: 5679 print("file end_col:", loc.end_col)80 81 assert range.is_a_file()82 # CHECK: file filename: foo2.txt83 print("file filename:", range.filename)84 # CHECK: file start_line: 12385 print("file start_line:", range.start_line)86 # CHECK: file start_col: 5687 print("file start_col:", range.start_col)88 # CHECK: file end_line: 12489 print("file end_line:", range.end_line)90 # CHECK: file end_col: 10091 print("file end_col:", range.end_col)92 93 with Context() as ctx:94 ctx.allow_unregistered_dialects = True95 loc = Location.file("foo3.txt", 127, 61)96 with loc:97 i32 = IntegerType.get_signless(32)98 module = Module.create()99 with InsertionPoint(module.body):100 new_value = Operation.create("custom.op1", results=[i32]).result101 # CHECK: new_value location: loc("foo3.txt":127:61)102 print("new_value location: ", new_value.location)103 104 105run(testFileLineCol)106 107 108# CHECK-LABEL: TEST: testName109def testName():110 with Context() as ctx:111 loc = Location.name("nombre")112 loc_with_child_loc = Location.name("naam", loc)113 114 ctx = None115 gc.collect()116 117 # CHECK: name str: loc("nombre")118 print("name str:", str(loc))119 # CHECK: name repr: loc("nombre")120 print("name repr:", repr(loc))121 # CHECK: name str: loc("naam"("nombre"))122 print("name str:", str(loc_with_child_loc))123 # CHECK: name repr: loc("naam"("nombre"))124 print("name repr:", repr(loc_with_child_loc))125 126 assert loc.is_a_name()127 # CHECK: name name_str: nombre128 print("name name_str:", loc.name_str)129 # CHECK: name child_loc: loc(unknown)130 print("name child_loc:", loc.child_loc)131 132 assert loc_with_child_loc.is_a_name()133 # CHECK: name name_str: naam134 print("name name_str:", loc_with_child_loc.name_str)135 # CHECK: name child_loc_with_child_loc: loc("nombre")136 print("name child_loc_with_child_loc:", loc_with_child_loc.child_loc)137 138 139run(testName)140 141 142# CHECK-LABEL: TEST: testCallSite143def testCallSite():144 with Context() as ctx:145 loc = Location.callsite(146 Location.file("foo.text", 123, 45),147 [Location.file("util.foo", 379, 21), Location.file("main.foo", 100, 63)],148 )149 ctx = None150 # CHECK: callsite str: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))151 print("callsite str:", str(loc))152 # CHECK: callsite repr: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))153 print("callsite repr:", repr(loc))154 155 assert loc.is_a_callsite()156 157 # CHECK: callsite callee: loc("foo.text":123:45)158 print("callsite callee:", loc.callee)159 # CHECK: callsite caller: loc(callsite("util.foo":379:21 at "main.foo":100:63))160 print("callsite caller:", loc.caller)161 162 163run(testCallSite)164 165 166# CHECK-LABEL: TEST: testFused167def testFused():168 with Context() as ctx:169 loc_single = Location.fused([Location.name("apple")])170 loc = Location.fused([Location.name("apple"), Location.name("banana")])171 attr = Attribute.parse('"sauteed"')172 loc_attr = Location.fused(173 [Location.name("carrot"), Location.name("potatoes")], attr174 )175 loc_empty = Location.fused([])176 loc_empty_attr = Location.fused([], attr)177 loc_single_attr = Location.fused([Location.name("apple")], attr)178 179 ctx = None180 181 assert not loc_single.is_a_fused()182 # CHECK: fused str: loc("apple")183 print("fused str:", str(loc_single))184 # CHECK: fused repr: loc("apple")185 print("fused repr:", repr(loc_single))186 # # CHECK: fused locations: []187 print("fused locations:", loc_single.locations)188 189 assert loc.is_a_fused()190 # CHECK: fused str: loc(fused["apple", "banana"])191 print("fused str:", str(loc))192 # CHECK: fused repr: loc(fused["apple", "banana"])193 print("fused repr:", repr(loc))194 # CHECK: fused locations: [loc("apple"), loc("banana")]195 print("fused locations:", loc.locations)196 197 assert loc_attr.is_a_fused()198 # CHECK: fused str: loc(fused<"sauteed">["carrot", "potatoes"])199 print("fused str:", str(loc_attr))200 # CHECK: fused repr: loc(fused<"sauteed">["carrot", "potatoes"])201 print("fused repr:", repr(loc_attr))202 # CHECK: fused locations: [loc("carrot"), loc("potatoes")]203 print("fused locations:", loc_attr.locations)204 205 assert not loc_empty.is_a_fused()206 # CHECK: fused str: loc(unknown)207 print("fused str:", str(loc_empty))208 # CHECK: fused repr: loc(unknown)209 print("fused repr:", repr(loc_empty))210 # CHECK: fused locations: []211 print("fused locations:", loc_empty.locations)212 213 assert loc_empty_attr.is_a_fused()214 # CHECK: fused str: loc(fused<"sauteed">[unknown])215 print("fused str:", str(loc_empty_attr))216 # CHECK: fused repr: loc(fused<"sauteed">[unknown])217 print("fused repr:", repr(loc_empty_attr))218 # CHECK: fused locations: [loc(unknown)]219 print("fused locations:", loc_empty_attr.locations)220 221 assert loc_single_attr.is_a_fused()222 # CHECK: fused str: loc(fused<"sauteed">["apple"])223 print("fused str:", str(loc_single_attr))224 # CHECK: fused repr: loc(fused<"sauteed">["apple"])225 print("fused repr:", repr(loc_single_attr))226 # CHECK: fused locations: [loc("apple")]227 print("fused locations:", loc_single_attr.locations)228 229 230run(testFused)231 232 233# CHECK-LABEL: TEST: testLocationCapsule234def testLocationCapsule():235 with Context() as ctx:236 loc1 = Location.file("foo.txt", 123, 56)237 # CHECK: mlir.ir.Location._CAPIPtr238 loc_capsule = loc1._CAPIPtr239 print(loc_capsule)240 loc2 = Location._CAPICreate(loc_capsule)241 assert loc2 == loc1242 assert loc2.context is ctx243 244 245run(testLocationCapsule)246