matcher.cpp 11.7 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#include <migraph/matcher.hpp>
#include <migraph/iterator_for.hpp>
#include <test.hpp>
#include <basic_ops.hpp>

Paul's avatar
Paul committed
6
namespace match = migraph::match;
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
template <class M>
Paul's avatar
Paul committed
9
migraph::match::matcher_result find_match(migraph::program& p, M&& m)
Paul's avatar
Paul committed
10
{
Paul's avatar
Paul committed
11
    migraph::match::matcher_result result;
Paul's avatar
Paul committed
12
    for(auto ins : migraph::iterator_for(p))
Paul's avatar
Paul committed
13
    {
Paul's avatar
Paul committed
14
        result = migraph::match::match_instruction(p, ins, m);
Paul's avatar
Paul committed
15
16
17
18
19
20
21
22
23
24
        if(result.result != p.end())
            return result;
    }
    return result;
}

void match1()
{
    migraph::program p;
    auto l = p.add_literal(1);
Paul's avatar
Paul committed
25
    auto m = match::standard_shape();
Paul's avatar
Paul committed
26
27
28
29
30
31
32
    auto r = find_match(p, m);
    EXPECT(bool{r.result == l});
}

void match_name1()
{
    migraph::program p;
Paul's avatar
Paul committed
33
34
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
35
36
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
37
    auto m = match::name("sum");
Paul's avatar
Paul committed
38
39
40
41
42
43
44
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_name2()
{
    migraph::program p;
Paul's avatar
Paul committed
45
46
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
47
48
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
49
    auto m = match::name("min");
Paul's avatar
Paul committed
50
51
52
53
54
55
56
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_name3()
{
    migraph::program p;
Paul's avatar
Paul committed
57
58
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
59
60
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
61
    auto m = match::name("sum")(match::standard_shape());
Paul's avatar
Paul committed
62
63
64
65
66
67
68
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg1()
{
    migraph::program p;
Paul's avatar
Paul committed
69
70
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
71
72
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
73
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
74
75
76
77
78
79
80
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg2()
{
    migraph::program p;
Paul's avatar
Paul committed
81
82
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
83
84
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
85
    auto m = match::name("sum")(match::arg(0)(match::name("sum")), match::standard_shape());
Paul's avatar
Paul committed
86
87
88
89
90
91
92
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_arg3()
{
    migraph::program p;
Paul's avatar
Paul committed
93
94
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
95
96
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
97
    auto m = match::name("sum")(match::arg(1)(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
98
99
100
101
102
103
104
105
106
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg4()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
107
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
108
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
109
110
    auto m    = match::name("pass")(match::arg(0)(match::name("sum")), match::standard_shape());
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
111
112
113
114
115
116
    EXPECT(bool{r.result == pass});
}

void match_arg5()
{
    migraph::program p;
Paul's avatar
Paul committed
117
118
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
119
120
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
121
    auto m = match::name("pass")(match::arg(1)(match::name("sum")), match::standard_shape());
Paul's avatar
Paul committed
122
123
124
125
126
127
128
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_arg6()
{
    migraph::program p;
Paul's avatar
Paul committed
129
130
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
131
132
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
133
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")));
Paul's avatar
Paul committed
134
135
136
137
138
139
140
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg7()
{
    migraph::program p;
Paul's avatar
Paul committed
141
142
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
143
144
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
145
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
146
                                match::arg(1)(match::name("@literal")));
Paul's avatar
Paul committed
147
148
149
150
151
152
153
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_args1()
{
    migraph::program p;
Paul's avatar
Paul committed
154
155
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
156
157
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
158
159
    auto m = match::name("sum")(match::args(match::name("@literal"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
160
161
162
163
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
164
165
166
167
168
169
170
void match_args2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
171
172
    auto m = match::name("sum")(match::args(match::name("@literal"), match::name("sum")),
                                match::standard_shape());
Paul's avatar
Paul committed
173
174
175
176
177
178
179
180
181
182
183
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_args3()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
184
    auto m = match::name("sum")(match::args(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
185
    auto r = find_match(p, m);
186
    EXPECT(bool{r.result == p.end()});
Paul's avatar
Paul committed
187
188
189
190
191
}

void match_args4()
{
    migraph::program p;
Paul's avatar
Paul committed
192
193
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
194
195
196
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
Paul's avatar
Paul committed
197
198
    auto m = match::name("sum")(match::args(match::name("sum"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
199
200
201
202
203
204
205
206
207
208
209
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum2});
}

void match_args5()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
210
211
    auto m = match::name("sum")(match::args(match::name("sum"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
212
213
214
215
216
217
218
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_args6()
{
    migraph::program p;
Paul's avatar
Paul committed
219
220
221
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
222
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
223
224
    auto m    = match::name("pass")(match::args(match::name("sum")), match::standard_shape());
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
225
226
227
228
229
230
    EXPECT(bool{r.result == pass});
}

void match_args7()
{
    migraph::program p;
Paul's avatar
Paul committed
231
232
233
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
234
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
235
    auto m    = match::name("pass")(match::args(match::name("sum")(match::args(
Paul's avatar
Paul committed
236
237
                                     match::name("@literal"), match::name("@literal")))),
                                 match::standard_shape());
Paul's avatar
Paul committed
238
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
239
240
241
    EXPECT(bool{r.result == pass});
}

Paul's avatar
Paul committed
242
void match_all_of1()
Paul's avatar
Paul committed
243
244
245
246
247
248
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
249
    auto m = match::name("sum")(match::all_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
250
                                              match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
251
252
253
254
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
255
256
257
258
259
260
261
void match_all_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
262
263
    auto m = match::name("sum")(
        match::all_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
264
265
266
267
268
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_any_of1()
Paul's avatar
Paul committed
269
270
271
272
273
274
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
275
276
    auto m = match::name("sum")(
        match::any_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
277
278
279
280
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
281
282
283
284
285
286
287
void match_any_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
288
289
    auto m = match::name("sum")(
        match::any_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("sum"))));
Paul's avatar
Paul committed
290
291
292
293
294
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_none_of1()
Paul's avatar
Paul committed
295
296
297
298
299
300
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
301
302
    auto m = match::name("sum")(
        match::none_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("sum"))));
Paul's avatar
Paul committed
303
304
305
306
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
307
308
309
310
311
312
313
void match_none_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
314
    auto m = match::name("sum")(match::none_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
315
                                               match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
316
317
318
319
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

Paul's avatar
Paul committed
320
321
322
void match_bind1()
{
    migraph::program p;
Paul's avatar
Paul committed
323
324
325
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
326
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
327
    auto m    = match::name("pass")(
Paul's avatar
Paul committed
328
329
330
                 match::args(match::name("sum")(match::args(match::name("@literal").bind("one"),
                                                            match::name("@literal").bind("two")))
                                 .bind("sum")),
Paul's avatar
Paul committed
331
                 match::standard_shape())
Paul's avatar
Paul committed
332
                 .bind("pass");
Paul's avatar
Paul committed
333
334
335
336
337
338
339
340
    auto r = find_match(p, m);
    EXPECT(bool{r.instructions.at("one") == one});
    EXPECT(bool{r.instructions.at("two") == two});
    EXPECT(bool{r.instructions.at("sum") == sum});
    EXPECT(bool{r.instructions.at("pass") == pass});
    EXPECT(bool{r.result == pass});
}

Paul's avatar
Paul committed
341
342
343
struct match_find_sum
{
    migraph::instruction_ref ins;
Paul's avatar
Paul committed
344
    auto matcher() const { return match::name("sum"); }
Paul's avatar
Paul committed
345

Paul's avatar
Paul committed
346
    void apply(migraph::program&, match::matcher_result r) const { EXPECT(bool{r.result == ins}); }
Paul's avatar
Paul committed
347
348
349
350
351
};

struct match_find_literal
{
    migraph::instruction_ref ins;
Paul's avatar
Paul committed
352
    auto matcher() const { return match::name("@literal"); }
Paul's avatar
Paul committed
353

Paul's avatar
Paul committed
354
    void apply(migraph::program&, match::matcher_result r) const
Paul's avatar
Paul committed
355
356
357
358
359
360
361
362
363
364
365
366
367
    {
        EXPECT(bool{r.result != ins});
        EXPECT(r.result->name() == "@literal");
    }
};

void match_finder()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
368
    match::find_matches(p, match_find_sum{sum}, match_find_literal{sum});
Paul's avatar
Paul committed
369
370
}

Paul's avatar
Paul committed
371
372
int main()
{
Paul's avatar
Paul committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    match1();
    match_name1();
    match_name2();
    match_name3();

    match_arg1();
    match_arg2();
    match_arg3();
    match_arg4();
    match_arg5();
    match_arg6();
    match_arg7();

    match_args1();
Paul's avatar
Paul committed
387
388
389
390
391
392
393
    match_args2();
    match_args3();
    match_args4();
    match_args5();
    match_args6();
    match_args7();

Paul's avatar
Paul committed
394
395
396
397
398
    match_all_of1();
    match_all_of2();

    match_any_of1();
    match_any_of2();
Paul's avatar
Paul committed
399

Paul's avatar
Paul committed
400
401
    match_none_of1();
    match_none_of2();
Paul's avatar
Paul committed
402

Paul's avatar
Paul committed
403
    match_bind1();
Paul's avatar
Paul committed
404
405

    match_finder();
Paul's avatar
Paul committed
406
}