xref: /llvm-project/mlir/test/python/ir/value.py (revision 21df32511b558b2c1e24fe23f677fffaad4da333)
175453714SJacques Pienaar# RUN: %PYTHON %s | FileCheck %s --enable-var-scope=false
29f3f6d7bSStella Laurenzo
39f3f6d7bSStella Laurenzoimport gc
49f3f6d7bSStella Laurenzofrom mlir.ir import *
581233c70Smaxfrom mlir.dialects import func
69f3f6d7bSStella Laurenzo
79f3f6d7bSStella Laurenzo
89f3f6d7bSStella Laurenzodef run(f):
99f3f6d7bSStella Laurenzo    print("\nTEST:", f.__name__)
109f3f6d7bSStella Laurenzo    f()
119f3f6d7bSStella Laurenzo    gc.collect()
129f3f6d7bSStella Laurenzo    assert Context._get_live_count() == 0
1378f2dae0SAlex Zinenko    return f
149f3f6d7bSStella Laurenzo
159f3f6d7bSStella Laurenzo
169f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testCapsuleConversions
1778f2dae0SAlex Zinenko@run
189f3f6d7bSStella Laurenzodef testCapsuleConversions():
199f3f6d7bSStella Laurenzo    ctx = Context()
209f3f6d7bSStella Laurenzo    ctx.allow_unregistered_dialects = True
219f3f6d7bSStella Laurenzo    with Location.unknown(ctx):
229f3f6d7bSStella Laurenzo        i32 = IntegerType.get_signless(32)
239f3f6d7bSStella Laurenzo        value = Operation.create("custom.op1", results=[i32]).result
249f3f6d7bSStella Laurenzo        value_capsule = value._CAPIPtr
259f3f6d7bSStella Laurenzo        assert '"mlir.ir.Value._CAPIPtr"' in repr(value_capsule)
269f3f6d7bSStella Laurenzo        value2 = Value._CAPICreate(value_capsule)
279f3f6d7bSStella Laurenzo        assert value2 == value
289f3f6d7bSStella Laurenzo
299f3f6d7bSStella Laurenzo
309f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testOpResultOwner
3178f2dae0SAlex Zinenko@run
329f3f6d7bSStella Laurenzodef testOpResultOwner():
339f3f6d7bSStella Laurenzo    ctx = Context()
349f3f6d7bSStella Laurenzo    ctx.allow_unregistered_dialects = True
359f3f6d7bSStella Laurenzo    with Location.unknown(ctx):
369f3f6d7bSStella Laurenzo        i32 = IntegerType.get_signless(32)
379f3f6d7bSStella Laurenzo        op = Operation.create("custom.op1", results=[i32])
389f3f6d7bSStella Laurenzo        assert op.result.owner == op
399f3f6d7bSStella Laurenzo
409f3f6d7bSStella Laurenzo
41d747a170SJohn Demme# CHECK-LABEL: TEST: testBlockArgOwner
42d747a170SJohn Demme@run
43d747a170SJohn Demmedef testBlockArgOwner():
44d747a170SJohn Demme    ctx = Context()
45d747a170SJohn Demme    ctx.allow_unregistered_dialects = True
46d747a170SJohn Demme    module = Module.parse(
47d747a170SJohn Demme        r"""
48d747a170SJohn Demme    func.func @foo(%arg0: f32) {
49d747a170SJohn Demme      return
50f9008e63STobias Hieta    }""",
51f9008e63STobias Hieta        ctx,
52f9008e63STobias Hieta    )
53d747a170SJohn Demme    func = module.body.operations[0]
54d747a170SJohn Demme    block = func.regions[0].blocks[0]
55d747a170SJohn Demme    assert block.arguments[0].owner == block
56d747a170SJohn Demme
57d747a170SJohn Demme
5878f2dae0SAlex Zinenko# CHECK-LABEL: TEST: testValueIsInstance
5978f2dae0SAlex Zinenko@run
6078f2dae0SAlex Zinenkodef testValueIsInstance():
6178f2dae0SAlex Zinenko    ctx = Context()
6278f2dae0SAlex Zinenko    ctx.allow_unregistered_dialects = True
6378f2dae0SAlex Zinenko    module = Module.parse(
6478f2dae0SAlex Zinenko        r"""
652310ced8SRiver Riddle    func.func @foo(%arg0: f32) {
6678f2dae0SAlex Zinenko      %0 = "some_dialect.some_op"() : () -> f64
6778f2dae0SAlex Zinenko      return
68f9008e63STobias Hieta    }""",
69f9008e63STobias Hieta        ctx,
70f9008e63STobias Hieta    )
7178f2dae0SAlex Zinenko    func = module.body.operations[0]
7278f2dae0SAlex Zinenko    assert BlockArgument.isinstance(func.regions[0].blocks[0].arguments[0])
7378f2dae0SAlex Zinenko    assert not OpResult.isinstance(func.regions[0].blocks[0].arguments[0])
7478f2dae0SAlex Zinenko
7578f2dae0SAlex Zinenko    op = func.regions[0].blocks[0].operations[0]
7678f2dae0SAlex Zinenko    assert not BlockArgument.isinstance(op.results[0])
7778f2dae0SAlex Zinenko    assert OpResult.isinstance(op.results[0])
78f78fe0b7Srkayaith
79f78fe0b7Srkayaith
80f78fe0b7Srkayaith# CHECK-LABEL: TEST: testValueHash
81f78fe0b7Srkayaith@run
82f78fe0b7Srkayaithdef testValueHash():
83f78fe0b7Srkayaith    ctx = Context()
84f78fe0b7Srkayaith    ctx.allow_unregistered_dialects = True
85f78fe0b7Srkayaith    module = Module.parse(
86f78fe0b7Srkayaith        r"""
872310ced8SRiver Riddle    func.func @foo(%arg0: f32) -> f32 {
88f78fe0b7Srkayaith      %0 = "some_dialect.some_op"(%arg0) : (f32) -> f32
89f78fe0b7Srkayaith      return %0 : f32
90f9008e63STobias Hieta    }""",
91f9008e63STobias Hieta        ctx,
92f9008e63STobias Hieta    )
93f78fe0b7Srkayaith
94f78fe0b7Srkayaith    [func] = module.body.operations
95f78fe0b7Srkayaith    block = func.entry_block
96f78fe0b7Srkayaith    op, ret = block.operations
97f78fe0b7Srkayaith    assert hash(block.arguments[0]) == hash(op.operands[0])
98f78fe0b7Srkayaith    assert hash(op.result) == hash(ret.operands[0])
99afb2ed80SMike Urbach
10081233c70Smax
101afb2ed80SMike Urbach# CHECK-LABEL: TEST: testValueUses
102afb2ed80SMike Urbach@run
103afb2ed80SMike Urbachdef testValueUses():
104afb2ed80SMike Urbach    ctx = Context()
105afb2ed80SMike Urbach    ctx.allow_unregistered_dialects = True
106afb2ed80SMike Urbach    with Location.unknown(ctx):
107afb2ed80SMike Urbach        i32 = IntegerType.get_signless(32)
108afb2ed80SMike Urbach        module = Module.create()
109afb2ed80SMike Urbach        with InsertionPoint(module.body):
110afb2ed80SMike Urbach            value = Operation.create("custom.op1", results=[i32]).results[0]
111afb2ed80SMike Urbach            op1 = Operation.create("custom.op2", operands=[value])
112afb2ed80SMike Urbach            op2 = Operation.create("custom.op2", operands=[value])
113afb2ed80SMike Urbach
114afb2ed80SMike Urbach    # CHECK: Use owner: "custom.op2"
115afb2ed80SMike Urbach    # CHECK: Use operand_number: 0
116afb2ed80SMike Urbach    # CHECK: Use owner: "custom.op2"
117afb2ed80SMike Urbach    # CHECK: Use operand_number: 0
118afb2ed80SMike Urbach    for use in value.uses:
119afb2ed80SMike Urbach        assert use.owner in [op1, op2]
120afb2ed80SMike Urbach        print(f"Use owner: {use.owner}")
121afb2ed80SMike Urbach        print(f"Use operand_number: {use.operand_number}")
1225b303f21Smax
12381233c70Smax
1245b303f21Smax# CHECK-LABEL: TEST: testValueReplaceAllUsesWith
1255b303f21Smax@run
1265b303f21Smaxdef testValueReplaceAllUsesWith():
1275b303f21Smax    ctx = Context()
1285b303f21Smax    ctx.allow_unregistered_dialects = True
1295b303f21Smax    with Location.unknown(ctx):
1305b303f21Smax        i32 = IntegerType.get_signless(32)
1315b303f21Smax        module = Module.create()
1325b303f21Smax        with InsertionPoint(module.body):
1335b303f21Smax            value = Operation.create("custom.op1", results=[i32]).results[0]
1345b303f21Smax            op1 = Operation.create("custom.op2", operands=[value])
1355b303f21Smax            op2 = Operation.create("custom.op2", operands=[value])
1365b303f21Smax            value2 = Operation.create("custom.op3", results=[i32]).results[0]
1375b303f21Smax            value.replace_all_uses_with(value2)
1385b303f21Smax
1395b303f21Smax    assert len(list(value.uses)) == 0
1405b303f21Smax
1415b303f21Smax    # CHECK: Use owner: "custom.op2"
1425b303f21Smax    # CHECK: Use operand_number: 0
1435b303f21Smax    # CHECK: Use owner: "custom.op2"
1445b303f21Smax    # CHECK: Use operand_number: 0
1455b303f21Smax    for use in value2.uses:
1465b303f21Smax        assert use.owner in [op1, op2]
1475b303f21Smax        print(f"Use owner: {use.owner}")
1485b303f21Smax        print(f"Use operand_number: {use.operand_number}")
14981233c70Smax
15081233c70Smax
151*21df3251SPerry Gibson# CHECK-LABEL: TEST: testValueReplaceAllUsesWithExcept
152*21df3251SPerry Gibson@run
153*21df3251SPerry Gibsondef testValueReplaceAllUsesWithExcept():
154*21df3251SPerry Gibson    ctx = Context()
155*21df3251SPerry Gibson    ctx.allow_unregistered_dialects = True
156*21df3251SPerry Gibson    with Location.unknown(ctx):
157*21df3251SPerry Gibson        i32 = IntegerType.get_signless(32)
158*21df3251SPerry Gibson        module = Module.create()
159*21df3251SPerry Gibson        with InsertionPoint(module.body):
160*21df3251SPerry Gibson            value = Operation.create("custom.op1", results=[i32]).results[0]
161*21df3251SPerry Gibson            op1 = Operation.create("custom.op1", operands=[value])
162*21df3251SPerry Gibson            op2 = Operation.create("custom.op2", operands=[value])
163*21df3251SPerry Gibson            value2 = Operation.create("custom.op3", results=[i32]).results[0]
164*21df3251SPerry Gibson            value.replace_all_uses_except(value2, op1)
165*21df3251SPerry Gibson
166*21df3251SPerry Gibson    assert len(list(value.uses)) == 1
167*21df3251SPerry Gibson
168*21df3251SPerry Gibson    # CHECK: Use owner: "custom.op2"
169*21df3251SPerry Gibson    # CHECK: Use operand_number: 0
170*21df3251SPerry Gibson    for use in value2.uses:
171*21df3251SPerry Gibson        assert use.owner in [op2]
172*21df3251SPerry Gibson        print(f"Use owner: {use.owner}")
173*21df3251SPerry Gibson        print(f"Use operand_number: {use.operand_number}")
174*21df3251SPerry Gibson
175*21df3251SPerry Gibson    # CHECK: Use owner: "custom.op1"
176*21df3251SPerry Gibson    # CHECK: Use operand_number: 0
177*21df3251SPerry Gibson    for use in value.uses:
178*21df3251SPerry Gibson        assert use.owner in [op1]
179*21df3251SPerry Gibson        print(f"Use owner: {use.owner}")
180*21df3251SPerry Gibson        print(f"Use operand_number: {use.operand_number}")
181*21df3251SPerry Gibson
182*21df3251SPerry Gibson
183*21df3251SPerry Gibson# CHECK-LABEL: TEST: testValueReplaceAllUsesWithMultipleExceptions
184*21df3251SPerry Gibson@run
185*21df3251SPerry Gibsondef testValueReplaceAllUsesWithMultipleExceptions():
186*21df3251SPerry Gibson    ctx = Context()
187*21df3251SPerry Gibson    ctx.allow_unregistered_dialects = True
188*21df3251SPerry Gibson    with Location.unknown(ctx):
189*21df3251SPerry Gibson        i32 = IntegerType.get_signless(32)
190*21df3251SPerry Gibson        module = Module.create()
191*21df3251SPerry Gibson        with InsertionPoint(module.body):
192*21df3251SPerry Gibson            value = Operation.create("custom.op1", results=[i32]).results[0]
193*21df3251SPerry Gibson            op1 = Operation.create("custom.op1", operands=[value])
194*21df3251SPerry Gibson            op2 = Operation.create("custom.op2", operands=[value])
195*21df3251SPerry Gibson            op3 = Operation.create("custom.op3", operands=[value])
196*21df3251SPerry Gibson            value2 = Operation.create("custom.op4", results=[i32]).results[0]
197*21df3251SPerry Gibson
198*21df3251SPerry Gibson            # Replace all uses of `value` with `value2`, except for `op1` and `op2`.
199*21df3251SPerry Gibson            value.replace_all_uses_except(value2, [op1, op2])
200*21df3251SPerry Gibson
201*21df3251SPerry Gibson    # After replacement, only `op3` should use `value2`, while `op1` and `op2` should still use `value`.
202*21df3251SPerry Gibson    assert len(list(value.uses)) == 2
203*21df3251SPerry Gibson    assert len(list(value2.uses)) == 1
204*21df3251SPerry Gibson
205*21df3251SPerry Gibson    # CHECK: Use owner: "custom.op3"
206*21df3251SPerry Gibson    # CHECK: Use operand_number: 0
207*21df3251SPerry Gibson    for use in value2.uses:
208*21df3251SPerry Gibson        assert use.owner in [op3]
209*21df3251SPerry Gibson        print(f"Use owner: {use.owner}")
210*21df3251SPerry Gibson        print(f"Use operand_number: {use.operand_number}")
211*21df3251SPerry Gibson
212*21df3251SPerry Gibson    # CHECK: Use owner: "custom.op2"
213*21df3251SPerry Gibson    # CHECK: Use operand_number: 0
214*21df3251SPerry Gibson    # CHECK: Use owner: "custom.op1"
215*21df3251SPerry Gibson    # CHECK: Use operand_number: 0
216*21df3251SPerry Gibson    for use in value.uses:
217*21df3251SPerry Gibson        assert use.owner in [op1, op2]
218*21df3251SPerry Gibson        print(f"Use owner: {use.owner}")
219*21df3251SPerry Gibson        print(f"Use operand_number: {use.operand_number}")
220*21df3251SPerry Gibson
221*21df3251SPerry Gibson
22281233c70Smax# CHECK-LABEL: TEST: testValuePrintAsOperand
22381233c70Smax@run
22481233c70Smaxdef testValuePrintAsOperand():
22581233c70Smax    ctx = Context()
22681233c70Smax    ctx.allow_unregistered_dialects = True
22781233c70Smax    with Location.unknown(ctx):
22881233c70Smax        i32 = IntegerType.get_signless(32)
22981233c70Smax        module = Module.create()
23081233c70Smax        with InsertionPoint(module.body):
23181233c70Smax            value = Operation.create("custom.op1", results=[i32]).results[0]
23281233c70Smax            # CHECK: Value(%[[VAL1:.*]] = "custom.op1"() : () -> i32)
23381233c70Smax            print(value)
23481233c70Smax
23581233c70Smax            value2 = Operation.create("custom.op2", results=[i32]).results[0]
23681233c70Smax            # CHECK: Value(%[[VAL2:.*]] = "custom.op2"() : () -> i32)
23781233c70Smax            print(value2)
23881233c70Smax
239a677a173SJacques Pienaar            topFn = func.FuncOp("test", ([i32, i32], []))
2407d055af1SJoshua Cao            entry_block = Block.create_at_start(topFn.operation.regions[0], [i32, i32])
24181233c70Smax
2427d055af1SJoshua Cao            with InsertionPoint(entry_block):
24381233c70Smax                value3 = Operation.create("custom.op3", results=[i32]).results[0]
24481233c70Smax                # CHECK: Value(%[[VAL3:.*]] = "custom.op3"() : () -> i32)
24581233c70Smax                print(value3)
24681233c70Smax                value4 = Operation.create("custom.op4", results=[i32]).results[0]
24781233c70Smax                # CHECK: Value(%[[VAL4:.*]] = "custom.op4"() : () -> i32)
24881233c70Smax                print(value4)
24981233c70Smax                func.ReturnOp([])
25081233c70Smax
25181233c70Smax        # CHECK: %[[VAL1]]
25281233c70Smax        print(value.get_name())
25381233c70Smax        # CHECK: %[[VAL2]]
25481233c70Smax        print(value2.get_name())
25581233c70Smax        # CHECK: %[[VAL3]]
25681233c70Smax        print(value3.get_name())
25781233c70Smax        # CHECK: %[[VAL4]]
25881233c70Smax        print(value4.get_name())
25981233c70Smax
26075453714SJacques Pienaar        print("With AsmState")
26175453714SJacques Pienaar        # CHECK-LABEL: With AsmState
262a677a173SJacques Pienaar        state = AsmState(topFn.operation, use_local_scope=True)
26375453714SJacques Pienaar        # CHECK: %0
26475453714SJacques Pienaar        print(value3.get_name(state=state))
26575453714SJacques Pienaar        # CHECK: %1
26675453714SJacques Pienaar        print(value4.get_name(state=state))
26775453714SJacques Pienaar
26875453714SJacques Pienaar        print("With use_local_scope")
26975453714SJacques Pienaar        # CHECK-LABEL: With use_local_scope
27081233c70Smax        # CHECK: %0
27181233c70Smax        print(value3.get_name(use_local_scope=True))
27281233c70Smax        # CHECK: %1
27381233c70Smax        print(value4.get_name(use_local_scope=True))
27481233c70Smax
27581233c70Smax        # CHECK: %[[ARG0:.*]]
2767d055af1SJoshua Cao        print(entry_block.arguments[0].get_name())
27781233c70Smax        # CHECK: %[[ARG1:.*]]
2787d055af1SJoshua Cao        print(entry_block.arguments[1].get_name())
27981233c70Smax
28081233c70Smax        # CHECK: module {
28181233c70Smax        # CHECK:   %[[VAL1]] = "custom.op1"() : () -> i32
28281233c70Smax        # CHECK:   %[[VAL2]] = "custom.op2"() : () -> i32
28381233c70Smax        # CHECK:   func.func @test(%[[ARG0]]: i32, %[[ARG1]]: i32) {
28481233c70Smax        # CHECK:     %[[VAL3]] = "custom.op3"() : () -> i32
28581233c70Smax        # CHECK:     %[[VAL4]] = "custom.op4"() : () -> i32
28681233c70Smax        # CHECK:     return
28781233c70Smax        # CHECK:   }
28881233c70Smax        # CHECK: }
28981233c70Smax        print(module)
29081233c70Smax
29181233c70Smax        value2.owner.detach_from_parent()
29281233c70Smax        # CHECK: %0
29381233c70Smax        print(value2.get_name())
29425b8433bSmax
29525b8433bSmax
29625b8433bSmax# CHECK-LABEL: TEST: testValueSetType
29725b8433bSmax@run
29825b8433bSmaxdef testValueSetType():
29925b8433bSmax    ctx = Context()
30025b8433bSmax    ctx.allow_unregistered_dialects = True
30125b8433bSmax    with Location.unknown(ctx):
30225b8433bSmax        i32 = IntegerType.get_signless(32)
30325b8433bSmax        i64 = IntegerType.get_signless(64)
30425b8433bSmax        module = Module.create()
30525b8433bSmax        with InsertionPoint(module.body):
30625b8433bSmax            value = Operation.create("custom.op1", results=[i32]).results[0]
30725b8433bSmax            # CHECK: Value(%[[VAL1:.*]] = "custom.op1"() : () -> i32)
30825b8433bSmax            print(value)
30925b8433bSmax
31025b8433bSmax            value.set_type(i64)
31125b8433bSmax            # CHECK: Value(%[[VAL1]] = "custom.op1"() : () -> i64)
31225b8433bSmax            print(value)
31325b8433bSmax
31425b8433bSmax            # CHECK: %[[VAL1]] = "custom.op1"() : () -> i64
31525b8433bSmax            print(value.owner)
3167c850867SMaksim Levental
3177c850867SMaksim Levental
3187c850867SMaksim Levental# CHECK-LABEL: TEST: testValueCasters
3197c850867SMaksim Levental@run
3207c850867SMaksim Leventaldef testValueCasters():
3217c850867SMaksim Levental    class NOPResult(OpResult):
3227c850867SMaksim Levental        def __init__(self, v):
3237c850867SMaksim Levental            super().__init__(v)
3247c850867SMaksim Levental
3257c850867SMaksim Levental        def __str__(self):
3267c850867SMaksim Levental            return super().__str__().replace(Value.__name__, NOPResult.__name__)
3277c850867SMaksim Levental
3287c850867SMaksim Levental    class NOPValue(Value):
3297c850867SMaksim Levental        def __init__(self, v):
3307c850867SMaksim Levental            super().__init__(v)
3317c850867SMaksim Levental
3327c850867SMaksim Levental        def __str__(self):
3337c850867SMaksim Levental            return super().__str__().replace(Value.__name__, NOPValue.__name__)
3347c850867SMaksim Levental
3357c850867SMaksim Levental    class NOPBlockArg(BlockArgument):
3367c850867SMaksim Levental        def __init__(self, v):
3377c850867SMaksim Levental            super().__init__(v)
3387c850867SMaksim Levental
3397c850867SMaksim Levental        def __str__(self):
3407c850867SMaksim Levental            return super().__str__().replace(Value.__name__, NOPBlockArg.__name__)
3417c850867SMaksim Levental
3427c850867SMaksim Levental    @register_value_caster(IntegerType.static_typeid)
3436ce51599SSergei Lebedev    def cast_int(v) -> Value:
3447c850867SMaksim Levental        print("in caster", v.__class__.__name__)
3457c850867SMaksim Levental        if isinstance(v, OpResult):
3467c850867SMaksim Levental            return NOPResult(v)
3477c850867SMaksim Levental        if isinstance(v, BlockArgument):
3487c850867SMaksim Levental            return NOPBlockArg(v)
3497c850867SMaksim Levental        elif isinstance(v, Value):
3507c850867SMaksim Levental            return NOPValue(v)
3517c850867SMaksim Levental
3527c850867SMaksim Levental    ctx = Context()
3537c850867SMaksim Levental    ctx.allow_unregistered_dialects = True
3547c850867SMaksim Levental    with Location.unknown(ctx):
3557c850867SMaksim Levental        i32 = IntegerType.get_signless(32)
3567c850867SMaksim Levental        module = Module.create()
3577c850867SMaksim Levental        with InsertionPoint(module.body):
3587c850867SMaksim Levental            values = Operation.create("custom.op1", results=[i32, i32]).results
3597c850867SMaksim Levental            # CHECK: in caster OpResult
3607c850867SMaksim Levental            # CHECK: result 0 NOPResult(%0:2 = "custom.op1"() : () -> (i32, i32))
3617c850867SMaksim Levental            print("result", values[0].result_number, values[0])
3627c850867SMaksim Levental            # CHECK: in caster OpResult
3637c850867SMaksim Levental            # CHECK: result 1 NOPResult(%0:2 = "custom.op1"() : () -> (i32, i32))
3647c850867SMaksim Levental            print("result", values[1].result_number, values[1])
3657c850867SMaksim Levental
3667c850867SMaksim Levental            # CHECK: results slice 0 NOPResult(%0:2 = "custom.op1"() : () -> (i32, i32))
3677c850867SMaksim Levental            print("results slice", values[:1][0].result_number, values[:1][0])
3687c850867SMaksim Levental
3697c850867SMaksim Levental            value0, value1 = values
3707c850867SMaksim Levental            # CHECK: in caster OpResult
3717c850867SMaksim Levental            # CHECK: result 0 NOPResult(%0:2 = "custom.op1"() : () -> (i32, i32))
3727c850867SMaksim Levental            print("result", value0.result_number, values[0])
3737c850867SMaksim Levental            # CHECK: in caster OpResult
3747c850867SMaksim Levental            # CHECK: result 1 NOPResult(%0:2 = "custom.op1"() : () -> (i32, i32))
3757c850867SMaksim Levental            print("result", value1.result_number, values[1])
3767c850867SMaksim Levental
3777c850867SMaksim Levental            op1 = Operation.create("custom.op2", operands=[value0, value1])
3787c850867SMaksim Levental            # CHECK: "custom.op2"(%0#0, %0#1) : (i32, i32) -> ()
3797c850867SMaksim Levental            print(op1)
3807c850867SMaksim Levental
3817c850867SMaksim Levental            # CHECK: in caster Value
3827c850867SMaksim Levental            # CHECK: operand 0 NOPValue(%0:2 = "custom.op1"() : () -> (i32, i32))
3837c850867SMaksim Levental            print("operand 0", op1.operands[0])
3847c850867SMaksim Levental            # CHECK: in caster Value
3857c850867SMaksim Levental            # CHECK: operand 1 NOPValue(%0:2 = "custom.op1"() : () -> (i32, i32))
3867c850867SMaksim Levental            print("operand 1", op1.operands[1])
3877c850867SMaksim Levental
3887c850867SMaksim Levental            # CHECK: in caster BlockArgument
3897c850867SMaksim Levental            # CHECK: in caster BlockArgument
3907c850867SMaksim Levental            @func.FuncOp.from_py_func(i32, i32)
3917c850867SMaksim Levental            def reduction(arg0, arg1):
3927c850867SMaksim Levental                # CHECK: as func arg 0 NOPBlockArg
3937c850867SMaksim Levental                print("as func arg", arg0.arg_number, arg0.__class__.__name__)
3947c850867SMaksim Levental                # CHECK: as func arg 1 NOPBlockArg
3957c850867SMaksim Levental                print("as func arg", arg1.arg_number, arg1.__class__.__name__)
3967c850867SMaksim Levental
3977c850867SMaksim Levental            # CHECK: args slice 0 NOPBlockArg(<block argument> of type 'i32' at index: 0)
3987c850867SMaksim Levental            print(
3997c850867SMaksim Levental                "args slice",
4007c850867SMaksim Levental                reduction.func_op.arguments[:1][0].arg_number,
4017c850867SMaksim Levental                reduction.func_op.arguments[:1][0],
4027c850867SMaksim Levental            )
4037c850867SMaksim Levental
4047c850867SMaksim Levental    try:
4057c850867SMaksim Levental
4067c850867SMaksim Levental        @register_value_caster(IntegerType.static_typeid)
4077c850867SMaksim Levental        def dont_cast_int_shouldnt_register(v):
4087c850867SMaksim Levental            ...
4097c850867SMaksim Levental
4107c850867SMaksim Levental    except RuntimeError as e:
4117c850867SMaksim Levental        # CHECK: Value caster is already registered: {{.*}}cast_int
4127c850867SMaksim Levental        print(e)
4137c850867SMaksim Levental
4147c850867SMaksim Levental    @register_value_caster(IntegerType.static_typeid, replace=True)
4157c850867SMaksim Levental    def dont_cast_int(v) -> OpResult:
4167c850867SMaksim Levental        assert isinstance(v, OpResult)
4177c850867SMaksim Levental        print("don't cast", v.result_number, v)
4187c850867SMaksim Levental        return v
4197c850867SMaksim Levental
4207c850867SMaksim Levental    with Location.unknown(ctx):
4217c850867SMaksim Levental        i32 = IntegerType.get_signless(32)
4227c850867SMaksim Levental        module = Module.create()
4237c850867SMaksim Levental        with InsertionPoint(module.body):
4247c850867SMaksim Levental            # CHECK: don't cast 0 Value(%0 = "custom.op1"() : () -> i32)
4257c850867SMaksim Levental            new_value = Operation.create("custom.op1", results=[i32]).result
4267c850867SMaksim Levental            # CHECK: result 0 Value(%0 = "custom.op1"() : () -> i32)
4277c850867SMaksim Levental            print("result", new_value.result_number, new_value)
4287c850867SMaksim Levental
4297c850867SMaksim Levental            # CHECK: don't cast 0 Value(%1 = "custom.op2"() : () -> i32)
4307c850867SMaksim Levental            new_value = Operation.create("custom.op2", results=[i32]).results[0]
4317c850867SMaksim Levental            # CHECK: result 0 Value(%1 = "custom.op2"() : () -> i32)
4327c850867SMaksim Levental            print("result", new_value.result_number, new_value)
433