aboutsummaryrefslogtreecommitdiff
path: root/mlir/test/python/integration/dialects/pdl.py
blob: fe27dd4203a218a0f9e9e48dbe10e37236db2cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# RUN: %PYTHON %s 2>&1 | FileCheck %s

from mlir.dialects import arith, func, pdl
from mlir.dialects.builtin import module
from mlir.ir import *
from mlir.rewrite import *


def construct_and_print_in_module(f):
    print("\nTEST:", f.__name__)
    with Context(), Location.unknown():
        module = Module.create()
        with InsertionPoint(module.body):
            module = f(module)
        if module is not None:
            print(module)
    return f


def get_pdl_patterns():
    # Create a rewrite from add to mul. This will match
    # - operation name is arith.addi
    # - operands are index types.
    # - there are two operands.
    with Location.unknown():
        m = Module.create()
        with InsertionPoint(m.body):
            # Change all arith.addi with index types to arith.muli.
            @pdl.pattern(benefit=1, sym_name="addi_to_mul")
            def pat():
                # Match arith.addi with index types.
                index_type = pdl.TypeOp(IndexType.get())
                operand0 = pdl.OperandOp(index_type)
                operand1 = pdl.OperandOp(index_type)
                op0 = pdl.OperationOp(
                    name="arith.addi", args=[operand0, operand1], types=[index_type]
                )

                # Replace the matched op with arith.muli.
                @pdl.rewrite()
                def rew():
                    newOp = pdl.OperationOp(
                        name="arith.muli", args=[operand0, operand1], types=[index_type]
                    )
                    pdl.ReplaceOp(op0, with_op=newOp)

    # Create a PDL module from module and freeze it. At this point the ownership
    # of the module is transferred to the PDL module. This ownership transfer is
    # not yet captured Python side/has sharp edges. So best to construct the
    # module and PDL module in same scope.
    # FIXME: This should be made more robust.
    return PDLModule(m).freeze()


# CHECK-LABEL: TEST: test_add_to_mul
# CHECK: arith.muli
@construct_and_print_in_module
def test_add_to_mul(module_):
    index_type = IndexType.get()

    # Create a test case.
    @module(sym_name="ir")
    def ir():
        @func.func(index_type, index_type)
        def add_func(a, b):
            return arith.addi(a, b)

    frozen = get_pdl_patterns()
    # Could apply frozen pattern set multiple times.
    apply_patterns_and_fold_greedily(module_, frozen)
    return module_


# CHECK-LABEL: TEST: test_add_to_mul_with_op
# CHECK: arith.muli
@construct_and_print_in_module
def test_add_to_mul_with_op(module_):
    index_type = IndexType.get()

    # Create a test case.
    @module(sym_name="ir")
    def ir():
        @func.func(index_type, index_type)
        def add_func(a, b):
            return arith.addi(a, b)

    frozen = get_pdl_patterns()
    apply_patterns_and_fold_greedily(module_.operation, frozen)
    return module_


# If we use arith.constant and arith.addi here,
# these C++-defined folding/canonicalization will be applied
# implicitly in the greedy pattern rewrite driver to
# make our Python-defined folding useless,
# so here we define a new dialect to workaround this.
def load_myint_dialect():
    from mlir.dialects import irdl

    m = Module.create()
    with InsertionPoint(m.body):
        myint = irdl.dialect("myint")
        with InsertionPoint(myint.body):
            constant = irdl.operation_("constant")
            with InsertionPoint(constant.body):
                iattr = irdl.base(base_name="#builtin.integer")
                i32 = irdl.is_(TypeAttr.get(IntegerType.get_signless(32)))
                irdl.attributes_([iattr], ["value"])
                irdl.results_([i32], ["cst"], [irdl.Variadicity.single])
            add = irdl.operation_("add")
            with InsertionPoint(add.body):
                i32 = irdl.is_(TypeAttr.get(IntegerType.get_signless(32)))
                irdl.operands_(
                    [i32, i32],
                    ["lhs", "rhs"],
                    [irdl.Variadicity.single, irdl.Variadicity.single],
                )
                irdl.results_([i32], ["res"], [irdl.Variadicity.single])

    m.operation.verify()
    irdl.load_dialects(m)


# This PDL pattern is to fold constant additions,
# including two patterns:
# 1. add(constant0, constant1) -> constant2
#    where constant2 = constant0 + constant1;
# 2. add(x, 0) or add(0, x) -> x.
def get_pdl_pattern_fold():
    m = Module.create()
    i32 = IntegerType.get_signless(32)
    with InsertionPoint(m.body):

        @pdl.pattern(benefit=1, sym_name="myint_add_fold")
        def pat():
            t = pdl.TypeOp(i32)
            a0 = pdl.AttributeOp()
            a1 = pdl.AttributeOp()
            c0 = pdl.OperationOp(
                name="myint.constant", attributes={"value": a0}, types=[t]
            )
            c1 = pdl.OperationOp(
                name="myint.constant", attributes={"value": a1}, types=[t]
            )
            v0 = pdl.ResultOp(c0, 0)
            v1 = pdl.ResultOp(c1, 0)
            op0 = pdl.OperationOp(name="myint.add", args=[v0, v1], types=[t])

            @pdl.rewrite()
            def rew():
                sum = pdl.apply_native_rewrite(
                    [pdl.AttributeType.get()], "add_fold", [a0, a1]
                )
                newOp = pdl.OperationOp(
                    name="myint.constant", attributes={"value": sum}, types=[t]
                )
                pdl.ReplaceOp(op0, with_op=newOp)

        @pdl.pattern(benefit=1, sym_name="myint_add_zero_fold")
        def pat():
            t = pdl.TypeOp(i32)
            v0 = pdl.OperandOp()
            v1 = pdl.OperandOp()
            v = pdl.apply_native_constraint([pdl.ValueType.get()], "has_zero", [v0, v1])
            op0 = pdl.OperationOp(name="myint.add", args=[v0, v1], types=[t])

            @pdl.rewrite()
            def rew():
                pdl.ReplaceOp(op0, with_values=[v])

    def add_fold(rewriter, results, values):
        a0, a1 = values
        results.append(IntegerAttr.get(i32, a0.value + a1.value))

    def is_zero(value):
        op = value.owner
        if isinstance(op, Operation):
            return op.name == "myint.constant" and op.attributes["value"].value == 0
        return False

    # Check if either operand is a constant zero,
    # and append the other operand to the results if so.
    def has_zero(rewriter, results, values):
        v0, v1 = values
        if is_zero(v0):
            results.append(v1)
            return False
        if is_zero(v1):
            results.append(v0)
            return False
        return True

    pdl_module = PDLModule(m)
    pdl_module.register_rewrite_function("add_fold", add_fold)
    pdl_module.register_constraint_function("has_zero", has_zero)
    return pdl_module.freeze()


# CHECK-LABEL: TEST: test_pdl_register_function
# CHECK: "myint.constant"() {value = 8 : i32} : () -> i32
@construct_and_print_in_module
def test_pdl_register_function(module_):
    load_myint_dialect()

    module_ = Module.parse(
        """
        %c0 = "myint.constant"() { value = 2 }: () -> (i32)
        %c1 = "myint.constant"() { value = 3 }: () -> (i32)
        %x = "myint.add"(%c0, %c1): (i32, i32) -> (i32)
        "myint.add"(%x, %c1): (i32, i32) -> (i32)
        """
    )

    frozen = get_pdl_pattern_fold()
    apply_patterns_and_fold_greedily(module_, frozen)

    return module_


# CHECK-LABEL: TEST: test_pdl_register_function_constraint
# CHECK: return %arg0 : i32
@construct_and_print_in_module
def test_pdl_register_function_constraint(module_):
    load_myint_dialect()

    module_ = Module.parse(
        """
        func.func @f(%x : i32) -> i32 {
            %c0 = "myint.constant"() { value = 1 }: () -> (i32)
            %c1 = "myint.constant"() { value = -1 }: () -> (i32)
            %a = "myint.add"(%c0, %c1): (i32, i32) -> (i32)
            %b = "myint.add"(%a, %x): (i32, i32) -> (i32)
            %c = "myint.add"(%b, %a): (i32, i32) -> (i32)
            func.return %c : i32
        }
        """
    )

    frozen = get_pdl_pattern_fold()
    apply_patterns_and_fold_greedily(module_, frozen)

    return module_


# This pattern is to expand constant to additions
# unless the constant is no more than 1,
# e.g. 3 -> 1 + 2 -> 1 + (1 + 1).
def get_pdl_pattern_expand():
    m = Module.create()
    i32 = IntegerType.get_signless(32)
    with InsertionPoint(m.body):

        @pdl.pattern(benefit=1, sym_name="myint_constant_expand")
        def pat():
            t = pdl.TypeOp(i32)
            cst = pdl.AttributeOp()
            pdl.apply_native_constraint([], "is_one", [cst])
            op0 = pdl.OperationOp(
                name="myint.constant", attributes={"value": cst}, types=[t]
            )

            @pdl.rewrite()
            def rew():
                expanded = pdl.apply_native_rewrite(
                    [pdl.OperationType.get()], "expand", [cst]
                )
                pdl.ReplaceOp(op0, with_op=expanded)

    def is_one(rewriter, results, values):
        cst = values[0].value
        return cst <= 1

    def expand(rewriter, results, values):
        cst = values[0].value
        c1 = cst // 2
        c2 = cst - c1
        with rewriter.ip:
            op1 = Operation.create(
                "myint.constant",
                results=[i32],
                attributes={"value": IntegerAttr.get(i32, c1)},
            )
            op2 = Operation.create(
                "myint.constant",
                results=[i32],
                attributes={"value": IntegerAttr.get(i32, c2)},
            )
            res = Operation.create(
                "myint.add", results=[i32], operands=[op1.result, op2.result]
            )
        results.append(res)

    pdl_module = PDLModule(m)
    pdl_module.register_constraint_function("is_one", is_one)
    pdl_module.register_rewrite_function("expand", expand)
    return pdl_module.freeze()


# CHECK-LABEL: TEST: test_pdl_register_function_expand
# CHECK: %0 = "myint.constant"() {value = 1 : i32} : () -> i32
# CHECK: %1 = "myint.constant"() {value = 1 : i32} : () -> i32
# CHECK: %2 = "myint.add"(%0, %1) : (i32, i32) -> i32
# CHECK: %3 = "myint.constant"() {value = 1 : i32} : () -> i32
# CHECK: %4 = "myint.constant"() {value = 1 : i32} : () -> i32
# CHECK: %5 = "myint.constant"() {value = 1 : i32} : () -> i32
# CHECK: %6 = "myint.add"(%4, %5) : (i32, i32) -> i32
# CHECK: %7 = "myint.add"(%3, %6) : (i32, i32) -> i32
# CHECK: %8 = "myint.add"(%2, %7) : (i32, i32) -> i32
# CHECK: return %8 : i32
@construct_and_print_in_module
def test_pdl_register_function_expand(module_):
    load_myint_dialect()

    module_ = Module.parse(
        """
        func.func @f() -> i32 {
          %0 = "myint.constant"() { value = 5 }: () -> (i32)
          return %0 : i32
        }
        """
    )

    frozen = get_pdl_pattern_expand()
    apply_patterns_and_fold_greedily(module_, frozen)

    return module_