Files
clang-p2996/mlir/test/python/ir/value.py
Alex Zinenko 78f2dae00d [mlir][python] Provide some methods and properties for API completeness
When writing the user-facing documentation, I noticed several inconsistencies
and asymmetries in the Python API we provide. Fix them by adding:

- the `owner` property to regions, similarly to blocks;
- the `isinstance` method to any class derived from `PyConcreteAttr`,
  `PyConcreteValue` and `PyConreteAffineExpr`, similar to `PyConcreteType` to
  enable `isa`-like calls without having to handle exceptions;
- a mechanism to create the first block in the region as we could only create
  blocks relative to other blocks, with is impossible in an empty region.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D111556
2021-10-13 14:30:55 +02:00

58 lines
1.5 KiB
Python

# RUN: %PYTHON %s | FileCheck %s
import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
return f
# CHECK-LABEL: TEST: testCapsuleConversions
@run
def testCapsuleConversions():
ctx = Context()
ctx.allow_unregistered_dialects = True
with Location.unknown(ctx):
i32 = IntegerType.get_signless(32)
value = Operation.create("custom.op1", results=[i32]).result
value_capsule = value._CAPIPtr
assert '"mlir.ir.Value._CAPIPtr"' in repr(value_capsule)
value2 = Value._CAPICreate(value_capsule)
assert value2 == value
# CHECK-LABEL: TEST: testOpResultOwner
@run
def testOpResultOwner():
ctx = Context()
ctx.allow_unregistered_dialects = True
with Location.unknown(ctx):
i32 = IntegerType.get_signless(32)
op = Operation.create("custom.op1", results=[i32])
assert op.result.owner == op
# CHECK-LABEL: TEST: testValueIsInstance
@run
def testValueIsInstance():
ctx = Context()
ctx.allow_unregistered_dialects = True
module = Module.parse(
r"""
func @foo(%arg0: f32) {
%0 = "some_dialect.some_op"() : () -> f64
return
}""", ctx)
func = module.body.operations[0]
assert BlockArgument.isinstance(func.regions[0].blocks[0].arguments[0])
assert not OpResult.isinstance(func.regions[0].blocks[0].arguments[0])
op = func.regions[0].blocks[0].operations[0]
assert not BlockArgument.isinstance(op.results[0])
assert OpResult.isinstance(op.results[0])