1# RUN: %PYTHON %s | FileCheck %s
2
3import gc
4from mlir.ir import *
5
6
7def run(f):
8  print("\nTEST:", f.__name__)
9  f()
10  gc.collect()
11  assert Context._get_live_count() == 0
12  return f
13
14
15# CHECK-LABEL: TEST: testCapsuleConversions
16@run
17def testCapsuleConversions():
18  ctx = Context()
19  ctx.allow_unregistered_dialects = True
20  with Location.unknown(ctx):
21    i32 = IntegerType.get_signless(32)
22    value = Operation.create("custom.op1", results=[i32]).result
23    value_capsule = value._CAPIPtr
24    assert '"mlir.ir.Value._CAPIPtr"' in repr(value_capsule)
25    value2 = Value._CAPICreate(value_capsule)
26    assert value2 == value
27
28
29# CHECK-LABEL: TEST: testOpResultOwner
30@run
31def testOpResultOwner():
32  ctx = Context()
33  ctx.allow_unregistered_dialects = True
34  with Location.unknown(ctx):
35    i32 = IntegerType.get_signless(32)
36    op = Operation.create("custom.op1", results=[i32])
37    assert op.result.owner == op
38
39
40# CHECK-LABEL: TEST: testValueIsInstance
41@run
42def testValueIsInstance():
43  ctx = Context()
44  ctx.allow_unregistered_dialects = True
45  module = Module.parse(
46      r"""
47    func.func @foo(%arg0: f32) {
48      %0 = "some_dialect.some_op"() : () -> f64
49      return
50    }""", ctx)
51  func = module.body.operations[0]
52  assert BlockArgument.isinstance(func.regions[0].blocks[0].arguments[0])
53  assert not OpResult.isinstance(func.regions[0].blocks[0].arguments[0])
54
55  op = func.regions[0].blocks[0].operations[0]
56  assert not BlockArgument.isinstance(op.results[0])
57  assert OpResult.isinstance(op.results[0])
58
59
60# CHECK-LABEL: TEST: testValueHash
61@run
62def testValueHash():
63  ctx = Context()
64  ctx.allow_unregistered_dialects = True
65  module = Module.parse(
66      r"""
67    func.func @foo(%arg0: f32) -> f32 {
68      %0 = "some_dialect.some_op"(%arg0) : (f32) -> f32
69      return %0 : f32
70    }""", ctx)
71
72  [func] = module.body.operations
73  block = func.entry_block
74  op, ret = block.operations
75  assert hash(block.arguments[0]) == hash(op.operands[0])
76  assert hash(op.result) == hash(ret.operands[0])
77