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")
@run
def testDialectLoadOnCreate():
with Context(load_on_create_dialects=[]) as ctx:
ctx.emit_error_diagnostics = True
ctx.allow_unregistered_dialects = True
def callback(d):
print(f"DIAGNOSTIC={d.message}")
return True
handler = ctx.attach_diagnostic_handler(callback)
loc = Location.unknown(ctx)
try:
op = Operation.create("arith.addi", loc=loc)
ctx.allow_unregistered_dialects = False
op.verify()
except MLIRError as e:
pass
with Context(load_on_create_dialects=["func"]) as ctx:
loc = Location.unknown(ctx)
fn = Operation.create("func.func", loc=loc)
print(f"Load on create: {get_load_on_create_dialects()}")
append_load_on_create_dialect("func")
print(f"Load on create: {get_load_on_create_dialects()}")
print(get_load_on_create_dialects())