1 # RUN: %PYTHON %s | FileCheck %s
6 from mlir
.dialects
._ods
_common
import _cext
10 print("\nTEST:", f
.__name
__)
13 assert Context
._get
_live
_count
() == 0
17 # CHECK-LABEL: TEST: testDialectDescriptor
19 def testDialectDescriptor():
21 d
= ctx
.get_dialect_descriptor("func")
22 # CHECK: <DialectDescriptor func>
27 _
= ctx
.get_dialect_descriptor("not_existing")
31 assert False, "Expected exception"
34 # CHECK-LABEL: TEST: testUserDialectClass
36 def testUserDialectClass():
38 # Access using attribute.
40 # CHECK: <Dialect func (class mlir.dialects._func_ops_gen._Dialect)>
43 _
= ctx
.dialects
.not_existing
44 except AttributeError:
47 assert False, "Expected exception"
50 d
= ctx
.dialects
["func"]
51 # CHECK: <Dialect func (class mlir.dialects._func_ops_gen._Dialect)>
54 _
= ctx
.dialects
["not_existing"]
58 assert False, "Expected exception"
60 # Using the 'd' alias.
62 # CHECK: <Dialect func (class mlir.dialects._func_ops_gen._Dialect)>
66 # CHECK-LABEL: TEST: testCustomOpView
67 # This test uses the standard dialect AddFOp as an example of a user op.
68 # TODO: Op creation and access is still quite verbose: simplify this test as
69 # additional capabilities come online.
71 def testCustomOpView():
73 op
= Operation
.create("pytest_dummy.intinput", results
=[f32
])
74 # TODO: Auto result cast from operation
77 with
Context() as ctx
, Location
.unknown():
78 ctx
.allow_unregistered_dialects
= True
81 with
InsertionPoint(m
.body
):
83 # Create via dialects context collection.
84 input1
= createInput()
85 input2
= createInput()
86 op1
= ctx
.dialects
.arith
.AddFOp(input1
, input2
)
88 # Create via an import
89 from mlir
.dialects
.arith
import AddFOp
91 AddFOp(input1
, op1
.result
)
93 # CHECK: %[[INPUT0:.*]] = "pytest_dummy.intinput"
94 # CHECK: %[[INPUT1:.*]] = "pytest_dummy.intinput"
95 # CHECK: %[[R0:.*]] = arith.addf %[[INPUT0]], %[[INPUT1]] : f32
96 # CHECK: %[[R1:.*]] = arith.addf %[[INPUT0]], %[[R0]] : f32
100 # CHECK-LABEL: TEST: testIsRegisteredOperation
102 def testIsRegisteredOperation():
105 # CHECK: cf.cond_br: True
106 print(f
"cf.cond_br: {ctx.is_registered_operation('cf.cond_br')}")
107 # CHECK: func.not_existing: False
108 print(f
"func.not_existing: {ctx.is_registered_operation('func.not_existing')}")
111 # CHECK-LABEL: TEST: testAppendPrefixSearchPath
113 def testAppendPrefixSearchPath():
115 ctx
.allow_unregistered_dialects
= True
116 with Location
.unknown(ctx
):
117 assert not _cext
.globals._check
_dialect
_module
_loaded
("custom")
118 Operation
.create("custom.op")
119 assert not _cext
.globals._check
_dialect
_module
_loaded
("custom")
122 _cext
.globals.append_dialect_search_prefix("custom_dialect")
123 assert _cext
.globals._check
_dialect
_module
_loaded
("custom")
126 # CHECK-LABEL: TEST: testDialectLoadOnCreate
128 def testDialectLoadOnCreate():
129 with
Context(load_on_create_dialects
=[]) as ctx
:
130 ctx
.emit_error_diagnostics
= True
131 ctx
.allow_unregistered_dialects
= True
135 # CHECK-SAME: op created with unregistered dialect
136 print(f
"DIAGNOSTIC={d.message}")
139 handler
= ctx
.attach_diagnostic_handler(callback
)
140 loc
= Location
.unknown(ctx
)
142 op
= Operation
.create("arith.addi", loc
=loc
)
143 ctx
.allow_unregistered_dialects
= False
145 except MLIRError
as e
:
148 with
Context(load_on_create_dialects
=["func"]) as ctx
:
149 loc
= Location
.unknown(ctx
)
150 fn
= Operation
.create("func.func", loc
=loc
)
152 # TODO: This may require an update if a site wide policy is set.
153 # CHECK: Load on create: []
154 print(f
"Load on create: {get_load_on_create_dialects()}")
155 append_load_on_create_dialect("func")
156 # CHECK: Load on create:
158 print(f
"Load on create: {get_load_on_create_dialects()}")
159 print(get_load_on_create_dialects())