import gc
import sys
from mlir.ir import *
from mlir.dialects._ods_common import _cext
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
return f
@run
def testDialectDescriptor():
ctx = Context()
d = ctx.get_dialect_descriptor("func")
print(d)
print(d.namespace)
try:
_ = ctx.get_dialect_descriptor("not_existing")
except ValueError:
pass
else:
assert False, "Expected exception"
@run
def testUserDialectClass():
ctx = Context()
d = ctx.dialects.func
print(d)
try:
_ = ctx.dialects.not_existing
except AttributeError:
pass
else:
assert False, "Expected exception"
d = ctx.dialects["func"]
print(d)
try:
_ = ctx.dialects["not_existing"]
except IndexError:
pass
else:
assert False, "Expected exception"
d = ctx.d["func"]
print(d)
@run
def testCustomOpView():
def createInput():
op = Operation.create("pytest_dummy.intinput", results=[f32])
return op.results[0]
with Context() as ctx, Location.unknown():
ctx.allow_unregistered_dialects = True
m = Module.create()
with InsertionPoint(m.body):
f32 = F32Type.get()
input1 = createInput()
input2 = createInput()
op1 = ctx.dialects.arith.AddFOp(input1, input2)
from mlir.dialects.arith import AddFOp
AddFOp(input1, op1.result)
m.operation.print()
@run
def testIsRegisteredOperation():
ctx = Context()
print(f"cf.cond_br: {ctx.is_registered_operation('cf.cond_br')}")
print(f"func.not_existing: {ctx.is_registered_operation('func.not_existing')}")
@run
def testAppendPrefixSearchPath():
ctx = Context()
ctx.allow_unregistered_dialects = True
with Location.unknown(ctx):
assert not _cext.globals._check_dialect_module_loaded("custom")
Operation.create("custom.op")
assert not _cext.globals._check_dialect_module_loaded("custom")
sys.path.append(".")
_cext.globals.append_dialect_search_prefix("custom_dialect")
assert _cext.globals._check_dialect_module_loaded("custom")