matcher.cpp 13.1 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
243
244
245
246
void match_either_args1()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
247
    auto sum1 = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
248
249
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
Paul's avatar
Paul committed
250
251
252
    auto m =
        match::name("sum")(match::either_arg(0, 1)(match::name("sum"), match::name("@literal")));
    auto r = find_match(p, m);
Paul's avatar
Paul committed
253
254
255
256
257
258
259
260
    EXPECT(bool{r.result == sum2});
}

void match_either_args2()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
261
    auto sum1 = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
262
263
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
Paul's avatar
Paul committed
264
265
266
    auto m =
        match::name("sum")(match::either_arg(0, 1)(match::name("@literal"), match::name("sum")));
    auto r = find_match(p, m);
Paul's avatar
Paul committed
267
268
269
270
271
272
273
274
    EXPECT(bool{r.result == sum2});
}

void match_either_args3()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
275
    auto sum1 = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
276
277
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
Paul's avatar
Paul committed
278
279
280
    auto m =
        match::name("sum")(match::either_arg(0, 1)(match::name("pass"), match::name("@literal")));
    auto r = find_match(p, m);
Paul's avatar
Paul committed
281
282
283
    EXPECT(bool{r.result == p.end()});
}

Paul's avatar
Paul committed
284
void match_all_of1()
Paul's avatar
Paul committed
285
286
287
288
289
290
{
    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
291
    auto m = match::name("sum")(match::all_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
292
                                              match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
293
294
295
296
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
297
298
299
300
301
302
303
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
304
305
    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
306
307
308
309
310
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_any_of1()
Paul's avatar
Paul committed
311
312
313
314
315
316
{
    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
317
318
    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
319
320
321
322
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
323
324
325
326
327
328
329
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
330
331
    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
332
333
334
335
336
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_none_of1()
Paul's avatar
Paul committed
337
338
339
340
341
342
{
    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
343
344
    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
345
346
347
348
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
349
350
351
352
353
354
355
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
356
    auto m = match::name("sum")(match::none_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
357
                                               match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
358
359
360
361
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

Paul's avatar
Paul committed
362
363
364
void match_bind1()
{
    migraph::program p;
Paul's avatar
Paul committed
365
366
367
    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
368
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
369
    auto m    = match::name("pass")(
Paul's avatar
Paul committed
370
371
372
                 match::args(match::name("sum")(match::args(match::name("@literal").bind("one"),
                                                            match::name("@literal").bind("two")))
                                 .bind("sum")),
Paul's avatar
Paul committed
373
                 match::standard_shape())
Paul's avatar
Paul committed
374
                 .bind("pass");
Paul's avatar
Paul committed
375
376
377
378
379
380
381
382
    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
383
384
385
struct match_find_sum
{
    migraph::instruction_ref ins;
Paul's avatar
Paul committed
386
    auto matcher() const { return match::name("sum"); }
Paul's avatar
Paul committed
387

Paul's avatar
Paul committed
388
    void apply(migraph::program&, match::matcher_result r) const { EXPECT(bool{r.result == ins}); }
Paul's avatar
Paul committed
389
390
391
392
393
};

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

Paul's avatar
Paul committed
396
    void apply(migraph::program&, match::matcher_result r) const
Paul's avatar
Paul committed
397
398
399
400
401
402
403
404
405
406
407
408
409
    {
        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
410
    match::find_matches(p, match_find_sum{sum}, match_find_literal{sum});
Paul's avatar
Paul committed
411
412
}

Paul's avatar
Paul committed
413
414
int main()
{
Paul's avatar
Paul committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    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
429
430
431
432
433
434
    match_args2();
    match_args3();
    match_args4();
    match_args5();
    match_args6();
    match_args7();
Paul's avatar
Paul committed
435

Paul's avatar
Paul committed
436
437
438
    match_either_args1();
    match_either_args2();
    match_either_args3();
Paul's avatar
Paul committed
439

Paul's avatar
Paul committed
440
441
442
443
444
    match_all_of1();
    match_all_of2();

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

Paul's avatar
Paul committed
446
447
    match_none_of1();
    match_none_of2();
Paul's avatar
Paul committed
448

Paul's avatar
Paul committed
449
    match_bind1();
Paul's avatar
Paul committed
450
451

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