xref: /llvm-project/mlir/test/python/dialects/pdl_ops.py (revision 8ec28af8eaff5acd0df3e53340159c034f08533d)
1ed21c927SDenys Shabalin# RUN: %PYTHON %s | FileCheck %s
2ed21c927SDenys Shabalin
3ed21c927SDenys Shabalinfrom mlir.ir import *
4ed21c927SDenys Shabalinfrom mlir.dialects.pdl import *
5ed21c927SDenys Shabalin
6ed21c927SDenys Shabalin
7ed21c927SDenys Shabalindef constructAndPrintInModule(f):
8ed21c927SDenys Shabalin    print("\nTEST:", f.__name__)
9ed21c927SDenys Shabalin    with Context(), Location.unknown():
10ed21c927SDenys Shabalin        module = Module.create()
11ed21c927SDenys Shabalin        with InsertionPoint(module.body):
12ed21c927SDenys Shabalin            f()
13ed21c927SDenys Shabalin        print(module)
14ed21c927SDenys Shabalin    return f
15ed21c927SDenys Shabalin
16ed21c927SDenys Shabalin
17ed21c927SDenys Shabalin# CHECK: module  {
18ed21c927SDenys Shabalin# CHECK:   pdl.pattern @operations : benefit(1)  {
19e99835ffSMogball# CHECK:     %0 = attribute
20e99835ffSMogball# CHECK:     %1 = type
21e99835ffSMogball# CHECK:     %2 = operation  {"attr" = %0} -> (%1 : !pdl.type)
22e99835ffSMogball# CHECK:     %3 = result 0 of %2
23e99835ffSMogball# CHECK:     %4 = operand
24e99835ffSMogball# CHECK:     %5 = operation(%3, %4 : !pdl.value, !pdl.value)
25e99835ffSMogball# CHECK:     rewrite %5 with "rewriter"
26ed21c927SDenys Shabalin# CHECK:   }
27ed21c927SDenys Shabalin# CHECK: }
28ed21c927SDenys Shabalin@constructAndPrintInModule
29ed21c927SDenys Shabalindef test_operations():
30ed21c927SDenys Shabalin    pattern = PatternOp(1, "operations")
31ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
32ed21c927SDenys Shabalin        attr = AttributeOp()
33ed21c927SDenys Shabalin        ty = TypeOp()
34ed21c927SDenys Shabalin        op0 = OperationOp(attributes={"attr": attr}, types=[ty])
35ed21c927SDenys Shabalin        op0_result = ResultOp(op0, 0)
36ed21c927SDenys Shabalin        input = OperandOp()
37ed21c927SDenys Shabalin        root = OperationOp(args=[op0_result, input])
38ed21c927SDenys Shabalin        RewriteOp(root, "rewriter")
39ed21c927SDenys Shabalin
40ed21c927SDenys Shabalin
41ed21c927SDenys Shabalin# CHECK: module  {
42ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_with_args : benefit(1)  {
43e99835ffSMogball# CHECK:     %0 = operand
44e99835ffSMogball# CHECK:     %1 = operation(%0 : !pdl.value)
45e99835ffSMogball# CHECK:     rewrite %1 with "rewriter"(%0 : !pdl.value)
46ed21c927SDenys Shabalin# CHECK:   }
47ed21c927SDenys Shabalin# CHECK: }
48ed21c927SDenys Shabalin@constructAndPrintInModule
49ed21c927SDenys Shabalindef test_rewrite_with_args():
50ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_with_args")
51ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
52ed21c927SDenys Shabalin        input = OperandOp()
53ed21c927SDenys Shabalin        root = OperationOp(args=[input])
54ed21c927SDenys Shabalin        RewriteOp(root, "rewriter", args=[input])
55ed21c927SDenys Shabalin
56f9008e63STobias Hieta
57ed21c927SDenys Shabalin# CHECK: module  {
58ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_multi_root_optimal : benefit(1)  {
59e99835ffSMogball# CHECK:     %0 = operand
60e99835ffSMogball# CHECK:     %1 = operand
61e99835ffSMogball# CHECK:     %2 = type
62e99835ffSMogball# CHECK:     %3 = operation(%0 : !pdl.value)  -> (%2 : !pdl.type)
63e99835ffSMogball# CHECK:     %4 = result 0 of %3
64e99835ffSMogball# CHECK:     %5 = operation(%4 : !pdl.value)
65e99835ffSMogball# CHECK:     %6 = operation(%1 : !pdl.value)  -> (%2 : !pdl.type)
66e99835ffSMogball# CHECK:     %7 = result 0 of %6
67e99835ffSMogball# CHECK:     %8 = operation(%4, %7 : !pdl.value, !pdl.value)
689595f356SRiver Riddle# CHECK:     rewrite with "rewriter"(%5, %8 : !pdl.operation, !pdl.operation)
69ed21c927SDenys Shabalin# CHECK:   }
70ed21c927SDenys Shabalin# CHECK: }
71ed21c927SDenys Shabalin@constructAndPrintInModule
72ed21c927SDenys Shabalindef test_rewrite_multi_root_optimal():
73ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_multi_root_optimal")
74ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
75ed21c927SDenys Shabalin        input1 = OperandOp()
76ed21c927SDenys Shabalin        input2 = OperandOp()
77ed21c927SDenys Shabalin        ty = TypeOp()
78ed21c927SDenys Shabalin        op1 = OperationOp(args=[input1], types=[ty])
79ed21c927SDenys Shabalin        val1 = ResultOp(op1, 0)
80ed21c927SDenys Shabalin        root1 = OperationOp(args=[val1])
81ed21c927SDenys Shabalin        op2 = OperationOp(args=[input2], types=[ty])
82ed21c927SDenys Shabalin        val2 = ResultOp(op2, 0)
83ed21c927SDenys Shabalin        root2 = OperationOp(args=[val1, val2])
849595f356SRiver Riddle        RewriteOp(name="rewriter", args=[root1, root2])
85ed21c927SDenys Shabalin
86f9008e63STobias Hieta
87ed21c927SDenys Shabalin# CHECK: module  {
88ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_multi_root_forced : benefit(1)  {
89e99835ffSMogball# CHECK:     %0 = operand
90e99835ffSMogball# CHECK:     %1 = operand
91e99835ffSMogball# CHECK:     %2 = type
92e99835ffSMogball# CHECK:     %3 = operation(%0 : !pdl.value)  -> (%2 : !pdl.type)
93e99835ffSMogball# CHECK:     %4 = result 0 of %3
94e99835ffSMogball# CHECK:     %5 = operation(%4 : !pdl.value)
95e99835ffSMogball# CHECK:     %6 = operation(%1 : !pdl.value)  -> (%2 : !pdl.type)
96e99835ffSMogball# CHECK:     %7 = result 0 of %6
97e99835ffSMogball# CHECK:     %8 = operation(%4, %7 : !pdl.value, !pdl.value)
989595f356SRiver Riddle# CHECK:     rewrite %5 with "rewriter"(%8 : !pdl.operation)
99ed21c927SDenys Shabalin# CHECK:   }
100ed21c927SDenys Shabalin# CHECK: }
101ed21c927SDenys Shabalin@constructAndPrintInModule
102ed21c927SDenys Shabalindef test_rewrite_multi_root_forced():
103ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_multi_root_forced")
104ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
105ed21c927SDenys Shabalin        input1 = OperandOp()
106ed21c927SDenys Shabalin        input2 = OperandOp()
107ed21c927SDenys Shabalin        ty = TypeOp()
108ed21c927SDenys Shabalin        op1 = OperationOp(args=[input1], types=[ty])
109ed21c927SDenys Shabalin        val1 = ResultOp(op1, 0)
110ed21c927SDenys Shabalin        root1 = OperationOp(args=[val1])
111ed21c927SDenys Shabalin        op2 = OperationOp(args=[input2], types=[ty])
112ed21c927SDenys Shabalin        val2 = ResultOp(op2, 0)
113ed21c927SDenys Shabalin        root2 = OperationOp(args=[val1, val2])
1149595f356SRiver Riddle        RewriteOp(root1, name="rewriter", args=[root2])
115ed21c927SDenys Shabalin
116f9008e63STobias Hieta
117ed21c927SDenys Shabalin# CHECK: module  {
118ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_add_body : benefit(1)  {
119e99835ffSMogball# CHECK:     %0 = type : i32
120e99835ffSMogball# CHECK:     %1 = type
121e99835ffSMogball# CHECK:     %2 = operation  -> (%0, %1 : !pdl.type, !pdl.type)
122e99835ffSMogball# CHECK:     rewrite %2  {
123e99835ffSMogball# CHECK:       %3 = type
124e99835ffSMogball# CHECK:       %4 = operation "foo.op"  -> (%0, %3 : !pdl.type, !pdl.type)
125e99835ffSMogball# CHECK:       replace %2 with %4
126ed21c927SDenys Shabalin# CHECK:     }
127ed21c927SDenys Shabalin# CHECK:   }
128ed21c927SDenys Shabalin# CHECK: }
129ed21c927SDenys Shabalin@constructAndPrintInModule
130ed21c927SDenys Shabalindef test_rewrite_add_body():
131ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_add_body")
132ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
133ed21c927SDenys Shabalin        ty1 = TypeOp(IntegerType.get_signless(32))
134ed21c927SDenys Shabalin        ty2 = TypeOp()
135ed21c927SDenys Shabalin        root = OperationOp(types=[ty1, ty2])
136ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
137ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
138ed21c927SDenys Shabalin            ty3 = TypeOp()
139ed21c927SDenys Shabalin            newOp = OperationOp(name="foo.op", types=[ty1, ty3])
140ed21c927SDenys Shabalin            ReplaceOp(root, with_op=newOp)
141ed21c927SDenys Shabalin
142f9008e63STobias Hieta
143ed21c927SDenys Shabalin# CHECK: module  {
144ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_type : benefit(1)  {
145e99835ffSMogball# CHECK:     %0 = type : i32
146e99835ffSMogball# CHECK:     %1 = type
147e99835ffSMogball# CHECK:     %2 = operation  -> (%0, %1 : !pdl.type, !pdl.type)
148e99835ffSMogball# CHECK:     rewrite %2  {
149e99835ffSMogball# CHECK:       %3 = operation "foo.op"  -> (%0, %1 : !pdl.type, !pdl.type)
150ed21c927SDenys Shabalin# CHECK:     }
151ed21c927SDenys Shabalin# CHECK:   }
152ed21c927SDenys Shabalin# CHECK: }
153ed21c927SDenys Shabalin@constructAndPrintInModule
154ed21c927SDenys Shabalindef test_rewrite_type():
155ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_type")
156ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
157ed21c927SDenys Shabalin        ty1 = TypeOp(IntegerType.get_signless(32))
158ed21c927SDenys Shabalin        ty2 = TypeOp()
159ed21c927SDenys Shabalin        root = OperationOp(types=[ty1, ty2])
160ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
161ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
162ed21c927SDenys Shabalin            newOp = OperationOp(name="foo.op", types=[ty1, ty2])
163ed21c927SDenys Shabalin
164f9008e63STobias Hieta
165ed21c927SDenys Shabalin# CHECK: module  {
166ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_types : benefit(1)  {
167e99835ffSMogball# CHECK:     %0 = types
168e99835ffSMogball# CHECK:     %1 = operation  -> (%0 : !pdl.range<type>)
169e99835ffSMogball# CHECK:     rewrite %1  {
170e99835ffSMogball# CHECK:       %2 = types : [i32, i64]
171e99835ffSMogball# CHECK:       %3 = operation "foo.op"  -> (%0, %2 : !pdl.range<type>, !pdl.range<type>)
172ed21c927SDenys Shabalin# CHECK:     }
173ed21c927SDenys Shabalin# CHECK:   }
174ed21c927SDenys Shabalin# CHECK: }
175ed21c927SDenys Shabalin@constructAndPrintInModule
176ed21c927SDenys Shabalindef test_rewrite_types():
177ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_types")
178ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
179ed21c927SDenys Shabalin        types = TypesOp()
180ed21c927SDenys Shabalin        root = OperationOp(types=[types])
181ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
182ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
183f9008e63STobias Hieta            otherTypes = TypesOp(
184f9008e63STobias Hieta                [IntegerType.get_signless(32), IntegerType.get_signless(64)]
185f9008e63STobias Hieta            )
186ed21c927SDenys Shabalin            newOp = OperationOp(name="foo.op", types=[types, otherTypes])
187ed21c927SDenys Shabalin
188f9008e63STobias Hieta
189ed21c927SDenys Shabalin# CHECK: module  {
190ed21c927SDenys Shabalin# CHECK:   pdl.pattern @rewrite_operands : benefit(1)  {
191e99835ffSMogball# CHECK:     %0 = types
192e99835ffSMogball# CHECK:     %1 = operands : %0
193e99835ffSMogball# CHECK:     %2 = operation(%1 : !pdl.range<value>)
194e99835ffSMogball# CHECK:     rewrite %2  {
195e99835ffSMogball# CHECK:       %3 = operation "foo.op"  -> (%0 : !pdl.range<type>)
196ed21c927SDenys Shabalin# CHECK:     }
197ed21c927SDenys Shabalin# CHECK:   }
198ed21c927SDenys Shabalin# CHECK: }
199ed21c927SDenys Shabalin@constructAndPrintInModule
200ed21c927SDenys Shabalindef test_rewrite_operands():
201ed21c927SDenys Shabalin    pattern = PatternOp(1, "rewrite_operands")
202ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
203ed21c927SDenys Shabalin        types = TypesOp()
204ed21c927SDenys Shabalin        operands = OperandsOp(types)
205ed21c927SDenys Shabalin        root = OperationOp(args=[operands])
206ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
207ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
208ed21c927SDenys Shabalin            newOp = OperationOp(name="foo.op", types=[types])
209ed21c927SDenys Shabalin
210f9008e63STobias Hieta
211ed21c927SDenys Shabalin# CHECK: module  {
212ed21c927SDenys Shabalin# CHECK:   pdl.pattern @native_rewrite : benefit(1)  {
213e99835ffSMogball# CHECK:     %0 = operation
214e99835ffSMogball# CHECK:     rewrite %0  {
215e99835ffSMogball# CHECK:       apply_native_rewrite "NativeRewrite"(%0 : !pdl.operation)
216ed21c927SDenys Shabalin# CHECK:     }
217ed21c927SDenys Shabalin# CHECK:   }
218ed21c927SDenys Shabalin# CHECK: }
219ed21c927SDenys Shabalin@constructAndPrintInModule
220ed21c927SDenys Shabalindef test_native_rewrite():
221ed21c927SDenys Shabalin    pattern = PatternOp(1, "native_rewrite")
222ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
223ed21c927SDenys Shabalin        root = OperationOp()
224ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
225ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
226ed21c927SDenys Shabalin            ApplyNativeRewriteOp([], "NativeRewrite", args=[root])
227ed21c927SDenys Shabalin
228f9008e63STobias Hieta
229ed21c927SDenys Shabalin# CHECK: module  {
230ed21c927SDenys Shabalin# CHECK:   pdl.pattern @attribute_with_value : benefit(1)  {
231e99835ffSMogball# CHECK:     %0 = operation
232e99835ffSMogball# CHECK:     rewrite %0  {
233d4381b3fSRiver Riddle# CHECK:       %1 = attribute = "value"
234e99835ffSMogball# CHECK:       apply_native_rewrite "NativeRewrite"(%1 : !pdl.attribute)
235ed21c927SDenys Shabalin# CHECK:     }
236ed21c927SDenys Shabalin# CHECK:   }
237ed21c927SDenys Shabalin# CHECK: }
238ed21c927SDenys Shabalin@constructAndPrintInModule
239ed21c927SDenys Shabalindef test_attribute_with_value():
240ed21c927SDenys Shabalin    pattern = PatternOp(1, "attribute_with_value")
241ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
242ed21c927SDenys Shabalin        root = OperationOp()
243ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
244ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
245ed21c927SDenys Shabalin            attr = AttributeOp(value=Attribute.parse('"value"'))
246ed21c927SDenys Shabalin            ApplyNativeRewriteOp([], "NativeRewrite", args=[attr])
247ed21c927SDenys Shabalin
248f9008e63STobias Hieta
249ed21c927SDenys Shabalin# CHECK: module  {
250ed21c927SDenys Shabalin# CHECK:   pdl.pattern @erase : benefit(1)  {
251e99835ffSMogball# CHECK:     %0 = operation
252e99835ffSMogball# CHECK:     rewrite %0  {
253e99835ffSMogball# CHECK:       erase %0
254ed21c927SDenys Shabalin# CHECK:     }
255ed21c927SDenys Shabalin# CHECK:   }
256ed21c927SDenys Shabalin# CHECK: }
257ed21c927SDenys Shabalin@constructAndPrintInModule
258ed21c927SDenys Shabalindef test_erase():
259ed21c927SDenys Shabalin    pattern = PatternOp(1, "erase")
260ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
261ed21c927SDenys Shabalin        root = OperationOp()
262ed21c927SDenys Shabalin        rewrite = RewriteOp(root)
263ed21c927SDenys Shabalin        with InsertionPoint(rewrite.add_body()):
264ed21c927SDenys Shabalin            EraseOp(root)
265ed21c927SDenys Shabalin
266f9008e63STobias Hieta
267ed21c927SDenys Shabalin# CHECK: module  {
268ed21c927SDenys Shabalin# CHECK:   pdl.pattern @operation_results : benefit(1)  {
269e99835ffSMogball# CHECK:     %0 = types
270e99835ffSMogball# CHECK:     %1 = operation  -> (%0 : !pdl.range<type>)
271e99835ffSMogball# CHECK:     %2 = results of %1
272e99835ffSMogball# CHECK:     %3 = operation(%2 : !pdl.range<value>)
273e99835ffSMogball# CHECK:     rewrite %3 with "rewriter"
274ed21c927SDenys Shabalin# CHECK:   }
275ed21c927SDenys Shabalin# CHECK: }
276ed21c927SDenys Shabalin@constructAndPrintInModule
277ed21c927SDenys Shabalindef test_operation_results():
278ed21c927SDenys Shabalin    valueRange = RangeType.get(ValueType.get())
279ed21c927SDenys Shabalin    pattern = PatternOp(1, "operation_results")
280ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
281ed21c927SDenys Shabalin        types = TypesOp()
282ed21c927SDenys Shabalin        inputOp = OperationOp(types=[types])
283ed21c927SDenys Shabalin        results = ResultsOp(valueRange, inputOp)
284ed21c927SDenys Shabalin        root = OperationOp(args=[results])
285ed21c927SDenys Shabalin        RewriteOp(root, name="rewriter")
286ed21c927SDenys Shabalin
287f9008e63STobias Hieta
288ed21c927SDenys Shabalin# CHECK: module  {
289ed21c927SDenys Shabalin# CHECK:   pdl.pattern : benefit(1)  {
290e99835ffSMogball# CHECK:     %0 = type
2919595f356SRiver Riddle# CHECK:     apply_native_constraint "typeConstraint"(%0 : !pdl.type)
292e99835ffSMogball# CHECK:     %1 = operation  -> (%0 : !pdl.type)
293e99835ffSMogball# CHECK:     rewrite %1 with "rewrite"
294ed21c927SDenys Shabalin# CHECK:   }
295ed21c927SDenys Shabalin# CHECK: }
296ed21c927SDenys Shabalin@constructAndPrintInModule
297ed21c927SDenys Shabalindef test_apply_native_constraint():
298ed21c927SDenys Shabalin    pattern = PatternOp(1)
299ed21c927SDenys Shabalin    with InsertionPoint(pattern.body):
300ed21c927SDenys Shabalin        resultType = TypeOp()
301*8ec28af8SMatthias Gehre        ApplyNativeConstraintOp([], "typeConstraint", args=[resultType])
302ed21c927SDenys Shabalin        root = OperationOp(types=[resultType])
303ed21c927SDenys Shabalin        RewriteOp(root, name="rewrite")
304